Split command management into initializer and handler

- Create Initializer class that only initializes AuthMe commands
- Move remaining method to CommandHandler
- Deprecate constructors on CommandDescription in favor of the builder
- Various cleanups and comments
This commit is contained in:
ljacqu 2015-11-29 12:51:11 +01:00
parent d6df921841
commit da0c5d1ea2
8 changed files with 170 additions and 229 deletions

View File

@ -418,8 +418,7 @@ public class AuthMe extends JavaPlugin {
* Set up the command handler.
*/
private void setupCommandHandler() {
this.commandHandler = new CommandHandler(false);
this.commandHandler.init();
this.commandHandler = new CommandHandler();
}
/**

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.command;
/**
* Wrapper for the description of a command argument.
*/
public class CommandArgumentDescription {
@ -9,15 +10,15 @@ public class CommandArgumentDescription {
/**
* Argument label (one-word description of the argument).
*/
private String label;
private final String label;
/**
* Argument description.
*/
private String description;
private final String description;
/**
* Defines whether the argument is optional.
*/
private boolean isOptional = false;
private final boolean isOptional;
/**
* Constructor.
@ -51,7 +52,7 @@ public class CommandArgumentDescription {
}
/**
* Check whether the argument is optional.
* Return whether the argument is optional.
*
* @return True if the argument is optional, false otherwise.
*/

View File

@ -15,9 +15,9 @@ import java.util.List;
* Command description - defines which labels ("names") will lead to a command and points to the
* {@link ExecutableCommand} implementation that executes the logic of the command.
*
* CommandDescription is built hierarchically and have one parent or {@code null} for base commands (main commands
* such as /authme) and may have multiple children extending the mapping of the parent: e.g. if /authme has a child
* whose label is "register", then "/authme register" is the command that the child defines.
* CommandDescription instances are built hierarchically and have one parent or {@code null} for base commands
* (main commands such as /authme) and may have multiple children extending the mapping of the parent: e.g. if
* /authme has a child whose label is "register", then "/authme register" is the command that the child defines.
*/
public class CommandDescription {
@ -68,6 +68,7 @@ public class CommandDescription {
* @param detailedDescription Detailed comment description.
* @param parent Parent command.
*/
@Deprecated
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent) {
this(executableCommand, labels, description, detailedDescription, parent, null);
}
@ -82,6 +83,7 @@ public class CommandDescription {
* @param parent Parent command.
* @param arguments Command arguments.
*/
@Deprecated
public CommandDescription(ExecutableCommand executableCommand, List<String> labels, String description, String detailedDescription, CommandDescription parent, List<CommandArgumentDescription> arguments) {
setExecutableCommand(executableCommand);
this.labels = labels;
@ -739,6 +741,9 @@ public class CommandDescription {
return new Builder();
}
/**
* Builder for initializing CommandDescription objects.
*/
public static final class Builder {
private List<String> labels;
private String description;
@ -750,7 +755,8 @@ public class CommandDescription {
private CommandPermissions permissions;
/**
* Build a CommandDescription from the builder.
* Build a CommandDescription from the builder or throw an exception if mandatory
* fields have not been set.
*
* @return The generated CommandDescription object
*/
@ -796,6 +802,16 @@ public class CommandDescription {
return this;
}
/**
* Add an argument that the command description requires. This method can be called multiples times to add
* multiple arguments.
*
* @param label The label of the argument (single word name of the argument)
* @param description The description of the argument
* @param isOptional True if the argument is option, false if it is mandatory
*
* @return The builder
*/
public Builder withArgument(String label, String description, boolean isOptional) {
arguments.add(new CommandArgumentDescription(label, description, isOptional));
return this;

View File

@ -10,78 +10,11 @@ import java.util.Arrays;
import java.util.List;
/**
* The AuthMe command handler, responsible for mapping incoming commands to the correct {@link CommandDescription}
* or to display help messages for unknown invocations.
*/
public class CommandHandler {
/**
* The command manager instance.
*/
private CommandManager commandManager;
/**
* Constructor.
*
* @param init True to immediately initialize.
*/
public CommandHandler(boolean init) {
// Initialize
if (init)
init();
}
/**
* Initialize the command handler.
*
* @return True if succeed, false on failure. True will also be returned if the command handler was already
* initialized.
*/
public boolean init() {
// Make sure the handler isn't initialized already
if (isInit())
return true;
// Initialize the command manager
this.commandManager = new CommandManager(false);
this.commandManager.registerCommands();
// Return the result
return true;
}
/**
* Check whether the command handler is initialized.
*
* @return True if the command handler is initialized.
*/
public boolean isInit() {
return this.commandManager != null;
}
/**
* Destroy the command handler.
*
* @return True if the command handler was destroyed successfully, false otherwise. True will also be returned if
* the command handler wasn't initialized.
*/
public boolean destroy() {
// Make sure the command handler is initialized
if (!isInit())
return true;
// Unset the command manager
this.commandManager = null;
return true;
}
/**
* Get the command manager.
*
* @return Command manager instance.
*/
public CommandManager getCommandManager() {
return this.commandManager;
}
/**
* Process a command.
*
@ -92,6 +25,7 @@ public class CommandHandler {
*
* @return True if the command was executed, false otherwise.
*/
// TODO ljacqu 20151129: Rename onCommand() method to something not suggesting it is auto-invoked by an event
public boolean onCommand(CommandSender sender, org.bukkit.command.Command bukkitCommand, String bukkitCommandLabel, String[] bukkitArgs) {
// Process the arguments
List<String> args = processArguments(bukkitArgs);
@ -102,7 +36,7 @@ public class CommandHandler {
return false;
// Get a suitable command for this reference, and make sure it isn't null
FoundCommandResult result = this.commandManager.findCommand(commandReference);
FoundCommandResult result = findCommand(commandReference);
if (result == null) {
sender.sendMessage(ChatColor.DARK_RED + "Failed to parse " + AuthMe.getPluginName() + " command!");
return false;
@ -207,4 +141,33 @@ public class CommandHandler {
// Return the argument
return arguments;
}
/**
* Find the best suitable command for the specified reference.
*
* @param queryReference The query reference to find a command for.
*
* @return The command found, or null.
*/
public FoundCommandResult findCommand(CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
return null;
// TODO ljacqu 20151129: If base commands are only used in here (or in the future CommandHandler after changes),
// it might make sense to make the CommandInitializer package-private and to return its result into this class
// instead of regularly fetching the list of base commands from the other class.
for (CommandDescription commandDescription : CommandInitializer.getBaseCommands()) {
// Check whether there's a command description available for the
// current command
if (!commandDescription.isSuitableLabel(queryReference))
continue;
// Find the command reference, return the result
return commandDescription.findCommand(queryReference);
}
// No applicable command description found, return false
return null;
}
}

View File

@ -1,7 +1,26 @@
package fr.xephi.authme.command;
import fr.xephi.authme.command.executable.HelpCommand;
import fr.xephi.authme.command.executable.authme.*;
import fr.xephi.authme.command.executable.authme.AccountsCommand;
import fr.xephi.authme.command.executable.authme.AuthMeCommand;
import fr.xephi.authme.command.executable.authme.ChangePasswordCommand;
import fr.xephi.authme.command.executable.authme.FirstSpawnCommand;
import fr.xephi.authme.command.executable.authme.ForceLoginCommand;
import fr.xephi.authme.command.executable.authme.GetEmailCommand;
import fr.xephi.authme.command.executable.authme.GetIpCommand;
import fr.xephi.authme.command.executable.authme.LastLoginCommand;
import fr.xephi.authme.command.executable.authme.PurgeBannedPlayersCommand;
import fr.xephi.authme.command.executable.authme.PurgeCommand;
import fr.xephi.authme.command.executable.authme.PurgeLastPositionCommand;
import fr.xephi.authme.command.executable.authme.RegisterCommand;
import fr.xephi.authme.command.executable.authme.ReloadCommand;
import fr.xephi.authme.command.executable.authme.SetEmailCommand;
import fr.xephi.authme.command.executable.authme.SetFirstSpawnCommand;
import fr.xephi.authme.command.executable.authme.SetSpawnCommand;
import fr.xephi.authme.command.executable.authme.SpawnCommand;
import fr.xephi.authme.command.executable.authme.SwitchAntiBotCommand;
import fr.xephi.authme.command.executable.authme.UnregisterCommand;
import fr.xephi.authme.command.executable.authme.VersionCommand;
import fr.xephi.authme.command.executable.captcha.CaptchaCommand;
import fr.xephi.authme.command.executable.converter.ConverterCommand;
import fr.xephi.authme.command.executable.email.AddEmailCommand;
@ -11,6 +30,7 @@ import fr.xephi.authme.command.executable.login.LoginCommand;
import fr.xephi.authme.command.executable.logout.LogoutCommand;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.UserPermission;
import fr.xephi.authme.util.Wrapper;
import java.util.ArrayList;
import java.util.Arrays;
@ -20,132 +40,125 @@ import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.ALLOW
import static fr.xephi.authme.command.CommandPermissions.DefaultPermission.OP_ONLY;
/**
* Initializes all available AuthMe commands.
*/
public class CommandManager {
public final class CommandInitializer {
/**
* The list of commandDescriptions.
*/
private final List<CommandDescription> commandDescriptions = new ArrayList<>();
private static List<CommandDescription> baseCommands;
/**
* Constructor.
*
* @param registerCommands True to register the commands, false otherwise.
*/
public CommandManager(boolean registerCommands) {
// Register the commands
if (registerCommands)
registerCommands();
private CommandInitializer() {
// Helper class
}
/**
* Register all commands.
*/
public void registerCommands() {
public static List<CommandDescription> getBaseCommands() {
if (baseCommands == null) {
Wrapper.getInstance().getLogger().info("Initializing AuthMe commands");
initializeCommands();
}
return baseCommands;
}
private static void initializeCommands() {
// Create a list of help command labels
final List<String> helpCommandLabels = Arrays.asList("help", "hlp", "h", "sos", "?");
ExecutableCommand helpCommandExecutable = new HelpCommand();
final ExecutableCommand helpCommandExecutable = new HelpCommand();
// Register the base AuthMe Reloaded command
CommandDescription authMeBaseCommand = CommandDescription.builder()
.executableCommand(new AuthMeCommand())
final CommandDescription AUTHME_BASE = CommandDescription.builder()
.labels("authme")
.description("Main command")
.detailedDescription("The main AuthMeReloaded command. The root for all admin commands.")
.parent(null)
.executableCommand(new AuthMeCommand())
.build();
// Register the help command
CommandDescription authMeHelpCommand = CommandDescription.builder()
.executableCommand(helpCommandExecutable)
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels(helpCommandLabels)
.description("View help")
.detailedDescription("View detailed help pages about AuthMeReloaded commands.")
.parent(authMeBaseCommand)
.withArgument("query", "The command or query to view help for.", true)
.executableCommand(helpCommandExecutable)
.build();
// Register the register command
CommandDescription registerCommand = CommandDescription.builder()
.executableCommand(new RegisterCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("register", "reg", "r")
.description("Register a player")
.detailedDescription("Register the specified player with the specified password.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.REGISTER)
.withArgument("player", "Player name", false)
.withArgument("password", "Password", false)
.permissions(OP_ONLY, UserPermission.REGISTER)
.executableCommand(new RegisterCommand())
.build();
// Register the unregister command
CommandDescription unregisterCommand = CommandDescription.builder()
.executableCommand(new UnregisterCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("unregister", "unreg", "unr")
.description("Unregister a player")
.detailedDescription("Unregister the specified player.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.UNREGISTER)
.withArgument("player", "Player name", false)
.permissions(OP_ONLY, UserPermission.UNREGISTER)
.executableCommand(new UnregisterCommand())
.build();
// Register the forcelogin command
CommandDescription forceLoginCommand = CommandDescription.builder()
.executableCommand(new ForceLoginCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("forcelogin", "login")
.description("Enforce login player")
.detailedDescription("Enforce the specified player to login.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.CAN_LOGIN_BE_FORCED)
.withArgument("player", "Online player name", true)
.permissions(OP_ONLY, UserPermission.CAN_LOGIN_BE_FORCED)
.executableCommand(new ForceLoginCommand())
.build();
// Register the changepassword command
CommandDescription changePasswordCommand = CommandDescription.builder()
.executableCommand(new ChangePasswordCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("password", "changepassword", "changepass", "cp")
.description("Change a player's password")
.detailedDescription("Change the password of a player.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, UserPermission.CHANGE_PASSWORD)
.withArgument("player", "Player name", false)
.withArgument("pwd", "New password", false)
.permissions(OP_ONLY, UserPermission.CHANGE_PASSWORD)
.executableCommand(new ChangePasswordCommand())
.build();
// Register the last login command
CommandDescription lastLoginCommand = CommandDescription.builder()
.executableCommand(new LastLoginCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("lastlogin", "ll")
.description("Player's last login")
.detailedDescription("View the date of the specified players last login.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, AdminPermission.LAST_LOGIN)
.withArgument("player", "Player name", true)
.permissions(OP_ONLY, AdminPermission.LAST_LOGIN)
.executableCommand(new LastLoginCommand())
.build();
// Register the accounts command
CommandDescription accountsCommand = CommandDescription.builder()
.executableCommand(new AccountsCommand())
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("accounts", "account")
.description("Display player accounts")
.detailedDescription("Display all accounts of a player by his player name or IP.")
.parent(authMeBaseCommand)
.permissions(OP_ONLY, AdminPermission.ACCOUNTS)
.withArgument("player", "Player name or IP", true)
.permissions(OP_ONLY, AdminPermission.ACCOUNTS)
.executableCommand(new AccountsCommand())
.build();
// Register the getemail command
CommandDescription getEmailCommand = new CommandDescription(new GetEmailCommand(), new ArrayList<String>() {
{
add("getemail");
add("getmail");
add("email");
add("mail");
}
}, "Display player's email", "Display the email address of the specified player if set.", authMeBaseCommand);
getEmailCommand.setCommandPermissions(AdminPermission.GET_EMAIL, OP_ONLY);
getEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
CommandDescription.builder()
.parent(AUTHME_BASE)
.labels("getemail", "getmail", "email", "mail")
.description("Display player's email")
.detailedDescription("Display the email address of the specified player if set.")
.withArgument("player", "Player name", true)
.permissions(OP_ONLY, AdminPermission.GET_EMAIL)
.executableCommand(new GetEmailCommand())
.build();
// Register the setemail command
CommandDescription setEmailCommand = new CommandDescription(new SetEmailCommand(), new ArrayList<String>() {
@ -156,7 +169,7 @@ public class CommandManager {
add("setemail");
add("setmail");
}
}, "Change player's email", "Change the email address of the specified player.", authMeBaseCommand);
}, "Change player's email", "Change the email address of the specified player.", AUTHME_BASE);
setEmailCommand.setCommandPermissions(AdminPermission.CHANGE_EMAIL, OP_ONLY);
setEmailCommand.addArgument(new CommandArgumentDescription("player", "Player name", false));
setEmailCommand.addArgument(new CommandArgumentDescription("email", "Player email", false));
@ -168,7 +181,7 @@ public class CommandManager {
add("getip");
add("ip");
}
}, "Get player's IP", "Get the IP address of the specified online player.", authMeBaseCommand);
}, "Get player's IP", "Get the IP address of the specified online player.", AUTHME_BASE);
getIpCommand.setCommandPermissions(AdminPermission.GET_IP, OP_ONLY);
getIpCommand.addArgument(new CommandArgumentDescription("player", "Online player name", true));
@ -179,7 +192,7 @@ public class CommandManager {
add("spawn");
add("home");
}
}, "Teleport to spawn", "Teleport to the spawn.", authMeBaseCommand);
}, "Teleport to spawn", "Teleport to the spawn.", AUTHME_BASE);
spawnCommand.setCommandPermissions(AdminPermission.SPAWN, OP_ONLY);
// Register the setspawn command
@ -189,7 +202,7 @@ public class CommandManager {
add("setspawn");
add("chgspawn");
}
}, "Change the spawn", "Change the player's spawn to your current position.", authMeBaseCommand);
}, "Change the spawn", "Change the player's spawn to your current position.", AUTHME_BASE);
setSpawnCommand.setCommandPermissions(AdminPermission.SET_SPAWN, OP_ONLY);
// Register the firstspawn command
@ -199,7 +212,7 @@ public class CommandManager {
add("firstspawn");
add("firsthome");
}
}, "Teleport to first spawn", "Teleport to the first spawn.", authMeBaseCommand);
}, "Teleport to first spawn", "Teleport to the first spawn.", AUTHME_BASE);
firstSpawnCommand.setCommandPermissions(AdminPermission.FIRST_SPAWN, OP_ONLY);
// Register the setfirstspawn command
@ -209,7 +222,7 @@ public class CommandManager {
add("setfirstspawn");
add("chgfirstspawn");
}
}, "Change the first spawn", "Change the first player's spawn to your current position.", authMeBaseCommand);
}, "Change the first spawn", "Change the first player's spawn to your current position.", AUTHME_BASE);
setFirstSpawnCommand.setCommandPermissions(AdminPermission.SET_FIRST_SPAWN, OP_ONLY);
// Register the purge command
@ -219,7 +232,7 @@ public class CommandManager {
add("purge");
add("delete");
}
}, "Purge old data", "Purge old AuthMeReloaded data longer than the specified amount of days ago.", authMeBaseCommand);
}, "Purge old data", "Purge old AuthMeReloaded data longer than the specified amount of days ago.", AUTHME_BASE);
purgeCommand.setCommandPermissions(AdminPermission.PURGE, OP_ONLY);
purgeCommand.addArgument(new CommandArgumentDescription("days", "Number of days", false));
@ -234,7 +247,7 @@ public class CommandManager {
add("resetlastposition");
add("resetlastpos");
}
}, "Purge player's last position", "Purge the last know position of the specified player.", authMeBaseCommand);
}, "Purge player's last position", "Purge the last know position of the specified player.", AUTHME_BASE);
purgeLastPositionCommand.setCommandPermissions(AdminPermission.PURGE_LAST_POSITION, OP_ONLY);
purgeLastPositionCommand.addArgument(new CommandArgumentDescription("player", "Player name", true));
@ -247,7 +260,7 @@ public class CommandManager {
add("deletebannedplayers");
add("deletebannedplayer");
}
}, "Purge banned palyers data", "Purge all AuthMeReloaded data for banned players.", authMeBaseCommand);
}, "Purge banned palyers data", "Purge all AuthMeReloaded data for banned players.", AUTHME_BASE);
purgeBannedPlayersCommand.setCommandPermissions(AdminPermission.PURGE_BANNED_PLAYERS, OP_ONLY);
// Register the switchantibot command
@ -258,7 +271,7 @@ public class CommandManager {
add("toggleantibot");
add("antibot");
}
}, "Switch AntiBot mode", "Switch or toggle the AntiBot mode to the specified state.", authMeBaseCommand);
}, "Switch AntiBot mode", "Switch or toggle the AntiBot mode to the specified state.", AUTHME_BASE);
switchAntiBotCommand.setCommandPermissions(AdminPermission.SWITCH_ANTIBOT, OP_ONLY);
switchAntiBotCommand.addArgument(new CommandArgumentDescription("mode", "ON / OFF", true));
@ -282,7 +295,7 @@ public class CommandManager {
add("reload");
add("rld");
}
}, "Reload plugin", "Reload the AuthMeReloaded plugin.", authMeBaseCommand);
}, "Reload plugin", "Reload the AuthMeReloaded plugin.", AUTHME_BASE);
reloadCommand.setCommandPermissions(AdminPermission.RELOAD, OP_ONLY);
// Register the version command
@ -292,7 +305,7 @@ public class CommandManager {
.description("Version info")
.detailedDescription("Show detailed information about the installed AuthMeReloaded version, and shows the "
+ "developers, contributors, license and other information.")
.parent(authMeBaseCommand)
.parent(AUTHME_BASE)
.build();
// Register the base login command
@ -461,59 +474,15 @@ public class CommandManager {
converterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true));
// Add the base commands to the commands array
this.commandDescriptions.add(authMeBaseCommand);
this.commandDescriptions.add(loginBaseCommand);
this.commandDescriptions.add(logoutBaseCommand);
this.commandDescriptions.add(registerBaseCommand);
this.commandDescriptions.add(unregisterBaseCommand);
this.commandDescriptions.add(changePasswordBaseCommand);
this.commandDescriptions.add(emailBaseCommand);
this.commandDescriptions.add(captchaBaseCommand);
this.commandDescriptions.add(converterBaseCommand);
}
/**
* Get the list of command descriptions
*
* @return List of command descriptions.
*/
public List<CommandDescription> getCommandDescriptions() {
return this.commandDescriptions;
}
/**
* Get the number of command description count.
*
* @return Command description count.
*/
public int getCommandDescriptionCount() {
return this.getCommandDescriptions().size();
}
/**
* Find the best suitable command for the specified reference.
*
* @param queryReference The query reference to find a command for.
*
* @return The command found, or null.
*/
public FoundCommandResult findCommand(CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
return null;
// Get the base command description
for (CommandDescription commandDescription : this.commandDescriptions) {
// Check whether there's a command description available for the
// current command
if (!commandDescription.isSuitableLabel(queryReference))
continue;
// Find the command reference, return the result
return commandDescription.findCommand(queryReference);
}
// No applicable command description found, return false
return null;
baseCommands = Arrays.asList(
AUTHME_BASE,
loginBaseCommand,
logoutBaseCommand,
registerBaseCommand,
unregisterBaseCommand,
changePasswordBaseCommand,
emailBaseCommand,
captchaBaseCommand,
converterBaseCommand);
}
}

