From 84a79fe1de4b40b05c633df856a2a002a276d86a Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 7 Oct 2018 18:28:48 -0700 Subject: [PATCH] Fixed and added tests for new deathCount option Fixes test failures from commit a6d25d68a9a964a3de7976fb705cbb2589d85a1c I added some more tests to check for in world as well as death count being on/off. --- .../bentobox/listeners/DeathListenerTest.java | 69 +++++++++++++++---- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java index d2ab5305b..0dc7d2806 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java @@ -13,6 +13,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.entity.PlayerDeathEvent; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -21,6 +22,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @@ -29,19 +31,21 @@ import world.bentobox.bentobox.util.Util; @PrepareForTest({BentoBox.class, Util.class, Bukkit.class }) public class DeathListenerTest { + private Player player; + private BentoBox plugin; + private PlayersManager pm; + private WorldSettings worldSettings; + private World world; + private UUID uuid; + private IslandWorldManager iwm; - @Test - public void testDeathListener() { - assertNotNull(new DeathListener(mock(BentoBox.class))); - } - - @Test - public void testOnPlayerDeathEvent() { + @Before + public void setUp() { // Set up plugin - BentoBox plugin = mock(BentoBox.class); + plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); + iwm = mock(IslandWorldManager.class); when(iwm.inWorld(any(World.class))).thenReturn(true); when(iwm.inWorld(any(Location.class))).thenReturn(true); when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock"); @@ -49,16 +53,32 @@ public class DeathListenerTest { when(plugin.getIWM()).thenReturn(iwm); // Player - Player player = mock(Player.class); - World world = mock(World.class); + player = mock(Player.class); + world = mock(World.class); when(player.getWorld()).thenReturn(world); when(player.getLocation()).thenReturn(mock(Location.class)); - UUID uuid = UUID.randomUUID(); + uuid = UUID.randomUUID(); when(player.getUniqueId()).thenReturn(uuid); - PlayersManager pm = mock(PlayersManager.class); + pm = mock(PlayersManager.class); when(plugin.getPlayers()).thenReturn(pm); + worldSettings = mock(WorldSettings.class); + when(worldSettings.isDeathsCounted()).thenReturn(true); + // Deaths counted + when(iwm.getWorldSettings(Mockito.any())).thenReturn(worldSettings ); + + } + + + @Test + public void testDeathListener() { + assertNotNull(new DeathListener(mock(BentoBox.class))); + } + + @Test + public void testOnPlayerDeathEventDeathsCounted() { + // Test DeathListener dl = new DeathListener(plugin); @@ -67,4 +87,27 @@ public class DeathListenerTest { Mockito.verify(pm).addDeath(world, uuid); } + @Test + public void testOnPlayerDeathEventDeathsNotCounted() { + when(worldSettings.isDeathsCounted()).thenReturn(false); + // Test + DeathListener dl = new DeathListener(plugin); + + PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); + dl.onPlayerDeathEvent(e); + Mockito.verify(pm, Mockito.never()).addDeath(world, uuid); + } + + @Test + public void testOnPlayerDeathEventDeathsCountedNotInWorld() { + when(iwm.inWorld(any(Location.class))).thenReturn(false); + // Test + DeathListener dl = new DeathListener(plugin); + + PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); + dl.onPlayerDeathEvent(e); + Mockito.verify(pm, Mockito.never()).addDeath(world, uuid); + } + + }