mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
Expand ConfigBeanFactory test coverage to all the primitive types.
This commit is contained in:
parent
14dbe09199
commit
7a74183190
23
config/src/test/java/beanconfig/StringsConfig.java
Normal file
23
config/src/test/java/beanconfig/StringsConfig.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package beanconfig;
|
||||||
|
|
||||||
|
|
||||||
|
public class StringsConfig {
|
||||||
|
String abcd;
|
||||||
|
String yes;
|
||||||
|
|
||||||
|
public String getAbcd() {
|
||||||
|
return abcd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAbcd(String s) {
|
||||||
|
abcd = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getYes() {
|
||||||
|
return yes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setYes(String s) {
|
||||||
|
yes = s;
|
||||||
|
}
|
||||||
|
}
|
@ -11,11 +11,11 @@
|
|||||||
"numbers" : {
|
"numbers" : {
|
||||||
"byteVal" : "1",
|
"byteVal" : "1",
|
||||||
"byteObj" : ${numbers.byteVal},
|
"byteObj" : ${numbers.byteVal},
|
||||||
"shortVal" : "1",
|
"shortVal" : "2",
|
||||||
"shortObj" : ${numbers.shortVal},
|
"shortObj" : ${numbers.shortVal},
|
||||||
"intVal" : "1",
|
"intVal" : "3",
|
||||||
"intObj" : ${numbers.intVal},
|
"intObj" : ${numbers.intVal},
|
||||||
"longVal" : "1",
|
"longVal" : "4",
|
||||||
"longObj" : ${numbers.longVal},
|
"longObj" : ${numbers.longVal},
|
||||||
"doubleVal" : "1.0",
|
"doubleVal" : "1.0",
|
||||||
"doubleObj" : ${numbers.doubleVal}
|
"doubleObj" : ${numbers.doubleVal}
|
||||||
|
@ -10,8 +10,34 @@ import beanconfig._
|
|||||||
import org.junit.Assert._
|
import org.junit.Assert._
|
||||||
import org.junit._
|
import org.junit._
|
||||||
|
|
||||||
|
import scala.collection.JavaConverters._
|
||||||
|
|
||||||
class ConfigBeanFactoryTest {
|
class ConfigBeanFactoryTest {
|
||||||
|
|
||||||
|
// TODO this is here temporarily to avoid moving to impl in this
|
||||||
|
// same commit
|
||||||
|
import scala.reflect.ClassTag
|
||||||
|
import scala.reflect.classTag
|
||||||
|
protected def intercept[E <: Throwable: ClassTag](block: => Any): E = {
|
||||||
|
val expectedClass = classTag[E].runtimeClass
|
||||||
|
var thrown: Option[Throwable] = None
|
||||||
|
val result = try {
|
||||||
|
Some(block)
|
||||||
|
} catch {
|
||||||
|
case t: Throwable =>
|
||||||
|
thrown = Some(t)
|
||||||
|
None
|
||||||
|
}
|
||||||
|
thrown match {
|
||||||
|
case Some(t) if expectedClass.isAssignableFrom(t.getClass) =>
|
||||||
|
t.asInstanceOf[E]
|
||||||
|
case Some(t) =>
|
||||||
|
throw new Exception(s"Expected exception ${expectedClass.getName} was not thrown, got $t", t)
|
||||||
|
case None =>
|
||||||
|
throw new Exception(s"Expected exception ${expectedClass.getName} was not thrown, no exception was thrown and got result $result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def toCamelCase() {
|
def toCamelCase() {
|
||||||
assertEquals("configProp", ConfigBeanFactory.toCamelCase("config-prop"))
|
assertEquals("configProp", ConfigBeanFactory.toCamelCase("config-prop"))
|
||||||
@ -27,6 +53,8 @@ class ConfigBeanFactoryTest {
|
|||||||
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
||||||
val beanConfig: TestBeanConfig = ConfigBeanFactory.create(config, classOf[TestBeanConfig])
|
val beanConfig: TestBeanConfig = ConfigBeanFactory.create(config, classOf[TestBeanConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
|
// recursive bean inside the first bean
|
||||||
|
assertEquals(3, beanConfig.getNumbers.getIntVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -34,61 +62,89 @@ class ConfigBeanFactoryTest {
|
|||||||
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
||||||
val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs),
|
val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs),
|
||||||
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
||||||
var expected: ConfigException.Generic = null
|
val expected = intercept[ConfigException.Generic] {
|
||||||
var beanConfig: NoFoundPropBeanConfig = null
|
ConfigBeanFactory.create(config, classOf[NoFoundPropBeanConfig])
|
||||||
try {
|
|
||||||
beanConfig = ConfigBeanFactory.create(config, classOf[NoFoundPropBeanConfig])
|
|
||||||
} catch {
|
|
||||||
case cge: ConfigException.Generic => expected = cge
|
|
||||||
case e: Exception => expected = null
|
|
||||||
}
|
}
|
||||||
assertNotNull(expected)
|
|
||||||
assertEquals("Could not find property 'propNotListedInConfig' from class 'beanconfig.NoFoundPropBeanConfig' in config.",
|
assertEquals("Could not find property 'propNotListedInConfig' from class 'beanconfig.NoFoundPropBeanConfig' in config.",
|
||||||
expected.getMessage)
|
expected.getMessage)
|
||||||
assertNull(beanConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testCreateBool() {
|
def testCreateBool() {
|
||||||
val beanConfig: BooleansConfig = ConfigBeanFactory.create(loadConfig().getConfig("booleans"), classOf[BooleansConfig])
|
val beanConfig: BooleansConfig = ConfigBeanFactory.create(loadConfig().getConfig("booleans"), classOf[BooleansConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
|
assertEquals(true, beanConfig.getTrueVal)
|
||||||
|
assertEquals(false, beanConfig.getFalseVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def testCreateString() {
|
||||||
|
val beanConfig: StringsConfig = ConfigBeanFactory.create(loadConfig().getConfig("strings"), classOf[StringsConfig])
|
||||||
|
assertNotNull(beanConfig)
|
||||||
|
assertEquals("abcd", beanConfig.getAbcd)
|
||||||
|
assertEquals("yes", beanConfig.getYes)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testCreateNumber() {
|
def testCreateNumber() {
|
||||||
val beanConfig: NumbersConfig = ConfigBeanFactory.create(loadConfig().getConfig("numbers"), classOf[NumbersConfig])
|
val beanConfig: NumbersConfig = ConfigBeanFactory.create(loadConfig().getConfig("numbers"), classOf[NumbersConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
|
assertEquals(1: Byte, beanConfig.getByteVal)
|
||||||
|
assertEquals(1: Byte, beanConfig.getByteObj)
|
||||||
|
assertEquals(2: Short, beanConfig.getShortVal)
|
||||||
|
assertEquals(2: Short, beanConfig.getShortObj)
|
||||||
|
|
||||||
|
assertEquals(3, beanConfig.getIntVal)
|
||||||
|
assertEquals(3, beanConfig.getIntObj)
|
||||||
|
|
||||||
|
assertEquals(4L, beanConfig.getLongVal)
|
||||||
|
assertEquals(4L, beanConfig.getLongObj)
|
||||||
|
|
||||||
|
assertEquals(1.0, beanConfig.getDoubleVal, 1e-6)
|
||||||
|
assertEquals(1.0, beanConfig.getDoubleObj, 1e-6)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testCreateList() {
|
def testCreateList() {
|
||||||
val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig])
|
val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
|
assertEquals(List().asJava, beanConfig.getEmpty)
|
||||||
|
assertEquals(List(1, 2, 3).asJava, beanConfig.getOfInt)
|
||||||
|
assertEquals(List("a", "b", "c").asJava, beanConfig.getOfString)
|
||||||
|
assertEquals(List(List("a", "b", "c").asJava,
|
||||||
|
List("a", "b", "c").asJava,
|
||||||
|
List("a", "b", "c").asJava).asJava,
|
||||||
|
beanConfig.getOfArray)
|
||||||
|
assertEquals(3, beanConfig.getOfObject.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testCreateDuration() {
|
def testCreateDuration() {
|
||||||
val beanConfig: DurationsConfig = ConfigBeanFactory.create(loadConfig().getConfig("durations"), classOf[DurationsConfig])
|
val beanConfig: DurationsConfig = ConfigBeanFactory.create(loadConfig().getConfig("durations"), classOf[DurationsConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
assertEquals(beanConfig.getHalfSecond, Duration.ofMillis(500))
|
assertEquals(Duration.ofMillis(500), beanConfig.getHalfSecond)
|
||||||
assertEquals(beanConfig.getSecond, Duration.ofMillis(1000))
|
assertEquals(Duration.ofMillis(1000), beanConfig.getSecond)
|
||||||
assertEquals(beanConfig.getSecondAsNumber, Duration.ofMillis(1000))
|
assertEquals(Duration.ofMillis(1000), beanConfig.getSecondAsNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def testCreateBytes() {
|
def testCreateBytes() {
|
||||||
val beanConfig: BytesConfig = ConfigBeanFactory.create(loadConfig().getConfig("bytes"), classOf[BytesConfig])
|
val beanConfig: BytesConfig = ConfigBeanFactory.create(loadConfig().getConfig("bytes"), classOf[BytesConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
assertEquals(beanConfig.getKibibyte, ConfigMemorySize.ofBytes(1024))
|
assertEquals(ConfigMemorySize.ofBytes(1024), beanConfig.getKibibyte)
|
||||||
assertEquals(beanConfig.getKilobyte, ConfigMemorySize.ofBytes(1000))
|
assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getKilobyte)
|
||||||
assertEquals(beanConfig.getThousandBytes, ConfigMemorySize.ofBytes(1000))
|
assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getThousandBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
private def loadConfig(): Config = {
|
private def loadConfig(): Config = {
|
||||||
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
||||||
val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs),
|
try {
|
||||||
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs),
|
||||||
config
|
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve
|
||||||
|
config
|
||||||
|
} finally {
|
||||||
|
configIs.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user