Added test classes for acid command and user command

Version up
This commit is contained in:
tastybento 2019-02-23 19:24:52 -08:00
parent be8ca1433e
commit b2340ef9d3
5 changed files with 287 additions and 5 deletions

View File

@ -6,7 +6,7 @@
<groupId>world.bentobox</groupId>
<artifactId>acidisland</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0</version>
<name>AcidIsland</name>
<description>AcidIsland is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like SkyBlock or AcidIsland.</description>
@ -91,7 +91,7 @@
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>bentobox</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -27,6 +27,7 @@ import world.bentobox.bentobox.api.commands.admin.team.AdminTeamAddCommand;
import world.bentobox.bentobox.api.commands.admin.team.AdminTeamDisbandCommand;
import world.bentobox.bentobox.api.commands.admin.team.AdminTeamKickCommand;
import world.bentobox.bentobox.api.commands.admin.team.AdminTeamSetownerCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
public class AcidCommand extends CompositeCommand {
@ -37,10 +38,12 @@ public class AcidCommand extends CompositeCommand {
@Override
public void setup() {
setPermission("acidisland.admin.*");
setPermission("admin");
setOnlyPlayer(false);
setParametersHelp("commands.admin.help.parameters");
setDescription("commands.admin.help.description");
// Commands
new AdminVersionCommand(this);
new AdminTeleportCommand(this, "tp");
new AdminTeleportCommand(this, "tpnether");
@ -85,7 +88,7 @@ public class AcidCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
if (!args.isEmpty()) {
user.sendMessage("general.errors.unknown-command", "[label]", "acid");
user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, label);
return false;
}
// By default run the attached help command, if it exists (it should)

View File

@ -19,6 +19,7 @@ import world.bentobox.bentobox.api.commands.island.IslandSettingsCommand;
import world.bentobox.bentobox.api.commands.island.IslandSpawnCommand;
import world.bentobox.bentobox.api.commands.island.IslandUnbanCommand;
import world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
public class AiCommand extends CompositeCommand {
@ -71,7 +72,7 @@ public class AiCommand extends CompositeCommand {
// No islands currently
return getSubCommand("create").map(createCmd -> createCmd.execute(user, label, new ArrayList<>())).orElse(false);
}
user.sendMessage("general.errors.unknown-command", "[label]", getLabel());
user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, getLabel());
return false;
}

View File

@ -0,0 +1,121 @@
/**
*
*/
package world.bentobox.acidisland.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Addon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AcidCommandTest {
private User user;
/**
* @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);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Locales
// Return the reference (USE THIS IN THE FUTURE)
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgumentAt(0, String.class));
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AcidCommand#AcidCommand(world.bentobox.bentobox.api.addons.Addon, java.lang.String)}.
*/
@Test
public void testAcidCommand() {
Addon addon = mock(Addon.class);
AcidCommand cmd = new AcidCommand(addon, "acid");
assertEquals("acid", cmd.getLabel());
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AcidCommand#setup()}.
*/
@Test
public void testSetup() {
Addon addon = mock(Addon.class);
when(addon.getPermissionPrefix()).thenReturn("acidisland.");
AcidCommand cmd = new AcidCommand(addon, "acid");
assertEquals("acidisland.admin", cmd.getPermission());
assertFalse(cmd.isOnlyPlayer());
assertEquals("commands.admin.help.parameters", cmd.getParameters());
assertEquals("commands.admin.help.description", cmd.getDescription());
// Number of commands = sub commands + help
assertEquals("Number of sub commands registered", 26, cmd.getSubCommands().values().size());
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AcidCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownCommand() {
Addon addon = mock(Addon.class);
AcidCommand cmd = new AcidCommand(addon, "acid");
assertFalse(cmd.execute(user, "acid", Collections.singletonList("unknown")));
Mockito.verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "acid");
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AcidCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoCommand() {
Addon addon = mock(Addon.class);
AcidCommand cmd = new AcidCommand(addon, "acid");
assertTrue(cmd.execute(user, "acid", Collections.emptyList()));
// Show help
Mockito.verify(user).sendMessage("commands.help.header", TextVariables.LABEL, "commands.help.console");
}
}

View File

@ -0,0 +1,157 @@
/**
*
*/
package world.bentobox.acidisland.commands;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Addon;
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;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class AiCommandTest {
private User user;
private IslandsManager im;
private Island island;
/**
* @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);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
im = mock(IslandsManager.class);
island = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Locales
// Return the reference (USE THIS IN THE FUTURE)
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgumentAt(0, String.class));
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#AiCommand(world.bentobox.bentobox.api.addons.Addon, java.lang.String)}.
*/
@Test
public void testAiCommand() {
Addon addon = mock(Addon.class);
AiCommand cmd = new AiCommand(addon, "ai");
assertEquals("ai", cmd.getLabel());
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#setup()}.
*/
@Test
public void testSetup() {
Addon addon = mock(Addon.class);
when(addon.getPermissionPrefix()).thenReturn("acidisland.");
AiCommand cmd = new AiCommand(addon, "ai");
assertEquals("acidisland.island", cmd.getPermission());
assertTrue(cmd.isOnlyPlayer());
assertEquals("commands.ai.parameters", cmd.getParameters());
assertEquals("commands.island.help.description", cmd.getDescription());
// Number of commands = sub commands + help
assertEquals("Number of sub commands registered", 16, cmd.getSubCommands().values().size());
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNullUsers() {
Addon addon = mock(Addon.class);
AiCommand cmd = new AiCommand(addon, "ai");
assertFalse(cmd.execute(null, "ai", Collections.emptyList()));
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownCommand() {
Addon addon = mock(Addon.class);
AiCommand cmd = new AiCommand(addon, "ai");
assertFalse(cmd.execute(user, "ai", Collections.singletonList("unknown")));
Mockito.verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "ai");
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
Addon addon = mock(Addon.class);
AiCommand cmd = new AiCommand(addon, "ai");
assertTrue(cmd.execute(user, "ai", Collections.emptyList()));
}
/**
* Test method for {@link world.bentobox.acidisland.commands.AiCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIsland() {
island = null;
Addon addon = mock(Addon.class);
AiCommand cmd = new AiCommand(addon, "ai");
assertTrue(cmd.execute(user, "ai", Collections.emptyList()));
}
}