-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
TestkitConfig.scala
72 lines (62 loc) · 2.96 KB
/
TestkitConfig.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.typesafe.slick.testkit.util
import java.util.concurrent.TimeUnit
import com.typesafe.config.{ConfigValueFactory, Config, ConfigFactory}
import java.io.{FileInputStream, File}
import java.util.Properties
import scala.collection.JavaConverters._
import scala.concurrent.duration.Duration
import slick.SlickException
/** Manages the configuration for TestKit tests.
*
* The standard config file `test-dbs/testkit.conf` can be changed through the system
* property `slick.testkit-config`. The defaults are loaded from `/testkit-reference.conf`
* on the classpath.
*/
object TestkitConfig {
private[this] lazy val (conf, testkitConfig, defaults, ref) = {
val configFileName = sys.props.get("slick.testkit-config")
val configFile = new File(configFileName.getOrElse("test-dbs/testkit.conf"))
if(configFileName.isDefined && !configFile.isFile)
throw new SlickException("TestKit config file \""+configFileName.get+"\" not found")
val ref = ConfigFactory.parseResources(getClass, "/testkit-reference.conf")
val conf = ConfigFactory.parseFile(configFile)
val testkitConfig = {
val c =
if(conf.hasPath("testkit")) conf.getConfig("testkit").withFallback(ref.getObject("testkit")).resolve()
else ref.getConfig("testkit").resolve()
c.withValue("absTestDir", ConfigValueFactory.fromAnyRef(new File(c.getString("testDir")).getAbsolutePath))
}
val defaults = ref.getObject("defaults").withValue("testkit", testkitConfig.root()).toConfig
(conf, testkitConfig, defaults, ref)
}
/** Get a resolved test configuration */
def testConfig(name: String) = {
val cRef = if(ref.hasPath(name)) ref.getConfig(name).withFallback(defaults) else defaults
val cApp = if(conf.hasPath(name)) conf.getConfig(name).withFallback(cRef) else cRef
cApp.resolve().withoutPath("testkit")
}
/** The `testkit.testDir` setting */
lazy val testDir = testkitConfig.getString("testDir")
/** A normalized version of `testDir` for use in URLs */
lazy val testDBPath = {
val f = new File(testDir)
val s = f.getPath().replace('\\', '/')
if(f.isAbsolute) s else "./" + s
}
/** The `testkit.testDBs` setting */
lazy val testDBs = getStrings(testkitConfig, "testDBs")
/** The `testkit.testClasses` setting */
lazy val testClasses: Seq[Class[_ <: GenericTest[_ >: Null <: TestDB]]] =
getStrings(testkitConfig, "testClasses").getOrElse(Nil).
map(n => Class.forName(n).asInstanceOf[Class[_ <: GenericTest[_ >: Null <: TestDB]]])
/** The duration after which asynchronous tests should be aborted and failed */
lazy val asyncTimeout = Duration(testkitConfig.getDuration("asyncTimeout", TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS)
def getStrings(config: Config, path: String): Option[Seq[String]] = {
if(config.hasPath(path)) {
config.getValue(path).unwrapped() match {
case l: java.util.List[_] => Some(l.asScala.map(_.toString))
case o => Some(List(o.toString))
}
} else None
}
}