|
| 1 | +package us.tastybento.bskyblock.commands.admin; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +import org.bukkit.Bukkit; |
| 12 | +import org.bukkit.World; |
| 13 | +import org.bukkit.entity.Player; |
| 14 | +import org.bukkit.scheduler.BukkitScheduler; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.mockito.Mockito; |
| 19 | +import org.powermock.api.mockito.PowerMockito; |
| 20 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 21 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 22 | +import org.powermock.reflect.Whitebox; |
| 23 | + |
| 24 | +import us.tastybento.bskyblock.BSkyBlock; |
| 25 | +import us.tastybento.bskyblock.Settings; |
| 26 | +import us.tastybento.bskyblock.api.user.User; |
| 27 | +import us.tastybento.bskyblock.commands.AdminCommand; |
| 28 | +import us.tastybento.bskyblock.managers.CommandsManager; |
| 29 | +import us.tastybento.bskyblock.managers.IslandWorldManager; |
| 30 | +import us.tastybento.bskyblock.managers.IslandsManager; |
| 31 | +import us.tastybento.bskyblock.managers.LocalesManager; |
| 32 | +import us.tastybento.bskyblock.managers.PlayersManager; |
| 33 | + |
| 34 | +@RunWith(PowerMockRunner.class) |
| 35 | +@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class }) |
| 36 | +public class AdminClearResetsAllCommandTest { |
| 37 | + |
| 38 | + private AdminCommand ac; |
| 39 | + private User user; |
| 40 | + private IslandsManager im; |
| 41 | + private PlayersManager pm; |
| 42 | + private UUID notUUID; |
| 43 | + |
| 44 | + /** |
| 45 | + * @throws java.lang.Exception |
| 46 | + */ |
| 47 | + @Before |
| 48 | + public void setUp() throws Exception { |
| 49 | + // Set up plugin |
| 50 | + BSkyBlock plugin = mock(BSkyBlock.class); |
| 51 | + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); |
| 52 | + |
| 53 | + // Command manager |
| 54 | + CommandsManager cm = mock(CommandsManager.class); |
| 55 | + when(plugin.getCommandsManager()).thenReturn(cm); |
| 56 | + |
| 57 | + // Settings |
| 58 | + Settings s = mock(Settings.class); |
| 59 | + when(s.getResetWait()).thenReturn(0L); |
| 60 | + when(s.getResetLimit()).thenReturn(3); |
| 61 | + when(plugin.getSettings()).thenReturn(s); |
| 62 | + |
| 63 | + // Player |
| 64 | + Player p = mock(Player.class); |
| 65 | + // Sometimes use Mockito.withSettings().verboseLogging() |
| 66 | + user = mock(User.class); |
| 67 | + when(user.isOp()).thenReturn(false); |
| 68 | + UUID uuid = UUID.randomUUID(); |
| 69 | + notUUID = UUID.randomUUID(); |
| 70 | + while(notUUID.equals(uuid)) { |
| 71 | + notUUID = UUID.randomUUID(); |
| 72 | + } |
| 73 | + when(user.getUniqueId()).thenReturn(uuid); |
| 74 | + when(user.getPlayer()).thenReturn(p); |
| 75 | + when(user.getName()).thenReturn("tastybento"); |
| 76 | + User.setPlugin(plugin); |
| 77 | + |
| 78 | + // Parent command has no aliases |
| 79 | + ac = mock(AdminCommand.class); |
| 80 | + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); |
| 81 | + |
| 82 | + // Island World Manager |
| 83 | + IslandWorldManager iwm = mock(IslandWorldManager.class); |
| 84 | + World world = mock(World.class); |
| 85 | + when(iwm.getBSBIslandWorld()).thenReturn(world); |
| 86 | + when(plugin.getIWM()).thenReturn(iwm); |
| 87 | + |
| 88 | + |
| 89 | + // Player has island to begin with |
| 90 | + im = mock(IslandsManager.class); |
| 91 | + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); |
| 92 | + when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); |
| 93 | + when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true); |
| 94 | + when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid); |
| 95 | + when(plugin.getIslands()).thenReturn(im); |
| 96 | + |
| 97 | + // Has team |
| 98 | + pm = mock(PlayersManager.class); |
| 99 | + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); |
| 100 | + |
| 101 | + when(plugin.getPlayers()).thenReturn(pm); |
| 102 | + |
| 103 | + // Server & Scheduler |
| 104 | + BukkitScheduler sch = mock(BukkitScheduler.class); |
| 105 | + PowerMockito.mockStatic(Bukkit.class); |
| 106 | + when(Bukkit.getScheduler()).thenReturn(sch); |
| 107 | + |
| 108 | + // Locales |
| 109 | + LocalesManager lm = mock(LocalesManager.class); |
| 110 | + when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); |
| 111 | + when(plugin.getLocalesManager()).thenReturn(lm); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsAllCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testExecuteCheckConfirm() { |
| 119 | + AdminClearResetsAllCommand itl = new AdminClearResetsAllCommand(ac); |
| 120 | + assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); |
| 121 | + Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.any()); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments