From b83e5d4a56d929a7340521cd46c7a86d546f1a6c Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 5 Oct 2018 17:34:21 -0700 Subject: [PATCH] Fixed tests for Invincible visitors. --- .../flags/InvincibleVisitorsListener.java | 2 +- .../flags/InvincibleVisitorsListenerTest.java | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListener.java index e2b54631b..9b20c92fd 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListener.java @@ -115,7 +115,7 @@ public class InvincibleVisitorsListener extends AbstractFlagListener implements // Will be set back after the teleport p.setGameMode(GameMode.SPECTATOR); getIslands().getIslandAt(p.getLocation()).ifPresent(i -> new SafeSpotTeleport.Builder(getPlugin()).entity(p).island(i).build()); - } else if (getIslands().hasIsland(p.getWorld(), User.getInstance(p))) { + } else if (getIslands().hasIsland(p.getWorld(), p.getUniqueId())) { // No island in this location - if the player has an island try to teleport them back getIslands().homeTeleport(p.getWorld(), p); } else { diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListenerTest.java index 1f52af345..a4d7dd155 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/InvincibleVisitorsListenerTest.java @@ -54,6 +54,7 @@ public class InvincibleVisitorsListenerTest { private IslandsManager im; private List ivSettings; private Player player; + private Optional optionalIsland; /** * @throws java.lang.Exception @@ -104,6 +105,7 @@ public class InvincibleVisitorsListenerTest { Island island = mock(Island.class); when(island.getOwner()).thenReturn(uuid); when(im.getIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(island); + optionalIsland = Optional.of(island); // Visitor when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false); when(plugin.getIslands()).thenReturn(im); @@ -220,15 +222,39 @@ public class InvincibleVisitorsListenerTest { Mockito.verify(player, Mockito.never()).setGameMode(Mockito.eq(GameMode.SPECTATOR)); } + @Test - public void testOnVisitorGetDamageVoid() { - // For testing, have no island to teleport to - when(im.getIslandAt(Mockito.any())).thenReturn(Optional.empty()); + public void testOnVisitorGetDamageVoidIslandHere() { + when(im.getIslandAt(Mockito.any())).thenReturn(optionalIsland); EntityDamageEvent e = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.VOID, 0D); + // Player should be teleported to this island listener.onVisitorGetDamage(e); assertTrue(e.isCancelled()); Mockito.verify(player).setGameMode(Mockito.eq(GameMode.SPECTATOR)); - Mockito.verify(im).getIslandAt(Mockito.any()); } + @Test + public void testOnVisitorGetDamageVoidNoIslandHerePlayerHasNoIsland() { + when(im.getIslandAt(Mockito.any())).thenReturn(Optional.empty()); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + EntityDamageEvent e = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.VOID, 0D); + // Player should die + listener.onVisitorGetDamage(e); + assertFalse(e.isCancelled()); + Mockito.verify(player, Mockito.never()).setGameMode(Mockito.eq(GameMode.SPECTATOR)); + } + + @Test + public void testOnVisitorGetDamageVoidPlayerHasIsland() { + when(im.getIslandAt(Mockito.any())).thenReturn(Optional.empty()); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + EntityDamageEvent e = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.VOID, 0D); + // Player should be teleported to their island + listener.onVisitorGetDamage(e); + assertTrue(e.isCancelled()); + Mockito.verify(player, Mockito.never()).setGameMode(Mockito.eq(GameMode.SPECTATOR)); + Mockito.verify(im).homeTeleport(Mockito.any(), Mockito.eq(player)); + } + + }