@Test public void testCommandMethodsUnexpectedError() throws Exception { Method method = CommandMethod1.class.getDeclaredMethod("times", int.class, int.class); CommandLine cmd = new CommandLine(method); Method execute = CommandLine.class.getDeclaredMethod("execute", CommandLine.class, List.class); execute.setAccessible(true); try { execute.invoke(null, cmd, null); } catch (InvocationTargetException ex) { ExecutionException actual = (ExecutionException) ex.getCause(); assertTrue(actual.getMessage(), actual.getMessage().startsWith("Unhandled error while calling command (")); } }
@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()); } } }
@SuppressWarnings("deprecation") private void verifyAllFail(Factory factory, String prefix, String suffix, String[] args) { IParseResultHandler[] handlers = new IParseResultHandler[] { new RunFirst(), new RunLast(), new RunAll() }; for (IParseResultHandler handler : handlers) { String descr = handler.getClass().getSimpleName(); try { new CommandLine(factory.create()).parseWithHandler(handler, System.out, args); fail(descr + ": expected exception"); } catch (ExecutionException ex) { String actual = ex.getMessage(); assertTrue(descr + ": " + actual, actual.startsWith(prefix)); assertTrue(descr + ": " + actual, actual.endsWith(suffix)); } } }
@Test public void testExecutionExceptionIfRunnableThrowsExecutionException() { @Command class App implements Runnable { @Spec CommandSpec spec; public void run() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.run(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testExecutionExceptionIfCallableThrowsExecutionException() { @Command class App implements Callable<Void> { @Spec CommandSpec spec; public Void call() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.call(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testHandlerThrowsExecutionException() { @Command class App { } try { new CommandLine(new App()).parseWithHandler(new IParseResultHandler2<Object>() { public Object handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.getMessage()); } }
@Test public void testCommandMethodsUnexpectedError() throws Exception { Method method = CommandMethod1.class.getDeclaredMethod("times", int.class, int.class); CommandLine cmd = new CommandLine(method); Method execute = CommandLine.class.getDeclaredMethod("execute", CommandLine.class, List.class); execute.setAccessible(true); try { execute.invoke(null, cmd, null); } catch (InvocationTargetException ex) { ExecutionException actual = (ExecutionException) ex.getCause(); assertTrue(actual.getMessage(), actual.getMessage().startsWith("Unhandled error while calling command (")); } }
@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 testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.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 (")); } }
@Test public void testCommandMethodsThatThrowsExecutionException() { try { CommandLine.invoke("throwsExecutionException", StaticMethodCommand.class); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testHandlerThrowsExecutionException2() { @Command class App { } IParseResultHandler2<Void> handler = new IParseResultHandler2<Void>() { public Void handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }; IExceptionHandler2<Void> exceptionHandler = new IExceptionHandler2<Void>() { public Void handleParseException(ParameterException ex, String[] args) { return null; } public Void handleExecutionException(ExecutionException ex, ParseResult parseResult) { return null; } }; try { new CommandLine(new App()).parseWithHandlers(handler, exceptionHandler, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.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 testExecutionExceptionIfRunnableThrowsExecutionException() { @Command class App implements Runnable { @Spec CommandSpec spec; public void run() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.run(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testExecutionExceptionIfCallableThrowsExecutionException() { @Command class App implements Callable<Void> { @Spec CommandSpec spec; public Void call() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.call(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testHandlerThrowsExecutionException() { @Command class App { } try { new CommandLine(new App()).parseWithHandler(new IParseResultHandler2<Object>() { public Object handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.getMessage()); } }
@Test public void testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.getMessage()); } }
@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 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 testCommandMethodsThatThrowsExecutionException() { try { CommandLine.invoke("throwsExecutionException", StaticMethodCommand.class); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }