Adds the ability for ConfigBeanImpl to handle a List of Beans.

Add test entry to beanconfig01.conf, ArraysConfig and
ConfigBeanFactoryTest.
This commit is contained in:
jason 2015-06-24 07:08:15 +09:30
parent e34433b7aa
commit 37e2dd3219
4 changed files with 37 additions and 2 deletions

View File

@ -193,6 +193,13 @@ public class ConfigBeanImpl {
return config.getObjectList(configPropName); return config.getObjectList(configPropName);
} else if (elementType == ConfigValue.class) { } else if (elementType == ConfigValue.class) {
return config.getList(configPropName); return config.getList(configPropName);
} else if (hasAtLeastOneBeanProperty((Class<?>) elementType)) {
List<Object> beanList = new ArrayList<>();
List<? extends Config> configList = config.getConfigList(configPropName);
for (Config listMember : configList) {
beanList.add(createInternal(listMember, (Class<?>) elementType));
}
return beanList;
} else { } else {
throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType); throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType);
} }

View File

@ -23,6 +23,7 @@ public class ArraysConfig {
List<ConfigValue> ofConfigValue; List<ConfigValue> ofConfigValue;
List<Duration> ofDuration; List<Duration> ofDuration;
List<ConfigMemorySize> ofMemorySize; List<ConfigMemorySize> ofMemorySize;
List<StringsConfig> ofStringBean;
public List<Integer> getEmpty() { public List<Integer> getEmpty() {
return empty; return empty;
@ -127,4 +128,12 @@ public class ArraysConfig {
public void setOfMemorySize(List<ConfigMemorySize> ofMemorySize) { public void setOfMemorySize(List<ConfigMemorySize> ofMemorySize) {
this.ofMemorySize = ofMemorySize; this.ofMemorySize = ofMemorySize;
} }
public List<StringsConfig> getOfStringBean() {
return ofStringBean;
}
public void setOfStringBean(List<StringsConfig> ofStringBean) {
this.ofStringBean = ofStringBean;
}
} }

View File

@ -52,7 +52,17 @@
"ofConfigObject" : [${numbers}, ${booleans}, ${strings}], "ofConfigObject" : [${numbers}, ${booleans}, ${strings}],
"ofConfigValue" : [1, 2, "a"], "ofConfigValue" : [1, 2, "a"],
"ofDuration" : [1, 2h, 3 days], "ofDuration" : [1, 2h, 3 days],
"ofMemorySize" : [1024, 1M, 1G] "ofMemorySize" : [1024, 1M, 1G],
"ofStringBean" : [
{
abcd : "testAbcdOne"
yes : "testYesOne"
},
{
abcd : "testAbcdTwo"
yes : "testYesTwo"
}
]
}, },
"bytes" : { "bytes" : {
"kilobyte" : "1kB", "kilobyte" : "1kB",

View File

@ -111,6 +111,15 @@ class ConfigBeanFactoryTest extends TestUtils {
ConfigMemorySize.ofBytes(1048576), ConfigMemorySize.ofBytes(1048576),
ConfigMemorySize.ofBytes(1073741824)), ConfigMemorySize.ofBytes(1073741824)),
beanConfig.getOfMemorySize.asScala) beanConfig.getOfMemorySize.asScala)
val stringsConfigOne = new StringsConfig();
stringsConfigOne.setAbcd("testAbcdOne")
stringsConfigOne.setYes("testYesOne")
val stringsConfigTwo = new StringsConfig();
stringsConfigTwo.setAbcd("testAbcdTwo")
stringsConfigTwo.setYes("testYesTwo")
assertEquals(List(stringsConfigOne, stringsConfigTwo), beanConfig.getOfStringBean)
} }
@Test @Test