Skip to content

Commit

Permalink
Fix test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 10, 2023
1 parent ae99db4 commit 07e4b91
Showing 1 changed file with 117 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -24,7 +23,6 @@
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;
Expand All @@ -34,124 +32,125 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class DefaultPlayerCommandTest {

@Mock
GameModeAddon addon;
private PlayerCommand dpc;
@Mock
private WorldSettings ws;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private @Nullable Island island;


class PlayerCommand extends DefaultPlayerCommand {

protected PlayerCommand(GameModeAddon addon) {
super(addon);
}

}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);

// Addon

// User
when(user.getUniqueId()).thenReturn(UUID.randomUUID());
// IM
when(plugin.getIslandsManager()).thenReturn(im);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(World.class), any(UUID.class))).thenReturn(island);

// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);

// World Settings
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);
dpc.setWorld(mock(World.class));

}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#DefaultPlayerCommand(world.bentobox.bentobox.api.addons.GameModeAddon)}.
*/
@Test
public void testDefaultPlayerCommand() {
assertNotNull(dpc);
}

/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("commands.island.help.description", dpc.getDescription());
assertTrue(dpc.isOnlyPlayer());
assertEquals("island", dpc.getPermission());
// 20 = 19 subcommands + help command
assertEquals(20, dpc.getSubCommands().size()); // Update when commands are added or removed
}

/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownCommand() {
assertFalse(dpc.execute(user, "label", List.of("unknown")));
verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "island");
}

/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNullUser() {
assertFalse(dpc.execute(null, "label", List.of()));
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringEmptyArgsHasIsland() {
assertFalse(dpc.execute(user, "label", List.of()));
verify(user).sendMessage("general.errors.use-in-game");
}

/**
@PrepareForTest({ Bukkit.class, BentoBox.class })
public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest {

@Mock
GameModeAddon addon;
private PlayerCommand dpc;
@Mock
private WorldSettings ws;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private @Nullable Island island;

class PlayerCommand extends DefaultPlayerCommand {

protected PlayerCommand(GameModeAddon addon) {
super(addon);
}

}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
// RanksManager
RanksManager rm = new RanksManager();
when(plugin.getRanksManager()).thenReturn(rm);

// Addon

// User
when(user.getUniqueId()).thenReturn(UUID.randomUUID());
// IM
when(plugin.getIslandsManager()).thenReturn(im);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(World.class), any(UUID.class))).thenReturn(island);

// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);

// World Settings
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);
dpc.setWorld(mock(World.class));

}

/**
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#DefaultPlayerCommand(world.bentobox.bentobox.api.addons.GameModeAddon)}.
*/
@Test
public void testDefaultPlayerCommand() {
assertNotNull(dpc);
}

/**
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("commands.island.help.description", dpc.getDescription());
assertTrue(dpc.isOnlyPlayer());
assertEquals("island", dpc.getPermission());
// 20 = 19 subcommands + help command
assertEquals(20, dpc.getSubCommands().size()); // Update when commands are added or removed
}

/**
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownCommand() {
assertFalse(dpc.execute(user, "label", List.of("unknown")));
verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "island");
}

/**
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNullUser() {
assertFalse(dpc.execute(null, "label", List.of()));
}

/**
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringEmptyArgsHasIsland() {
assertFalse(dpc.execute(user, "label", List.of()));
verify(user).sendMessage("general.errors.use-in-game");
}

/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
Expand All @@ -161,7 +160,7 @@ public void testExecuteUserStringListOfStringEmptyArgsHasNoIsland() {
verify(user).sendMessage("general.errors.use-in-game");
}

/**
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
Expand All @@ -172,7 +171,7 @@ public void testExecuteUserStringListOfStringEmptyArgsHasIslandUnknownCommand()
verify(user).performCommand("label goxxx");
}

/**
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
Expand All @@ -185,7 +184,7 @@ public void testExecuteUserStringListOfStringEmptyArgsHasNoIslandUnknownCommand(
verify(user).performCommand("label createxxx");
}

/**
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
Expand All @@ -196,7 +195,7 @@ public void testExecuteUserStringListOfStringEmptyArgsHasIslandUnknownCommandSla
verify(user).performCommand("goxxx");
}

/**
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.DefaultPlayerCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
Expand Down

0 comments on commit 07e4b91

Please sign in to comment.