Fixed crash executing ""

This commit is contained in:
Nathan Adams 2017-06-28 12:22:35 +02:00
parent e9a930c3b5
commit fbf3546f18
2 changed files with 20 additions and 3 deletions

View file

@ -39,9 +39,13 @@ public class CommandDispatcher<S> {
root.addChild(command.build()); root.addChild(command.build());
} }
public int execute(String command, S source) throws CommandException { public int execute(String input, S source) throws CommandException {
CommandContext<S> context = parse(command, source).build(); CommandContext<S> context = parse(input, source).build();
return context.getCommand().run(context); Command<S> command = context.getCommand();
if (command == null) {
throw ERROR_UNKNOWN_COMMAND.create();
}
return command.run(context);
} }
public CommandContextBuilder<S> parse(String command, S source) throws CommandException { public CommandContextBuilder<S> parse(String command, S source) throws CommandException {

View file

@ -117,6 +117,19 @@ public class CommandDispatcherTest {
} }
} }
@Test
public void testExecuteEmptyCommand() throws Exception {
subject.register(literal(""));
try {
subject.execute("", source);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
}
@Test @Test
public void testExecuteUnknownSubcommand() throws Exception { public void testExecuteUnknownSubcommand() throws Exception {
subject.register(literal("foo").executes(command)); subject.register(literal("foo").executes(command));