diff --git a/.travis.yml b/.travis.yml index 18d67b5..655de81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: scala scala: - "2.10.4" - - "2.11.1" + - "2.11.7" diff --git a/README.md b/README.md index 2232b5e..20c931d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ You can change thow default values: Modify `project/Build.scala` and add -`"com.github.athieriot" %% "specs2-embedmongo" % "0.7.0"` +`"com.github.athieriot" %% "specs2-embedmongo" % "0.8.1"` [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/athieriot/specs2-embedmongo/trend.png)](https://bitdeli.com/free "Bitdeli Badge") diff --git a/project/Build.scala b/project/Build.scala index 0120f0a..c238e73 100755 --- a/project/Build.scala +++ b/project/Build.scala @@ -3,14 +3,14 @@ import sbt.Keys._ object ProjectBuild extends Build { - lazy val buildVersion = "0.7.0" + lazy val buildVersion = "0.8.1" lazy val root = Project(id = "specs2-embedmongo", base = file("."), settings = Project.defaultSettings).settings( organization := "com.github.athieriot", description := "Specs2 helper to configure a EmbedMongo based instance", version := buildVersion, - scalaVersion := "2.10.4", - crossScalaVersions := Seq("2.10.4", "2.11.1"), + scalaVersion := "2.11.7", + crossScalaVersions := Seq("2.10.4", "2.11.7"), resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/", resolvers += "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/", @@ -19,11 +19,10 @@ object ProjectBuild extends Build { resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/", libraryDependencies <++= scalaVersion(sv => Seq( - "de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.46.0", - "org.specs2" %% "specs2" % "2.3.12", - "org.mongodb" %% "casbah-core" % "2.7.2" % "provided", - "com.novus" %% "salat-core" % "1.9.8" % "test", - "junit" % "junit" % "4.11" % "test" + "de.svenkubiak" % "embedded-mongodb" % "4.2.9", + "org.specs2" %% "specs2-core" % "3.8.2" % "provided", + "org.reactivemongo" %% "reactivemongo" % "0.11.8" % "provided", + "junit" % "junit" % "4.12" % "test" )), parallelExecution in Test := false, @@ -52,12 +51,7 @@ object ProjectBuild extends Build { ), - publishTo <<= version { version: String => - val nexus = "https://oss.sonatype.org/" - if (version.trim.endsWith("SNAPSHOT")) - Some("snapshots" at nexus + "content/repositories/snapshots") - else - Some("releases" at nexus + "service/local/staging/deploy/maven2") - } + publishTo := Some("cody" at "http://cody:8082/nexus/content/repositories/releases"), + credentials += Credentials(Path.userHome / ".sbt" / "sonatype.credentials") ) } diff --git a/project/build.properties b/project/build.properties index 0974fce..a6e117b 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.0 +sbt.version=0.13.8 diff --git a/src/main/scala/com/github/athieriot/CleanAfterExample.scala b/src/main/scala/com/github/athieriot/CleanAfterExample.scala index 56da6a1..c833683 100644 --- a/src/main/scala/com/github/athieriot/CleanAfterExample.scala +++ b/src/main/scala/com/github/athieriot/CleanAfterExample.scala @@ -1,16 +1,24 @@ package com.github.athieriot -import scala.collection.JavaConversions._ -import com.mongodb.{ MongoClient, ServerAddress } -import org.specs2.specification.AfterExample +import org.specs2.specification.AfterEach +import reactivemongo.api.MongoDriver -trait CleanAfterExample extends AfterExample { +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ + +trait CleanAfterExample extends AfterEach { self: EmbedConnection => - lazy val mongoClient = new MongoClient(new ServerAddress(network.getServerAddress(), network.getPort())); + def getConn = { + val driver = new MongoDriver + val db = driver.connection(List(s"127.0.0.1:${network.getPort}"))("testdb") + Await.ready(db.connection.waitForPrimary(10 seconds), 11 seconds) + db + } - def after() { - mongoClient.getDatabaseNames().map { mongoClient.getDB(_) }.foreach { _.dropDatabase() } + def after = { + Await.ready(getConn.drop(), 1 second) } } \ No newline at end of file diff --git a/src/main/scala/com/github/athieriot/EmbedConnection.scala b/src/main/scala/com/github/athieriot/EmbedConnection.scala index b77208e..422c8d6 100644 --- a/src/main/scala/com/github/athieriot/EmbedConnection.scala +++ b/src/main/scala/com/github/athieriot/EmbedConnection.scala @@ -1,27 +1,28 @@ package com.github.athieriot -import de.flapdoodle.embed._ -import process.runtime.Network -import mongo._ -import config._ -import distribution._ -import org.specs2.specification._ -import org.specs2.mutable.SpecificationLike +import de.flapdoodle.embed.mongo._ +import de.flapdoodle.embed.mongo.config._ +import de.flapdoodle.embed.mongo.distribution._ +import de.flapdoodle.embed.process.runtime.Network import org.specs2.main.Arguments +import org.specs2.mutable.{Specification, SpecificationLike} +import org.specs2.specification.core.{Fragment, Fragments} -trait EmbedConnection extends FragmentsBuilder { +trait EmbedConnection extends Specification { self: SpecificationLike => isolated + def sequentialyIsolated: Arguments = args(isolated = true, sequential = true) + override def sequential: Arguments = args(isolated = false, sequential = true) override def isolated: Arguments = args(isolated = true, sequential = false) //Override this method to personalize testing port - def embedConnectionPort(): Int = { 12345 } + def embedConnectionPort: Int = 12345 //Override this method to personalize MongoDB version - def embedMongoDBVersion(): Version.Main = { Version.Main.PRODUCTION } + def embedMongoDBVersion: Version.Main = Version.Main.PRODUCTION lazy val network = new Net(embedConnectionPort, Network.localhostIsIPv6) @@ -36,11 +37,11 @@ trait EmbedConnection extends FragmentsBuilder { override def map(fs: => Fragments) = startMongo ^ fs ^ stoptMongo - private def startMongo() = { - Example("Start Mongo", { mongodExecutable.start; success }) + private def startMongo = { + step(mongodExecutable.start) } - private def stoptMongo() = { - Example("Stop Mongo", { mongodExecutable.stop; success }) + private def stoptMongo = { + step(mongodExecutable.stop()) } } diff --git a/src/test/scala/com/github/athieriot/CleanAfterExampleSpec.scala b/src/test/scala/com/github/athieriot/CleanAfterExampleSpec.scala index fdf312a..67dfbc8 100644 --- a/src/test/scala/com/github/athieriot/CleanAfterExampleSpec.scala +++ b/src/test/scala/com/github/athieriot/CleanAfterExampleSpec.scala @@ -1,36 +1,45 @@ package com.github.athieriot -import org.junit.runner.RunWith -import org.specs2.runner.JUnitRunner import org.specs2.mutable.Specification +import reactivemongo.bson.BSONDocument -@RunWith(classOf[JUnitRunner]) -class CleanAfterExampleSpec extends Specification with EmbedConnection with CleanAfterExample { +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ + +class CleanAfterExampleSpec extends Specification with EmbedConnection with MongoInit with CleanAfterExample { + + sequentialyIsolated "Embed database" should { "be able to save a Model I" in { - Model.save(Model(name = "test")) - Model.count() must be_==(1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } "be able to save a Model II" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } "be able to save a Model III" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } "be able to save a Model IV" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } "be able to save a Model V" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } } } \ No newline at end of file diff --git a/src/test/scala/com/github/athieriot/EmbedConnectionSpec.scala b/src/test/scala/com/github/athieriot/EmbedConnectionSpec.scala index c9f0972..4aabd33 100644 --- a/src/test/scala/com/github/athieriot/EmbedConnectionSpec.scala +++ b/src/test/scala/com/github/athieriot/EmbedConnectionSpec.scala @@ -1,37 +1,43 @@ package com.github.athieriot -import org.junit.runner.RunWith -import org.specs2.runner.JUnitRunner import org.specs2.mutable.Specification +import reactivemongo.bson.BSONDocument -@RunWith(classOf[JUnitRunner]) -class EmbedConnectionSpec extends Specification with EmbedConnection { +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ +class EmbedConnectionSpec extends Specification with EmbedConnection with MongoInit { sequential "Embed database" should { "be able to save a Model I" in { - Model.save(Model(name = "test")) - Model.count() must be_==(1) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(1) } "be able to save a Model II" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (2) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(2) } "be able to save a Model III" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (3) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(3) } "be able to save a Model IV" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (4) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(4) } "be able to save a Model V" in { - Model.save(Model(name = "test")) - Model.count() must be be_== (5) + val coll = getColl + Await.ready(coll.insert(BSONDocument("hello" -> "World")), 1 second) + Await.result(coll.count(None), 5 second) must be_==(5) } } } diff --git a/src/test/scala/com/github/athieriot/Model.scala b/src/test/scala/com/github/athieriot/Model.scala deleted file mode 100644 index eb6f912..0000000 --- a/src/test/scala/com/github/athieriot/Model.scala +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.athieriot - -import com.mongodb.casbah.Imports._ -import org.bson.types.ObjectId -import com.novus.salat.global._ -import com.novus.salat._ -import com.novus.salat.dao._ - -case class Model(id: ObjectId = new ObjectId, name: String) - -object Model extends SalatDAO[Model, ObjectId](collection = MongoConnection("localhost", 12345)("test")("model")) {} \ No newline at end of file diff --git a/src/test/scala/com/github/athieriot/MongoInit.scala b/src/test/scala/com/github/athieriot/MongoInit.scala new file mode 100644 index 0000000..e674ca0 --- /dev/null +++ b/src/test/scala/com/github/athieriot/MongoInit.scala @@ -0,0 +1,16 @@ +package com.github.athieriot + +import reactivemongo.api.MongoDriver +import reactivemongo.api.collections.bson.BSONCollection +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ + +trait MongoInit { + def getColl: BSONCollection = { + val driver = new MongoDriver + val db = driver.connection(List("localhost:12345"))("testdb") + Await.ready(db.connection.waitForPrimary(10 seconds), 11 seconds) + db("testcoll") + } +}