From 913d16ecde0c03eca64d176536fc978a4c339f6c Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Fri, 9 Jan 2015 10:44:14 +0100
Subject: [PATCH] Added unit tests to Configurable

---
 .../biopet/core/config/ConfigurableTest.scala | 71 +++++++++++++++++++
 1 file changed, 71 insertions(+)
 create mode 100644 public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigurableTest.scala

diff --git a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigurableTest.scala b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigurableTest.scala
new file mode 100644
index 000000000..e204656d7
--- /dev/null
+++ b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigurableTest.scala
@@ -0,0 +1,71 @@
+package nl.lumc.sasc.biopet.core.config
+
+import nl.lumc.sasc.biopet.utils.{ ConfigUtils, ConfigUtilsTest }
+import org.scalatest.Matchers
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.testng.TestNGSuite
+import org.testng.annotations.Test
+
+/**
+ * Created by pjvan_thof on 1/8/15.
+ */
+class ConfigurableTest extends TestNGSuite with MockitoSugar with Matchers {
+  @Test def testConfigurable: Unit = {
+    Config.global.map = Map()
+    Config.global.loadConfigFile(ConfigurableTest.file)
+
+    val classC = new ClassC
+
+    classC.configPath shouldBe Nil
+    classC.configFullPath shouldBe List("classc")
+    classC.classB.configPath shouldBe List("classc")
+    classC.classB.configFullPath shouldBe List("classc", "classb")
+    classC.classB.classA.configPath shouldBe List("classc", "classb")
+    classC.classB.classA.configFullPath shouldBe List("classc", "classb", "classa")
+
+    classC.get("k1").asString shouldBe "c1"
+    classC.classB.get("k1").asString shouldBe "c1"
+    classC.classB.classA.get("k1").asString shouldBe "c1"
+
+    classC.get("notexist") shouldBe null
+    intercept[IllegalStateException] {
+      classC.get("notexist", required = true)
+    }
+
+    classC.get("k1", freeVar = false).asString shouldBe "c1"
+    classC.classB.get("k1", freeVar = false).asString shouldBe "b1"
+    classC.classB.classA.get("k1", freeVar = false).asString shouldBe "a1"
+  }
+}
+
+abstract class Cfg extends Configurable {
+  def get(key: String, default: String = null, submodule: String = null, required: Boolean = false, freeVar: Boolean = true) = {
+    config(key, default, submodule, required, freeVar = freeVar)
+  }
+
+}
+
+class ClassA(val root: Configurable) extends Cfg
+
+class ClassB(val root: Configurable) extends Cfg {
+  val classA = new ClassA(this)
+}
+
+class ClassC(val root: Configurable) extends Cfg {
+  def this() = this(null)
+  val classB = new ClassB(this)
+}
+
+object ConfigurableTest {
+  val map = Map(
+    "classa" -> Map(
+      "k1" -> "a1"
+    ), "classb" -> Map(
+      "k1" -> "b1"
+    ), "classc" -> Map(
+      "k1" -> "c1"
+    )
+  )
+
+  val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2)
+}
-- 
GitLab