2018-06-10 08:06:59 +02:00
|
|
|
/**
|
2018-06-25 02:57:31 +02:00
|
|
|
*
|
2018-06-10 08:06:59 +02:00
|
|
|
*/
|
2019-02-05 05:39:21 +01:00
|
|
|
package world.bentobox.bentobox.listeners.flags.worldsettings;
|
2018-06-10 08:06:59 +02:00
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
import org.powermock.api.mockito.PowerMockito;
|
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
|
import org.powermock.reflect.Whitebox;
|
|
|
|
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.BentoBox;
|
|
|
|
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
|
|
|
import world.bentobox.bentobox.lists.Flags;
|
|
|
|
import world.bentobox.bentobox.managers.IslandWorldManager;
|
|
|
|
import world.bentobox.bentobox.managers.IslandsManager;
|
|
|
|
import world.bentobox.bentobox.util.Util;
|
2018-06-10 08:06:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@RunWith(PowerMockRunner.class)
|
2018-07-29 22:21:46 +02:00
|
|
|
@PrepareForTest({BentoBox.class, Util.class })
|
2018-06-10 08:06:59 +02:00
|
|
|
public class RemoveMobsListenerTest {
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
private IslandsManager im;
|
|
|
|
private World world;
|
|
|
|
private Location inside;
|
|
|
|
private Player player;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws java.lang.Exception
|
|
|
|
*/
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
// Set up plugin
|
2018-07-29 22:21:46 +02:00
|
|
|
BentoBox plugin = mock(BentoBox.class);
|
|
|
|
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
// World
|
|
|
|
world = mock(World.class);
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
// Owner
|
2018-08-05 06:50:10 +02:00
|
|
|
UUID uuid1 = UUID.randomUUID();
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
// Island initialization
|
2018-08-05 06:50:10 +02:00
|
|
|
Island island = mock(Island.class);
|
|
|
|
when(island.getOwner()).thenReturn(uuid1);
|
2018-06-10 08:06:59 +02:00
|
|
|
|
|
|
|
im = mock(IslandsManager.class);
|
|
|
|
when(plugin.getIslands()).thenReturn(im);
|
|
|
|
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
|
|
|
|
|
|
|
|
inside = mock(Location.class);
|
|
|
|
when(inside.getWorld()).thenReturn(world);
|
|
|
|
|
|
|
|
Optional<Island> opIsland = Optional.ofNullable(island);
|
|
|
|
when(im.getProtectedIslandAt(Mockito.eq(inside))).thenReturn(opIsland);
|
|
|
|
// On island
|
|
|
|
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(Util.class);
|
|
|
|
when(Util.getWorld(Mockito.any())).thenReturn(world);
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
// World Settings
|
|
|
|
IslandWorldManager iwm = mock(IslandWorldManager.class);
|
|
|
|
when(plugin.getIWM()).thenReturn(iwm);
|
|
|
|
WorldSettings ws = mock(WorldSettings.class);
|
|
|
|
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
|
|
|
|
Map<String, Boolean> worldFlags = new HashMap<>();
|
|
|
|
when(ws.getWorldFlags()).thenReturn(worldFlags);
|
|
|
|
Flags.REMOVE_MOBS.setSetting(world, true);
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
// Sometimes use Mockito.withSettings().verboseLogging()
|
|
|
|
player = mock(Player.class);
|
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
when(player.getUniqueId()).thenReturn(uuid);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 10:22:44 +01:00
|
|
|
* Test method for {@link RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
|
2018-06-10 08:06:59 +02:00
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testOnUserTeleport() {
|
|
|
|
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
|
|
new RemoveMobsListener().onUserTeleport(e);
|
2018-06-25 02:57:31 +02:00
|
|
|
Mockito.verify(im).clearArea(Mockito.any());
|
2018-06-10 08:06:59 +02:00
|
|
|
}
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
/**
|
2019-02-03 10:22:44 +01:00
|
|
|
* Test method for {@link RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
|
2018-06-10 08:06:59 +02:00
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testOnUserTeleportDoNotRemove() {
|
|
|
|
Flags.REMOVE_MOBS.setSetting(world, false);
|
|
|
|
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
|
|
new RemoveMobsListener().onUserTeleport(e);
|
2018-06-25 02:57:31 +02:00
|
|
|
Mockito.verify(im, Mockito.never()).clearArea(Mockito.any());
|
2018-06-10 08:06:59 +02:00
|
|
|
}
|
2018-06-25 02:57:31 +02:00
|
|
|
|
2018-06-10 08:06:59 +02:00
|
|
|
/**
|
2019-02-03 10:22:44 +01:00
|
|
|
* Test method for {@link RemoveMobsListener#onUserTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
|
2018-06-10 08:06:59 +02:00
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testOnUserTeleportToNotIsland() {
|
|
|
|
// Not on island
|
|
|
|
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
|
|
|
|
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, inside, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
|
|
new RemoveMobsListener().onUserTeleport(e);
|
2018-06-25 02:57:31 +02:00
|
|
|
Mockito.verify(im, Mockito.never()).clearArea(Mockito.any());
|
2018-06-10 08:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|