/** * Runs this class as a standalone application, printing the resulting JSON String to a file or to {@code System.out}. * @param args one or more fully qualified class names of {@code @Command}-annotated classes. */ public static void main(String... args) { CommandLine.call(new App(), args); }
public static void main(String[] args) throws Exception { // CheckSum implements Callable, // so parsing and error handling can be done in one line of code CommandLine.call(new CheckSum(), System.err, args); }
@Test(expected = InitializationException.class) public void testCallRequiresAnnotatedCommand() throws Exception { class App implements Callable<Object> { public Object call() { return null; } } CommandLine.call(new App(), System.err); }
@Test public void testCallReturnsCallableResultParseSucceeds() throws Exception { @Command class App implements Callable<Boolean> { public Boolean call() { return true; } } assertTrue(CommandLine.call(new App(), System.err)); }
@Test public void testCallableClassNullResult() { Object result = CommandLine.call(Executable.class, CommandLine.defaultFactory(), "-x"); assertNull(result); } }
@Test public void testCallNullResult() { Object result = CommandLine.call(new Executable(), "-x"); assertNull(result); }
@Test public void testCall3WithHelpRequest() { CommandLine.call(new MyCallable(), System.out, Help.Ansi.ON, "--help"); assertEquals("", systemErrRule.getLog()); assertEquals(MYCALLABLE_USAGE_ANSI, systemOutRule.getLog()); }
@Test public void testCall4WithHelpRequest_ToStderr() { CommandLine.call(new MyCallable(), System.err, System.out, Help.Ansi.ON, "--help"); assertEquals(MYCALLABLE_USAGE_ANSI, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall2WithInvalidInput() { CommandLine.call(new MyCallable(), System.out, "invalid input"); assertEquals(MYCALLABLE_INVALID_INPUT, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall4WithInvalidInput() { CommandLine.call(new MyCallable(), System.out, System.err, Help.Ansi.ON, "invalid input"); assertEquals(INVALID_INPUT + MYCALLABLE_USAGE_ANSI, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall1WithHelpRequest() { CommandLine.call(new MyCallable(), "--help"); assertEquals("", systemErrRule.getLog()); assertEquals(MYCALLABLE_USAGE, systemOutRule.getLog()); }
@Test public void testCall4WithInvalidInput_ToStdout() { CommandLine.call(new MyCallable(), System.out, System.out, Help.Ansi.ON, "invalid input"); assertEquals("", systemErrRule.getLog()); assertEquals(INVALID_INPUT + MYCALLABLE_USAGE_ANSI, systemOutRule.getLog()); }
@Test public void testCall3WithHelpRequest_ToStderr() { CommandLine.call(new MyCallable(), System.err, Help.Ansi.ON, "--help"); assertEquals(MYCALLABLE_USAGE_ANSI, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall4WithHelpRequest() { CommandLine.call(new MyCallable(), System.out, System.err, Help.Ansi.ON, "--help"); assertEquals("", systemErrRule.getLog()); assertEquals(MYCALLABLE_USAGE_ANSI, systemOutRule.getLog()); }
@Test public void testCall1WithInvalidInput() { CommandLine.call(new MyCallable(), "invalid input"); assertEquals(MYCALLABLE_INVALID_INPUT, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall3WithInvalidInput() { CommandLine.call(new MyCallable(), System.out, Help.Ansi.ON, "invalid input"); assertEquals(INVALID_INPUT + MYCALLABLE_USAGE_ANSI, systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall2WithHelpRequest() { CommandLine.call(new MyCallable(), System.out, "--help"); assertEquals("", systemErrRule.getLog()); assertEquals(MYCALLABLE_USAGE, systemOutRule.getLog()); }
@Test public void testCall1DefaultExceptionHandlerRethrows() { try { CommandLine.call(new MyCallable(), "-x abc"); } catch (ExecutionException ex) { String cmd = ex.getCommandLine().getCommand().toString(); String msg = "Error while calling command (" + cmd + "): java.lang.IllegalStateException: this is a test"; assertEquals(msg, ex.getMessage()); } assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall3WithHelpRequest_ToCustomStream() { StringPrintStream sps = new StringPrintStream(); CommandLine.call(new MyCallable(), sps.stream(), Help.Ansi.ON, "--help"); assertEquals(MYCALLABLE_USAGE_ANSI, sps.toString()); assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testCall4WithHelpRequest_ToCustomStream() { StringPrintStream sps = new StringPrintStream(); CommandLine.call(new MyCallable(), sps.stream(), System.out, Help.Ansi.ON, "--help"); assertEquals(MYCALLABLE_USAGE_ANSI, sps.toString()); assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); } //---