public ClassShellCommand(Class<T> clazz) throws IntrospectionException { CommandFactory factory = new CommandFactory(getClass().getClassLoader()); this.clazz = clazz; this.descriptor = HelpDescriptor.create(factory.create(clazz)); }
private void assertIllegalValueType(Class<?> type) throws IntrospectionException { try { CommandFactory.DEFAULT.create(type); fail(); } catch (IllegalValueTypeException e) { } }
private void assertIllegalParameter(Class<?> type) throws IntrospectionException { try { CommandFactory.DEFAULT.create(type); fail(); } catch (IllegalParameterException e) { } } }
private static <T> void handle(Class<T> commandClass, String line) throws Exception { ObjectCommandDescriptor<T> descriptor = CommandFactory.DEFAULT.create(commandClass); HelpDescriptor<Instance<T>> helpDescriptor = HelpDescriptor.create(descriptor); InvocationMatcher<Instance<T>> matcher = helpDescriptor.matcher(); InvocationMatch<Instance<T>> match = matcher.parse(line); final T instance = commandClass.newInstance(); Object o = match.invoke(Util.wrap(instance)); if (o != null) { System.out.println(o); } } }
public void testUnkownClassOption() throws Exception { class A { } CommandDescriptor<Instance<A>> cmd = CommandFactory.DEFAULT.create(A.class); // Tester<A> tester = new Tester<A>(cmd, "-o"); tester.assertEnd(Event.Stop.Unresolved.NoSuchOption.class, 0); }
public void testUnkownMethodOption2() throws Exception { class A { @Command void m() {} } CommandDescriptor<Instance<A>> cmd = CommandFactory.DEFAULT.create(A.class); // Tester<A> tester = new Tester<A>(cmd, "-o"); tester.assertEnd(Event.Stop.Unresolved.NoSuchOption.class, 0); }
public void testOptionWithUpperCase() throws IntrospectionException { class A { @Option(names = "I") private int i; } CommandDescriptor<Instance<A>> ai = CommandFactory.DEFAULT.create(A.class); assertEquals(1,ai.getOptions().size()); OptionDescriptor i = ai.getOption("-I"); assertEquals(Arrays.asList("I"),i.getNames()); }
public void testOption() throws IntrospectionException { class A { @Option(names = "i") private int i; } CommandDescriptor<Instance<A>> ai = CommandFactory.DEFAULT.create(A.class); assertEquals(1,ai.getOptions().size()); OptionDescriptor i = ai.getOption("-i"); assertEquals(Arrays.asList("i"),i.getNames()); }
public void testCommandImplicitDescription() throws IntrospectionException { class A { } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); assertEquals("", c.getUsage()); assertEquals(0, c.getArguments().size()); assertEquals(0, c.getOptions().size()); }
public <C> void assertPreserveHelp(Class<C> clazz, C instance, String option) throws Exception { CommandDescriptor<Instance<C>> desc = HelpDescriptor.create(CommandFactory.DEFAULT.create(clazz)); InvocationMatcher<Instance<C>> matcher = desc.matcher(); InvocationMatch<Instance<C>> match = matcher.options(Collections.<String, List<?>>singletonMap(option, Collections.singletonList(Boolean.TRUE))).arguments(Collections.emptyList()); CommandInvoker<Instance<C>, ?> invoker = match.getInvoker(); Object ret = invoker.invoke(Util.wrap(instance)); assertEquals("my help " + true, ret); }
public void testAnnotation() throws IntrospectionException { class A { @Level String l; } CommandDescriptor<Instance<A>> a = CommandFactory.DEFAULT.create(A.class); assertEquals(1,a.getOptions().size()); OptionDescriptor i = a.getOption("-l"); assertEquals(Arrays.asList("l"),i.getNames()); assertTrue(i.getAnnotation() instanceof Level); } }
public void testNoDescription() throws Exception { class A { } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); assertEquals("", c.getUsage()); assertEquals(new Description(), c.getDescription()); }
public void testOptionIntListType() throws IntrospectionException { class A { @Option(names = "o") List<Integer> o; } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); OptionDescriptor i = c.getOption("-o"); assertEquals(Multiplicity.MULTI, i.getMultiplicity()); assertEquals(ValueType.INTEGER, i.getType()); }
public void testOptionStringListType() throws IntrospectionException { class A { @Option(names = "o") List<String> o; } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); OptionDescriptor i = c.getOption("-o"); assertEquals(Multiplicity.MULTI, i.getMultiplicity()); assertEquals(ValueType.STRING, i.getType()); }
public void testOptionBooleanWrapperType() throws IntrospectionException { class A { @Option(names = "o") Boolean o; } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); OptionDescriptor i = c.getOption("-o"); assertEquals(Multiplicity.SINGLE, i.getMultiplicity()); assertEquals(false, i.isRequired()); assertEquals(ValueType.BOOLEAN, i.getType()); }
public void testImplicitSubordinateOption() throws Exception { CommandDescriptor<Instance<M>> desc = CommandFactory.DEFAULT.create(M.class); M m = new M(); desc.matcher().parse("-o foo").invoke(Util.wrap(m)); assertEquals("foo", m.opt); m = new M(); desc.matcher().option("o", Collections.singletonList("foo")).arguments(Collections.emptyList()).invoke(Util.wrap(m)); assertEquals("foo", m.opt); }
public void testOptionIntType() throws IntrospectionException { class A { @Option(names = "o") int o; } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); OptionDescriptor i = c.getOption("-o"); assertEquals(Multiplicity.SINGLE, i.getMultiplicity()); assertEquals(false, i.isRequired()); assertEquals(ValueType.INTEGER, i.getType()); }
public void testOptionIntWrapperType() throws IntrospectionException { class A { @Option(names = "o") Integer o; } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); OptionDescriptor i = c.getOption("-o"); assertEquals(Multiplicity.SINGLE, i.getMultiplicity()); assertEquals(false, i.isRequired()); assertEquals(ValueType.INTEGER, i.getType()); }
public void testSpecifyClassOptionAfterSubordinate() throws Exception { CommandDescriptor<Instance<K>> desc = CommandFactory.DEFAULT.create(K.class); K k = new K(); desc.matcher().parse("cmd -o foo").invoke(Util.wrap(k)); assertEquals(null, k.opt); k = new K(); desc.matcher().subordinate("cmd").option("o", Collections.singletonList("foo")).arguments(Collections.emptyList()).invoke(Util.wrap(k)); assertEquals(null, k.opt); }
public void testClassDescription() throws Exception { @Usage("class_usage") @Man("class_man") class A { } CommandDescriptor<Instance<A>> c = CommandFactory.DEFAULT.create(A.class); assertEquals("class_usage", c.getUsage()); assertEquals("class_usage", c.getDescription().getUsage()); assertEquals("class_man", c.getDescription().getMan()); }