Permalink
Browse files

Merge pull request #664 from tototoshi/options-for-in-memory-database…

…-helper

Support H2 database options in play.api.test.Helpers.inMemoryDatabase
  • Loading branch information...
2 parents 8e3cb82 + ec6e99f commit bb10d87a9ba946185de2883af76ea257aab69455 @guillaumebort guillaumebort committed Jan 22, 2013
View
9 framework/src/play-test/src/main/java/play/test/Helpers.java
@@ -115,7 +115,14 @@ public static GlobalSettings fakeGlobal() {
* Constructs a in-memory (h2) database configuration to add to a FakeApplication.
*/
public static Map<String,String> inMemoryDatabase(String name) {
- return Scala.asJava(play.api.test.Helpers.inMemoryDatabase(name));
+ return inMemoryDatabase(name, Collections.<String, String>emptyMap());
+ }
+
+ /**
+ * Constructs a in-memory (h2) database configuration to add to a FakeApplication.
+ */
+ public static Map<String,String> inMemoryDatabase(String name, Map<String, String> options) {
+ return Scala.asJava(play.api.test.Helpers.inMemoryDatabase(name, Scala.asScala(options)));
}
/**
View
6 framework/src/play-test/src/main/scala/play/api/test/Helpers.scala
@@ -264,10 +264,12 @@ object Helpers extends Status with HeaderNames {
/**
* Constructs a in-memory (h2) database configuration to add to a FakeApplication.
*/
- def inMemoryDatabase(name: String = "default"): Map[String, String] = {
+ def inMemoryDatabase(name: String = "default", options: Map[String, String] = Map.empty[String, String]): Map[String, String] = {
+ val optionsForDbUrl = options.map { case (k, v) => k + "=" + v }.mkString(";", ";", "")
+
Map(
("db." + name + ".driver") -> "org.h2.Driver",
- ("db." + name + ".url") -> ("jdbc:h2:mem:play-test-" + scala.util.Random.nextInt)
+ ("db." + name + ".url") -> ("jdbc:h2:mem:play-test-" + scala.util.Random.nextInt + optionsForDbUrl)
)
}
View
28 framework/src/play-test/src/test/scala/play/api/test/HelpersSpec.scala
@@ -0,0 +1,28 @@
+package play.api.test
+
+import play.api.test._
+import play.api.test.Helpers._
+import org.specs2.mutable._
+
+class HelpersSpec extends Specification {
+
+ "inMemoryDatabase" should {
+
+ "change database with a name argument" in {
+ val inMemoryDatabaseConfiguration = inMemoryDatabase("test")
+ inMemoryDatabaseConfiguration.get("db.test.driver") must beSome("org.h2.Driver")
+ inMemoryDatabaseConfiguration.get("db.test.url") must beSome.which { url =>
+ url.startsWith("jdbc:h2:mem:play-test-")
+ }
+ }
+
+ "add options" in {
+ val inMemoryDatabaseConfiguration = inMemoryDatabase("test", Map("MODE" -> "PostgreSQL", "DB_CLOSE_DELAY" -> "-1"))
+ inMemoryDatabaseConfiguration.get("db.test.driver") must beSome("org.h2.Driver")
+ inMemoryDatabaseConfiguration.get("db.test.url") must beSome.which { url =>
+ """^jdbc:h2:mem:play-test([0-9-]+);MODE=PostgreSQL;DB_CLOSE_DELAY=-1$""".r.findFirstIn(url).isDefined
+ }
+ }
+ }
+
+}

0 comments on commit bb10d87

Please sign in to comment.