Skip to content

Commit

Permalink
let subclass of ConfigOption be able to override parent options (#24)
Browse files Browse the repository at this point in the history
also implement ConfigOption.toString()

Change-Id: I1f1fd5eeaa80252fc093e7cba5af525a32a23c99
  • Loading branch information
javeme authored and zhoney committed May 28, 2019
1 parent 5ea077f commit 0822dd9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/baidu/hugegraph/config/ConfigOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,10 @@ public void check(Object value) {
this.name, value);
}
}

@Override
public String toString() {
return String.format("[%s]%s=%s", this.dataType.getSimpleName(),
this.name, this.defaultValue);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/baidu/hugegraph/config/OptionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected void registerOptions() {
for (Field field : this.getClass().getFields()) {
try {
ConfigOption<?> option = (ConfigOption<?>) field.get(this);
this.options.put(option.name(), option);
// Fields of subclass first, don't overwrite by superclass
this.options.putIfAbsent(option.name(), option);
} catch (Exception e) {
LOG.error("Failed to register option: {}", field, e);
throw new ConfigException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ public static void init() {
OptionSpace.register("test", TestOptions.class.getName());
}

@Test
public void testOptionsToString() {
Assert.assertEquals("[String]group1.text1=text1-value",
TestOptions.text1.toString());
Assert.assertEquals("[Integer]group1.int1=1",
TestOptions.int1.toString());
Assert.assertEquals("[Long]group1.long1=100",
TestOptions.long1.toString());
Assert.assertEquals("[Float]group1.float1=100.0",
TestOptions.float1.toString());
Assert.assertEquals("[Double]group1.double1=100.0",
TestOptions.double1.toString());
Assert.assertEquals("[Boolean]group1.bool=true",
TestOptions.bool.toString());
Assert.assertEquals("[List]group1.list=[list-value1, list-value2]",
TestOptions.list.toString());
Assert.assertEquals("[List]group1.map=[key1:value1, key2:value2]",
TestOptions.map.toString());

Assert.assertEquals("[String]group1.text1=text1-value",
TestSubOptions.text1.toString());
Assert.assertEquals("[String]group1.text2=text2-value-override",
TestSubOptions.text2.toString());
Assert.assertEquals("[String]group1.textsub=textsub-value",
TestSubOptions.textsub.toString());
}

@Test
public void testHugeConfig() throws Exception {
Configuration conf = new PropertiesConfiguration();
Expand Down Expand Up @@ -117,7 +144,22 @@ public void testHugeConfigWithConfiguration() throws Exception {
Assert.assertEquals("CHOICE-3", config.get(TestOptions.text3));
}

public static final class TestOptions extends OptionHolder {
@Test
public void testHugeConfigWithOverride() throws Exception {
Configuration conf = new PropertiesConfiguration();
Whitebox.setInternalState(conf, "delimiterParsingDisabled", true);

HugeConfig config = new HugeConfig(conf);

Assert.assertEquals("text1-value", config.get(TestSubOptions.text1));

Assert.assertEquals("text2-value-override",
config.get(TestSubOptions.text2));
Assert.assertEquals("textsub-value",
config.get(TestSubOptions.textsub));
}

public static class TestOptions extends OptionHolder {

private static volatile TestOptions instance;

Expand Down Expand Up @@ -229,4 +271,23 @@ public static synchronized TestOptions instance() {
"key1:value1", "key2:value2"
);
}

public static class TestSubOptions extends TestOptions {

public static final ConfigOption<String> text2 =
new ConfigOption<>(
"group1.text2",
"description of group1.text2",
disallowEmpty(),
"text2-value-override"
);

public static final ConfigOption<String> textsub =
new ConfigOption<>(
"group1.textsub",
"description of group1.textsub",
disallowEmpty(),
"textsub-value"
);
}
}

0 comments on commit 0822dd9

Please sign in to comment.