ConfigDef { public ConfigDef define(String name, Type type, Object defaultValue, Validator validator, Importance importance, String documentation) { if (configKeys.containsKey(name)) { throw new ConfigException("Configuration " + name + " is defined twice."); } Object parsedDefault = NO_DEFAULT_VALUE.equals(defaultValue) ? NO_DEFAULT_VALUE : parseType(name, defaultValue, type); configKeys .put(name, new ConfigKey(name, type, parsedDefault, validator, importance, documentation)); return this; } ConfigDef();  ConfigDef(ConfigDef configDef); Set<String> names(); ConfigDef define(String name, Type type, Object defaultValue, Validator validator,
                          Importance importance, String documentation); ConfigDef defineOverride(String name, Type type, Object defaultValue, Validator validator,
                                  Importance importance, String documentation); ConfigDef define(String name, Type type, Object defaultValue, Importance importance,
                          String documentation); ConfigDef defineOverride(String name, Type type, Object defaultValue, Importance importance,
                          String documentation); ConfigDef define(String name, Type type, Validator validator, Importance importance,
                          String documentation); ConfigDef defineOverride(String name, Type type, Validator validator, Importance importance,
                          String documentation); ConfigDef define(String name, Type type, Importance importance, String documentation); ConfigDef defineOverride(String name, Type type, Importance importance, String documentation); Map<String, Object> parse(Map<?, ?> props); String toHtmlTable(); String toRst();  }
@Test(expected = ConfigException.class) public void testInvalidDefault() { new ConfigDef().define("a", Type.INT, "hello", ConfigDef.Importance.HIGH, "docs"); }
@Test(expected = ConfigException.class) public void testNullDefault() { new ConfigDef().define("a", Type.INT, null, null, null, "docs"); }
@Test(expected = ConfigException.class) public void testDefinedTwice() { new ConfigDef().define("a", Type.STRING, ConfigDef.Importance.HIGH, "docs") .define("a", Type.INT, ConfigDef.Importance.HIGH, "docs"); }
@Test(expected = ConfigException.class) public void testInvalidDefaultRange() { ConfigDef def = new ConfigDef() .define("name", Type.INT, -1, ConfigDef.Range.between(0, 10), ConfigDef.Importance.HIGH, "docs"); }
@Test(expected = ConfigException.class) public void testInvalidDefaultString() { ConfigDef def = new ConfigDef().define("name", Type.STRING, "bad", ConfigDef.ValidString .in(Arrays.asList("valid", "values")), ConfigDef.Importance.HIGH, "docs"); }
@Test public void testClassLoading() throws IOException { ClassLoader systemLoader = Thread.currentThread().getContextClassLoader(); String configName = "type.class.property"; String configNameDefault = ConfigUtils.class.getName(); ConfigDef configDef = new ConfigDef(); configDef.define(configName, Type.CLASS, configNameDefault, ConfigDef.Importance.MEDIUM, "system class"); AbstractConfig config = new AbstractConfig(configDef, new HashMap<>()); Class<?> systemKlass = config.getClass(configName); assertEquals(systemLoader, systemKlass.getClassLoader()); Set<String> allowedKlasses = Collections.singleton(configNameDefault); List<URL> pluginUrls = Collections.singletonList(Paths.get("target/classes/").toUri().toURL()); URLClassLoader pluginLoader = new PluginClassLoader(pluginUrls.toArray(new URL[0]), systemLoader, allowedKlasses); try { Thread.currentThread().setContextClassLoader(pluginLoader); configDef = new ConfigDef(); configDef.define(configName, Type.CLASS, configNameDefault, ConfigDef.Importance.MEDIUM, "plugin class"); config = new AbstractConfig(configDef, new HashMap<>()); Class<?> pluginKlass = config.getClass(configName); assertEquals(pluginLoader, pluginKlass.getClassLoader()); assertNotEquals(pluginKlass, systemKlass); assertEquals(pluginKlass.getName(), systemKlass.getName()); } finally { Thread.currentThread().setContextClassLoader(systemLoader); } }