Added AdminCommand test.

Added "no such command" error message when trying a command that does
not exist.

Removed unnecessary imports.
This commit is contained in:
tastybento 2018-04-28 14:36:17 +09:00
parent aad5239ba3
commit 1e50b9e4cf
14 changed files with 174 additions and 35 deletions

View File

@ -33,6 +33,7 @@ general:
offline-player: "&cThat player is offline or doesn't exist."
unknown-player: "&cUnknown player!"
general: "&cThat command is not ready yet - contact admin"
unknown-command: "&cUnknown command. Do &b/[label] help &cfor help."
warp-not-safe: "&cThat warp is not safe right now!"
wrong-world: "&cYou are not in the right world to do that!"
tips:

View File

@ -429,8 +429,9 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* Show help
* @param command
* @param user - the User
* @return result of help command or false if no help defined
*/
protected void showHelp(CompositeCommand command, User user) {
command.getSubCommand("help").ifPresent(helpCommand -> helpCommand.execute(user, new ArrayList<>()));
protected boolean showHelp(CompositeCommand command, User user) {
return command.getSubCommand("help").map(helpCommand -> helpCommand.execute(user, new ArrayList<>())).orElse(false);
}
}

View File

@ -30,9 +30,12 @@ public class AdminCommand extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
if (!args.isEmpty()) {
user.sendMessage("general.errors.unknown-command", "[label]", Constants.ADMINCOMMAND);
return false;
}
// By default run the attached help command, if it exists (it should)
showHelp(this, user);
return false;
return showHelp(this, user);
}
}

View File

@ -6,7 +6,15 @@ import java.util.List;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.island.*;
import us.tastybento.bskyblock.commands.island.IslandAboutCommand;
import us.tastybento.bskyblock.commands.island.IslandCreateCommand;
import us.tastybento.bskyblock.commands.island.IslandGoCommand;
import us.tastybento.bskyblock.commands.island.IslandLanguageCommand;
import us.tastybento.bskyblock.commands.island.IslandResetCommand;
import us.tastybento.bskyblock.commands.island.IslandResetnameCommand;
import us.tastybento.bskyblock.commands.island.IslandSethomeCommand;
import us.tastybento.bskyblock.commands.island.IslandSetnameCommand;
import us.tastybento.bskyblock.commands.island.IslandSettingsCommand;
import us.tastybento.bskyblock.commands.island.teams.IslandTeamCommand;
public class IslandCommand extends CompositeCommand {
@ -46,14 +54,18 @@ public class IslandCommand extends CompositeCommand {
if (user == null) {
return false;
}
// If this player does not have an island, create one
if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) {
getSubCommand("create").ifPresent(createCmd -> createCmd.execute(user, new ArrayList<>()));
if (args.isEmpty()) {
// If this player does not have an island, create one
if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) {
return getSubCommand("create").map(createCmd -> createCmd.execute(user, new ArrayList<>())).orElse(false);
} else {
// Otherwise, currently, just go home
return getSubCommand("go").map(goCmd -> goCmd.execute(user, new ArrayList<>())).orElse(false);
}
} else {
user.sendMessage("general.errors.unknown-command", "[label]", Constants.ISLANDCOMMAND);
return false;
}
// Otherwise, currently, just go home
else getSubCommand("go").ifPresent(goCmd -> goCmd.execute(user, new ArrayList<>()));
return true;
}

View File

@ -1,13 +1,13 @@
package us.tastybento.bskyblock.commands.island;
import java.util.List;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.panels.LanguagePanel;
import java.util.List;
/**
* @author Poslovitch
*/

View File

@ -12,7 +12,6 @@ import java.util.Map.Entry;
import java.util.Scanner;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import us.tastybento.bskyblock.BSkyBlock;

View File

@ -2,7 +2,6 @@ package us.tastybento.bskyblock.listeners;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

View File

@ -3,6 +3,9 @@
*/
package us.tastybento.bskyblock.listeners;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
@ -16,9 +19,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
import java.util.ArrayList;
import java.util.List;
/**
* Enables changing of obsidian back into lava
* @author tastybento

View File

@ -8,7 +8,7 @@ import org.bukkit.command.CommandMap;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
public final class CommandsManager {
public class CommandsManager {
private HashMap<String, CompositeCommand> commands = new HashMap<>();

View File

@ -1,12 +1,12 @@
package us.tastybento.bskyblock.panels;
import java.util.Locale;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
import us.tastybento.bskyblock.api.user.User;
import java.util.Locale;
/**
* @author Poslovitch
*/

View File

@ -1,6 +1,10 @@
package us.tastybento.bskyblock.api.user;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@ -0,0 +1,109 @@
/**
*
*/
package us.tastybento.bskyblock.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.plugin.PluginManager;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
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.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.managers.CommandsManager;
/**
* @author ben
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( { BSkyBlock.class })
public class AdminCommandTest {
@Mock
static BSkyBlock plugin;
private static World world;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Server server = mock(Server.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
Bukkit.setServer(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.AdminCommand#AdminCommand()}.
*/
@Test
public void testAdminCommand() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
assertNotNull(new AdminCommand());
// Verify the command has been registered
Mockito.verify(cm).registerCommand(Mockito.any());
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.AdminCommand#setup()}.
*/
@Test
public void testSetup() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
AdminCommand ac = new AdminCommand();
ac.setup();
assertEquals(Constants.PERMPREFIX + "admin.*", ac.getPermission());
assertFalse(ac.isOnlyPlayer());
assertEquals("commands.admin.help.parameters", ac.getParameters());
assertEquals("commands.admin.help.description", ac.getDescription());
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.AdminCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUserListOfString() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
AdminCommand ac = new AdminCommand();
assertTrue(ac.execute(mock(User.class), new ArrayList<>()));
// No such command
String[] args2 = {"random", "junk"};
assertFalse(ac.execute(mock(User.class), Arrays.asList(args2)));
}
}

View File

@ -8,6 +8,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import java.util.logging.Logger;
@ -15,7 +16,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -40,7 +40,7 @@ public class IslandCommandTest {
@Mock
static BSkyBlock plugin;
private static World world;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Server server = mock(Server.class);
@ -58,22 +58,22 @@ public class IslandCommandTest {
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
}
@Before
public void setUp() {
CommandsManager cm = new CommandsManager();
when(plugin.getCommandsManager()).thenReturn(cm);
}
@Test
public void testIslandCommand() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
assertNotNull(new IslandCommand());
// Verify the command has been registered
Mockito.verify(cm).registerCommand(Mockito.any());
}
@Test
public void testSetup() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
IslandCommand ic = new IslandCommand();
assertEquals("commands.island.help.description", ic.getDescription());
assertTrue(ic.isOnlyPlayer());
@ -84,6 +84,8 @@ public class IslandCommandTest {
@Test
public void testExecuteUserListOfString() {
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Setup
IslandCommand ic = new IslandCommand();
assertFalse(ic.execute(null, null));
@ -96,14 +98,18 @@ public class IslandCommandTest {
when(plugin.getPlayers()).thenReturn(pm);
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
// User has an island - so go there!
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
assertTrue(ic.execute(user, new ArrayList<>()));
// No island yet
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
assertTrue(ic.execute(user, new ArrayList<>()));
// No such command
String[] args2 = {"random", "junk"};
assertFalse(ic.execute(user, Arrays.asList(args2)));
}
}

View File

@ -1,6 +1,11 @@
package us.tastybento.bskyblock.managers.island;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;