From b72d1853b5ae4f6495a3ea260050777095213d6e Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 21 Jul 2018 14:06:40 -0700 Subject: [PATCH] Added Range command tests and put in some locale messages. --- locales/en-US.yml | 8 +- .../admin/range/AdminRangeResetCommand.java | 14 +- .../admin/range/AdminRangeSetCommand.java | 11 +- .../admin/range/AdminRangeCommandTest.java | 145 +++++++++ .../range/AdminRangeDisplayCommandTest.java | 175 +++++++++++ .../range/AdminRangeResetCommandTest.java | 206 ++++++++++++ .../admin/range/AdminRangeSetCommandTest.java | 295 ++++++++++++++++++ 7 files changed, 840 insertions(+), 14 deletions(-) create mode 100644 src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeCommandTest.java create mode 100644 src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeDisplayCommandTest.java create mode 100644 src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommandTest.java create mode 100644 src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommandTest.java diff --git a/locales/en-US.yml b/locales/en-US.yml index c9881d79c..a5e71a2eb 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -89,12 +89,14 @@ commands: description: "Sets the island protected range" invalid-value: not-numeric: "&c[number] is not a whole number!" - too-low: "&cThe protection range must be higher than 1!" - too-high: "&cThe protection range should be less than the island distance!" + too-low: "&cThe protection range must be greater than 1!" + too-high: "&cThe protection range should be equal or less than [number]!" same-as-before: "&cThe protection range is already set to [number]!" + success: "&2Set island protection range to [number]" reset: parameters: "" - description: "Resets the island protected range to the server's default" + description: "Resets the island protected range to the world default" + success: "&2Reset island protection range to [number]" register: parameters: "" description: "register player to unowned island you are on" diff --git a/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommand.java b/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommand.java index 2d95dec5e..6046d283e 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommand.java @@ -1,12 +1,13 @@ package us.tastybento.bskyblock.commands.admin.range; -import us.tastybento.bskyblock.api.commands.CompositeCommand; -import us.tastybento.bskyblock.api.user.User; -import us.tastybento.bskyblock.database.objects.Island; - import java.util.List; import java.util.UUID; +import us.tastybento.bskyblock.api.commands.CompositeCommand; +import us.tastybento.bskyblock.api.localization.TextVariables; +import us.tastybento.bskyblock.api.user.User; +import us.tastybento.bskyblock.database.objects.Island; + public class AdminRangeResetCommand extends CompositeCommand { public AdminRangeResetCommand(CompositeCommand parent) { @@ -43,8 +44,9 @@ public class AdminRangeResetCommand extends CompositeCommand { Island island = getIslands().getIsland(getWorld(), targetUUID); // Reset the protection range - island.setProtectionRange(getSettings().getIslandProtectionRange()); - // TODO send message? + int range = getIWM().getIslandProtectionRange(getWorld()); + island.setProtectionRange(range); + user.sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, String.valueOf(range)); return true; } diff --git a/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommand.java b/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommand.java index 9cbe28da8..3c9ec6773 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommand.java @@ -1,14 +1,15 @@ package us.tastybento.bskyblock.commands.admin.range; +import java.util.List; +import java.util.UUID; + import org.apache.commons.lang.StringUtils; + import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.localization.TextVariables; import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.database.objects.Island; -import java.util.List; -import java.util.UUID; - public class AdminRangeSetCommand extends CompositeCommand { public AdminRangeSetCommand(CompositeCommand parent) { @@ -57,7 +58,7 @@ public class AdminRangeSetCommand extends CompositeCommand { return false; } if (range > island.getRange()) { - user.sendMessage("commands.admin.range.set.invalid-value.too-high", TextVariables.NUMBER, args.get(1)); + user.sendMessage("commands.admin.range.set.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(island.getRange())); return false; } if (range == island.getProtectionRange()) { @@ -67,7 +68,7 @@ public class AdminRangeSetCommand extends CompositeCommand { // Well, now it can be applied without taking any risks ! island.setProtectionRange(range); - // TODO send message? + user.sendMessage("commands.admin.range.set.success", TextVariables.NUMBER, String.valueOf(range)); return true; } diff --git a/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeCommandTest.java new file mode 100644 index 000000000..2fb58a1b4 --- /dev/null +++ b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeCommandTest.java @@ -0,0 +1,145 @@ +package us.tastybento.bskyblock.commands.admin.range; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; + +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.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +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 us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.Settings; +import us.tastybento.bskyblock.api.user.User; +import us.tastybento.bskyblock.commands.AdminCommand; +import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.IslandWorldManager; +import us.tastybento.bskyblock.managers.IslandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; +import us.tastybento.bskyblock.managers.PlayersManager; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) +public class AdminRangeCommandTest { + + private BSkyBlock plugin; + private AdminCommand ac; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + plugin = mock(BSkyBlock.class); + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + Settings s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getResetLimit()).thenReturn(3); + 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.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ac = mock(AdminCommand.class); + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + World world = mock(World.class); + when(iwm.getBSBIslandWorld()).thenReturn(world); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + 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); + when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid); + when(plugin.getIslands()).thenReturn(im); + + // Has team + pm = mock(PlayersManager.class); + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + Answer answer = new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArgumentAt(1, String.class); + }}; + when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(answer ); + when(plugin.getLocalesManager()).thenReturn(lm); + } + + + @Test + public void testExecuteConsoleNoArgs() { + AdminRangeCommand arc = new AdminRangeCommand(ac); + CommandSender sender = mock(CommandSender.class); + User console = User.getInstance(sender); + arc.execute(console, "", new ArrayList<>()); + // Show help + Mockito.verify(sender).sendMessage("commands.help.header"); + } + + @Test + public void testExecutePlayerNoArgs() { + AdminRangeCommand arc = new AdminRangeCommand(ac); + arc.execute(user, "", new ArrayList<>()); + // Show help" + Mockito.verify(user).sendMessage("commands.help.header","[label]","BSkyBlock"); + } + +} diff --git a/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeDisplayCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeDisplayCommandTest.java new file mode 100644 index 000000000..c42d2c51a --- /dev/null +++ b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeDisplayCommandTest.java @@ -0,0 +1,175 @@ +/** + * + */ +package us.tastybento.bskyblock.commands.admin.range; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +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 us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.Settings; +import us.tastybento.bskyblock.api.user.User; +import us.tastybento.bskyblock.commands.AdminCommand; +import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.IslandWorldManager; +import us.tastybento.bskyblock.managers.IslandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; +import us.tastybento.bskyblock.managers.PlayersManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) +public class AdminRangeDisplayCommandTest { + + private BSkyBlock plugin; + private AdminCommand ac; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + plugin = mock(BSkyBlock.class); + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + Settings s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getResetLimit()).thenReturn(3); + 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.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ac = mock(AdminCommand.class); + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + World world = mock(World.class); + when(iwm.getBSBIslandWorld()).thenReturn(world); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + 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); + when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid); + when(plugin.getIslands()).thenReturn(im); + + // Has team + pm = mock(PlayersManager.class); + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + Answer answer = new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArgumentAt(1, String.class); + }}; + when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(answer ); + when(plugin.getLocalesManager()).thenReturn(lm); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeDisplayCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerDisplayArgs() { + AdminRangeDisplayCommand ardc = new AdminRangeDisplayCommand(ac); + ardc.execute(user, "display", new ArrayList<>()); + // Show display + Mockito.verify(user).sendMessage("commands.admin.range.display.showing"); + Mockito.verify(user).sendMessage("commands.admin.range.display.hint"); + // Run command again + ardc.execute(user, "display", new ArrayList<>()); + // Remove + Mockito.verify(user).sendMessage("commands.admin.range.display.hiding"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeDisplayCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayeShowArgs() { + AdminRangeDisplayCommand ardc = new AdminRangeDisplayCommand(ac); + ardc.execute(user, "show", new ArrayList<>()); + // Show display + Mockito.verify(user).sendMessage("commands.admin.range.display.showing"); + Mockito.verify(user).sendMessage("commands.admin.range.display.hint"); + // Run command again + ardc.execute(user, "show", new ArrayList<>()); + Mockito.verify(user).sendMessage("commands.admin.range.display.already-on"); + ardc.execute(user, "hide", new ArrayList<>()); + Mockito.verify(user).sendMessage("commands.admin.range.display.hiding"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeDisplayCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayeHideArgs() { + AdminRangeDisplayCommand ardc = new AdminRangeDisplayCommand(ac); + ardc.execute(user, "hide", new ArrayList<>()); + Mockito.verify(user).sendMessage("commands.admin.range.display.already-off"); + } + +} diff --git a/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommandTest.java new file mode 100644 index 000000000..5c477e710 --- /dev/null +++ b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeResetCommandTest.java @@ -0,0 +1,206 @@ +/** + * + */ +package us.tastybento.bskyblock.commands.admin.range; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +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.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +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 us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.Settings; +import us.tastybento.bskyblock.api.localization.TextVariables; +import us.tastybento.bskyblock.api.user.User; +import us.tastybento.bskyblock.commands.AdminCommand; +import us.tastybento.bskyblock.database.objects.Island; +import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.IslandWorldManager; +import us.tastybento.bskyblock.managers.IslandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; +import us.tastybento.bskyblock.managers.PlayersManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) +public class AdminRangeResetCommandTest { + + private BSkyBlock plugin; + private AdminCommand ac; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + plugin = mock(BSkyBlock.class); + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + Settings s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getResetLimit()).thenReturn(3); + 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.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ac = mock(AdminCommand.class); + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + World world = mock(World.class); + when(iwm.getBSBIslandWorld()).thenReturn(world); + 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); + when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid); + Island island = mock(Island.class); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + pm = mock(PlayersManager.class); + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + Answer answer = new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArgumentAt(1, String.class); + } + }; + + when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(answer ); + when(plugin.getLocalesManager()).thenReturn(lm); + + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteConsoleNoArgs() { + AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); + CommandSender sender = mock(CommandSender.class); + User console = User.getInstance(sender); + arc.execute(console, "", new ArrayList<>()); + // Show help + Mockito.verify(sender).sendMessage("commands.help.header"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerNoArgs() { + AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); + arc.execute(user, "", new ArrayList<>()); + // Show help + Mockito.verify(user).sendMessage("commands.help.header","[label]","BSkyBlock"); + } + + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("general.errors.unknown-player"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteKnownPlayerNoIsland() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteKnownPlayer() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, "200"); + } + +} diff --git a/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommandTest.java new file mode 100644 index 000000000..1fa40c15d --- /dev/null +++ b/src/test/java/us/tastybento/bskyblock/commands/admin/range/AdminRangeSetCommandTest.java @@ -0,0 +1,295 @@ +/** + * + */ +package us.tastybento.bskyblock.commands.admin.range; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +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.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +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 us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.Settings; +import us.tastybento.bskyblock.api.localization.TextVariables; +import us.tastybento.bskyblock.api.user.User; +import us.tastybento.bskyblock.commands.AdminCommand; +import us.tastybento.bskyblock.database.objects.Island; +import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.IslandWorldManager; +import us.tastybento.bskyblock.managers.IslandsManager; +import us.tastybento.bskyblock.managers.LocalesManager; +import us.tastybento.bskyblock.managers.PlayersManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) +public class AdminRangeSetCommandTest { + + private BSkyBlock plugin; + private AdminCommand ac; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + plugin = mock(BSkyBlock.class); + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + Settings s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getResetLimit()).thenReturn(3); + 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.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ac = mock(AdminCommand.class); + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + World world = mock(World.class); + when(iwm.getBSBIslandWorld()).thenReturn(world); + 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); + when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid); + Island island = mock(Island.class); + when(island.getRange()).thenReturn(50); + when(island.getProtectionRange()).thenReturn(50); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + pm = mock(PlayersManager.class); + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + Answer answer = new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArgumentAt(1, String.class); + } + }; + + when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(answer ); + when(plugin.getLocalesManager()).thenReturn(lm); + + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteConsoleNoArgs() { + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + CommandSender sender = mock(CommandSender.class); + User console = User.getInstance(sender); + arc.execute(console, "", new ArrayList<>()); + // Show help + Mockito.verify(sender).sendMessage("commands.help.header"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerNoArgs() { + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + arc.execute(user, "", new ArrayList<>()); + // Show help + Mockito.verify(user).sendMessage("commands.help.header","[label]","BSkyBlock"); + } + + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("100"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("general.errors.unknown-player"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteKnownPlayerNoIsland() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("100"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteTooHigh() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("100"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.too-high", TextVariables.NUMBER, "50"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNotANumber() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("NAN"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.not-numeric", TextVariables.NUMBER, "NAN"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteDoubleNumber() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("3.141592654"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.not-numeric", TextVariables.NUMBER, "3.141592654"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteZero() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("0"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.too-low", TextVariables.NUMBER, "0"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNegNumber() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("-437645"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.not-numeric", TextVariables.NUMBER, "-437645"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteSame() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("50"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.invalid-value.same-as-before", TextVariables.NUMBER, "50"); + } + + /** + * Test method for {@link us.tastybento.bskyblock.commands.admin.range.AdminRangeSetCommand#execute(us.tastybento.bskyblock.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecute() { + when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); + AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); + List args = new ArrayList<>(); + args.add("tastybento"); + args.add("48"); + arc.execute(user, "", args); + Mockito.verify(user).sendMessage("commands.admin.range.set.success", TextVariables.NUMBER, "48"); + } + +}