1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-13 06:14:21 +01:00

Added WarpsCommand test class

This commit is contained in:
tastybento 2019-10-31 17:33:34 -07:00
parent d7e0b36623
commit 9a234ef950
4 changed files with 200 additions and 80 deletions

View File

@ -1,6 +1,5 @@
package world.bentobox.warps.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

View File

@ -46,9 +46,9 @@ public class WarpsCommand extends CompositeCommand {
if (addon.getWarpSignsManager().listWarps(world).isEmpty()) {
user.sendMessage("warps.error.no-warps-yet");
user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine());
} else {
addon.getWarpPanelManager().showWarpPanel(world, user,0);
return false;
}
addon.getWarpPanelManager().showWarpPanel(world, user, 0);
return true;
}

View File

@ -4,8 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -14,35 +12,26 @@ import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
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 org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
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.warps.Warp;
import world.bentobox.warps.WarpSignsManager;
@ -63,16 +52,10 @@ public class WarpCommandTest {
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private GameModeAddon gameModeAddon;
@Mock
private Warp addon;
// Command under test
private WarpCommand wc;
@ -91,7 +74,6 @@ public class WarpCommandTest {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
@ -99,49 +81,18 @@ public class WarpCommandTest {
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
when(ic.getLabel()).thenReturn("island");
when(ic.getTopLabel()).thenReturn("island");
when(ic.getWorld()).thenReturn(world);
when(ic.getTopLabel()).thenReturn("bsb");
// IWM friendly name
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.inWorld(any(World.class))).thenReturn(true);
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
// Game Mode Addon
@NonNull
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
// World
when(world.toString()).thenReturn("world");
// 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.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true);
when(user.getWorld()).thenReturn(world);
// Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
// Island
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(User.class))).thenReturn(island);
// settings
when(addon.getSettings()).thenReturn(settings);
when(settings.getWarpCommand()).thenReturn("warp");
@ -163,28 +114,12 @@ public class WarpCommandTest {
when(pm.getName(any())).thenReturn("tastybento", "tastybento", "poslovich", "poslovich", "BONNe", "BONNe", "Joe");
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#WarpCommand(world.bentobox.warps.Warp, world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testWarpCommandWarpCompositeCommand() {
public void warpCommandWarpCompositeCommand() {
// Command under test
wc = new WarpCommand(addon, ic);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#WarpCommand(world.bentobox.warps.Warp)}.
*/
@Test
public void testWarpCommandWarp() {
public void warpCommandWarp() {
// Command under test
wc = new WarpCommand(addon);
}
@ -194,7 +129,7 @@ public class WarpCommandTest {
*/
@Test
public void testSetupWarpCompositeCommand() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertEquals("bskyblock.island.warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warp.help.parameters", wc.getParameters());
@ -206,7 +141,7 @@ public class WarpCommandTest {
*/
@Test
public void testSetupWarp() {
testWarpCommandWarp();
warpCommandWarp();
assertEquals(Warp.WELCOME_WARP_SIGNS + ".warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warp.help.parameters", wc.getParameters());
@ -218,7 +153,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
wc.execute(user, "warp", Collections.emptyList());
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
}
@ -228,7 +163,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayer() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tastybento")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
@ -238,7 +173,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerWarp() {
testWarpCommandWarp();
warpCommandWarp();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tastybento")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
@ -248,7 +183,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerMixedCase() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tAsTyBEnTo")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
@ -258,7 +193,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerStartOnly() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tAsTy")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
@ -269,7 +204,7 @@ public class WarpCommandTest {
*/
@Test
public void testExecuteUserStringListOfStringUnknownPlayer() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertFalse(wc.execute(user, "warp", Collections.singletonList("LSPVicky")));
verify(user).sendMessage(eq("warps.error.does-not-exist"));
}
@ -280,7 +215,7 @@ public class WarpCommandTest {
@Test
public void testExecuteUserStringListOfStringNoWarpsYet() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
assertFalse(wc.execute(user, "warp", Collections.singletonList("LSPVicky")));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
@ -291,7 +226,7 @@ public class WarpCommandTest {
*/
@Test
public void testTabCompleteUserStringListOfString() {
testWarpCommandWarpCompositeCommand();
warpCommandWarpCompositeCommand();
List<String> op = wc.tabComplete(user, "warp", Collections.singletonList("tas")).get();
assertEquals("tastybento", op.get(0));
assertEquals("tastybento", op.get(1));

View File

@ -0,0 +1,186 @@
package world.bentobox.warps.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
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.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.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.PlayersManager;
import world.bentobox.warps.Warp;
import world.bentobox.warps.WarpPanelManager;
import world.bentobox.warps.WarpSignsManager;
import world.bentobox.warps.config.Settings;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class WarpsCommandTest {
private static final String WELCOME_LINE = "[Welcome]";
@Mock
private CompositeCommand ic;
@Mock
private User user;
@Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private Warp addon;
// Command under test
private WarpsCommand wc;
@Mock
private Settings settings;
@Mock
private WarpSignsManager wsm;
@Mock
private PlayersManager pm;
@Mock
private WarpPanelManager wpm;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
// World
when(world.toString()).thenReturn("world");
// Player
when(user.getWorld()).thenReturn(world);
// settings
when(addon.getSettings()).thenReturn(settings);
when(settings.getWarpsCommand()).thenReturn("warps");
when(settings.getWelcomeLine()).thenReturn(WELCOME_LINE);
// Warp Signs Manager
when(addon.getWarpSignsManager()).thenReturn(wsm);
@NonNull
Set<UUID> set = new HashSet<>();
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
when(wsm.listWarps(eq(world))).thenReturn(set);
// Warp Panel Manager
when(addon.getWarpPanelManager()).thenReturn(wpm);
}
public void warpCommandWarpsCompositeCommand() {
// Command under test
wc = new WarpsCommand(addon, ic);
}
public void warpCommandWarps() {
// Command under test
wc = new WarpsCommand(addon);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#setup()}.
*/
@Test
public void testSetupWarpCompositeCommand() {
warpCommandWarpsCompositeCommand();
assertEquals("bskyblock.island.warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warps.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#setup()}.
*/
@Test
public void testSetupWarp() {
warpCommandWarps();
assertEquals(Warp.WELCOME_WARP_SIGNS + ".warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warps.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoWarpsYet() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
warpCommandWarpsCompositeCommand();
assertFalse(wc.execute(user, "warps", Collections.emptyList()));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoWarpsYetNoAddon() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
warpCommandWarps();
assertFalse(wc.execute(user, "warps", Collections.emptyList()));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
warpCommandWarpsCompositeCommand();
assertTrue(wc.execute(user, "warps", Collections.emptyList()));
verify(wpm).showWarpPanel(world, user, 0);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoAddon() {
warpCommandWarps();
assertTrue(wc.execute(user, "warps", Collections.emptyList()));
verify(wpm).showWarpPanel(world, user, 0);
}
}