mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-09-30 15:47:33 +02:00
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:
parent
aad5239ba3
commit
1e50b9e4cf
@ -33,6 +33,7 @@ general:
|
|||||||
offline-player: "&cThat player is offline or doesn't exist."
|
offline-player: "&cThat player is offline or doesn't exist."
|
||||||
unknown-player: "&cUnknown player!"
|
unknown-player: "&cUnknown player!"
|
||||||
general: "&cThat command is not ready yet - contact admin"
|
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!"
|
warp-not-safe: "&cThat warp is not safe right now!"
|
||||||
wrong-world: "&cYou are not in the right world to do that!"
|
wrong-world: "&cYou are not in the right world to do that!"
|
||||||
tips:
|
tips:
|
||||||
|
@ -429,8 +429,9 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
|
|||||||
* Show help
|
* Show help
|
||||||
* @param command
|
* @param command
|
||||||
* @param user - the User
|
* @param user - the User
|
||||||
|
* @return result of help command or false if no help defined
|
||||||
*/
|
*/
|
||||||
protected void showHelp(CompositeCommand command, User user) {
|
protected boolean showHelp(CompositeCommand command, User user) {
|
||||||
command.getSubCommand("help").ifPresent(helpCommand -> helpCommand.execute(user, new ArrayList<>()));
|
return command.getSubCommand("help").map(helpCommand -> helpCommand.execute(user, new ArrayList<>())).orElse(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,12 @@ public class AdminCommand extends CompositeCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(User user, List<String> args) {
|
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)
|
// By default run the attached help command, if it exists (it should)
|
||||||
showHelp(this, user);
|
return showHelp(this, user);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,15 @@ import java.util.List;
|
|||||||
import us.tastybento.bskyblock.Constants;
|
import us.tastybento.bskyblock.Constants;
|
||||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||||
import us.tastybento.bskyblock.api.user.User;
|
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;
|
import us.tastybento.bskyblock.commands.island.teams.IslandTeamCommand;
|
||||||
|
|
||||||
public class IslandCommand extends CompositeCommand {
|
public class IslandCommand extends CompositeCommand {
|
||||||
@ -46,14 +54,18 @@ public class IslandCommand extends CompositeCommand {
|
|||||||
if (user == null) {
|
if (user == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// If this player does not have an island, create one
|
if (args.isEmpty()) {
|
||||||
if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) {
|
// If this player does not have an island, create one
|
||||||
getSubCommand("create").ifPresent(createCmd -> createCmd.execute(user, new ArrayList<>()));
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package us.tastybento.bskyblock.commands.island;
|
package us.tastybento.bskyblock.commands.island;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import us.tastybento.bskyblock.Constants;
|
import us.tastybento.bskyblock.Constants;
|
||||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||||
import us.tastybento.bskyblock.api.user.User;
|
import us.tastybento.bskyblock.api.user.User;
|
||||||
import us.tastybento.bskyblock.commands.IslandCommand;
|
import us.tastybento.bskyblock.commands.IslandCommand;
|
||||||
import us.tastybento.bskyblock.panels.LanguagePanel;
|
import us.tastybento.bskyblock.panels.LanguagePanel;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Poslovitch
|
* @author Poslovitch
|
||||||
*/
|
*/
|
||||||
|
@ -12,7 +12,6 @@ import java.util.Map.Entry;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
|
@ -2,7 +2,6 @@ package us.tastybento.bskyblock.listeners;
|
|||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
*/
|
*/
|
||||||
package us.tastybento.bskyblock.listeners;
|
package us.tastybento.bskyblock.listeners;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
@ -16,9 +19,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
|||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
import us.tastybento.bskyblock.api.user.User;
|
import us.tastybento.bskyblock.api.user.User;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables changing of obsidian back into lava
|
* Enables changing of obsidian back into lava
|
||||||
* @author tastybento
|
* @author tastybento
|
||||||
|
@ -8,7 +8,7 @@ import org.bukkit.command.CommandMap;
|
|||||||
|
|
||||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||||
|
|
||||||
public final class CommandsManager {
|
public class CommandsManager {
|
||||||
|
|
||||||
private HashMap<String, CompositeCommand> commands = new HashMap<>();
|
private HashMap<String, CompositeCommand> commands = new HashMap<>();
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package us.tastybento.bskyblock.panels;
|
package us.tastybento.bskyblock.panels;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
|
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
|
||||||
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
||||||
import us.tastybento.bskyblock.api.user.User;
|
import us.tastybento.bskyblock.api.user.User;
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Poslovitch
|
* @author Poslovitch
|
||||||
*/
|
*/
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package us.tastybento.bskyblock.api.user;
|
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.Matchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
@ -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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -15,7 +16,6 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -61,19 +61,19 @@ public class IslandCommandTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
CommandsManager cm = new CommandsManager();
|
|
||||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIslandCommand() {
|
public void testIslandCommand() {
|
||||||
|
CommandsManager cm = mock(CommandsManager.class);
|
||||||
|
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||||
assertNotNull(new IslandCommand());
|
assertNotNull(new IslandCommand());
|
||||||
|
// Verify the command has been registered
|
||||||
|
Mockito.verify(cm).registerCommand(Mockito.any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetup() {
|
public void testSetup() {
|
||||||
|
CommandsManager cm = mock(CommandsManager.class);
|
||||||
|
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||||
IslandCommand ic = new IslandCommand();
|
IslandCommand ic = new IslandCommand();
|
||||||
assertEquals("commands.island.help.description", ic.getDescription());
|
assertEquals("commands.island.help.description", ic.getDescription());
|
||||||
assertTrue(ic.isOnlyPlayer());
|
assertTrue(ic.isOnlyPlayer());
|
||||||
@ -84,6 +84,8 @@ public class IslandCommandTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExecuteUserListOfString() {
|
public void testExecuteUserListOfString() {
|
||||||
|
CommandsManager cm = mock(CommandsManager.class);
|
||||||
|
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||||
// Setup
|
// Setup
|
||||||
IslandCommand ic = new IslandCommand();
|
IslandCommand ic = new IslandCommand();
|
||||||
assertFalse(ic.execute(null, null));
|
assertFalse(ic.execute(null, null));
|
||||||
@ -104,6 +106,10 @@ public class IslandCommandTest {
|
|||||||
// No island yet
|
// No island yet
|
||||||
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
|
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
|
||||||
assertTrue(ic.execute(user, new ArrayList<>()));
|
assertTrue(ic.execute(user, new ArrayList<>()));
|
||||||
|
|
||||||
|
// No such command
|
||||||
|
String[] args2 = {"random", "junk"};
|
||||||
|
assertFalse(ic.execute(user, Arrays.asList(args2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package us.tastybento.bskyblock.managers.island;
|
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.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user