@Test public void testAnnotatedMethodMultipleMixinsSubcommandWithEmptyMixin() throws Exception { Method m = CommandLine.getCommandMethods(AnnotatedClassWithMultipleEmptyParameters.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-a 3".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args int arg0 = (Integer) methodArgValues[0]; assertEquals(3, arg0); EmptyMixin arg1 = (EmptyMixin) methodArgValues[1]; // verify method is callable with args long result = (Long) m.invoke(new AnnotatedClassWithMultipleEmptyParameters(), methodArgValues); assertEquals(3, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(3L, results.get(0)); }
@Test public void testAnnotatedMethodMultipleMixinsSubcommandWithDoubleMixin() throws Exception { Method m = CommandLine.getCommandMethods(AnnotatedClassWithMultipleMixinParameters.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-a 3 -b 5 -c 7".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args SomeMixin arg0 = (SomeMixin) methodArgValues[0]; assertEquals(3, arg0.a); assertEquals(5, arg0.b); OtherMixin arg1 = (OtherMixin) methodArgValues[1]; assertEquals(7, arg1.c); // verify method is callable with args long result = (Long) m.invoke(new AnnotatedClassWithMultipleMixinParameters(), methodArgValues); assertEquals(15, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(15L, results.get(0)); }
@Test public void testAnnotatedSubcommandWithDoubleMixin() throws Exception { AnnotatedClassWithMixinParameters command = new AnnotatedClassWithMixinParameters(); CommandLine commandLine = new CommandLine(command); List<CommandLine> parsed = commandLine.parse("-a 3 -b 5 sum -y foo -y bar -a 7 -b 11 13 42".split(" ")); assertEquals(2, parsed.size()); // get method args Object[] methodArgValues = parsed.get(1).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args String[] arg0 = (String[]) methodArgValues[0]; assertArrayEquals(new String[] {"foo", "bar"}, arg0); SomeMixin arg1 = (SomeMixin) methodArgValues[1]; assertEquals(7, arg1.a); assertEquals(11, arg1.b); int[] arg2 = (int[]) methodArgValues[2]; assertArrayEquals(new int[] {13, 42}, arg2); // verify method is callable with args Method m = AnnotatedClassWithMixinParameters.class.getDeclaredMethod("sum", String[].class, SomeMixin.class, int[].class); long result = (Long) m.invoke(command, methodArgValues); assertEquals(30, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(30L, results.get(0)); }
@Test public void testUnannotatedCommandWithMixin() throws Exception { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinAndOptionsAndPositionals.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-y foo -y bar -a 7 -b 11 13 42".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args String[] arg0 = (String[]) methodArgValues[0]; assertArrayEquals(new String[] {"foo", "bar"}, arg0); SomeMixin arg1 = (SomeMixin) methodArgValues[1]; assertEquals(7, arg1.a); assertEquals(11, arg1.b); int[] arg2 = (int[]) methodArgValues[2]; assertArrayEquals(new int[] {13, 42}, arg2); // verify method is callable with args long result = (Long) m.invoke(new UnannotatedClassWithMixinAndOptionsAndPositionals(), methodArgValues); assertEquals(22, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(22L, results.get(0)); }
@Test public void testAnnotatedSubcommandWithDoubleMixin() throws Exception { AnnotatedClassWithMixinParameters command = new AnnotatedClassWithMixinParameters(); CommandLine commandLine = new CommandLine(command); List<CommandLine> parsed = commandLine.parse("-a 3 -b 5 sum -y foo -y bar -a 7 -b 11 13 42".split(" ")); assertEquals(2, parsed.size()); // get method args Object[] methodArgValues = parsed.get(1).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args String[] arg0 = (String[]) methodArgValues[0]; assertArrayEquals(new String[] {"foo", "bar"}, arg0); SomeMixin arg1 = (SomeMixin) methodArgValues[1]; assertEquals(7, arg1.a); assertEquals(11, arg1.b); int[] arg2 = (int[]) methodArgValues[2]; assertArrayEquals(new int[] {13, 42}, arg2); // verify method is callable with args Method m = AnnotatedClassWithMixinParameters.class.getDeclaredMethod("sum", String[].class, SomeMixin.class, int[].class); long result = (Long) m.invoke(command, methodArgValues); assertEquals(30, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(30L, results.get(0)); }
@Test public void testAnnotatedMethodMultipleMixinsSubcommandWithDoubleMixin() throws Exception { Method m = CommandLine.getCommandMethods(AnnotatedClassWithMultipleMixinParameters.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-a 3 -b 5 -c 7".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args SomeMixin arg0 = (SomeMixin) methodArgValues[0]; assertEquals(3, arg0.a); assertEquals(5, arg0.b); OtherMixin arg1 = (OtherMixin) methodArgValues[1]; assertEquals(7, arg1.c); // verify method is callable with args long result = (Long) m.invoke(new AnnotatedClassWithMultipleMixinParameters(), methodArgValues); assertEquals(15, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(15L, results.get(0)); }
@Test public void testAnnotatedMethodMultipleMixinsSubcommandWithEmptyMixin() throws Exception { Method m = CommandLine.getCommandMethods(AnnotatedClassWithMultipleEmptyParameters.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-a 3".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args int arg0 = (Integer) methodArgValues[0]; assertEquals(3, arg0); EmptyMixin arg1 = (EmptyMixin) methodArgValues[1]; // verify method is callable with args long result = (Long) m.invoke(new AnnotatedClassWithMultipleEmptyParameters(), methodArgValues); assertEquals(3, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(3L, results.get(0)); }
@Test public void testUnannotatedCommandWithMixin() throws Exception { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinAndOptionsAndPositionals.class, "sum").get(0); CommandLine commandLine = new CommandLine(m); List<CommandLine> parsed = commandLine.parse("-y foo -y bar -a 7 -b 11 13 42".split(" ")); assertEquals(1, parsed.size()); // get method args Object[] methodArgValues = parsed.get(0).getCommandSpec().argValues(); assertNotNull(methodArgValues); // verify args String[] arg0 = (String[]) methodArgValues[0]; assertArrayEquals(new String[] {"foo", "bar"}, arg0); SomeMixin arg1 = (SomeMixin) methodArgValues[1]; assertEquals(7, arg1.a); assertEquals(11, arg1.b); int[] arg2 = (int[]) methodArgValues[2]; assertArrayEquals(new int[] {13, 42}, arg2); // verify method is callable with args long result = (Long) m.invoke(new UnannotatedClassWithMixinAndOptionsAndPositionals(), methodArgValues); assertEquals(22, result); // verify same result with result handler List<Object> results = new RunLast().handleParseResult(parsed, System.out, CommandLine.Help.Ansi.OFF); assertEquals(1, results.size()); assertEquals(22L, results.get(0)); }