From 7a741831905f3932c342f3c44d14236aec965b3e Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 26 Feb 2015 13:18:17 -0500 Subject: [PATCH] Expand ConfigBeanFactory test coverage to all the primitive types. --- .../test/java/beanconfig/StringsConfig.java | 23 +++++ .../resources/beanconfig/beanconfig01.conf | 6 +- .../config/ConfigBeanFactoryTest.scala | 92 +++++++++++++++---- 3 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 config/src/test/java/beanconfig/StringsConfig.java diff --git a/config/src/test/java/beanconfig/StringsConfig.java b/config/src/test/java/beanconfig/StringsConfig.java new file mode 100644 index 00000000..b6c7f574 --- /dev/null +++ b/config/src/test/java/beanconfig/StringsConfig.java @@ -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; + } +} diff --git a/config/src/test/resources/beanconfig/beanconfig01.conf b/config/src/test/resources/beanconfig/beanconfig01.conf index 3fe260af..db2dca6b 100644 --- a/config/src/test/resources/beanconfig/beanconfig01.conf +++ b/config/src/test/resources/beanconfig/beanconfig01.conf @@ -11,11 +11,11 @@ "numbers" : { "byteVal" : "1", "byteObj" : ${numbers.byteVal}, - "shortVal" : "1", + "shortVal" : "2", "shortObj" : ${numbers.shortVal}, - "intVal" : "1", + "intVal" : "3", "intObj" : ${numbers.intVal}, - "longVal" : "1", + "longVal" : "4", "longObj" : ${numbers.longVal}, "doubleVal" : "1.0", "doubleObj" : ${numbers.doubleVal} diff --git a/config/src/test/scala/com/typesafe/config/ConfigBeanFactoryTest.scala b/config/src/test/scala/com/typesafe/config/ConfigBeanFactoryTest.scala index 15d7117b..416016fb 100644 --- a/config/src/test/scala/com/typesafe/config/ConfigBeanFactoryTest.scala +++ b/config/src/test/scala/com/typesafe/config/ConfigBeanFactoryTest.scala @@ -10,8 +10,34 @@ import beanconfig._ import org.junit.Assert._ import org.junit._ +import scala.collection.JavaConverters._ + 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 def toCamelCase() { assertEquals("configProp", ConfigBeanFactory.toCamelCase("config-prop")) @@ -27,6 +53,8 @@ class ConfigBeanFactoryTest { ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve val beanConfig: TestBeanConfig = ConfigBeanFactory.create(config, classOf[TestBeanConfig]) assertNotNull(beanConfig) + // recursive bean inside the first bean + assertEquals(3, beanConfig.getNumbers.getIntVal) } @Test @@ -34,61 +62,89 @@ class ConfigBeanFactoryTest { val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf") val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs), ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve - var expected: ConfigException.Generic = null - var beanConfig: NoFoundPropBeanConfig = null - try { - beanConfig = ConfigBeanFactory.create(config, classOf[NoFoundPropBeanConfig]) - } catch { - case cge: ConfigException.Generic => expected = cge - case e: Exception => expected = null + val expected = intercept[ConfigException.Generic] { + ConfigBeanFactory.create(config, classOf[NoFoundPropBeanConfig]) } - assertNotNull(expected) assertEquals("Could not find property 'propNotListedInConfig' from class 'beanconfig.NoFoundPropBeanConfig' in config.", expected.getMessage) - assertNull(beanConfig) } @Test def testCreateBool() { val beanConfig: BooleansConfig = ConfigBeanFactory.create(loadConfig().getConfig("booleans"), classOf[BooleansConfig]) 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 def testCreateNumber() { val beanConfig: NumbersConfig = ConfigBeanFactory.create(loadConfig().getConfig("numbers"), classOf[NumbersConfig]) 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 def testCreateList() { val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig]) 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 def testCreateDuration() { val beanConfig: DurationsConfig = ConfigBeanFactory.create(loadConfig().getConfig("durations"), classOf[DurationsConfig]) assertNotNull(beanConfig) - assertEquals(beanConfig.getHalfSecond, Duration.ofMillis(500)) - assertEquals(beanConfig.getSecond, Duration.ofMillis(1000)) - assertEquals(beanConfig.getSecondAsNumber, Duration.ofMillis(1000)) + assertEquals(Duration.ofMillis(500), beanConfig.getHalfSecond) + assertEquals(Duration.ofMillis(1000), beanConfig.getSecond) + assertEquals(Duration.ofMillis(1000), beanConfig.getSecondAsNumber) } @Test def testCreateBytes() { val beanConfig: BytesConfig = ConfigBeanFactory.create(loadConfig().getConfig("bytes"), classOf[BytesConfig]) assertNotNull(beanConfig) - assertEquals(beanConfig.getKibibyte, ConfigMemorySize.ofBytes(1024)) - assertEquals(beanConfig.getKilobyte, ConfigMemorySize.ofBytes(1000)) - assertEquals(beanConfig.getThousandBytes, ConfigMemorySize.ofBytes(1000)) + assertEquals(ConfigMemorySize.ofBytes(1024), beanConfig.getKibibyte) + assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getKilobyte) + assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getThousandBytes) } private def loadConfig(): Config = { val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf") - val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs), - ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve - config + try { + val config: Config = ConfigFactory.parseReader(new InputStreamReader(configIs), + ConfigParseOptions.defaults.setSyntax(ConfigSyntax.CONF)).resolve + config + } finally { + configIs.close() + } } }