From 29c50574c357f922b963bb600cdc1ddf98e5a012 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 4 Oct 2024 22:25:43 -0700 Subject: [PATCH] Fix tests, optimize tests, reord imports. --- .../commands/admin/AdminResetHomeCommand.java | 2 - .../world/bentobox/bentobox/lists/Flags.java | 2 +- .../bentobox/managers/island/IslandGrid.java | 1 - .../world/bentobox/bentobox/util/Util.java | 16 +- .../flags => }/AbstractCommonSetup.java | 82 +++++++- .../RanksManagerBeforeClassTest.java | 9 +- .../world/bentobox/bentobox/TestBentoBox.java | 1 - .../commands/admin/AdminInfoCommandTest.java | 24 +-- .../admin/AdminResetFlagsCommandTest.java | 28 +-- .../admin/AdminSetrankCommandTest.java | 2 +- .../admin/AdminSettingsCommandTest.java | 2 +- .../admin/range/AdminRangeCommandTest.java | 35 +--- .../range/AdminRangeResetCommandTest.java | 41 ++-- .../admin/range/AdminRangeSetCommandTest.java | 48 ++--- .../team/AdminTeamDisbandCommandTest.java | 46 +---- .../island/DefaultPlayerCommandTest.java | 16 +- .../commands/island/IslandBanCommandTest.java | 43 ++-- .../island/IslandBanlistCommandTest.java | 22 +- .../island/IslandDeletehomeCommandTest.java | 32 +-- .../island/IslandExpelCommandTest.java | 41 ++-- .../commands/island/IslandGoCommandTest.java | 63 +++--- .../island/IslandInfoCommandTest.java | 37 +--- .../island/IslandResetCommandTest.java | 37 +--- .../island/IslandSpawnCommandTest.java | 54 ++--- .../island/IslandUnbanCommandTest.java | 26 +-- .../island/team/IslandTeamCommandTest.java | 38 +--- .../team/IslandTeamCoopCommandTest.java | 12 +- .../team/IslandTeamInviteCommandTest.java | 33 +-- .../team/IslandTeamKickCommandTest.java | 21 +- .../team/IslandTeamPromoteCommandTest.java | 29 +-- .../team/IslandTeamTrustCommandTest.java | 50 +++-- .../team/IslandTeamUncoopCommandTest.java | 28 +-- .../team/IslandTeamUntrustCommandTest.java | 37 +--- .../bentobox/bentobox/api/user/UserTest.java | 193 ++++++++---------- .../commands/BentoBoxPermsCommandTest.java | 37 +--- .../bentobox/hooks/ItemsAdderHookTest.java | 2 +- .../bentobox/listeners/DeathListenerTest.java | 2 +- .../listeners/JoinLeaveListenerTest.java | 92 +++------ .../StandardSpawnProtectionListenerTest.java | 103 ++++------ .../CommandRankClickListenerTest.java | 2 +- .../BlockInteractionListenerTest.java | 2 +- .../protection/BreakBlocksListenerTest.java | 2 +- .../protection/BreedingListenerTest.java | 2 +- .../flags/protection/BucketListenerTest.java | 2 +- .../flags/protection/CandleListenerTest.java | 2 +- .../flags/protection/EggListenerTest.java | 2 +- .../flags/protection/ElytraListenerTest.java | 2 +- .../EntityInteractListenerTest.java | 2 +- .../ExperiencePickupListenerTest.java | 2 +- .../flags/protection/HurtingListenerTest.java | 2 +- .../protection/InventoryListenerTest.java | 2 +- .../PhysicalInteractionListenerTest.java | 2 +- .../protection/PlaceBlocksListenerTest.java | 2 +- .../protection/SculkSensorListenerTest.java | 2 +- .../flags/protection/TNTListenerTest.java | 2 +- .../protection/ThrowingListenerTest.java | 2 +- .../ChestDamageListenerTest.java | 2 +- .../worldsettings/CreeperListenerTest.java | 2 +- .../worldsettings/EnderChestListenerTest.java | 2 +- .../IslandRespawnListenerTest.java | 2 +- .../ObsidianScoopingListenerTest.java | 62 ++---- .../PetTeleportListenerTest.java | 2 +- .../VisitorKeepInventoryListenerTest.java | 69 ++----- .../worldsettings/WitherListenerTest.java | 2 +- .../teleports/EntityTeleportListenerTest.java | 2 +- .../bentobox/managers/IslandsManagerTest.java | 2 +- .../managers/island/IslandCacheTest.java | 2 +- .../bentobox/bentobox/util/UtilTest.java | 5 + 68 files changed, 592 insertions(+), 983 deletions(-) rename src/test/java/world/bentobox/bentobox/{listeners/flags => }/AbstractCommonSetup.java (68%) rename src/test/java/world/bentobox/bentobox/{managers => }/RanksManagerBeforeClassTest.java (96%) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminResetHomeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminResetHomeCommand.java index 135e33c48..cd70db37d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminResetHomeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminResetHomeCommand.java @@ -138,9 +138,7 @@ public Optional> tabComplete(User user, String alias, List Map getNameIslandMap(User user) { Map islandMap = new HashMap<>(); int index = 0; - System.out.println("Getting for " + user.getName()); for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { - System.out.println("Island - " + island); index++; if (island.getName() != null && !island.getName().isBlank()) { // Name has been set diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 15eb623d7..0a9b71776 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -25,6 +25,7 @@ import world.bentobox.bentobox.listeners.flags.protection.ElytraListener; import world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener; import world.bentobox.bentobox.listeners.flags.protection.ExperiencePickupListener; +import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener; import world.bentobox.bentobox.listeners.flags.protection.FireListener; import world.bentobox.bentobox.listeners.flags.protection.HurtingListener; import world.bentobox.bentobox.listeners.flags.protection.InventoryListener; @@ -39,7 +40,6 @@ import world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener; import world.bentobox.bentobox.listeners.flags.protection.SculkShriekerListener; import world.bentobox.bentobox.listeners.flags.protection.ShearingListener; -import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener; import world.bentobox.bentobox.listeners.flags.protection.TeleportationListener; import world.bentobox.bentobox.listeners.flags.protection.ThrowingListener; import world.bentobox.bentobox.listeners.flags.settings.DecayListener; diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java index d7d7b2c43..11a76c367 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java @@ -1,7 +1,6 @@ package world.bentobox.bentobox.managers.island; import java.util.Map.Entry; - import java.util.TreeMap; import world.bentobox.bentobox.database.objects.Island; diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index 2ee05e934..411409f8b 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -28,7 +28,21 @@ import org.bukkit.attribute.Attribute; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.entity.*; +import org.bukkit.entity.Allay; +import org.bukkit.entity.Animals; +import org.bukkit.entity.Bat; +import org.bukkit.entity.EnderDragon; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Flying; +import org.bukkit.entity.IronGolem; +import org.bukkit.entity.Monster; +import org.bukkit.entity.Player; +import org.bukkit.entity.PufferFish; +import org.bukkit.entity.Shulker; +import org.bukkit.entity.Slime; +import org.bukkit.entity.Snowman; +import org.bukkit.entity.Tameable; +import org.bukkit.entity.WaterMob; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java similarity index 68% rename from src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java rename to src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java index 8bb864e37..11adee0ad 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java +++ b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java @@ -1,8 +1,11 @@ -package world.bentobox.bentobox.listeners.flags; +package world.bentobox.bentobox; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Collections; @@ -19,6 +22,7 @@ import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.inventory.ItemFactory; @@ -31,6 +35,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; @@ -40,8 +45,7 @@ import com.google.common.collect.ImmutableSet; -import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.Settings; +import net.md_5.bungee.api.chat.TextComponent; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.User; @@ -97,6 +101,8 @@ public abstract class AbstractCommonSetup { protected Notifier notifier; @Mock protected FlagsManager fm; + @Mock + protected Spigot spigot; public void setUp() throws Exception { @@ -129,6 +135,7 @@ public void setUp() throws Exception { when(mockPlayer.getWorld()).thenReturn(world); when(mockPlayer.getName()).thenReturn("tastybento"); when(mockPlayer.getInventory()).thenReturn(inv); + when(mockPlayer.spigot()).thenReturn(spigot); User.setPlugin(plugin); User.clearUsers(); @@ -176,12 +183,13 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(settings); when(settings.getFakePlayers()).thenReturn(new HashSet<>()); - PowerMockito.mockStatic(Util.class); + PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS); when(Util.getWorld(any())).thenReturn(mock(World.class)); + // Util when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Util translate color codes (used in user translate methods) - when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + //when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); // Tags for (Material m : Material.values()) { @@ -217,6 +225,70 @@ public void tearDown() throws Exception { Mockito.framework().clearInlineMocks(); } + /** + * Check that spigot sent the message + * @param message - message to check + */ + public void checkSpigotMessage(String expectedMessage) { + checkSpigotMessage(expectedMessage, 1); + } + + /* + public void checkSpigotMessage(String expectedMessage, boolean shouldBePresent) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + + if (shouldBePresent) { + // If we expect a message to be present, verify that sendMessage() was called at least once + verify(spigot, atLeastOnce()).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Check if any captured message contains the expected text + boolean messageFound = capturedMessages.stream() + .map(component -> component.toPlainText()) // Convert each TextComponent to plain text + .anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the expected message is present + + // Assert that the message was found + assertTrue("Expected message not found: " + expectedMessage, messageFound); + + } else { + // If we expect no messages with this text, capture any sent messages to ensure none match the given message + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Check that none of the captured messages contain the forbidden text + boolean messageFound = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text + .anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the message is present + + // Assert that the message was NOT found + assertFalse("Unexpected message found: " + expectedMessage, messageFound); + } + }*/ + + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + + // Verify that sendMessage() was called at least 0 times (capture any sent messages) + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Count the number of occurrences of the expectedMessage in the captured messages + long actualOccurrences = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text + .count(); // Count how many times the expected message appears + + // Assert that the number of occurrences matches the expectedOccurrences + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, + actualOccurrences); + } + /** * Get the explode event * @param entity diff --git a/src/test/java/world/bentobox/bentobox/managers/RanksManagerBeforeClassTest.java b/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java similarity index 96% rename from src/test/java/world/bentobox/bentobox/managers/RanksManagerBeforeClassTest.java rename to src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java index ff956dfb9..4f6f589b8 100644 --- a/src/test/java/world/bentobox/bentobox/managers/RanksManagerBeforeClassTest.java +++ b/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java @@ -1,4 +1,4 @@ -package world.bentobox.bentobox.managers; +package world.bentobox.bentobox; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -29,10 +29,10 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.DatabaseSetup; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento @@ -40,7 +40,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, DatabaseSetup.class, RanksManager.class, Bukkit.class }) -public abstract class RanksManagerBeforeClassTest { +public abstract class RanksManagerBeforeClassTest extends AbstractCommonSetup { // Constants that define the hard coded rank values public static final String ADMIN_RANK_REF = "ranks.admin"; @@ -68,8 +68,6 @@ public abstract class RanksManagerBeforeClassTest { TRUSTED_RANK_REF, TRUSTED_RANK, COOP_RANK_REF, COOP_RANK, VISITOR_RANK_REF, VISITOR_RANK, BANNED_RANK_REF, BANNED_RANK); - @Mock - public BentoBox plugin; @Mock public RanksManager rm; @@ -113,6 +111,7 @@ public static void beforeClass() throws IllegalAccessException, InvocationTarget @Before public void setUp() throws Exception { + super.setUp(); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(Bukkit.getBukkitVersion()).thenReturn(""); // Set up plugin diff --git a/src/test/java/world/bentobox/bentobox/TestBentoBox.java b/src/test/java/world/bentobox/bentobox/TestBentoBox.java index 170cbb918..59326b9c8 100644 --- a/src/test/java/world/bentobox/bentobox/TestBentoBox.java +++ b/src/test/java/world/bentobox/bentobox/TestBentoBox.java @@ -48,7 +48,6 @@ import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.FlagsManager; diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminInfoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminInfoCommandTest.java index 67f01afb7..4bb2f9d0d 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminInfoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminInfoCommandTest.java @@ -17,12 +17,8 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.NonNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,16 +30,15 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util; /** @@ -59,27 +54,15 @@ public class AdminInfoCommandTest extends RanksManagerBeforeClassTest { @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; private Island island; private AdminInfoCommand iic; - @Mock - private Player player; - @Mock - private World world; @Mock private PlaceholdersManager phm; - @Mock - private @NonNull Location location; - @Mock - private IslandWorldManager iwm; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -96,12 +79,12 @@ public void setUp() throws Exception { when(plugin.getCommandsManager()).thenReturn(cm); // Player - when(player.isOp()).thenReturn(false); + when(mockPlayer.isOp()).thenReturn(false); UUID uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); when(user.getName()).thenReturn("tastybento"); when(user.getWorld()).thenReturn(world); - when(user.getPlayer()).thenReturn(player); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.isPlayer()).thenReturn(true); //user = User.getInstance(player); // Set the User class plugin as this one @@ -164,6 +147,7 @@ public void testExecuteUserStringListOfStringTooManyArgs() { @Test public void testExecuteUserStringListOfStringNoArgsConsole() { CommandSender console = mock(CommandSender.class); + when(console.spigot()).thenReturn(spigot); User sender = User.getInstance(console); assertFalse(iic.execute(sender, "", Collections.emptyList())); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console"); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java index 4fda23321..3e2be1e67 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -19,7 +18,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -30,11 +28,10 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -43,25 +40,23 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.FlagsManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) -public class AdminResetFlagsCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) +public class AdminResetFlagsCommandTest extends AbstractCommonSetup { @Mock private CompositeCommand ac; private final UUID uuid = UUID.randomUUID(); @Mock - private IslandsManager im; - @Mock private PlayersManager pm; @Mock private FlagsManager fm; @@ -71,8 +66,6 @@ public class AdminResetFlagsCommandTest { private Flag flag2; @Mock private Flag flag3; - @Mock - private Player player; private AdminResetFlagsCommand arf; private @Nullable User user; @@ -81,10 +74,7 @@ public class AdminResetFlagsCommandTest { */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // Command manager CommandsManager cm = mock(CommandsManager.class); @@ -96,8 +86,8 @@ public void setUp() throws Exception { when(ac.getPermissionPrefix()).thenReturn("bskyblock."); // Player - when(player.getUniqueId()).thenReturn(uuid); - user = User.getInstance(player); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + user = User.getInstance(mockPlayer); // Flags manager when(plugin.getFlagsManager()).thenReturn(fm); @@ -175,7 +165,7 @@ public void testSetup() { public void testExecuteUserStringListOfStringTwoArgs() { List args = Arrays.asList("sdfsd", "werwerw"); assertFalse(arf.execute(user, "", args)); - verify(player).sendMessage(eq("commands.help.header")); + checkSpigotMessage("commands.help.header"); } /** @@ -184,7 +174,7 @@ public void testExecuteUserStringListOfStringTwoArgs() { @Test public void testExecuteUserStringListOfStringOneArgNotFlag() { assertFalse(arf.execute(user, "", Collections.singletonList("FLAG3"))); - verify(player).sendMessage(eq("commands.help.header")); + checkSpigotMessage("commands.help.header"); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommandTest.java index 4dc8c42e8..edd20932c 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSetrankCommandTest.java @@ -30,13 +30,13 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util; /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java index 007bd91d7..d4e73e4a4 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java @@ -40,6 +40,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; @@ -51,7 +52,6 @@ import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util; /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java index 1e001f1de..0d87b0d1e 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java @@ -11,78 +11,64 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) -public class AdminRangeCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) +public class AdminRangeCommandTest extends AbstractCommonSetup { + @Mock private CompositeCommand ac; + @Mock private User user; /** */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); Util.setPlugin(plugin); // Command manager CommandsManager cm = mock(CommandsManager.class); when(plugin.getCommandsManager()).thenReturn(cm); - // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); + // User when(user.isOp()).thenReturn(false); UUID uuid = UUID.randomUUID(); - UUID notUUID = UUID.randomUUID(); - while (notUUID.equals(uuid)) { - notUUID = UUID.randomUUID(); - } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getTranslation("commands.help.console")).thenReturn("Console"); User.setPlugin(plugin); // Parent command has no aliases - ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getWorld()).thenReturn(mock(World.class)); // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); // Player has island to begin with - IslandsManager im = mock(IslandsManager.class); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(plugin.getIslands()).thenReturn(im); @@ -117,10 +103,11 @@ public void tearDown() { public void testExecuteConsoleNoArgs() { AdminRangeCommand arc = new AdminRangeCommand(ac); CommandSender sender = mock(CommandSender.class); + when(sender.spigot()).thenReturn(spigot); User console = User.getInstance(sender); arc.execute(console, "", new ArrayList<>()); // Show help - Mockito.verify(sender).sendMessage("commands.help.header"); + checkSpigotMessage("commands.help.header"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java index 9cc92d44e..49fd2c3ac 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java @@ -2,6 +2,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -14,7 +15,6 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.junit.After; @@ -24,19 +24,16 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @@ -46,13 +43,14 @@ * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) -public class AdminRangeResetCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) +public class AdminRangeResetCommandTest extends AbstractCommonSetup { + @Mock private CompositeCommand ac; private UUID uuid; + @Mock private User user; - private IslandsManager im; private PlayersManager pm; @Mock private PluginManager pim; @@ -61,21 +59,14 @@ public class AdminRangeResetCommandTest { */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + super.setUp(); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); Util.setPlugin(plugin); // Command manager CommandsManager cm = mock(CommandsManager.class); when(plugin.getCommandsManager()).thenReturn(cm); - // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); UUID notUUID = UUID.randomUUID(); @@ -83,23 +74,20 @@ public void setUp() throws Exception { notUUID = UUID.randomUUID(); } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); User.setPlugin(plugin); // Parent command has no aliases - ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getWorld()).thenReturn(mock(World.class)); // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getIslandProtectionRange(Mockito.any())).thenReturn(200); when(plugin.getIWM()).thenReturn(iwm); // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); Island island = mock(Island.class); @@ -142,10 +130,11 @@ public void tearDown() { public void testExecuteConsoleNoArgs() { AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); CommandSender sender = mock(CommandSender.class); + when(sender.spigot()).thenReturn(spigot); User console = User.getInstance(sender); arc.execute(console, "", new ArrayList<>()); // Show help - Mockito.verify(sender).sendMessage("commands.help.header"); + checkSpigotMessage("commands.help.header"); } /** @@ -157,7 +146,7 @@ public void testExecutePlayerNoArgs() { AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); arc.execute(user, "", new ArrayList<>()); // Show help - Mockito.verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock"); + verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock"); } /** @@ -169,7 +158,7 @@ public void testExecuteUnknownPlayer() { AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); String[] name = { "tastybento" }; arc.execute(user, "", Arrays.asList(name)); - Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]); + verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]); } /** @@ -184,7 +173,7 @@ public void testExecuteKnownPlayerNotOwnerNoTeam() { List args = new ArrayList<>(); args.add("tastybento"); arc.execute(user, "", args); - Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); + verify(user).sendMessage("general.errors.player-has-no-island"); } @Test @@ -196,7 +185,7 @@ public void testExecuteKnownPlayerNotOwnerButInTeam() { List args = new ArrayList<>(); args.add("tastybento"); arc.execute(user, "", args); - Mockito.verify(user, never()).sendMessage("general.errors.player-has-no-island"); + verify(user, never()).sendMessage("general.errors.player-has-no-island"); } /** @@ -209,7 +198,7 @@ public void testExecuteKnownPlayer() { List args = new ArrayList<>(); args.add("tastybento"); arc.execute(user, "", args); - Mockito.verify(user).sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, "200"); + verify(user).sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, "200"); } } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java index 99d144034..01ed546c7 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java @@ -17,14 +17,10 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,19 +28,15 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @@ -54,64 +46,49 @@ * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) -public class AdminRangeSetCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) +public class AdminRangeSetCommandTest extends AbstractCommonSetup { + @Mock + private CommandsManager cm; + + @Mock private CompositeCommand ac; private UUID uuid; + @Mock private User user; - private IslandsManager im; private PlayersManager pm; - @Mock - private PluginManager pim; - @Mock - private @NonNull Location location; /** */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); + Util.setPlugin(plugin); // Command manager - CommandsManager cm = mock(CommandsManager.class); when(plugin.getCommandsManager()).thenReturn(cm); - // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); - UUID notUUID = UUID.randomUUID(); - while (notUUID.equals(uuid)) { - notUUID = UUID.randomUUID(); - } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); User.setPlugin(plugin); // Parent command has no aliases - ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getWorld()).thenReturn(mock(World.class)); // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getIslandProtectionRange(any())).thenReturn(200); when(plugin.getIWM()).thenReturn(iwm); // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true); - Island island = mock(Island.class); when(island.getRange()).thenReturn(50); when(island.getProtectionRange()).thenReturn(50); when(location.toVector()).thenReturn(new Vector(2, 3, 4)); @@ -155,10 +132,11 @@ public void tearDown() { public void testExecuteConsoleNoArgs() { AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); CommandSender sender = mock(CommandSender.class); + when(sender.spigot()).thenReturn(spigot); User console = User.getInstance(sender); assertFalse(arc.canExecute(console, "", new ArrayList<>())); // Show help - verify(sender).sendMessage("commands.help.header"); + checkSpigotMessage("commands.help.header"); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java index 5ec46b4e6..70aa816f9 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java @@ -21,13 +21,10 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -38,18 +35,15 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; import com.google.common.collect.ImmutableSet; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; @@ -61,7 +55,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) -public class AdminTeamDisbandCommandTest { +public class AdminTeamDisbandCommandTest extends AbstractCommonSetup { @Mock private CompositeCommand ac; @@ -69,31 +63,17 @@ public class AdminTeamDisbandCommandTest { @Mock private User user; @Mock - private Player p; - @Mock private Player p2; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; - @Mock - private PluginManager pim; + private UUID notUUID; - @Mock - private @Nullable Island island; - @Mock - private @NonNull Location location; + private AdminTeamDisbandCommand itl; - /** - */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); Util.setPlugin(plugin); // Command manager @@ -109,25 +89,21 @@ public void setUp() throws Exception { notUUID = UUID.randomUUID(); } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); User.setPlugin(plugin); // Set up users - when(p.getUniqueId()).thenReturn(uuid); + when(mockPlayer.getUniqueId()).thenReturn(uuid); when(p2.getUniqueId()).thenReturn(notUUID); - User.getInstance(p); + User.getInstance(mockPlayer); + when(p2.spigot()).thenReturn(spigot); User.getInstance(p2); // Parent command has no aliases ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); - // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); - when(plugin.getIWM()).thenReturn(iwm); - // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(island.getOwner()).thenReturn(uuid); @@ -166,6 +142,7 @@ public void setUp() throws Exception { // Online players PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); + when(Util.getOnlinePlayerList(user)).thenReturn(List.of("tastybento", "BONNe")); when(Util.translateColorCodes(anyString())) .thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); @@ -246,8 +223,7 @@ public void testExecuteSuccess() { verify(im, never()).removePlayer(island, uuid); verify(im).removePlayer(island, notUUID); verify(user).sendMessage("commands.admin.team.disband.success", TextVariables.NAME, "tastybento"); - verify(p).sendMessage("commands.admin.team.disband.disbanded"); - verify(p2).sendMessage("commands.admin.team.disband.disbanded"); + checkSpigotMessage("commands.admin.team.disband.disbanded", 2); // 2 + 1 verify(pim, times(3)).callEvent(any()); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommandTest.java index d703ef44a..59d40d9d3 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommandTest.java @@ -14,32 +14,29 @@ import org.bukkit.Bukkit; import org.bukkit.World; -import org.eclipse.jdt.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandsManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, IslandsManager.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, IslandsManager.class, Util.class }) public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -49,10 +46,6 @@ public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest { private WorldSettings ws; @Mock private User user; - @Mock - private IslandsManager im; - @Mock - private @Nullable Island island; class PlayerCommand extends DefaultPlayerCommand { @@ -68,7 +61,6 @@ protected PlayerCommand(GameModeAddon addon) { @Before public void setUp() throws Exception { super.setUp(); - PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); // User when(user.getUniqueId()).thenReturn(UUID.randomUUID()); @@ -85,8 +77,6 @@ public void setUp() throws Exception { when(ws.getDefaultPlayerAction()).thenReturn("go"); when(ws.getDefaultNewPlayerAction()).thenReturn("create"); - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - when(ws.getPlayerCommandAliases()).thenReturn("island is"); when(addon.getWorldSettings()).thenReturn(ws); dpc = new PlayerCommand(addon); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java index 4ea40b3b6..62b2b2e74 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java @@ -39,27 +39,26 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, Util.class }) public class IslandBanCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -68,16 +67,10 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest { @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; @Mock - private Island island; - @Mock private Addon addon; private IslandBanCommand ibc; - @Mock - private Player targetPlayer; @Before public void setUp() throws Exception { @@ -153,13 +146,13 @@ public void setUp() throws Exception { // Target bill - default target. Non Op, online, no ban prevention permission UUID uuid = UUID.randomUUID(); when(pm.getUUID(anyString())).thenReturn(uuid); - when(targetPlayer.getName()).thenReturn("bill"); - when(targetPlayer.getDisplayName()).thenReturn("&Cbill"); - when(targetPlayer.getUniqueId()).thenReturn(uuid); - when(targetPlayer.isOp()).thenReturn(false); - when(targetPlayer.isOnline()).thenReturn(true); - when(targetPlayer.hasPermission(anyString())).thenReturn(false); - User.getInstance(targetPlayer); + when(mockPlayer.getName()).thenReturn("bill"); + when(mockPlayer.getDisplayName()).thenReturn("&Cbill"); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + when(mockPlayer.isOp()).thenReturn(false); + when(mockPlayer.isOnline()).thenReturn(true); + when(mockPlayer.hasPermission(anyString())).thenReturn(false); + User.getInstance(mockPlayer); // Island Ban Command ibc = new IslandBanCommand(ic); @@ -242,15 +235,15 @@ public void testBanAlreadyBanned() { @Test public void testBanOp() { - when(targetPlayer.isOp()).thenReturn(true); + when(mockPlayer.isOp()).thenReturn(true); assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill"))); verify(user).sendMessage("commands.island.ban.cannot-ban"); } @Test public void testBanOnlineNoBanPermission() { - when(targetPlayer.hasPermission(anyString())).thenReturn(true); - User.getInstance(targetPlayer); + when(mockPlayer.hasPermission(anyString())).thenReturn(true); + User.getInstance(mockPlayer); assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("billy"))); verify(user).sendMessage("commands.island.ban.cannot-ban"); @@ -258,7 +251,7 @@ public void testBanOnlineNoBanPermission() { @Test public void testBanOfflineUserSuccess() { - when(targetPlayer.isOnline()).thenReturn(false); + when(mockPlayer.isOnline()).thenReturn(false); assertTrue(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill"))); // Allow adding to ban list @@ -266,7 +259,7 @@ public void testBanOfflineUserSuccess() { // Run execute assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill", TextVariables.DISPLAY_NAME, "&Cbill"); - verify(targetPlayer).sendMessage("commands.island.ban.owner-banned-you"); + checkSpigotMessage("commands.island.ban.owner-banned-you"); } @Test @@ -279,7 +272,7 @@ public void testBanOnlineUserSuccess() { assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill", TextVariables.DISPLAY_NAME, "&Cbill"); - verify(targetPlayer).sendMessage("commands.island.ban.owner-banned-you"); + checkSpigotMessage("commands.island.ban.owner-banned-you"); } @Test @@ -291,8 +284,8 @@ public void testCancelledBan() { assertFalse(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); verify(user, never()).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, - targetPlayer.getName(), TextVariables.DISPLAY_NAME, targetPlayer.getDisplayName()); - verify(targetPlayer, never()).sendMessage("commands.island.ban.owner-banned-you"); + mockPlayer.getName(), TextVariables.DISPLAY_NAME, mockPlayer.getDisplayName()); + verify(mockPlayer, never()).sendMessage("commands.island.ban.owner-banned-you"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java index 5e10b916a..67b396679 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java @@ -19,7 +19,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -31,40 +30,31 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandBanlistCommandTest extends RanksManagerBeforeClassTest { @Mock private CompositeCommand ic; - private UUID uuid; @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; - @Mock - private Island island; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -78,12 +68,10 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1); // Unlimited bans when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); @@ -112,9 +100,7 @@ public void setUp() throws Exception { when(im.getIsland(any(), any(UUID.class))).thenReturn(island); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); - when(plugin.getIWM()).thenReturn(iwm); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandDeletehomeCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandDeletehomeCommandTest.java index a95434c81..1ff0a42bb 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandDeletehomeCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandDeletehomeCommandTest.java @@ -18,59 +18,43 @@ import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.eclipse.jdt.annotation.NonNull; import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandDeletehomeCommandTest extends RanksManagerBeforeClassTest { @Mock private CompositeCommand ic; - private UUID uuid; + @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; - @Mock - private Island island; private IslandDeletehomeCommand idh; - @Mock - private IslandWorldManager iwm; - @Mock - private @NonNull World world; - @Mock - private Location location; /** * @throws java.lang.Exception @@ -99,13 +83,11 @@ public void setUp() throws Exception { when(ic.getSubCommand(Mockito.anyString())).thenReturn(Optional.empty()); when(ic.getAddon()).thenReturn(addon); when(ic.getWorld()).thenReturn(world); - when(plugin.getIslands()).thenReturn(im); // Player - Player player = mock(Player.class); when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(player); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getWorld()).thenReturn(world); when(user.getTranslation(anyString())).thenAnswer(i -> i.getArgument(0, String.class)); @@ -131,10 +113,6 @@ public void setUp() throws Exception { when(iwm.isEnd(any())).thenReturn(false); // Number of homes default when(iwm.getMaxHomes(any())).thenReturn(3); - when(plugin.getIWM()).thenReturn(iwm); - - // Bukkit - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); idh = new IslandDeletehomeCommand(ic); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java index a21b03bd3..fed4c3d5a 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java @@ -30,15 +30,14 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.addons.Addon; @@ -48,20 +47,18 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -70,23 +67,15 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; @Mock - private Island island; - @Mock private LocalesManager lm; @Mock - private IslandWorldManager iwm; - @Mock private Addon addon; private IslandExpelCommand iec; @Mock private Server server; - @Mock - private Player p; @Before public void setUp() throws Exception { @@ -108,10 +97,11 @@ public void setUp() throws Exception { uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); - when(p.getServer()).thenReturn(server); - when(user.getPlayer()).thenReturn(p); + when(mockPlayer.getServer()).thenReturn(server); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento"); + when(user.isOnline()).thenReturn(true); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); // Parent command has no aliases @@ -130,7 +120,6 @@ public void setUp() throws Exception { // Server & Scheduler BukkitScheduler sch = mock(BukkitScheduler.class); - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(Bukkit.getScheduler()).thenReturn(sch); // Island Banned list initialization @@ -296,7 +285,7 @@ public void testCanExecuteOfflinePlayer() { public void testCanExecuteInvisiblePlayer() { when(im.hasIsland(any(), any(User.class))).thenReturn(true); Player t = setUpTarget(); - when(p.canSee(t)).thenReturn(false); + when(mockPlayer.canSee(t)).thenReturn(false); //when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); assertFalse(iec.canExecute(user, "", Collections.singletonList("tasty"))); verify(user).sendMessage("general.errors.offline-player"); @@ -365,10 +354,11 @@ private Player setUpTarget() { when(t.getName()).thenReturn("target"); when(t.getDisplayName()).thenReturn("&Ctarget"); when(t.getServer()).thenReturn(server); + when(t.spigot()).thenReturn(spigot); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); User.getInstance(t); when(pm.getUUID(anyString())).thenReturn(target); - when(p.canSee(t)).thenReturn(true); + when(mockPlayer.canSee(t)).thenReturn(true); when(Bukkit.getPlayer(target)).thenReturn(t); return t; } @@ -472,20 +462,25 @@ public void testTabCompleteUserStringListPlayersOnIsland() { List list = new ArrayList<>(); Player p1 = mock(Player.class); when(p1.getName()).thenReturn("normal"); - when(p.canSee(p1)).thenReturn(true); + when(p1.spigot()).thenReturn(spigot); + when(mockPlayer.canSee(p1)).thenReturn(true); Player p2 = mock(Player.class); when(p2.getName()).thenReturn("op"); - when(p.canSee(p2)).thenReturn(true); + when(p2.spigot()).thenReturn(spigot); + when(mockPlayer.canSee(p2)).thenReturn(true); when(p2.isOp()).thenReturn(true); Player p3 = mock(Player.class); when(p3.getName()).thenReturn("invisible"); + when(p3.spigot()).thenReturn(spigot); Player p4 = mock(Player.class); when(p4.getName()).thenReturn("adminPerm"); - when(p.canSee(p4)).thenReturn(true); + when(p4.spigot()).thenReturn(spigot); + when(mockPlayer.canSee(p4)).thenReturn(true); when(p4.hasPermission(eq("bskyblock.admin.noexpel"))).thenReturn(true); Player p5 = mock(Player.class); when(p5.getName()).thenReturn("modPerm"); - when(p.canSee(p5)).thenReturn(true); + when(p5.spigot()).thenReturn(spigot); + when(mockPlayer.canSee(p5)).thenReturn(true); when(p5.hasPermission(eq("bskyblock.mod.bypassexpel"))).thenReturn(true); list.add(p1); list.add(p2); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java index dfa13d661..5d3af68ef 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java @@ -24,7 +24,6 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; @@ -42,8 +41,8 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -51,7 +50,6 @@ import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; @@ -69,22 +67,18 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, Util.class }) -public class IslandGoCommandTest { +public class IslandGoCommandTest extends AbstractCommonSetup { @Mock private CompositeCommand ic; private User user; @Mock private IslandsManager im; @Mock - private Island island; - @Mock private PluginManager pim; @Mock private Settings s; @Mock private BukkitTask task; - @Mock - private Player player; private IslandGoCommand igc; @Mock private Notifier notifier; @@ -97,10 +91,7 @@ public class IslandGoCommandTest { */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // Command manager CommandsManager cm = mock(CommandsManager.class); @@ -110,11 +101,11 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(s); // Player - when(player.isOp()).thenReturn(false); - when(player.getUniqueId()).thenReturn(uuid); - when(player.getName()).thenReturn("tastybento"); - when(player.getWorld()).thenReturn(world); - user = User.getInstance(player); + when(mockPlayer.isOp()).thenReturn(false); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + when(mockPlayer.getName()).thenReturn("tastybento"); + when(mockPlayer.getWorld()).thenReturn(world); + user = User.getInstance(mockPlayer); // Set the User class plugin as this one User.setPlugin(plugin); @@ -197,7 +188,7 @@ public void tearDown() { public void testExecuteMidTeleport() { when(im.isGoingHome(user)).thenReturn(true); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); - verify(player).sendMessage("commands.island.go.in-progress"); + checkSpigotMessage("commands.island.go.in-progress"); } /** @@ -207,7 +198,7 @@ public void testExecuteMidTeleport() { public void testExecuteNoArgsNoIsland() { when(im.getIslands(world, uuid)).thenReturn(List.of()); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); - verify(player).sendMessage("general.errors.no-island"); + checkSpigotMessage("general.errors.no-island"); } /** @@ -248,9 +239,9 @@ public void testExecuteNoArgsReservedIslandNoCreateCommand() { @Test public void testExecuteNoArgsNoTeleportWhenFalling() { Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true); - when(player.getFallDistance()).thenReturn(10F); + when(mockPlayer.getFallDistance()).thenReturn(10F); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); - verify(player).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); + checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint"); } /** @@ -259,7 +250,7 @@ public void testExecuteNoArgsNoTeleportWhenFalling() { @Test public void testExecuteNoArgsNoTeleportWhenFallingNotFalling() { Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true); - when(player.getFallDistance()).thenReturn(0F); + when(mockPlayer.getFallDistance()).thenReturn(0F); assertTrue(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); } @@ -279,9 +270,9 @@ public void testExecuteNoArgsMultipleHomes() { @Test public void testExecuteArgs1MultipleHomes() { assertFalse(igc.execute(user, igc.getLabel(), Collections.singletonList("1"))); - verify(player).sendMessage("commands.island.go.unknown-home"); - verify(player).sendMessage("commands.island.sethome.homes-are"); - verify(player).sendMessage("commands.island.sethome.home-list-syntax"); + checkSpigotMessage("commands.island.go.unknown-home"); + checkSpigotMessage("commands.island.sethome.homes-are"); + checkSpigotMessage("commands.island.sethome.home-list-syntax"); } /** @@ -292,7 +283,7 @@ public void testExecuteNoArgsDelay() { when(s.getDelayTime()).thenReturn(10); assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList())); - verify(player).sendMessage(eq("commands.delay.stand-still")); + checkSpigotMessage("commands.delay.stand-still"); } /** @@ -306,8 +297,8 @@ public void testExecuteNoArgsDelayTwice() { // Twice assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList())); verify(task).cancel(); - verify(player).sendMessage(eq("commands.delay.previous-command-cancelled")); - verify(player, Mockito.times(2)).sendMessage(eq("commands.delay.stand-still")); + checkSpigotMessage("commands.delay.previous-command-cancelled"); + checkSpigotMessage("commands.delay.stand-still", 2); } /** @@ -318,10 +309,10 @@ public void testOnPlayerMoveHeadMoveNothing() { Location l = mock(Location.class); Vector vector = mock(Vector.class); when(l.toVector()).thenReturn(vector); - when(player.getLocation()).thenReturn(l); - PlayerMoveEvent e = new PlayerMoveEvent(player, l, l); + when(mockPlayer.getLocation()).thenReturn(l); + PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l); igc.onPlayerMove(e); - verify(player, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled")); + verify(mockPlayer, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled")); } /** @@ -332,11 +323,11 @@ public void testOnPlayerMoveHeadMoveTeleportPending() { Location l = mock(Location.class); Vector vector = mock(Vector.class); when(l.toVector()).thenReturn(vector); - when(player.getLocation()).thenReturn(l); + when(mockPlayer.getLocation()).thenReturn(l); testExecuteNoArgsDelay(); - PlayerMoveEvent e = new PlayerMoveEvent(player, l, l); + PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l); igc.onPlayerMove(e); - verify(player, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled")); + checkSpigotMessage("commands.delay.moved-so-command-cancelled", 0); } /** @@ -347,12 +338,12 @@ public void testOnPlayerMovePlayerMoveTeleportPending() { Location l = mock(Location.class); Vector vector = mock(Vector.class); when(l.toVector()).thenReturn(vector); - when(player.getLocation()).thenReturn(l); + when(mockPlayer.getLocation()).thenReturn(l); testExecuteNoArgsDelay(); Location l2 = mock(Location.class); Vector vector2 = mock(Vector.class); when(l2.toVector()).thenReturn(vector2); - PlayerMoveEvent e = new PlayerMoveEvent(player, l, l2); + PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l2); igc.onPlayerMove(e); verify(notifier).notify(any(), eq("commands.delay.moved-so-command-cancelled")); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java index 1a3c383c4..8e68866cc 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandInfoCommandTest.java @@ -17,33 +17,27 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.NonNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util; /** @@ -59,49 +53,29 @@ public class IslandInfoCommandTest extends RanksManagerBeforeClassTest { @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; + @Mock + private PlaceholdersManager phm; private Island island; private IslandInfoCommand iic; - @Mock - private Player player; - @Mock - private World world; - @Mock - private PlaceholdersManager phm; - @Mock - private @NonNull Location location; - @Mock - private IslandWorldManager iwm; - /** - */ @Before public void setUp() throws Exception { super.setUp(); - PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); - - // IWM - when(plugin.getIWM()).thenReturn(iwm); - - // Bukkit - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Command manager CommandsManager cm = mock(CommandsManager.class); when(plugin.getCommandsManager()).thenReturn(cm); // Player - when(player.isOp()).thenReturn(false); - UUID uuid = UUID.randomUUID(); + when(mockPlayer.isOp()).thenReturn(false); when(user.getUniqueId()).thenReturn(uuid); when(user.getName()).thenReturn("tastybento"); when(user.getWorld()).thenReturn(world); - when(user.getPlayer()).thenReturn(player); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.isPlayer()).thenReturn(true); //user = User.getInstance(player); // Set the User class plugin as this one @@ -163,6 +137,7 @@ public void testExecuteUserStringListOfStringTooManyArgs() { public void testExecuteUserStringListOfStringNoArgsConsole() { CommandSender console = mock(CommandSender.class); User sender = User.getInstance(console); + when(console.spigot()).thenReturn(spigot); assertFalse(iic.execute(sender, "", Collections.emptyList())); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console"); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java index 0ba16a1fb..ca5c6a65f 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java @@ -18,12 +18,10 @@ import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; -import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -34,11 +32,11 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.AddonDescription; @@ -50,20 +48,20 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.BlueprintsManager; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.island.NewIsland; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, NewIsland.class, IslandsManager.class }) -public class IslandResetCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, NewIsland.class, IslandsManager.class, Util.class }) +public class IslandResetCommandTest extends AbstractCommonSetup { @Mock private CompositeCommand ic; @@ -72,37 +70,22 @@ public class IslandResetCommandTest { @Mock private Settings s; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; @Mock - private World world; - @Mock - private IslandWorldManager iwm; - @Mock private BlueprintsManager bpm; @Mock - private @Nullable Island island; - @Mock private PluginManager pim; private IslandResetCommand irc; - @Mock - private Player pp; - private UUID uuid; /** */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - + super.setUp(); PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); // Command manager CommandsManager cm = mock(CommandsManager.class); @@ -115,8 +98,10 @@ public void setUp() throws Exception { // Player Player p = mock(Player.class); when(p.getUniqueId()).thenReturn(uuid); - User.getInstance(p); + when(p.spigot()).thenReturn(spigot); when(p.isOnline()).thenReturn(true); + User.getInstance(p); + // User User.setPlugin(plugin); @@ -170,8 +155,8 @@ public void setUp() throws Exception { // Put a team on the island for (int j = 0; j < 11; j++) { UUID temp = UUID.randomUUID(); - when(pp.getUniqueId()).thenReturn(temp); - User.getInstance(pp); + when(mockPlayer.getUniqueId()).thenReturn(temp); + User.getInstance(mockPlayer); members.add(temp); } when(island.getMemberSet()).thenReturn(members.build()); @@ -266,7 +251,7 @@ public void testNoConfirmationRequired() throws Exception { verify(user).sendMessage("commands.island.create.creating-island"); verify(user, never()).sendMessage(eq("commands.island.reset.kicked-from-island"), eq(TextVariables.GAMEMODE), anyString()); // Only 11 because the leader should not see this - verify(pp, times(11)).sendMessage("commands.island.reset.kicked-from-island"); + checkSpigotMessage("commands.island.reset.kicked-from-island", 11); } @After diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java index 3a5e73426..47c87a3a1 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java @@ -5,10 +5,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Collections; @@ -19,7 +16,6 @@ import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; @@ -31,11 +27,10 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.GameModeAddon; @@ -43,37 +38,26 @@ import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class}) -public class IslandSpawnCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, Util.class }) +public class IslandSpawnCommandTest extends AbstractCommonSetup { - @Mock - private BentoBox plugin; - @Mock - private IslandsManager im; @Mock private CompositeCommand ic; private IslandSpawnCommand isc; - @Mock - private IslandWorldManager iwm; private @Nullable User user; @Mock - private World world; - @Mock private @Nullable WorldSettings ws; private Map map; @Mock - private Player player; - @Mock private BukkitTask task; @Mock private PluginManager pim; @@ -84,22 +68,21 @@ public class IslandSpawnCommandTest { */ @Before public void setUp() throws Exception { - // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // Command manager CommandsManager cm = mock(CommandsManager.class); when(plugin.getCommandsManager()).thenReturn(cm); // Player - when(player.isOp()).thenReturn(false); + when(mockPlayer.isOp()).thenReturn(false); UUID uuid = UUID.randomUUID(); - when(player.getUniqueId()).thenReturn(uuid); - when(player.hasPermission(anyString())).thenReturn(true); - when(player.getWorld()).thenReturn(world); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + when(mockPlayer.hasPermission(anyString())).thenReturn(true); + when(mockPlayer.getWorld()).thenReturn(world); User.setPlugin(plugin); // Set up user already - user = User.getInstance(player); + user = User.getInstance(mockPlayer); // Addon GameModeAddon addon = mock(GameModeAddon.class); @@ -116,7 +99,6 @@ public void setUp() throws Exception { // Server & Scheduler BukkitScheduler sch = mock(BukkitScheduler.class); - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(Bukkit.getScheduler()).thenReturn(sch); when(sch.runTaskLater(any(), any(Runnable.class), any(Long.class))).thenReturn(task); @@ -187,11 +169,11 @@ public void testExecuteUserStringListOfString() { */ @Test public void testExecuteUserStringListOfStringInWorldNoTeleportFalling() { - when(player.getFallDistance()).thenReturn(10F); + when(mockPlayer.getFallDistance()).thenReturn(10F); map.put("PREVENT_TELEPORT_WHEN_FALLING", true); when(iwm.inWorld(any(World.class))).thenReturn(true); assertFalse(isc.execute(user, "spawn", Collections.emptyList())); - verify(player).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); + checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint"); } /** @@ -199,11 +181,11 @@ public void testExecuteUserStringListOfStringInWorldNoTeleportFalling() { */ @Test public void testExecuteUserStringListOfStringInWorldTeleportOkFalling() { - when(player.getFallDistance()).thenReturn(10F); + when(mockPlayer.getFallDistance()).thenReturn(10F); map.put("PREVENT_TELEPORT_WHEN_FALLING", false); when(iwm.inWorld(any(World.class))).thenReturn(true); assertTrue(isc.execute(user, "spawn", Collections.emptyList())); - verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); + checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0); } /** @@ -211,11 +193,11 @@ public void testExecuteUserStringListOfStringInWorldTeleportOkFalling() { */ @Test public void testExecuteUserStringListOfStringWrongWorldTeleportOkFalling() { - when(player.getFallDistance()).thenReturn(10F); + when(mockPlayer.getFallDistance()).thenReturn(10F); map.put("PREVENT_TELEPORT_WHEN_FALLING", true); when(iwm.inWorld(any(World.class))).thenReturn(false); assertTrue(isc.execute(user, "spawn", Collections.emptyList())); - verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); + checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0); } /** @@ -223,11 +205,11 @@ public void testExecuteUserStringListOfStringWrongWorldTeleportOkFalling() { */ @Test public void testExecuteUserStringListOfStringInWorldTeleportNotFalling() { - when(player.getFallDistance()).thenReturn(0F); + when(mockPlayer.getFallDistance()).thenReturn(0F); map.put("PREVENT_TELEPORT_WHEN_FALLING", true); when(iwm.inWorld(any(World.class))).thenReturn(true); assertTrue(isc.execute(user, "spawn", Collections.emptyList())); - verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); + checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0); } } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java index c9451291a..4c66cbcce 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java @@ -21,8 +21,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -34,40 +32,31 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest { @Mock private CompositeCommand ic; - private UUID uuid; @Mock private User user; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; - @Mock - private Island island; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -81,13 +70,11 @@ public void setUp() throws Exception { Settings s = mock(Settings.class); when(plugin.getSettings()).thenReturn(s); - // Player - Player p = mock(Player.class); // User when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); @@ -97,8 +84,6 @@ public void setUp() throws Exception { // No island for player to begin with (set it later in the tests) when(im.hasIsland(any(), eq(uuid))).thenReturn(false); - // when(im.isOwner(any(), eq(uuid))).thenReturn(false); - when(plugin.getIslands()).thenReturn(im); // Has team when(im.inTeam(any(), eq(uuid))).thenReturn(true); @@ -116,12 +101,9 @@ public void setUp() throws Exception { when(im.getIsland(any(), any(UUID.class))).thenReturn(island); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); - when(plugin.getIWM()).thenReturn(iwm); // Server and Plugin Manager for events - PluginManager pim = mock(PluginManager.class); when(Bukkit.getPluginManager()).thenReturn(pim); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java index 8537a1f7e..c698ede32 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommandTest.java @@ -18,37 +18,31 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.plugin.PluginManager; -import org.eclipse.jdt.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.TeamInvite.Type; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -56,33 +50,14 @@ public class IslandTeamCommandTest extends RanksManagerBeforeClassTest { private IslandTeamCommand tc; - private UUID uuid; - private UUID invitee; - @Mock - private IslandsManager im; - @Mock private User user; - @Mock - private World world; - - @Mock - private PluginManager pim; - - @Mock - private IslandWorldManager iwm; - - @Mock - private @Nullable Island island; - @Mock private GameModeAddon addon; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -97,13 +72,11 @@ public void setUp() throws Exception { when(ic.getAddon()).thenReturn(addon); // user - uuid = UUID.randomUUID(); invitee = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); when(user.getPermissionValue(eq("bskyblock.team.maxsize"), anyInt())).thenReturn(3); // island Manager - when(plugin.getIslands()).thenReturn(im); // is owner of island when(im.getPrimaryIsland(world, uuid)).thenReturn(island); when(im.getIsland(world, user)).thenReturn(island); @@ -122,12 +95,7 @@ public void setUp() throws Exception { // island when(im.getIsland(any(), eq(uuid))).thenReturn(island); - // Bukkit - PowerMockito.mockStatic(Bukkit.class); - when(Bukkit.getPluginManager()).thenReturn(pim); - // IWM - when(plugin.getIWM()).thenReturn(iwm); when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); // Command under test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java index c09cb590b..b911cf2b4 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java @@ -29,10 +29,10 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; @@ -40,14 +40,14 @@ import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -62,8 +62,6 @@ public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest { private UUID notUUID; @Mock private Settings s; - @Mock - private Island island; @Before public void setUp() throws Exception { @@ -76,7 +74,6 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() when(user.isOp()).thenReturn(false); when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4); @@ -87,7 +84,7 @@ public void setUp() throws Exception { notUUID = UUID.randomUUID(); } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); @@ -300,6 +297,7 @@ public void testExecuteSuccess() { when(p.getUniqueId()).thenReturn(notUUID); when(p.getName()).thenReturn("target"); when(p.getDisplayName()).thenReturn("&Ctarget"); + when(p.spigot()).thenReturn(spigot); User target = User.getInstance(p); // Can execute when(pm.getUUID(any())).thenReturn(notUUID); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java index 6fb04804c..5ad3b61b9 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java @@ -18,13 +18,10 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; import org.eclipse.jdt.annotation.NonNull; import org.junit.Before; @@ -38,40 +35,32 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.TeamInvite; import world.bentobox.bentobox.database.objects.TeamInvite.Type; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { @Mock private IslandTeamCommand ic; @Mock - private IslandsManager im; - @Mock - private Island island; - @Mock - private PluginManager pim; - @Mock private PlayersManager pm; @Mock private Settings s; @@ -80,14 +69,9 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { @Mock private User user; - private UUID uuid; private UUID islandUUID; private IslandTeamInviteCommand itl; private UUID notUUID; - @Mock - private Player p; - @Mock - private @NonNull World world; @Before public void setUp() throws Exception { @@ -111,7 +95,7 @@ public void setUp() throws Exception { when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastbento"); when(user.isOnline()).thenReturn(true); @@ -120,13 +104,13 @@ public void setUp() throws Exception { when(User.getInstance(uuid)).thenReturn(user); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); // Vanished players - when(p.canSee(any())).thenReturn(true); + when(mockPlayer.canSee(any())).thenReturn(true); User.setPlugin(plugin); // Target notUUID = UUID.randomUUID(); when(target.getUniqueId()).thenReturn(notUUID); - when(target.getPlayer()).thenReturn(p); + when(target.getPlayer()).thenReturn(mockPlayer); when(target.isOnline()).thenReturn(true); when(target.getName()).thenReturn("target"); when(target.getDisplayName()).thenReturn("&Ctarget"); @@ -170,7 +154,6 @@ public void setUp() throws Exception { when(plugin.getLocalesManager()).thenReturn(lm); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); @NonNull WorldSettings ws = new TestWorldSettings(); @@ -246,7 +229,7 @@ public void testCanExecuteNoIsland() { public void testCanExecuteNoTarget() { assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList())); // Show panel - verify(p).openInventory(any(Inventory.class)); + verify(mockPlayer).openInventory(any(Inventory.class)); } /** @@ -264,7 +247,7 @@ public void testCanExecuteOfflinePlayer() { */ @Test public void testCanExecuteVanishedPlayer() { - when(p.canSee(any())).thenReturn(false); + when(mockPlayer.canSee(any())).thenReturn(false); assertFalse(itl.canExecute(user, itl.getLabel(), List.of("target"))); verify(user).sendMessage(eq("general.errors.offline-player")); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java index 92bbf2c63..bc5af89bb 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java @@ -20,7 +20,6 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; @@ -36,6 +35,7 @@ import com.google.common.collect.ImmutableSet.Builder; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.AddonDescription; @@ -51,13 +51,13 @@ import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -68,27 +68,15 @@ public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest { @Mock private Settings s; @Mock - private IslandsManager im; - @Mock private PlayersManager pm; private UUID notUUID; @Mock - private IslandWorldManager iwm; - @Mock - private Player player; - @Mock private Player target; @Mock private CompositeCommand subCommand; @Mock - private Island island; - @Mock private Addon addon; - @Mock - private World world; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -112,10 +100,11 @@ public void setUp() throws Exception { when(target.isOnline()).thenReturn(true); when(target.getName()).thenReturn("poslovitch"); when(target.getDisplayName()).thenReturn("&Cposlovich"); + when(target.spigot()).thenReturn(spigot); // Set the target user User.getInstance(target); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(player); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java index 2dfb203b7..78163b4eb 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java @@ -17,9 +17,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.eclipse.jdt.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,35 +29,29 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest { - @Mock - Player player; @Mock private IslandTeamCommand ic; @Mock User user; - @Mock - IslandsManager im; // DUT private IslandTeamPromoteCommand ipc; @@ -68,12 +59,6 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest { @Mock private PlayersManager pm; @Mock - private World world; - @Mock - private IslandWorldManager iwm; - @Mock - private @Nullable Island island; - @Mock private User target; @@ -99,7 +84,7 @@ public void setUp() throws Exception { UUID uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); when(user.getName()).thenReturn("tastybento"); - when(user.getPlayer()).thenReturn(player); + when(user.getPlayer()).thenReturn(mockPlayer); when(pm.getUser("target")).thenReturn(target); when(target.getName()).thenReturn("target"); when(target.getDisplayName()).thenReturn("Target"); @@ -138,8 +123,8 @@ public void setUp() throws Exception { // Bukkit PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - when(Bukkit.getOfflinePlayer(uuid)).thenReturn(player); - when(player.getName()).thenReturn("tastybento"); + when(Bukkit.getOfflinePlayer(uuid)).thenReturn(mockPlayer); + when(mockPlayer.getName()).thenReturn("tastybento"); ipc = new IslandTeamPromoteCommand(ic, "promote"); @@ -179,7 +164,7 @@ public void testSetup() { @Test public void testCanExecuteUserStringListOfStringShowHelp() { assertFalse(ipc.canExecute(user, "promote", List.of())); // Nothing - verify(user).sendMessage("commands.help.header", TextVariables.LABEL, null); + verify(user).sendMessage("commands.help.header", TextVariables.LABEL, "BSkyBlock"); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java index 0738e01ef..1afe1cf90 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java @@ -31,10 +31,10 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; @@ -42,14 +42,14 @@ import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest { @Mock @@ -64,10 +64,6 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest { private UUID notUUID; @Mock private Settings s; - @Mock - private Island island; - @Mock - private Player targetPlayer; /** */ @@ -101,24 +97,24 @@ public void setUp() throws Exception { when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); User.setPlugin(plugin); // Target player - when(targetPlayer.getUniqueId()).thenReturn(notUUID); - when(targetPlayer.getName()).thenReturn("target"); - when(targetPlayer.getDisplayName()).thenReturn("&Cposlovich"); - User.getInstance(targetPlayer); + when(mockPlayer.getUniqueId()).thenReturn(notUUID); + when(mockPlayer.getName()).thenReturn("target"); + when(mockPlayer.getDisplayName()).thenReturn("&Cposlovich"); + User.getInstance(mockPlayer); // Parent command has no aliases when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); // Player has island to begin with - when(im.hasIsland(any(), Mockito.any(UUID.class))).thenReturn(true); - when(im.inTeam(any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); + when(im.inTeam(any(), any(UUID.class))).thenReturn(true); // when(im.isOwner(any(), any())).thenReturn(true); // when(im.getOwner(any(), any())).thenReturn(uuid); // Island when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(island.getMemberSet(anyInt(), any(Boolean.class))).thenReturn(ImmutableSet.of(uuid)); - when(im.getIsland(any(), Mockito.any(User.class))).thenReturn(island); - when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island); + when(im.getIsland(any(), any(User.class))).thenReturn(island); + when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(plugin.getIslands()).thenReturn(im); // Has team @@ -157,7 +153,7 @@ public void testCanExecuteNoisland() { when(im.inTeam(any(), Mockito.any(UUID.class))).thenReturn(false); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bill"))); - verify(user).sendMessage(eq("general.errors.no-island")); + verify(user).sendMessage("general.errors.no-island"); } /** @@ -207,7 +203,7 @@ public void testCanExecuteSamePlayer() { IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); when(pm.getUUID(any())).thenReturn(uuid); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); - verify(user).sendMessage(eq("commands.island.team.trust.trust-in-yourself")); + verify(user).sendMessage("commands.island.team.trust.trust-in-yourself"); } /** @@ -224,7 +220,7 @@ public void testCanExecutePlayerHasRank() { when(im.inTeam(any(), any())).thenReturn(true); // when(im.getMembers(any(), any())).thenReturn(Collections.singleton(notUUID)); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bento"))); - verify(user).sendMessage(eq("commands.island.team.trust.player-already-trusted")); + verify(user).sendMessage("commands.island.team.trust.player-already-trusted"); } /** @@ -235,7 +231,7 @@ public void testCanExecuteCannottrustSelf() { when(pm.getUUID(any())).thenReturn(uuid); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); - verify(user).sendMessage(eq("commands.island.team.trust.trust-in-yourself")); + verify(user).sendMessage("commands.island.team.trust.trust-in-yourself"); } /** @@ -249,7 +245,7 @@ public void testCanExecuteCannotAlreadyHasRank() { // when(im.getMembers(any(), any())).thenReturn(Collections.singleton(other)); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); - verify(user).sendMessage(eq("commands.island.team.trust.player-already-trusted")); + verify(user).sendMessage("commands.island.team.trust.player-already-trusted"); } /** @@ -266,7 +262,7 @@ public void testExecuteNullIsland() { // Execute when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(null); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); - verify(user).sendMessage(eq("general.errors.general")); + verify(user).sendMessage("general.errors.general"); } /** @@ -284,7 +280,7 @@ public void testExecuteSuccessNoConfirmationTooMany() { // Execute when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); - verify(user).sendMessage(eq("commands.island.team.trust.is-full")); + verify(user).sendMessage("commands.island.team.trust.is-full"); } /** @@ -293,7 +289,7 @@ public void testExecuteSuccessNoConfirmationTooMany() { */ @Test public void testExecuteSuccessNoConfirmation() { - User target = User.getInstance(targetPlayer); + User target = User.getInstance(mockPlayer); // Can execute when(pm.getUUID(any())).thenReturn(notUUID); // when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); @@ -308,7 +304,7 @@ public void testExecuteSuccessNoConfirmation() { verify(user).sendMessage("commands.island.team.trust.success", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Cposlovich"); verify(island).setRank(target, RanksManager.TRUSTED_RANK); - verify(targetPlayer).sendMessage("commands.island.team.trust.you-are-trusted"); + checkSpigotMessage("commands.island.team.trust.you-are-trusted"); } /** @@ -317,7 +313,7 @@ public void testExecuteSuccessNoConfirmation() { @Test public void testExecuteSuccessConfirmation() { when(s.isInviteConfirmation()).thenReturn(true); - User target = User.getInstance(targetPlayer); + User target = User.getInstance(mockPlayer); // Can execute when(pm.getUUID(any())).thenReturn(notUUID); //when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); @@ -330,8 +326,8 @@ public void testExecuteSuccessConfirmation() { assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); verify(user).sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Cposlovich"); // Send message to online player - verify(targetPlayer).sendMessage(eq("commands.island.team.trust.name-has-invited-you")); - verify(targetPlayer).sendMessage(eq("commands.island.team.invite.to-accept-or-reject")); + checkSpigotMessage("commands.island.team.trust.name-has-invited-you"); + checkSpigotMessage("commands.island.team.invite.to-accept-or-reject"); verify(island, never()).setRank(target, RanksManager.TRUSTED_RANK); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java index dd57146c7..8031c546d 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java @@ -21,7 +21,6 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -34,39 +33,34 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest { + @Mock private CompositeCommand ic; - private UUID uuid; + @Mock private User user; - private IslandsManager im; private PlayersManager pm; private UUID notUUID; @Mock private Settings s; - private Island island; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -79,9 +73,6 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); - // Sometimes use withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); notUUID = UUID.randomUUID(); @@ -89,27 +80,21 @@ public void setUp() throws Exception { notUUID = UUID.randomUUID(); } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); User.setPlugin(plugin); // Parent command has no aliases - ic = mock(CompositeCommand.class); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true); - // when(im.isOwner(any(), any())).thenReturn(true); - // when(im.getOwner(any(), any())).thenReturn(uuid); - island = mock(Island.class); when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(im.getIsland(any(), any(User.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getPrimaryIsland(any(), any())).thenReturn(island); - when(plugin.getIslands()).thenReturn(im); // Has team when(im.inTeam(any(), eq(uuid))).thenReturn(true); @@ -129,7 +114,6 @@ public void setUp() throws Exception { when(plugin.getLocalesManager()).thenReturn(lm); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java index 2955222e1..cfa8bacbf 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java @@ -20,7 +20,6 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -33,39 +32,35 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest { + @Mock private CompositeCommand ic; - private UUID uuid; + @Mock private User user; - private IslandsManager im; + @Mock private PlayersManager pm; private UUID notUUID; @Mock private Settings s; - private Island island; - /** - */ @Before public void setUp() throws Exception { super.setUp(); @@ -78,17 +73,11 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); notUUID = UUID.randomUUID(); - while (notUUID.equals(uuid)) { - notUUID = UUID.randomUUID(); - } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); @@ -99,17 +88,12 @@ public void setUp() throws Exception { when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true); - // when(im.isOwner(any(), any())).thenReturn(true); - // when(im.getOwner(any(), any())).thenReturn(uuid); - island = mock(Island.class); when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(im.getIsland(any(), any(User.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getPrimaryIsland(any(), any())).thenReturn(island); - when(plugin.getIslands()).thenReturn(im); // Has team when(im.inTeam(any(), eq(uuid))).thenReturn(true); @@ -129,7 +113,6 @@ public void setUp() throws Exception { when(plugin.getLocalesManager()).thenReturn(lm); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); @@ -144,7 +127,7 @@ public void testExecuteNoisland() { when(im.inTeam(any(), any(UUID.class))).thenReturn(false); IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); - verify(user).sendMessage(eq("general.errors.no-island")); + verify(user).sendMessage("general.errors.no-island"); } /** @@ -194,7 +177,7 @@ public void testExecuteSamePlayer() { IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); when(pm.getUUID(any())).thenReturn(uuid); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); - verify(user).sendMessage(eq("commands.island.team.untrust.cannot-untrust-yourself")); + verify(user).sendMessage("commands.island.team.untrust.cannot-untrust-yourself"); } /** @@ -212,7 +195,7 @@ public void testExecutePlayerHasRank() { when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); when(island.inTeam(notUUID)).thenReturn(true); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); - verify(user).sendMessage(eq("commands.island.team.untrust.cannot-untrust-member")); + verify(user).sendMessage("commands.island.team.untrust.cannot-untrust-member"); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java index ca6775329..2cc6fe8c3 100644 --- a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java +++ b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java @@ -33,12 +33,10 @@ import org.bukkit.Server; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachmentInfo; -import org.bukkit.plugin.PluginManager; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -49,11 +47,10 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon; @@ -62,36 +59,28 @@ import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.metadata.MetaDataValue; import world.bentobox.bentobox.database.objects.Players; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ BentoBox.class, Bukkit.class }) -public class UserTest { +@PrepareForTest({ BentoBox.class, Bukkit.class, Util.class }) +public class UserTest extends AbstractCommonSetup { private static final String TEST_TRANSLATION = "mock &a translation &b [test]"; private static final String TEST_TRANSLATION_WITH_COLOR = "mock §atranslation §b[test]"; @Mock - private Player player; - @Mock - private BentoBox plugin; - @Mock private LocalesManager lm; private User user; - @Mock - private IslandWorldManager iwm; private UUID uuid; @Mock - private PluginManager pim; - @Mock private CommandSender sender; @Mock private Server server; @@ -101,28 +90,26 @@ public class UserTest { @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); - User.setPlugin(plugin); + super.setUp(); uuid = UUID.randomUUID(); - when(player.getUniqueId()).thenReturn(uuid); + when(mockPlayer.getUniqueId()).thenReturn(uuid); ItemFactory itemFactory = mock(ItemFactory.class); - when(Bukkit.getPlayer(any(UUID.class))).thenReturn(player); + when(Bukkit.getPlayer(any(UUID.class))).thenReturn(mockPlayer); when(Bukkit.getPluginManager()).thenReturn(pim); when(Bukkit.getItemFactory()).thenReturn(itemFactory); when(Bukkit.getServer()).thenReturn(server); // Player - when(player.getServer()).thenReturn(server); + when(mockPlayer.getServer()).thenReturn(server); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); + when(sender.spigot()).thenReturn(spigot); @NonNull World world = mock(World.class); when(world.getName()).thenReturn("BSkyBlock"); - when(player.getWorld()).thenReturn(world); + when(mockPlayer.getWorld()).thenReturn(world); // IWM when(plugin.getIWM()).thenReturn(iwm); @@ -130,7 +117,7 @@ public void setUp() throws Exception { when(iwm.getAddon(any())).thenReturn(Optional.empty()); when(iwm.getFriendlyName(world)).thenReturn("BSkyBlock-Fiendly"); - user = User.getInstance(player); + user = User.getInstance(mockPlayer); // Locales when(plugin.getLocalesManager()).thenReturn(lm); @@ -164,7 +151,7 @@ public void testGetInstanceCommandSender() { @Test public void testGetInstancePlayer() { - assertEquals(player,user.getPlayer()); + assertEquals(mockPlayer, user.getPlayer()); } @Test @@ -179,7 +166,7 @@ public void testGetInstanceUUID() { public void testRemovePlayer() { assertNotNull(User.getInstance(uuid)); assertEquals(user, User.getInstance(uuid)); - User.removePlayer(player); + User.removePlayer(mockPlayer); // If the player has been removed from the cache, then code will ask server for player // Return null and check if instance is null will show that the player is not in the cache when(Bukkit.getPlayer(any(UUID.class))).thenReturn(null); @@ -190,7 +177,7 @@ public void testSetPlugin() { BentoBox plugin = mock(BentoBox.class); User.setPlugin(plugin); user.addPerm("testing123"); - verify(player).addAttachment(eq(plugin), eq("testing123"), eq(true)); + verify(mockPlayer).addAttachment(eq(plugin), eq("testing123"), eq(true)); } @Test @@ -206,9 +193,9 @@ public void testGetEffectivePermissions() { @Test public void testGetInventory() { PlayerInventory value = mock(PlayerInventory.class); - when(player.getInventory()).thenReturn(value); - assertEquals(value, player.getInventory()); - User user = User.getInstance(player); + when(mockPlayer.getInventory()).thenReturn(value); + assertEquals(value, mockPlayer.getInventory()); + User user = User.getInstance(mockPlayer); assertNotNull(user.getInventory()); assertEquals(value, user.getInventory()); } @@ -216,8 +203,8 @@ public void testGetInventory() { @Test public void testGetLocation() { Location loc = mock(Location.class); - when(player.getLocation()).thenReturn(loc); - User user = User.getInstance(player); + when(mockPlayer.getLocation()).thenReturn(loc); + User user = User.getInstance(mockPlayer); assertNotNull(user.getLocation()); assertEquals(loc, user.getLocation()); } @@ -225,8 +212,8 @@ public void testGetLocation() { @Test public void testGetName() { String name = "tastybento"; - when(player.getName()).thenReturn(name); - User user = User.getInstance(player); + when(mockPlayer.getName()).thenReturn(name); + User user = User.getInstance(mockPlayer); assertNotNull(user.getName()); assertEquals(name, user.getName()); @@ -234,15 +221,15 @@ public void testGetName() { @Test public void testGetPlayer() { - User user = User.getInstance(player); - assertEquals(player, user.getPlayer()); + User user = User.getInstance(mockPlayer); + assertEquals(mockPlayer, user.getPlayer()); } @Test public void testIsPlayer() { User user = User.getInstance(sender); assertFalse(user.isPlayer()); - user = User.getInstance(player); + user = User.getInstance(mockPlayer); assertTrue(user.isPlayer()); } @@ -255,8 +242,8 @@ public void testGetSender() { @Test public void testGetUniqueId() { UUID uuid = UUID.randomUUID(); - when(player.getUniqueId()).thenReturn(uuid); - user = User.getInstance(player); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + user = User.getInstance(mockPlayer); assertEquals(uuid, user.getUniqueId()); } @@ -267,7 +254,7 @@ public void testHasPermission() { assertTrue(user.hasPermission(null)); // test if player has the permission - when(player.hasPermission(anyString())).thenReturn(true); + when(mockPlayer.hasPermission(anyString())).thenReturn(true); assertTrue(user.hasPermission("perm")); } @@ -283,13 +270,13 @@ public void testHasNotPermissionButIsOp() { @Test public void testIsOnline() { - when(player.isOnline()).thenReturn(true); + when(mockPlayer.isOnline()).thenReturn(true); assertTrue(user.isOnline()); } @Test public void testIsOp() { - when(player.isOp()).thenReturn(true); + when(mockPlayer.isOp()).thenReturn(true); assertTrue(user.isOp()); } @@ -325,7 +312,7 @@ public void testGetTranslationOrNothing() { when(lm.get(any(), any())).thenReturn("fake.reference"); when(lm.get(any())).thenReturn("fake.reference"); - User user = User.getInstance(player); + User user = User.getInstance(mockPlayer); assertEquals("", user.getTranslationOrNothing("fake.reference")); assertEquals("", user.getTranslationOrNothing("fake.reference", "[test]", "variable")); } @@ -333,7 +320,7 @@ public void testGetTranslationOrNothing() { @Test public void testSendMessage() { user.sendMessage("a.reference"); - verify(player).sendMessage(TEST_TRANSLATION_WITH_COLOR); + checkSpigotMessage(TEST_TRANSLATION_WITH_COLOR); } @Test @@ -347,8 +334,8 @@ public void testSendMessageOverrideWithAddon() { when(iwm .getAddon(any())).thenReturn(optionalAddon); when(lm.get(any(), eq("name.a.reference"))).thenReturn("mockmockmock"); user.sendMessage("a.reference"); - verify(player, never()).sendMessage(eq(TEST_TRANSLATION)); - verify(player).sendMessage(eq("mockmockmock")); + verify(mockPlayer, never()).sendMessage(eq(TEST_TRANSLATION)); + checkSpigotMessage("mockmockmock"); } @Test @@ -356,7 +343,7 @@ public void testSendMessageBlankTranslation() { // Nothing - blank translation when(lm.get(any(), any())).thenReturn(""); user.sendMessage("a.reference"); - verify(player, never()).sendMessage(anyString()); + checkSpigotMessage("a.reference", 0); } @Test @@ -368,21 +355,21 @@ public void testSendMessageOnlyColors() { } when(lm.get(any(), any())).thenReturn(allColors.toString()); user.sendMessage("a.reference"); - verify(player, never()).sendMessage(anyString()); + verify(mockPlayer, never()).sendMessage(anyString()); } @Test public void testSendMessageColorsAndSpaces() { when(lm.get(any(), any())).thenReturn(ChatColor.COLOR_CHAR + "6 Hello there"); user.sendMessage("a.reference"); - verify(player).sendMessage(eq(ChatColor.COLOR_CHAR + "6Hello there")); + checkSpigotMessage(ChatColor.COLOR_CHAR + "6Hello there"); } @Test public void testSendRawMessage() { String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; user.sendRawMessage(raw); - verify(player).sendMessage(raw); + checkSpigotMessage(raw); } @Test @@ -390,7 +377,7 @@ public void testSendRawMessageNullUser() { String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; user = User.getInstance((CommandSender)null); user.sendRawMessage(raw); - verify(player, never()).sendMessage(anyString()); + checkSpigotMessage(raw, 0); } @Test @@ -414,29 +401,29 @@ public void testSetGameMode() { for (GameMode gm: GameMode.values()) { user.setGameMode(gm); } - verify(player, times(GameMode.values().length)).setGameMode(any()); + verify(mockPlayer, times(GameMode.values().length)).setGameMode(any()); } @Test public void testTeleport() { - when(player.teleport(any(Location.class))).thenReturn(true); + when(mockPlayer.teleport(any(Location.class))).thenReturn(true); Location loc = mock(Location.class); user.teleport(loc); - verify(player).teleport(loc); + verify(mockPlayer).teleport(loc); } @Test public void testGetWorld() { World world = mock(World.class); - when(player.getWorld()).thenReturn(world); - User user = User.getInstance(player); + when(mockPlayer.getWorld()).thenReturn(world); + User user = User.getInstance(mockPlayer); assertEquals(world, user.getWorld()); } @Test public void testCloseInventory() { user.closeInventory(); - verify(player).closeInventory(); + verify(mockPlayer).closeInventory(); } @Test @@ -468,13 +455,13 @@ public void testGetLocaleConsole() { @Test public void testUpdateInventory() { user.updateInventory(); - verify(player).updateInventory(); + verify(mockPlayer).updateInventory(); } @Test public void testPerformCommand() { user.performCommand("test"); - verify(player).performCommand("test"); + verify(mockPlayer).performCommand("test"); } @Test @@ -520,8 +507,8 @@ public void testGetPermissionValue() { permSet.add(pai); permSet.add(pai2); permSet.add(pai3); - when(player.getEffectivePermissions()).thenReturn(permSet); - User u = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(permSet); + User u = User.getInstance(mockPlayer); assertEquals(33, u.getPermissionValue("bskyblock.max", 2)); } @@ -544,8 +531,8 @@ public void testGetPermissionValueNegativePerm() { permSet.add(pai); permSet.add(pai2); permSet.add(pai3); - when(player.getEffectivePermissions()).thenReturn(permSet); - User u = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(permSet); + User u = User.getInstance(mockPlayer); assertEquals(7, u.getPermissionValue("bskyblock.max", 2)); } @@ -579,8 +566,8 @@ public void testGetPermissionValueNegative() { permSet.add(pai); permSet.add(pai2); permSet.add(pai3); - when(player.getEffectivePermissions()).thenReturn(permSet); - User u = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(permSet); + User u = User.getInstance(mockPlayer); assertEquals(-1, u.getPermissionValue("bskyblock.max", 2)); } @@ -600,8 +587,8 @@ public void testGetPermissionValueStar() { permSet.add(pai); permSet.add(pai2); permSet.add(pai3); - when(player.getEffectivePermissions()).thenReturn(permSet); - User u = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(permSet); + User u = User.getInstance(mockPlayer); assertEquals(22, u.getPermissionValue("bskyblock.max", 22)); } @@ -614,14 +601,14 @@ public void testGetPermissionValueSmall() { PermissionAttachmentInfo pai = mock(PermissionAttachmentInfo.class); when(pai.getPermission()).thenReturn("bskyblock.max.3"); when(pai.getValue()).thenReturn(true); - when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pai)); - User u = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pai)); + User u = User.getInstance(mockPlayer); assertEquals(3, u.getPermissionValue("bskyblock.max", 22)); } @Test public void testMetaData() { - User u = User.getInstance(player); + User u = User.getInstance(mockPlayer); assertTrue(u.getMetaData().get().isEmpty()); // Store a string in a new key assertFalse(u.putMetaData("string", new MetaDataValue("a string")).isPresent()); @@ -686,7 +673,7 @@ public void testIsOfflinePlayer() { User s = User.getInstance(sender); assertFalse(s.isOfflinePlayer()); User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); assertTrue(p.isOfflinePlayer()); } @@ -696,9 +683,9 @@ public void testIsOfflinePlayer() { @Test public void testAddPerm() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); p.addPerm("test.perm"); - verify(player).addAttachment(plugin, "test.perm", true); + verify(mockPlayer).addAttachment(plugin, "test.perm", true); } /** @@ -708,19 +695,19 @@ public void testAddPerm() { public void testRemovePerm() { User.clearUsers(); // No perms to start - when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); - when(player.hasPermission(anyString())).thenReturn(false); - User p = User.getInstance(player); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.emptySet()); + when(mockPlayer.hasPermission(anyString())).thenReturn(false); + User p = User.getInstance(mockPlayer); assertTrue(p.removePerm("test.perm")); - verify(player).recalculatePermissions(); + verify(mockPlayer).recalculatePermissions(); // Has the perm PermissionAttachmentInfo pi = mock(PermissionAttachmentInfo.class); when(pi.getPermission()).thenReturn("test.perm"); PermissionAttachment attachment = mock(PermissionAttachment.class); when(pi.getAttachment()).thenReturn(attachment); - when(player.getEffectivePermissions()).thenReturn(Set.of(pi)); + when(mockPlayer.getEffectivePermissions()).thenReturn(Set.of(pi)); assertTrue(p.removePerm("test.perm")); - verify(player).removeAttachment(attachment); + verify(mockPlayer).removeAttachment(attachment); } @@ -731,7 +718,7 @@ public void testRemovePerm() { @Test public void testGetTranslationWorldStringStringArray() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); // No addon World world = mock(World.class); assertEquals("mock §atranslation §btastybento", p.getTranslation(world, "test.ref", "[test]", "tastybento")); @@ -743,7 +730,7 @@ public void testGetTranslationWorldStringStringArray() { @Test public void testGetTranslationWorldStringStringArrayWwithAddon() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); World world = mock(World.class); GameModeAddon gameAddon = mock(GameModeAddon.class); @@ -759,7 +746,7 @@ public void testGetTranslationWorldStringStringArrayWwithAddon() { @Test public void testGetTranslationStringStringArray() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); assertEquals("mock §atranslation §btastybento", p.getTranslation("test.ref", "[test]", "tastybento")); } @@ -771,7 +758,7 @@ public void testNotifyStringStringArray() { Notifier notifier = mock(Notifier.class); when(plugin.getNotifier()).thenReturn(notifier); User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); p.notify(TEST_TRANSLATION, "[test]", "tastybento"); verify(notifier).notify(any(User.class), eq("mock §atranslation §btastybento")); @@ -785,7 +772,7 @@ public void testNotifyWorldStringStringArray() { Notifier notifier = mock(Notifier.class); when(plugin.getNotifier()).thenReturn(notifier); User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); World world = mock(World.class); GameModeAddon gameAddon = mock(GameModeAddon.class); @@ -820,7 +807,7 @@ public void testGetLocale() { when(plugin.getSettings()).thenReturn(settings); when(pm.getLocale(uuid)).thenReturn("fr-FR"); User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); assertEquals(Locale.FRANCE, p.getLocale()); } @@ -830,8 +817,8 @@ public void testGetLocale() { @Test public void testInWorld() { User.clearUsers(); - User p = User.getInstance(player); - when(player.getLocation()).thenReturn(mock(Location.class)); + User p = User.getInstance(mockPlayer); + when(mockPlayer.getLocation()).thenReturn(mock(Location.class)); when(iwm.inWorld(any(Location.class))).thenReturn(false); assertFalse(p.inWorld()); when(iwm.inWorld(any(Location.class))).thenReturn(true); @@ -844,7 +831,7 @@ public void testInWorld() { @Test public void testSpawnParticleParticleObjectDoubleDoubleDoubleError() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); try { p.spawnParticle(Particle.DUST, 4, 0.0d, 0.0d, 0.0d); } catch (Exception e) { @@ -860,13 +847,13 @@ public void testSpawnParticleParticleObjectDoubleDoubleDoubleError() { public void testSpawnParticleParticleObjectDoubleDoubleDouble() { User.clearUsers(); Location loc = mock(Location.class); - when(player.getLocation()).thenReturn(loc); + when(mockPlayer.getLocation()).thenReturn(loc); when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(server.getViewDistance()).thenReturn(16); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); p.spawnParticle(Particle.SHRIEK, 4, 0.0d, 0.0d, 0.0d); - verify(player).spawnParticle(Particle.SHRIEK, 0.0d, 0.0d, 0.0d, 1, 4); + verify(mockPlayer).spawnParticle(Particle.SHRIEK, 0.0d, 0.0d, 0.0d, 1, 4); } @@ -877,14 +864,14 @@ public void testSpawnParticleParticleObjectDoubleDoubleDouble() { public void testSpawnParticleParticleObjectDoubleDoubleDoubleRedstone() { User.clearUsers(); Location loc = mock(Location.class); - when(player.getLocation()).thenReturn(loc); + when(mockPlayer.getLocation()).thenReturn(loc); when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(server.getViewDistance()).thenReturn(16); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); DustOptions dust = mock(DustOptions.class); p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); - verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } @@ -895,14 +882,14 @@ public void testSpawnParticleParticleObjectDoubleDoubleDoubleRedstone() { public void testSpawnParticleParticleDustOptionsDoubleDoubleDouble() { User.clearUsers(); Location loc = mock(Location.class); - when(player.getLocation()).thenReturn(loc); + when(mockPlayer.getLocation()).thenReturn(loc); when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(server.getViewDistance()).thenReturn(16); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); DustOptions dust = mock(DustOptions.class); p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); - verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } @@ -913,14 +900,14 @@ public void testSpawnParticleParticleDustOptionsDoubleDoubleDouble() { public void testSpawnParticleParticleDustOptionsIntIntInt() { User.clearUsers(); Location loc = mock(Location.class); - when(player.getLocation()).thenReturn(loc); + when(mockPlayer.getLocation()).thenReturn(loc); when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(server.getViewDistance()).thenReturn(16); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); DustOptions dust = mock(DustOptions.class); p.spawnParticle(Particle.DUST, dust, 0, 0, 0); - verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } @@ -930,7 +917,7 @@ public void testSpawnParticleParticleDustOptionsIntIntInt() { @Test public void testSetAddon() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); Addon addon = mock(Addon.class); when(addon.getDescription()).thenReturn(new Builder("main", "gameAddon", "1.0").build()); p.setAddon(addon); @@ -944,7 +931,7 @@ public void testSetAddon() { @Test public void testGetMetaData() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); when(pm.getPlayer(uuid)).thenReturn(players); assertEquals(Optional.of(new HashMap<>()), p.getMetaData()); } @@ -955,7 +942,7 @@ public void testGetMetaData() { @Test public void testSetMetaData() { User.clearUsers(); - User p = User.getInstance(player); + User p = User.getInstance(mockPlayer); when(pm.getPlayer(uuid)).thenReturn(players); Map metaData = new HashMap<>(); p.setMetaData(metaData); diff --git a/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java b/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java index 0604f1af8..338a6ea5d 100644 --- a/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java @@ -7,7 +7,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -21,7 +20,6 @@ import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; -import org.bukkit.plugin.PluginManager; import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; @@ -30,29 +28,26 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.CommandsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) -public class BentoBoxPermsCommandTest { +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) +public class BentoBoxPermsCommandTest extends AbstractCommonSetup { - @Mock - private BentoBox plugin; @Mock private CompositeCommand ac; @Mock @@ -64,8 +59,6 @@ public class BentoBoxPermsCommandTest { @Mock private PlaceholdersManager phm; @Mock - private PluginManager pim; - @Mock private Permission perm; private PermissionDefault defaultPerm = PermissionDefault.OP; @@ -74,8 +67,7 @@ public class BentoBoxPermsCommandTest { */ @Before public void setUp() throws Exception { - // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // Command manager CommandsManager cm = mock(CommandsManager.class); @@ -101,10 +93,8 @@ public void setUp() throws Exception { User.setPlugin(plugin); // Bukkit - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(perm.getDefault()).thenReturn(defaultPerm); when(pim.getPermission(anyString())).thenReturn(perm); - when(Bukkit.getPluginManager()).thenReturn(pim); // Placeholders when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); @@ -112,11 +102,6 @@ public void setUp() throws Exception { // BentoBox when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getPlaceholdersManager()).thenReturn(phm); - IslandWorldManager iwm = mock(IslandWorldManager.class); - when(plugin.getIWM()).thenReturn(iwm); - - // Commands for perms - cmd = new BentoBoxPermsCommand(ac); } @@ -172,9 +157,10 @@ public void testExecuteUserStringListOfString() { public void testExecuteUserStringListOfStringConsole() { String[] args = new String[1]; args[0] = ""; - CommandSender p = mock(CommandSender.class); - assertTrue(cmd.execute(p, "perms", args)); - verify(p, never()).sendMessage("general.errors.use-in-console"); + CommandSender console = mock(CommandSender.class); + when(console.spigot()).thenReturn(spigot); + assertTrue(cmd.execute(console, "perms", args)); + checkSpigotMessage("general.errors.use-in-console", 0); } /** @@ -185,8 +171,7 @@ public void testExecuteUserStringListOfStringIsPlayer() { when(user.isPlayer()).thenReturn(true); String[] args = new String[1]; args[0] = ""; - Player p = mock(Player.class); - assertFalse(cmd.execute(p, "perms", args)); - verify(p).sendMessage("general.errors.use-in-console"); + assertFalse(cmd.execute(mockPlayer, "perms", args)); + checkSpigotMessage("general.errors.use-in-console"); } } diff --git a/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java b/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java index d00b4ec03..f591f6793 100644 --- a/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java +++ b/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java @@ -41,13 +41,13 @@ import org.powermock.reflect.Whitebox; import dev.lone.itemsadder.api.CustomBlock; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.hooks.ItemsAdderHook.BlockInteractListener; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; diff --git a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java index 3ab2469f8..89176b84d 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java @@ -22,10 +22,10 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java index 431de3936..6ac0e9044 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java @@ -21,8 +21,6 @@ import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.player.PlayerJoinEvent; @@ -30,9 +28,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.PermissionAttachmentInfo; -import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; @@ -41,19 +37,17 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.managers.AddonsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; @@ -67,34 +61,24 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, Util.class, Bukkit.class, IslandsManager.class }) -public class JoinLeaveListenerTest { +public class JoinLeaveListenerTest extends RanksManagerBeforeClassTest { private static final String[] NAMES = { "adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe" }; - @Mock - private BentoBox plugin; @Mock private PlayersManager pm; @Mock - private Player player; - @Mock private Player coopPlayer; - @Mock - private World world; private JoinLeaveListener jll; @Mock private Players pls; @Mock - private IslandWorldManager iwm; - @Mock private Inventory chest; @Mock private Settings settings; @Mock - private IslandsManager im; - @Mock private BukkitScheduler scheduler; @Mock private PlayerInventory inv; @@ -103,10 +87,6 @@ public class JoinLeaveListenerTest { private @Nullable Island island; @Mock private GameModeAddon gameMode; - @Mock - private PluginManager pim; - @Mock - private @NonNull Location location; @Mock private AddonsManager am; @@ -115,10 +95,7 @@ public class JoinLeaveListenerTest { */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); - - // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // World when(world.getName()).thenReturn("worldname"); @@ -138,12 +115,12 @@ public void setUp() throws Exception { UUID uuid = UUID.randomUUID(); // Player - when(player.getUniqueId()).thenReturn(uuid); - when(player.getWorld()).thenReturn(world); - when(player.getEnderChest()).thenReturn(chest); - when(player.getName()).thenReturn("tastybento"); - when(player.getInventory()).thenReturn(inv); - when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + when(mockPlayer.getWorld()).thenReturn(world); + when(mockPlayer.getEnderChest()).thenReturn(chest); + when(mockPlayer.getName()).thenReturn("tastybento"); + when(mockPlayer.getInventory()).thenReturn(inv); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.emptySet()); // Player is pending kicks set = new HashSet<>(); @@ -178,12 +155,12 @@ public void setUp() throws Exception { // Add a coop member UUID uuid2 = UUID.randomUUID(); when(coopPlayer.getUniqueId()).thenReturn(uuid2); + when(coopPlayer.spigot()).thenReturn(spigot); User.getInstance(coopPlayer); memberMap.put(uuid2, RanksManager.COOP_RANK); island.setMembers(memberMap); // Bukkit - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(Bukkit.getScheduler()).thenReturn(scheduler); when(Bukkit.getPluginManager()).thenReturn(pim); @@ -200,14 +177,13 @@ public void setUp() throws Exception { online.put(u, name); onlinePlayers.add(p1); } - onlinePlayers.add(player); + onlinePlayers.add(mockPlayer); when(Bukkit.getOnlinePlayers()).then((Answer>) invocation -> onlinePlayers); User.setPlugin(plugin); - User.getInstance(player); + User.getInstance(mockPlayer); // Util - PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); // Util translate color codes (used in user translate methods) when(Util.translateColorCodes(anyString())) @@ -241,11 +217,11 @@ public void tearDown() { */ @Test public void testOnPlayerJoinNotKnownNoAutoCreate() { - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify verify(pm, times(3)).getPlayer(any()); - verify(player, never()).sendMessage(anyString()); + verify(mockPlayer, never()).sendMessage(anyString()); // Verify resets verify(pm).setResets(eq(world), any(), eq(0)); // Verify inventory clear because of kick @@ -260,9 +236,9 @@ public void testOnPlayerJoinNotKnownNoAutoCreate() { */ @Test public void testOnPlayerJoinNullWorld() { - when(player.getWorld()).thenReturn(null); // Null + when(mockPlayer.getWorld()).thenReturn(null); // Null when(Util.getWorld(any())).thenReturn(null); // Make null - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify inventory clear because of kick // Check inventory cleared @@ -280,11 +256,11 @@ public void testOnPlayerJoinRangeChangeTooLargePerm() { PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); when(pa.getPermission()).thenReturn("acidisland.island.range.1000"); when(pa.getValue()).thenReturn(true); - when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify - verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); + checkSpigotMessage("commands.admin.setrange.range-updated"); // Verify island setting assertEquals(100, island.getProtectionRange()); // Verify log @@ -300,11 +276,11 @@ public void testOnPlayerJoinRangeChangeSmallerPerm() { PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); when(pa.getPermission()).thenReturn("acidisland.island.range.10"); when(pa.getValue()).thenReturn(true); - when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify - verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); + checkSpigotMessage("commands.admin.setrange.range-updated"); // Verify island setting assertEquals(10, island.getProtectionRange()); // Verify log @@ -320,11 +296,11 @@ public void testOnPlayerJoinRangeChangeSmallIncreasePerm() { PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); when(pa.getPermission()).thenReturn("acidisland.island.range.55"); when(pa.getValue()).thenReturn(true); - when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify - verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); + checkSpigotMessage("commands.admin.setrange.range-updated"); // Verify island setting assertEquals(55, island.getProtectionRange()); // Verify log @@ -340,11 +316,11 @@ public void testOnPlayerJoinRangeChangeSamePerm() { PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); when(pa.getPermission()).thenReturn("acidisland.island.range.50"); when(pa.getValue()).thenReturn(true); - when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify - verify(player, never()).sendMessage(eq("commands.admin.setrange.range-updated")); + verify(mockPlayer, never()).sendMessage(eq("commands.admin.setrange.range-updated")); // Verify that the island protection range is not changed if it is already at // that value assertEquals(50, island.getProtectionRange()); @@ -358,11 +334,11 @@ public void testOnPlayerJoinRangeChangeSamePerm() { @Test public void testOnPlayerJoinNotKnownAutoCreate() { when(iwm.isCreateIslandOnFirstLoginEnabled(eq(world))).thenReturn(true); - PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, ""); jll.onPlayerJoin(event); // Verify verify(pm, times(3)).getPlayer(any()); - verify(player).sendMessage(eq("commands.island.create.on-first-login")); + checkSpigotMessage("commands.island.create.on-first-login"); } /** @@ -371,7 +347,7 @@ public void testOnPlayerJoinNotKnownAutoCreate() { */ @Test public void testOnPlayerSwitchWorld() { - PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, world); + PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(mockPlayer, world); jll.onPlayerSwitchWorld(event); // Check inventory cleared verify(chest).clear(); @@ -385,7 +361,7 @@ public void testOnPlayerSwitchWorld() { @Test public void testOnPlayerSwitchWorldNullWorld() { when(Util.getWorld(any())).thenReturn(null); - PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, world); + PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(mockPlayer, world); jll.onPlayerSwitchWorld(event); // These should not happen verify(chest, never()).clear(); @@ -399,9 +375,9 @@ public void testOnPlayerSwitchWorldNullWorld() { */ @Test public void testOnPlayerQuit() { - PlayerQuitEvent event = new PlayerQuitEvent(player, ""); + PlayerQuitEvent event = new PlayerQuitEvent(mockPlayer, ""); jll.onPlayerQuit(event); - verify(coopPlayer).sendMessage(eq("commands.island.team.uncoop.all-members-logged-off")); + checkSpigotMessage("commands.island.team.uncoop.all-members-logged-off"); // Team is now only 1 big assertEquals(1, island.getMembers().size()); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java index b0e48b3f6..2aad266a2 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java @@ -6,13 +6,10 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -20,7 +17,6 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; -import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; @@ -34,17 +30,13 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; @@ -58,25 +50,13 @@ @PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class }) public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup { - @Mock - private BentoBox plugin; - @Mock - private IslandsManager im; @Mock private PlayersManager pm; @Mock - private IslandWorldManager iwm; - @Mock - private World world; - @Mock private World nether; @Mock private World end; @Mock - private Player player; - @Mock - private Location location; - @Mock private Block block; private StandardSpawnProtectionListener ssp; @@ -91,10 +71,7 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup { */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Setup plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); - when(plugin.getIWM()).thenReturn(iwm); + super.setUp(); // Worlds when(world.getEnvironment()).thenReturn(World.Environment.NORMAL); when(nether.getEnvironment()).thenReturn(World.Environment.NETHER); @@ -112,7 +89,6 @@ public void setUp() throws Exception { when(iwm.getWorldSettings(any())).thenReturn(ws); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); // Util - PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); // Location when(location.toVector()).thenReturn(new Vector(5,5,5)); @@ -120,9 +96,9 @@ public void setUp() throws Exception { when(spawnLocation.toVector()).thenReturn(new Vector(0,0,0)); when(spawnLocation.getWorld()).thenReturn(nether); // Player - when(player.getWorld()).thenReturn(nether); - when(player.getUniqueId()).thenReturn(UUID.randomUUID()); - User.getInstance(player); + when(mockPlayer.getWorld()).thenReturn(nether); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + User.getInstance(mockPlayer); // Locales LocalesManager lm = mock(LocalesManager.class); when(lm.get(any(), any())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); @@ -157,10 +133,10 @@ public void tearDown() { */ @Test public void testOnBlockPlaceDisallowed() { - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertTrue(e.isCancelled()); - verify(player).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected"); } /** @@ -169,10 +145,10 @@ public void testOnBlockPlaceDisallowed() { @Test public void testOnBlockPlaceDisallowedNoProtection() { when(iwm.isNetherIslands(any())).thenReturn(true); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -180,11 +156,11 @@ public void testOnBlockPlaceDisallowedNoProtection() { */ @Test public void testOnBlockPlaceAllowed() { - when(player.isOp()).thenReturn(true); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + when(mockPlayer.isOp()).thenReturn(true); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -193,10 +169,10 @@ public void testOnBlockPlaceAllowed() { @Test public void testOnBlockPlaceAllowedOutsideSpawn() { when(iwm.getNetherSpawnRadius(any())).thenReturn(1); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -205,11 +181,11 @@ public void testOnBlockPlaceAllowedOutsideSpawn() { @Test public void testOnBlockPlaceAllowedWrongWorld() { when(location.getWorld()).thenReturn(world); - when(player.getWorld()).thenReturn(world); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + when(mockPlayer.getWorld()).thenReturn(world); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -218,10 +194,10 @@ public void testOnBlockPlaceAllowedWrongWorld() { @Test public void testOnBlockPlaceAllowedNetherIslandWorlds() { when(iwm.isNetherIslands(any())).thenReturn(true); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -230,13 +206,13 @@ public void testOnBlockPlaceAllowedNetherIslandWorlds() { @Test public void testOnBlockPlaceAllowedEndIslandWorlds() { when(location.getWorld()).thenReturn(end); - when(player.getWorld()).thenReturn(end); + when(mockPlayer.getWorld()).thenReturn(end); when(spawnLocation.getWorld()).thenReturn(end); when(iwm.isEndIslands(any())).thenReturn(true); - BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); + BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND); ssp.onBlockPlace(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -244,10 +220,10 @@ public void testOnBlockPlaceAllowedEndIslandWorlds() { */ @Test public void testOnBlockBreakDisallowed() { - BlockBreakEvent e = new BlockBreakEvent(block, player); + BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer); ssp.onBlockBreak(e); assertTrue(e.isCancelled()); - verify(player).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected"); } /** @@ -256,10 +232,10 @@ public void testOnBlockBreakDisallowed() { @Test public void testOnBlockBreakDisallowedNoProtection() { when(ws.isMakeNetherPortals()).thenReturn(true); - BlockBreakEvent e = new BlockBreakEvent(block, player); + BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer); ssp.onBlockBreak(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -267,11 +243,11 @@ public void testOnBlockBreakDisallowedNoProtection() { */ @Test public void testOnBlockBreakAllowed() { - when(player.isOp()).thenReturn(true); - BlockBreakEvent e = new BlockBreakEvent(block, player); + when(mockPlayer.isOp()).thenReturn(true); + BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer); ssp.onBlockBreak(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -291,7 +267,7 @@ public void testOnExplosion() { new Vector(0,0,0), new Vector(0,0,0), new Vector(10000,0,0)); - EntityExplodeEvent e = getExplodeEvent(player, location, blockList); + EntityExplodeEvent e = getExplodeEvent(mockPlayer, location, blockList); ssp.onExplosion(e); // 4 blocks inside the spawn should be removed, leaving one assertEquals(1, blockList.size()); @@ -316,7 +292,7 @@ public void testOnExplosionNoProtection() { new Vector(0,0,0), new Vector(0,0,0), new Vector(10000,0,0)); - EntityExplodeEvent e = getExplodeEvent(player, location, blockList); + EntityExplodeEvent e = getExplodeEvent(mockPlayer, location, blockList); ssp.onExplosion(e); // No blocks should be removed assertEquals(5, blockList.size()); @@ -328,10 +304,11 @@ public void testOnExplosionNoProtection() { */ @Test public void testOnBucketEmptyDisallowed() { - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null, + null); ssp.onBucketEmpty(e); assertTrue(e.isCancelled()); - verify(player).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected"); } /** @@ -340,10 +317,11 @@ public void testOnBucketEmptyDisallowed() { @Test public void testOnBucketEmptyDisallowedNoProtection() { when(ws.isMakeNetherPortals()).thenReturn(true); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null, + null); ssp.onBucketEmpty(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } /** @@ -351,11 +329,12 @@ public void testOnBucketEmptyDisallowedNoProtection() { */ @Test public void testOnBucketEmptyAllowed() { - when(player.isOp()).thenReturn(true); - PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); + when(mockPlayer.isOp()).thenReturn(true); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null, + null); ssp.onBucketEmpty(e); assertFalse(e.isCancelled()); - verify(player, never()).sendMessage("protection.spawn-protected"); + checkSpigotMessage("protection.spawn-protected", 0); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java index 20165cd76..7a8feddb4 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java @@ -36,6 +36,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.RanksManagerBeforeClassTest; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; @@ -48,7 +49,6 @@ import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.panels.settings.SettingsTab; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java index 4560bb7f6..7d67d21a5 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java @@ -34,10 +34,10 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag.Type; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java index d503fefc9..c85479552 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java @@ -45,8 +45,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListenerTest.java index fc88fc7aa..f5ff0d08a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListenerTest.java @@ -31,8 +31,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BucketListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BucketListenerTest.java index 049841d37..daf0ce85b 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BucketListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BucketListenerTest.java @@ -26,8 +26,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/CandleListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/CandleListenerTest.java index 6d709b421..005a51b8b 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/CandleListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/CandleListenerTest.java @@ -21,8 +21,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EggListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EggListenerTest.java index dfd94361a..65551ded9 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EggListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EggListenerTest.java @@ -19,8 +19,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ElytraListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ElytraListenerTest.java index 74ac31b0d..dba1f0278 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ElytraListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ElytraListenerTest.java @@ -18,8 +18,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.util.Util; /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java index c703dfbfe..57ea0e43f 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java @@ -33,9 +33,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ExperiencePickupListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ExperiencePickupListenerTest.java index f4883d1a0..4c7e58a48 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ExperiencePickupListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ExperiencePickupListenerTest.java @@ -24,8 +24,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListenerTest.java index 86740fe9b..a8dfbe066 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListenerTest.java @@ -29,9 +29,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java index 0d9c49b79..59225192d 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/InventoryListenerTest.java @@ -42,8 +42,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java index 8832948ca..79c15bfdd 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java @@ -39,8 +39,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PlaceBlocksListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PlaceBlocksListenerTest.java index cfd70160a..ca1e5cc9e 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PlaceBlocksListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PlaceBlocksListenerTest.java @@ -34,8 +34,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/SculkSensorListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/SculkSensorListenerTest.java index 8d9cdb0e5..d83743e51 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/SculkSensorListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/SculkSensorListenerTest.java @@ -19,9 +19,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java index 1153feb88..7aeec0766 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java @@ -43,8 +43,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ThrowingListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ThrowingListenerTest.java index 6839b3d5e..5adaaade5 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ThrowingListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/ThrowingListenerTest.java @@ -18,8 +18,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java index 6cc3f3e78..d44ec3ab0 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java @@ -46,12 +46,12 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java index 2733fe692..cf4c4f225 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java @@ -22,9 +22,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java index b7384a7db..e617450a7 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java @@ -35,9 +35,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java index 940b08d90..8649e1457 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java @@ -38,13 +38,13 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java index e4165e258..7700c446f 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java @@ -20,14 +20,12 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.entity.Player; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import org.bukkit.plugin.PluginManager; import org.bukkit.util.RayTraceResult; import org.junit.After; import org.junit.Before; @@ -35,48 +33,34 @@ import org.junit.runner.RunWith; import org.mockito.Mock; 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; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; +import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) -@PrepareForTest({BentoBox.class, PlayerEvent.class, PlayerInteractEvent.class, Bukkit.class}) -public class ObsidianScoopingListenerTest { +@PrepareForTest({ BentoBox.class, PlayerEvent.class, PlayerInteractEvent.class, Bukkit.class, Util.class }) +public class ObsidianScoopingListenerTest extends AbstractCommonSetup { - @Mock - private World world; private ObsidianScoopingListener listener; @Mock private ItemStack item; @Mock private Block clickedBlock; @Mock - private BentoBox plugin; - @Mock - private Player p; - @Mock - private IslandWorldManager iwm; - @Mock - private IslandsManager im; - @Mock private LocalesManager lm; private Material inHand; private Material block; @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + super.setUp(); // Mock server Server server = mock(Server.class); @@ -84,9 +68,6 @@ public void setUp() throws Exception { when(server.getWorld("world")).thenReturn(world); when(server.getVersion()).thenReturn("BSB_Mocking"); - PluginManager pluginManager = mock(PluginManager.class); - when(Bukkit.getPluginManager()).thenReturn(pluginManager); - // Mock item factory (for itemstacks) ItemFactory itemFactory = mock(ItemFactory.class); when(server.getItemFactory()).thenReturn(itemFactory); @@ -95,30 +76,25 @@ public void setUp() throws Exception { listener = new ObsidianScoopingListener(); // Mock player - when(p.getWorld()).thenReturn(world); + when(mockPlayer.getWorld()).thenReturn(world); RayTraceResult rtr = mock(RayTraceResult.class); - when(p.rayTraceBlocks(5, FluidCollisionMode.ALWAYS)).thenReturn(rtr); + when(mockPlayer.rayTraceBlocks(5, FluidCollisionMode.ALWAYS)).thenReturn(rtr); when(rtr.getHitBlock()).thenReturn(clickedBlock); - Location location = mock(Location.class); when(location.getWorld()).thenReturn(world); when(location.getBlockX()).thenReturn(0); when(location.getBlockY()).thenReturn(0); when(location.getBlockZ()).thenReturn(0); - when(p.getLocation()).thenReturn(location); + when(mockPlayer.getLocation()).thenReturn(location); - when(p.getInventory()).thenReturn(mock(PlayerInventory.class)); + when(mockPlayer.getInventory()).thenReturn(mock(PlayerInventory.class)); // Worlds - when(plugin.getIWM()).thenReturn(iwm); when(iwm.getIslandWorld(Mockito.any())).thenReturn(world); when(iwm.getNetherWorld(Mockito.any())).thenReturn(world); when(iwm.getEndWorld(Mockito.any())).thenReturn(world); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); - // Mock up IslandsManager - when(plugin.getIslands()).thenReturn(im); - // Mock up items and blocks when(clickedBlock.getX()).thenReturn(0); when(clickedBlock.getY()).thenReturn(0); @@ -136,7 +112,7 @@ public void setUp() throws Exception { // Put player on island when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true); // Set as survival - when(p.getGameMode()).thenReturn(GameMode.SURVIVAL); + when(mockPlayer.getGameMode()).thenReturn(GameMode.SURVIVAL); // Locales when(plugin.getLocalesManager()).thenReturn(lm); @@ -157,7 +133,7 @@ public void setUp() throws Exception { PlayerInventory playerInventory = mock(PlayerInventory.class); when(playerInventory.getItemInMainHand()).thenReturn(item); when(playerInventory.getItemInOffHand()).thenReturn(new ItemStack(Material.AIR)); - when(p.getInventory()).thenReturn(playerInventory); + when(mockPlayer.getInventory()).thenReturn(playerInventory); // Addon when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); @@ -219,7 +195,8 @@ public void testOnPlayerInteractObsidianManyBucketsInHand() { @Test public void testOnPlayerInteractNotInWorld() { - PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); + PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, + BlockFace.EAST); // Test not in world when(iwm.inWorld(any(World.class))).thenReturn(false); when(iwm.inWorld(any(Location.class))).thenReturn(false); @@ -235,11 +212,12 @@ public void testOnPlayerInteractInWorld() { @Test public void testOnPlayerInteractGameModes() { - PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); + PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, + BlockFace.EAST); // Test different game modes for (GameMode gm : GameMode.values()) { - when(p.getGameMode()).thenReturn(gm); + when(mockPlayer.getGameMode()).thenReturn(gm); if (!gm.equals(GameMode.SURVIVAL)) { assertFalse(listener.onPlayerInteract(event)); } @@ -248,10 +226,11 @@ public void testOnPlayerInteractGameModes() { @Test public void testOnPlayerInteractSurvivalNotOnIsland() { - PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); + PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, + BlockFace.EAST); // Set as survival - when(p.getGameMode()).thenReturn(GameMode.SURVIVAL); + when(mockPlayer.getGameMode()).thenReturn(GameMode.SURVIVAL); // Positive test with 1 bucket in the stack inHand = Material.BUCKET; @@ -273,7 +252,8 @@ private void testEvent() { when(airBlock.getType()).thenReturn(Material.AIR); ObsidianScoopingListener listener = new ObsidianScoopingListener(); - PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); + PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, + BlockFace.EAST); if (!item.getType().equals(Material.BUCKET) || !clickedBlock.getType().equals(Material.OBSIDIAN)) { assertFalse(listener.onPlayerInteract(event)); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java index f18e22374..2e8d1a513 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java @@ -21,9 +21,9 @@ import com.google.common.collect.ImmutableSet; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java index 0110c7566..c58c77417 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java @@ -24,7 +24,6 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.entity.Player; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -33,23 +32,18 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; 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; import com.google.common.collect.ImmutableSet; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.util.Util; /** @@ -62,49 +56,24 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup { // Class under test private VisitorKeepInventoryListener l; - @Mock - private Player player; - /* IslandWorldManager */ - @Mock - private IslandWorldManager iwm; - /* World */ - @Mock - private World world; - - /* Islands */ - @Mock - private IslandsManager islandsManager; - - @Mock - private Island island; private PlayerDeathEvent e; - @Mock - private Location location; - - /** - */ @Before public void setUp() throws Exception { - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); - /* Island World Manager */ - when(plugin.getIWM()).thenReturn(iwm); + super.setUp(); // User User.setPlugin(plugin); UUID uuid = UUID.randomUUID(); - when(player.getUniqueId()).thenReturn(uuid); - when(player.getName()).thenReturn("tastybento"); - when(player.getLocation()).thenReturn(location); + when(mockPlayer.getUniqueId()).thenReturn(uuid); + when(mockPlayer.getName()).thenReturn("tastybento"); + when(mockPlayer.getLocation()).thenReturn(location); when(location.getWorld()).thenReturn(world); when(location.toVector()).thenReturn(new Vector(1,2,3)); // Turn on why for player - when(player.getMetadata(eq("bskyblock_world_why_debug"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, true))); - when(player.getMetadata(eq("bskyblock_world_why_debug_issuer"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, uuid.toString()))); - User.getInstance(player); + when(mockPlayer.getMetadata(eq("bskyblock_world_why_debug"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, true))); + when(mockPlayer.getMetadata(eq("bskyblock_world_why_debug_issuer"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, uuid.toString()))); + User.getInstance(mockPlayer); // WorldSettings and World Flags WorldSettings ws = mock(WorldSettings.class); @@ -124,11 +93,11 @@ public void setUp() throws Exception { Flags.VISITOR_KEEP_INVENTORY.setSetting(world, false); /* Islands */ - when(plugin.getIslands()).thenReturn(islandsManager); + when(plugin.getIslands()).thenReturn(im); // Visitor when(island.getMemberSet(anyInt())).thenReturn(ImmutableSet.of()); // By default, there should be an island. - when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.of(island)); + when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island)); // Util PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS); @@ -137,7 +106,7 @@ public void setUp() throws Exception { // Default death event List drops = new ArrayList<>(); drops.add(new ItemStack(Material.ACACIA_BOAT)); - e = getPlayerDeathEvent(player, drops, 100, 0, 0, 0, "Death message"); + e = getPlayerDeathEvent(mockPlayer, drops, 100, 0, 0, 0, "Death message"); // Make new l = new VisitorKeepInventoryListener(); } @@ -169,8 +138,8 @@ public void testOnVisitorDeathFalseFlag() { assertFalse(e.getDrops().isEmpty()); assertEquals(100, e.getDroppedExp()); // Why - verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); - verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); + checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); + checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); } /** @@ -185,8 +154,8 @@ public void testOnVisitorDeathTrueFlag() { assertTrue(e.getDrops().isEmpty()); assertEquals(0, e.getDroppedExp()); // Why - verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); - verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_ALLOWED_IN_WORLD"); + checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); + checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_ALLOWED_IN_WORLD"); } /** @@ -202,8 +171,8 @@ public void testOnVisitorDeathNotInWorld() { assertFalse(e.getDrops().isEmpty()); assertEquals(100, e.getDroppedExp()); // Why - verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); - verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); + checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); + checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); } /** @@ -211,7 +180,7 @@ public void testOnVisitorDeathNotInWorld() { */ @Test public void testOnVisitorDeathTrueFlagNoIsland() { - when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.empty()); + when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty()); Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true); l.onVisitorDeath(e); assertFalse(e.getKeepInventory()); @@ -219,7 +188,7 @@ public void testOnVisitorDeathTrueFlagNoIsland() { assertFalse(e.getDrops().isEmpty()); assertEquals(100, e.getDroppedExp()); // Why - verify(player, never()).sendMessage(anyString()); + verify(mockPlayer, never()).sendMessage(anyString()); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java index 4542a600b..15c963c01 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java @@ -33,10 +33,10 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandWorldManager; diff --git a/src/test/java/world/bentobox/bentobox/listeners/teleports/EntityTeleportListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/teleports/EntityTeleportListenerTest.java index 03e697bf2..aa7ed3a70 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/teleports/EntityTeleportListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/teleports/EntityTeleportListenerTest.java @@ -23,8 +23,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index d31f95b7b..730b4547f 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -76,6 +76,7 @@ import io.papermc.lib.PaperLib; import io.papermc.lib.environments.CraftBukkitEnvironment; import io.papermc.lib.environments.Environment; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.configuration.WorldSettings; @@ -85,7 +86,6 @@ import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.island.IslandCache; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java index 7317a78a4..787459983 100644 --- a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java @@ -46,13 +46,13 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.util.Util; diff --git a/src/test/java/world/bentobox/bentobox/util/UtilTest.java b/src/test/java/world/bentobox/bentobox/util/UtilTest.java index 0577d687b..4745b6ce3 100644 --- a/src/test/java/world/bentobox/bentobox/util/UtilTest.java +++ b/src/test/java/world/bentobox/bentobox/util/UtilTest.java @@ -29,6 +29,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.util.Vector; import org.junit.After; import org.junit.Before; @@ -72,6 +73,8 @@ public class UtilTest { private User user; @Mock private ConsoleCommandSender sender; + @Mock + private Spigot spigot; /** */ @@ -100,6 +103,7 @@ public void setUp() throws Exception { Server server = mock(Server.class); when(Bukkit.getServer()).thenReturn(server); when(Bukkit.getWorld(anyString())).thenReturn(world); + when(sender.spigot()).thenReturn(spigot); when(Bukkit.getConsoleSender()).thenReturn(sender); // Bukkit - online players @@ -113,6 +117,7 @@ public void setUp() throws Exception { when(p1.getUniqueId()).thenReturn(uuid); when(p1.getName()).thenReturn(name); when(p1.hasPermission(anyString())).thenReturn(true); + when(p1.spigot()).thenReturn(spigot); online.put(uuid, name); onlinePlayers.add(p1); // Add to User cache