From 707188f144b1261f3066bcbf4bae1ce08ef11eb0 Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Sun, 21 Feb 2021 14:56:55 -0800 Subject: [PATCH 1/4] test: use try-with-resources around receivers so they don't leak between tests --- .../terasology/logic/health/RestoreTest.java | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/terasology/logic/health/RestoreTest.java b/src/test/java/org/terasology/logic/health/RestoreTest.java index e930d13..2c906c4 100644 --- a/src/test/java/org/terasology/logic/health/RestoreTest.java +++ b/src/test/java/org/terasology/logic/health/RestoreTest.java @@ -1,24 +1,9 @@ -/* - * Copyright 2019 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2021 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 package org.terasology.logic.health; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.terasology.entitySystem.entity.EntityManager; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.logic.health.event.BeforeRestoreEvent; @@ -37,9 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(MTEExtension.class) -@Dependencies({"Health"}) +@Dependencies("Health") public class RestoreTest { - private static final Logger logger = LoggerFactory.getLogger(RestoreTest.class); @In private EntityManager entityManager; @@ -57,18 +41,14 @@ public void restoreTest() { player.addComponent(healthComponent); player.send(new DoRestoreEvent(10)); - assertEquals(player.getComponent(HealthComponent.class).currentHealth, 10); + assertEquals(10, player.getComponent(HealthComponent.class).currentHealth); player.send(new DoRestoreEvent(999)); - assertEquals(player.getComponent(HealthComponent.class).currentHealth, 100); + assertEquals(100, player.getComponent(HealthComponent.class).currentHealth); } @Test public void restoreEventSentTest() { - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class); - List list = receiver.getEvents(); - assertTrue(list.isEmpty()); - HealthComponent healthComponent = new HealthComponent(); healthComponent.currentHealth = 0; healthComponent.maxHealth = 100; @@ -77,8 +57,14 @@ public void restoreEventSentTest() { player.addComponent(new PlayerCharacterComponent()); player.addComponent(healthComponent); - player.send(new DoRestoreEvent(10)); - assertEquals(1, list.size()); + try (TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), + BeforeRestoreEvent.class)) { + List list = receiver.getEvents(); + assertTrue(list.isEmpty()); + + player.send(new DoRestoreEvent(10)); + assertEquals(1, list.size()); + } } @Test @@ -91,11 +77,13 @@ public void restoreEventCancelTest() { player.addComponent(new PlayerCharacterComponent()); player.addComponent(healthComponent); - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, - (event, entity) -> { + try (TestEventReceiver ignored = + new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, + (event, entity) -> { event.consume(); - }); - player.send(new DoRestoreEvent(10)); + })) { + player.send(new DoRestoreEvent(10)); + } assertEquals(0, player.getComponent(HealthComponent.class).currentHealth); } @@ -109,14 +97,16 @@ public void restoreModifyEventTest() { player.addComponent(new PlayerCharacterComponent()); player.addComponent(healthComponent); - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, + try (TestEventReceiver ignored = new TestEventReceiver<>(helper.getHostContext(), + BeforeRestoreEvent.class, (event, entity) -> { event.add(10); event.add(-5); event.multiply(2); - }); - // Expected value is ( initial:10 + 10 - 5 ) * 2 == 30 - player.send(new DoRestoreEvent(10)); + })) { + // Expected value is ( initial:10 + 10 - 5 ) * 2 == 30 + player.send(new DoRestoreEvent(10)); + } assertEquals(30, player.getComponent(HealthComponent.class).currentHealth); } From 897806bc197112ff545f17eb54940865d2ed841c Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Sun, 21 Feb 2021 15:01:10 -0800 Subject: [PATCH 2/4] test: factor out newPlayer method --- .../logic/health/RestorationTest.java | 70 +++++-------------- 1 file changed, 16 insertions(+), 54 deletions(-) diff --git a/src/test/java/org/terasology/logic/health/RestorationTest.java b/src/test/java/org/terasology/logic/health/RestorationTest.java index 12998af..5189e25 100644 --- a/src/test/java/org/terasology/logic/health/RestorationTest.java +++ b/src/test/java/org/terasology/logic/health/RestorationTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2019 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2021 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 package org.terasology.logic.health; import org.junit.jupiter.api.Test; @@ -34,7 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(MTEExtension.class) -@Dependencies({"Health"}) +@Dependencies("Health") public class RestorationTest { @In @@ -42,15 +29,20 @@ public class RestorationTest { @In protected ModuleTestingHelper helper; - @Test - public void restoreTest() { + private EntityRef newPlayer(int currentHealth) { HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 0; + healthComponent.currentHealth = currentHealth; healthComponent.maxHealth = 100; final EntityRef player = entityManager.create(); player.addComponent(new PlayerCharacterComponent()); player.addComponent(healthComponent); + return player; + } + + @Test + public void restoreTest() { + final EntityRef player = newPlayer(0); player.send(new DoRestoreEvent(10)); assertEquals(player.getComponent(HealthComponent.class).currentHealth, 10); @@ -66,13 +58,7 @@ public void restoreEventSentTest() { List list = receiver.getEvents(); assertTrue(list.isEmpty()); - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 0; - healthComponent.maxHealth = 100; - - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); + final EntityRef player = newPlayer(0); player.send(new DoRestoreEvent(10)); assertEquals(1, list.size()); @@ -80,13 +66,7 @@ public void restoreEventSentTest() { @Test public void restoreEventCancelTest() { - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 0; - healthComponent.maxHealth = 100; - - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); + final EntityRef player = newPlayer(0); TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, @@ -99,13 +79,7 @@ public void restoreEventCancelTest() { @Test public void restorationModifyEventTest() { - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 0; - healthComponent.maxHealth = 100; - - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); + final EntityRef player = newPlayer(0); TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, @@ -120,13 +94,7 @@ public void restorationModifyEventTest() { @Test public void restorationNegativeTest() { - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 50; - healthComponent.maxHealth = 100; - - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); + final EntityRef player = newPlayer(50); player.send(new DoRestoreEvent(-10)); // No healing happens when base restoration amount is negative, with no negative multipliers. @@ -135,13 +103,7 @@ public void restorationNegativeTest() { @Test public void restorationNegativeModifyEventTest() { - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 50; - healthComponent.maxHealth = 100; - - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); + final EntityRef player = newPlayer(50); TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, From 67c9d139fc92909e240dc883e236d1e7e26edee6 Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Sun, 21 Feb 2021 15:04:31 -0800 Subject: [PATCH 3/4] test: use try-with-resources around receivers so they don't leak between tests --- .../logic/health/RestorationTest.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/terasology/logic/health/RestorationTest.java b/src/test/java/org/terasology/logic/health/RestorationTest.java index 5189e25..42ee38f 100644 --- a/src/test/java/org/terasology/logic/health/RestorationTest.java +++ b/src/test/java/org/terasology/logic/health/RestorationTest.java @@ -53,27 +53,27 @@ public void restoreTest() { @Test public void restoreEventSentTest() { - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), - BeforeRestoreEvent.class); - List list = receiver.getEvents(); - assertTrue(list.isEmpty()); + try (TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), + BeforeRestoreEvent.class)) { + List list = receiver.getEvents(); + assertTrue(list.isEmpty()); - final EntityRef player = newPlayer(0); + final EntityRef player = newPlayer(0); - player.send(new DoRestoreEvent(10)); - assertEquals(1, list.size()); + player.send(new DoRestoreEvent(10)); + assertEquals(1, list.size()); + } } @Test public void restoreEventCancelTest() { final EntityRef player = newPlayer(0); - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), + try (TestEventReceiver ignored = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, - (event, entity) -> { - event.consume(); - }); - player.send(new DoRestoreEvent(10)); + (event, entity) -> event.consume())) { + player.send(new DoRestoreEvent(10)); + } assertEquals(0, player.getComponent(HealthComponent.class).currentHealth); } @@ -81,14 +81,15 @@ public void restoreEventCancelTest() { public void restorationModifyEventTest() { final EntityRef player = newPlayer(0); - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), + try (TestEventReceiver ignored = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, (event, entity) -> { event.add(5); event.multiply(2); - }); - // Expected value is ( initial:10 + 5 ) * 2 == 30 - player.send(new DoRestoreEvent(10)); + })) { + // Expected value is ( initial:10 + 5 ) * 2 == 30 + player.send(new DoRestoreEvent(10)); + } assertEquals(30, player.getComponent(HealthComponent.class).currentHealth); } @@ -105,14 +106,15 @@ public void restorationNegativeTest() { public void restorationNegativeModifyEventTest() { final EntityRef player = newPlayer(50); - TestEventReceiver receiver = new TestEventReceiver<>(helper.getHostContext(), + try (TestEventReceiver ignored = new TestEventReceiver<>(helper.getHostContext(), BeforeRestoreEvent.class, (event, entity) -> { event.multiply(-2); - }); - // Expected restoration value is ( initial:10 ) * (-2) == (-20) - // So, final health value: 50 + (-20) == 30 - player.send(new DoRestoreEvent(10)); + })) { + // Expected restoration value is ( initial:10 ) * (-2) == (-20) + // So, final health value: 50 + (-20) == 30 + player.send(new DoRestoreEvent(10)); + } assertEquals(30, player.getComponent(HealthComponent.class).currentHealth); } } From a32efe051e132202be5149925da03b0cd1590561 Mon Sep 17 00:00:00 2001 From: Kevin Turner <83819+keturn@users.noreply.github.com> Date: Sun, 21 Feb 2021 15:08:16 -0800 Subject: [PATCH 4/4] test: junit5 assertions --- .../terasology/logic/health/RegenTest.java | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/terasology/logic/health/RegenTest.java b/src/test/java/org/terasology/logic/health/RegenTest.java index 9eddb69..8c29c86 100644 --- a/src/test/java/org/terasology/logic/health/RegenTest.java +++ b/src/test/java/org/terasology/logic/health/RegenTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2019 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2021 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 package org.terasology.logic.health; import org.junit.jupiter.api.Test; @@ -29,8 +16,8 @@ import org.terasology.moduletestingenvironment.extension.Dependencies; import org.terasology.registry.In; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; @ExtendWith(MTEExtension.class) @Dependencies({"Health"})