ConfigUtil { @SuppressWarnings("unchecked") public static synchronized <C> ConfigWrapper<C> wrapperFor(Class<C> targetClass) { ConfigWrapper<C> configWrapper = (ConfigWrapper<C>) wrapperCache.get(targetClass); if (configWrapper == null) { configWrapper = new ConfigWrapper<>(targetClass); wrapperCache.put(targetClass, configWrapper); } return configWrapper; } private  ConfigUtil(); @SuppressWarnings("unchecked") static synchronized ConfigWrapper<C> wrapperFor(Class<C> targetClass); @SuppressWarnings("unchecked") static void parseUri(C configObject, String uri); @SuppressWarnings("unchecked") static String generateUri(C configObject); static ConfigWrapper<?> storageConfigWrapperFor(String uri); static ConfigWrapper<?> filterConfigWrapperFor(String cliName); static Iterable<ConfigWrapper<?>> allStorageConfigWrappers(); static Iterable<ConfigWrapper<?>> allFilterConfigWrappers(); static String hyphenate(String name); static String labelize(String name); @SuppressWarnings("unchecked") static String summarize(C configObject); static void validate(Object configObject); static org.apache.commons.cli.Option cliOptionFromAnnotation(PropertyDescriptor descriptor,
                                                                        Option _option,
                                                                        String prefix); static String join(String[] parts); static final String XML_NAMESPACE; }
@Test public void testCliOptionGeneration() { Options options = ConfigUtil.wrapperFor(Foo.class).getOptions(); assertOption(options.getOption("my-value"), "my-value", false, 1, "my-value"); assertOption(options.getOption("my-num"), "my-num", false, 1, "my-num"); assertOption(options.getOption("my-flag"), "my-flag", false, -1, null); assertOption(options.getOption("no-negative-flag"), "no-negative-flag", false, -1, null); assertOption(options.getOption("my-enum"), "my-enum", false, 1, "my-enum"); }
@Test public void testCliParse() throws Exception { String[] args = { "--my-value", "value", "--my-num", "7", "--no-negative-flag", "--my-enum", "TwoFoo", "--my-array", "one", "--my-array", "two", "--my-array", "three", "--my-flag" }; ConfigWrapper<Foo> wrapper = ConfigUtil.wrapperFor(Foo.class); CommandLine commandLine = new DefaultParser().parse(wrapper.getOptions(), args); Foo foo = new Foo(); foo.setMyValue("value"); foo.setMyNum(7); foo.setMyFlag(true); foo.setNegativeFlag(false); foo.setMyEnum(FooType.TwoFoo); foo.setMyArray(new String[]{"one", "two", "three"}); Foo foo2 = wrapper.parse(commandLine); Assert.assertEquals(foo.getMyValue(), foo2.getMyValue()); Assert.assertEquals(foo.getMyNum(), foo2.getMyNum()); Assert.assertEquals(foo.isMyFlag(), foo2.isMyFlag()); Assert.assertEquals(foo.isNegativeFlag(), foo2.isNegativeFlag()); Assert.assertEquals(foo.getMyEnum(), foo2.getMyEnum()); Assert.assertArrayEquals(foo.getMyArray(), foo2.getMyArray()); }
@Test public void testSingleValueInArray() throws Exception { String[] args = {"--my-array", "foo"}; Foo foo = new Foo(); foo.setMyArray(new String[]{"foo"}); ConfigWrapper<Foo> configWrapper = ConfigUtil.wrapperFor(Foo.class); Foo foo2 = configWrapper.parse(new DefaultParser().parse(configWrapper.getOptions(), args)); Assert.assertArrayEquals(foo.getMyArray(), foo2.getMyArray()); }
@Test public void testRole() throws Exception { ConfigWrapper<Foo> configWrapper = ConfigUtil.wrapperFor(Foo.class); Assert.assertEquals(RoleType.Source, configWrapper.getRole()); }