DispatcherCommand extends Command implements PluginIdentifiableCommand { @Override public boolean execute(CommandSender sender, String label, String... arguments) { if (!testPermission(sender)) { return true; } var reader = new StringReader(join(label, arguments)); if (reader.canRead() && reader.peek() == '/') { reader.skip(); } try { dispatcher.execute(reader, sender); } catch (CommandSyntaxException e) { Exceptions.report(sender, e); } catch (Exception e) { Exceptions.report(sender, e); } return true; } DispatcherCommand(String name, Plugin plugin, CommandDispatcher<CommandSender> dispatcher, String usage, List<String> aliases); @Override boolean execute(CommandSender sender, String label, String... arguments); @Override Plugin getPlugin();  }
@Test void execute_testPermission() { doReturn(false).when(command).testPermission(sender); assertTrue(command.execute(sender, "command", "a", "b")); verifyNoMoreInteractions(dispatcher); }
@Test void execute() throws CommandSyntaxException { doReturn(true).when(command).testPermission(sender); var captor = ArgumentCaptor.forClass(StringReader.class); assertTrue(command.execute(sender, "command", "a", "b")); verify(dispatcher).execute(captor.capture(), eq(sender)); var reader = captor.getValue(); assertEquals("/command a b", reader.getString()); assertEquals("command a b", reader.getRemaining()); }
@Test void execute_CommandSyntaxException() throws CommandSyntaxException { doReturn(true).when(command).testPermission(sender); doThrow(new CommandSyntaxException(null, new LiteralMessage("message"), "abc", 2)).when(dispatcher).execute(any(StringReader.class), any(CommandSender.class)); assertTrue(command.execute(sender, "command", "a", "b")); verify(listener, times(2)).sendFailureMessage(any()); }
@Test void execute_Exception() throws CommandSyntaxException { doReturn(true).when(command).testPermission(sender); doThrow(RuntimeException.class).when(dispatcher).execute(any(StringReader.class), any(CommandSender.class)); assertTrue(command.execute(sender, "command", "a", "b")); verify(listener, times(1)).sendFailureMessage(any()); }