@Test public void testSetFailsIfObjectNotSet_ForSetterMethod() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof NullPointerException); } }
@Test public void testSetInvokesMethod_FailsForGetterMethod() throws Exception { Method getX = ModelMethodBindingBean.class.getDeclaredMethod("getX"); getX.setAccessible(true); ModelMethodBindingBean bean = new ModelMethodBindingBean(); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(bean, getX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof IllegalArgumentException); assertEquals("wrong number of arguments", pex.getCause().getMessage()); } }
@Test public void testExceptionFromMethod() { class App { @Option(names = "--jvm") public void jvmException(String value) { throw new IllegalArgumentException("Boo!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--jvm", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof IllegalArgumentException); assertEquals("Boo!", ex.getCause().getMessage()); assertEquals("Could not invoke public void picocli.CommandLineAnnotatedMethodImplTest$1App.jvmException(java.lang.String) with abc", ex.getMessage()); } }
@Test public void testPicocliExceptionFromMethod() { class App { @Option(names = "--pico") public void picocliException(String value) { throw new PicocliException("Pico!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--pico", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof PicocliException); assertEquals("Pico!", ex.getCause().getMessage()); assertEquals("PicocliException: Pico! while processing argument at or before arg[1] 'abc' in [--pico, abc]: picocli.CommandLine$PicocliException: Pico!", ex.getMessage()); } } }
@Test public void testExceptionFromMethod() { class App { @Option(names = "--jvm") public void jvmException(String value) { throw new IllegalArgumentException("Boo!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--jvm", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof IllegalArgumentException); assertEquals("Boo!", ex.getCause().getMessage()); assertEquals("Could not invoke public void picocli.CommandLineAnnotatedMethodImplTest$1App.jvmException(java.lang.String) with abc", ex.getMessage()); } }
@Test public void testSetInvokesMethod_FailsForGetterMethod() throws Exception { Method getX = ModelMethodBindingBean.class.getDeclaredMethod("getX"); getX.setAccessible(true); ModelMethodBindingBean bean = new ModelMethodBindingBean(); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(bean, getX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof IllegalArgumentException); assertEquals("wrong number of arguments", pex.getCause().getMessage()); } }
@Test public void testSetFailsIfObjectNotSet_ForSetterMethod() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); setX.setAccessible(true); CommandSpec spec = CommandSpec.create(); MethodBinding binding = new MethodBinding(null, setX, spec); try { binding.set(41); fail("Expect exception"); } catch (Exception ex) { ParameterException pex = (ParameterException) ex; assertSame(spec, pex.getCommandLine().getCommandSpec()); assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof NullPointerException); } }
@Test public void testParameterExceptionFromMethod() { class App { @Option(names = "--param") public void paramException(String value) { throw new ParameterException(new CommandLine(new App()), "Param!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--param", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNull(ex.getCause()); assertEquals("Param!", ex.getMessage()); } }
@Test public void testPicocliExceptionFromMethod() { class App { @Option(names = "--pico") public void picocliException(String value) { throw new PicocliException("Pico!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--pico", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof PicocliException); assertEquals("Pico!", ex.getCause().getMessage()); assertEquals("PicocliException: Pico! while processing argument at or before arg[1] 'abc' in [--pico, abc]: picocli.CommandLine$PicocliException: Pico!", ex.getMessage()); } } }
@Test public void testParameterExceptionFromMethod() { class App { @Option(names = "--param") public void paramException(String value) { throw new ParameterException(new CommandLine(new App()), "Param!"); } } CommandLine parser = new CommandLine(new App()); try { parser.parse("--param", "abc"); fail("Expected exception"); } catch (ParameterException ex) { assertNull(ex.getCause()); assertEquals("Param!", ex.getMessage()); } }