@Test public void testCallWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage().startsWith("Error while calling command (picocli.CommandLineParseWithHandlersTest$MyCallable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testRunWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while running command (picocli.CommandLineParseWithHandlersTest$MyRunnable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testCommandMethodsWhereConstructorThrowsException() { try { CommandLine.invoke("cannotBeCalled", ErroringCommand.class); } catch (ExecutionException ex) { // InvocationTargetException when invoking constructor assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while calling command (")); } }
@Override public R handleExecutionException(ExecutionException ex, ParseResult parseResult) { // find top level command CommandLine cmdLine = ex.getCommandLine(); while (cmdLine.getParent() != null) { cmdLine = cmdLine.getParent(); } JetCommandLine jetCmd = cmdLine.getCommand(); if (jetCmd.isVerbose) { ex.printStackTrace(err()); } else { err().println("ERROR: " + ex.getCause().getMessage()); err().println(); err().println("To see the full stack trace, re-run with the -v/--verbose option"); } if (hasExitCode()) { exit(exitCode()); } throw ex; } }
@Test public void testCallWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage().startsWith("Error while calling command (picocli.CommandLineParseWithHandlersTest$MyCallable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testRunWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while running command (picocli.CommandLineParseWithHandlersTest$MyRunnable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testCommandMethodsThatThrowsException() { try { CommandLine.invoke("throwsOtherException", StaticMethodCommand.class); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof IndexOutOfBoundsException); } }
@Test public void testCommandMethodsRequireNonArgConstructor() { try { CommandLine.invoke("cannotBeCalled", StaticMethodCommand.class); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof UnsupportedOperationException); } }
@Test public void testCommandMethodsWhereConstructorThrowsException() { try { CommandLine.invoke("cannotBeCalled", ErroringCommand.class); } catch (ExecutionException ex) { // InvocationTargetException when invoking constructor assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while calling command (")); } }
@Test public void testCommandMethodsThatThrowsException() { try { CommandLine.invoke("throwsOtherException", StaticMethodCommand.class); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof IndexOutOfBoundsException); } }
@Test public void testCommandMethodsRequireNonArgConstructor() { try { CommandLine.invoke("cannotBeCalled", StaticMethodCommand.class); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof UnsupportedOperationException); } }