View File

@ -37,9 +37,9 @@ public class HelpProvider {
*/
public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) {
// Find the command for this help query, one with and one without a prefixed base command
FoundCommandResult result = AuthMe.getInstance().getCommandHandler().getCommandManager().findCommand(new CommandParts(helpQuery.getList()));
FoundCommandResult result = AuthMe.getInstance().getCommandHandler().findCommand(new CommandParts(helpQuery.getList()));
CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList());
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().getCommandManager().findCommand(commandReferenceOther);
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther);
if (resultOther != null) {
if (result == null)
result = resultOther;

View File

@ -260,6 +260,7 @@ public class PermissionsManager {
*
* @param event Event instance.
*/
// TODO ljacqu 20151129: Misleading name since onPluginEnable is a typical event-based method name
public void onPluginEnable(PluginEnableEvent event) {
// Get the plugin and it's name
Plugin plugin = event.getPlugin();

View File

@ -18,7 +18,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Test for {@link CommandManager}, especially to guarantee the integrity of the defined commands.
* Test for {@link CommandInitializer} to guarantee the integrity of the defined commands.
*/
public class CommandManagerTest {
@ -28,24 +28,20 @@ public class CommandManagerTest {
*/
private static int MAX_ALLOWED_DEPTH = 1;
private static CommandManager manager;
private static List<CommandDescription> commands;
@BeforeClass
public static void initializeCommandManager() {
manager = new CommandManager(true);
commands = CommandInitializer.getBaseCommands();
}
@Test
public void shouldInitializeCommands() {
// given/when
int commandCount = manager.getCommandDescriptionCount();
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
// given/when/then
// It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes"
assertThat(commandCount, equalTo(9));
assertThat(commands.size(), equalTo(9));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
@ -62,7 +58,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), descriptionTester);
walkThroughCommands(commands, descriptionTester);
}
/** Ensure that all children of a command stored the parent. */
@ -84,7 +80,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), connectionTester);
walkThroughCommands(commands, connectionTester);
}
@Test
@ -105,7 +101,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), uniqueMappingTester);
walkThroughCommands(commands, uniqueMappingTester);
}
/**
@ -132,7 +128,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), descriptionTester);
walkThroughCommands(commands, descriptionTester);
}
/**
@ -143,7 +139,6 @@ public class CommandManagerTest {
public void shouldNotHaveMultipleInstancesOfSameExecutableCommandSubType() {
// given
final Map<Class<? extends ExecutableCommand>, ExecutableCommand> implementations = new HashMap<>();
CommandManager manager = new CommandManager(true);
BiConsumer descriptionTester = new BiConsumer() {
@Override
public void accept(CommandDescription command, int depth) {
@ -160,10 +155,7 @@ public class CommandManagerTest {
}
};
// when
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
// when/then
walkThroughCommands(commands, descriptionTester);
}
@ -186,7 +178,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), argumentOrderTester);
walkThroughCommands(commands, argumentOrderTester);
}
/**
@ -209,7 +201,7 @@ public class CommandManagerTest {
};
// when/then
walkThroughCommands(manager.getCommandDescriptions(), noArgumentForParentChecker);
walkThroughCommands(commands, noArgumentForParentChecker);
}