Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/mongodb/casbah
Browse files Browse the repository at this point in the history
  • Loading branch information
teigen committed Jan 20, 2012
2 parents 1fdc0d9 + e5b44eb commit 5d0d430
Show file tree
Hide file tree
Showing 24 changed files with 585 additions and 262 deletions.
20 changes: 20 additions & 0 deletions casbah-commons/src/main/scala/MongoDBObject.scala
Expand Up @@ -91,6 +91,26 @@ class MongoDBObject(val underlying: DBObject = new BasicDBObject) extends scala.
super.++(other: DBObject)
}

/**
* Returns a new list with this MongoDBObject at the *end*
* Currently only supports composing with other DBObjects, primarily for
* the use of the Query DSL; if you want heterogenous lists, use
* MongoDBList directly.
* @see MongoDBList
*
*/
def ::[A <% DBObject](elem: A): Seq[DBObject] = Seq(elem: DBObject, this)

/**
* Returns a new list with this MongoDBObject at the *end*
* Currently only supports composing with other DBObjects, primarily for
* the use of the Query DSL; if you want heterogenous lists, use
* MongoDBList directly.
* @see MongoDBList
*
*/
def ::(elem: (String, Any)): Seq[DBObject] = Seq(MongoDBObject(elem), this)

/** Lazy utility method to allow typing without conflicting with Map's required get() method and causing ambiguity */
def getAs[A <: Any: Manifest](key: String): Option[A] = {
require(manifest[A] != manifest[scala.Nothing],
Expand Down
9 changes: 6 additions & 3 deletions casbah-commons/src/main/scala/test/MongoDBSpecification.scala
Expand Up @@ -18,19 +18,22 @@
package com.mongodb.casbah.commons
package test

import org.specs2.mutable._
import org.specs2._
import org.specs2.data.Sized
import org.specs2.matcher.{ Expectable, Matcher, MapMatchers }
import org.specs2.matcher.Matchers._
import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.MongoDBObject
import javax.management.remote.rmi._RMIConnection_Stub

object `package` {

}

trait CasbahSpecification extends Specification with DBObjectMatchers with Logging {
trait CasbahMutableSpecification extends mutable.Specification with CasbahSpecificationBase

trait CasbahSpecification extends Specification with CasbahSpecificationBase

trait CasbahSpecificationBase extends DBObjectMatchers with Logging {
implicit val sizedOptDBObj = new Sized[Option[DBObject]] {
def size(t: Option[DBObject]) = t.getOrElse(MongoDBObject.empty).size
}
Expand Down
4 changes: 2 additions & 2 deletions casbah-commons/src/test/scala/MongoDBListSpec.scala
Expand Up @@ -23,9 +23,9 @@
package com.mongodb.casbah.test.commons

import com.mongodb.casbah.commons._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

class MongoDBListSpec extends CasbahSpecification {
class MongoDBListSpec extends CasbahMutableSpecification {
val x = Seq(5, 9, 212, "x", "y", 22.98)
val y = Seq("spam", "eggs", "foo", "bar")

Expand Down
25 changes: 23 additions & 2 deletions casbah-commons/src/test/scala/MongoDBObjectSpec.scala
Expand Up @@ -23,9 +23,9 @@
package com.mongodb.casbah.test.commons

import com.mongodb.casbah.commons._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

class MongoDBObjectSpec extends CasbahSpecification {
class MongoDBObjectSpec extends CasbahMutableSpecification {

"MongoDBObject expand operations" should {

Expand Down Expand Up @@ -249,6 +249,27 @@ class MongoDBObjectSpec extends CasbahSpecification {
explicit.hashCode must beEqualTo(control.hashCode())
explicit.equals(explicit) must beEqualTo(control.equals(control))
}

"Support list creation operators" in {
"Prepend to end of a new list" in {
"With explicitly created Elements" in {
val list = MongoDBObject("x" -> "y") :: MongoDBObject("foo" -> "bar")
list must haveSize(2)
list(0) must beDBObject
(list(0): DBObject) must haveEntries("x" -> "y")
list(1) must beDBObject
(list(1): DBObject) must haveEntries("foo" -> "bar")
}
"With implicitly created Elements with an explicit" in {
val list = ("x" -> "y") :: MongoDBObject("foo" -> "bar")
list must haveSize(2)
list(0) must beDBObject
(list(0): DBObject) must haveEntries("x" -> "y")
list(1) must beDBObject
(list(1): DBObject) must haveEntries("foo" -> "bar")
}
}
}
}

}
Expand Down
6 changes: 6 additions & 0 deletions casbah-core/src/main/scala/Implicits.scala
Expand Up @@ -117,6 +117,12 @@ object Imports extends Imports with commons.Imports with commons.Exports with qu
trait Imports extends BaseImports with TypeImports with Implicits

package core {
/**
* You can import core to get "just" Casbah core; no commons, query, etc.
* This is useful to pick which QueryDSL type you want, etc.
*/
object `package` extends Imports

trait Exports {
type MongoCursor = com.mongodb.casbah.MongoCursor
type MongoCollection = com.mongodb.casbah.MongoCollection
Expand Down
48 changes: 33 additions & 15 deletions casbah-core/src/main/scala/MongoURI.scala
Expand Up @@ -44,7 +44,7 @@ object MongoURI {
*
* @param uri (String)
*/
def apply(uri: String) = new MongoURI(new com.mongodb.MongoURI(uri))
def apply(uri: String): MongoURI = new MongoURI(new com.mongodb.MongoURI(uri))
}

/**
Expand All @@ -60,20 +60,38 @@ object MongoURI {
* @since 2.0
*/
class MongoURI(val underlying: com.mongodb.MongoURI) {
def username = underlying.getUsername
def password = underlying.getPassword
def hosts = underlying.getHosts.asScala
def database = underlying.getDatabase
def collection = underlying.getCollection
def options = underlying.getOptions
def connect = underlying.connect.asScala
def connectDB = underlying.connectDB.asScala
def connectDB(m: MongoConnection) =
underlying.connectDB(m.underlying).asScala
def connectCollection(db: MongoDB) =
underlying.connectCollection(db.underlying).asScala
def connectCollection(m: MongoConnection) =
underlying.connectCollection(m.underlying).asScala
def username: Option[String] = Option(underlying.getUsername)
def password: Option[Array[Char]] = Option(underlying.getPassword)
def hosts: Seq[String] = underlying.getHosts.asScala
def database: Option[String] = Option(underlying.getDatabase)
def collection: Option[String] = Option(underlying.getCollection)
def options: MongoOptions = underlying.getOptions
def connect: Either[Throwable, MongoConnection] = try {
Right(underlying.connect.asScala)
} catch {
case t => Left(t)
}
def connectDB: Either[Throwable, MongoDB] = {
try {
require(database.isDefined, "Cannot connect to Database as none is defined.")
Right(underlying.connectDB.asScala)
} catch {
case t => Left(t)
}
}
def connectCollection: Either[Throwable, MongoCollection] = {
try {
require(collection.isDefined, "Cannot connect to Collection as none is defined.")
connectDB match {
case Right(db) =>
Right(db(collection.get))
case Left(t) => Left(t)
}
} catch {
case t => Left(t)
}

}

override def toString = underlying.toString
}
Expand Down
4 changes: 2 additions & 2 deletions casbah-core/src/test/scala/ConversionsSpec.scala
Expand Up @@ -27,11 +27,11 @@ import com.mongodb.casbah.commons.conversions.scala._
import com.mongodb.casbah._

import org.scala_tools.time.Imports._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification
import org.specs2.specification.BeforeExample
import org.bson.BSON

class ConversionsSpec extends CasbahSpecification with BeforeExample {
class ConversionsSpec extends CasbahMutableSpecification with BeforeExample {

type JDKDate = java.util.Date

Expand Down
4 changes: 2 additions & 2 deletions casbah-core/src/test/scala/CoreWrappersSpec.scala
Expand Up @@ -24,9 +24,9 @@ package com.mongodb.casbah


import com.mongodb.casbah._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

class CoreWrappersSpec extends CasbahSpecification {
class CoreWrappersSpec extends CasbahMutableSpecification {

"Casbah behavior between Scala and Java versions of Objects" should {

Expand Down
4 changes: 2 additions & 2 deletions casbah-core/src/test/scala/GroupSpec.scala
Expand Up @@ -26,9 +26,9 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._

import org.scala_tools.time.Imports._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

class GroupSpec extends CasbahSpecification {
class GroupSpec extends CasbahMutableSpecification {

"Casbah's Group Interfaces" should {
implicit val mongoDB = MongoConnection()("casbahIntegration")
Expand Down
6 changes: 3 additions & 3 deletions casbah-core/src/test/scala/LazyDecodingSpec.scala
Expand Up @@ -21,14 +21,14 @@
*/
package com.mongodb.casbah.test

import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification
import com.mongodb.casbah._
import com.mongodb.casbah.util.bson.decoding.OptimizedLazyDBObject
import java.util.{UUID , Date}
import org.bson.types.{MinKey, MaxKey, BSONTimestamp, Binary}
import java.util.regex.Pattern

class LazyDecodingSpec extends CasbahSpecification {
class LazyDecodingSpec extends CasbahMutableSpecification {
implicit val mongoInt = MongoConnection()("casbahIntegration")
implicit val mongoTest = MongoConnection()("casbahTest_Lazy")

Expand Down Expand Up @@ -163,7 +163,7 @@ class LazyDecodingSpec extends CasbahSpecification {
doc must haveEntry("double245_6289" -> 245.6289 )
doc must haveEntry("oid" -> testOid )
doc must haveEntry("str" -> "foobarbaz" )
//doc.getAs[DBRef]("ref") must beSome( new DBRef(mongoInt.underlying, "books", testRefOid) )
doc must haveEntry("uuid" -> testUUID)
doc must haveEntry("object.abc" -> testDoc.get("abc") )
doc.getAs[Pattern]("regex").get.pattern() must_==( testRE.pattern.pattern() )
}
Expand Down
4 changes: 2 additions & 2 deletions casbah-core/src/test/scala/MapReduceSpec.scala
Expand Up @@ -27,10 +27,10 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._

import org.scala_tools.time.Imports._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

@SuppressWarnings(Array("deprecation"))
class MapReduceSpec extends CasbahSpecification {
class MapReduceSpec extends CasbahMutableSpecification {

"Casbah's Map/Reduce Engine" should {

Expand Down
4 changes: 2 additions & 2 deletions casbah-core/src/test/scala/RawMapReduceSpec.scala
Expand Up @@ -27,10 +27,10 @@ import com.mongodb.casbah.util.Logging
import com.mongodb.casbah.commons.conversions.scala._

import org.scala_tools.time.Imports._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification

@SuppressWarnings(Array("deprecation"))
class RawMapReduceSpec extends CasbahSpecification {
class RawMapReduceSpec extends CasbahMutableSpecification {

"MongoDB 1.7+ Map/Reduce functionality" should {
implicit val mongoDB = MongoConnection()("casbahIntegration")
Expand Down
4 changes: 2 additions & 2 deletions casbah-gridfs/src/test/scala/GridFSSpec.scala
Expand Up @@ -29,10 +29,10 @@ import com.mongodb.casbah.gridfs._

import java.security.MessageDigest
import java.io._
import com.mongodb.casbah.commons.test.CasbahSpecification
import com.mongodb.casbah.commons.test.CasbahMutableSpecification


class GridFSSpec extends CasbahSpecification {
class GridFSSpec extends CasbahMutableSpecification {
/* override def is = args(sequential = true) ^ super.is*/

implicit val mongo = MongoConnection()("casbah_test")
Expand Down

0 comments on commit 5d0d430

Please sign in to comment.