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"}) diff --git a/src/test/java/org/terasology/logic/health/RestorationTest.java b/src/test/java/org/terasology/logic/health/RestorationTest.java index 12998af..42ee38f 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); @@ -61,72 +53,49 @@ 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()); - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 0; - healthComponent.maxHealth = 100; + final EntityRef player = newPlayer(0); - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); - - player.send(new DoRestoreEvent(10)); - assertEquals(1, list.size()); + player.send(new DoRestoreEvent(10)); + assertEquals(1, list.size()); + } } @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(), + 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); } @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(), + 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); } @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,22 +104,17 @@ public void restorationNegativeTest() { @Test public void restorationNegativeModifyEventTest() { - HealthComponent healthComponent = new HealthComponent(); - healthComponent.currentHealth = 50; - healthComponent.maxHealth = 100; + final EntityRef player = newPlayer(50); - final EntityRef player = entityManager.create(); - player.addComponent(new PlayerCharacterComponent()); - player.addComponent(healthComponent); - - 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); } } 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); }