Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 795 - Add support for MongoOptions in MongoDB
- Loading branch information
Tim Nelson
committed
Mar 2, 2011
1 parent
e6deacf
commit b043c81
Showing
3 changed files
with
128 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
persistence/mongodb/src/test/scala/net/liftweb/mongodb/MongoSpecs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2011 WorldWide Conferencing, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.liftweb | ||
package mongodb | ||
|
||
import org.specs.Specification | ||
import org.specs.runner.JUnit4 | ||
|
||
import com.mongodb._ | ||
|
||
class MongoSpecsTest extends JUnit4(MongoSpecs) | ||
|
||
object MongoSpecs extends Specification { | ||
|
||
case object TestMongoIdentifier extends MongoIdentifier { | ||
val jndiName = "test_a" | ||
} | ||
|
||
def passDefinitionTests(id: MongoIdentifier, ma: MongoAddress): Unit = { | ||
// define the db | ||
MongoDB.close | ||
MongoDB.defineDb(id, ma) | ||
// make sure it can be used | ||
try { | ||
MongoDB.use(id) { db => | ||
db.getLastError.ok must beEqualTo(true) | ||
} | ||
} | ||
catch { | ||
case e: MongoInternalException if (e.getMessage == "DBPort.findOne failed") => skip("MongoDB is not running") | ||
case e: NullPointerException => skip("MongoDB is not running") | ||
case e: MongoException if (e.getMessage == "can't find a master") => skip("MongoDB is not running") | ||
} | ||
// using an undefined identifier throws an exception | ||
MongoDB.use(DefaultMongoIdentifier) { db => | ||
db.getLastError.ok must beEqualTo(true) | ||
} must throwA(new MongoException("Mongo not found: MongoIdentifier(test)")) | ||
// remove defined db | ||
MongoDB.close | ||
} | ||
|
||
"Mongo" should { | ||
"Define DB with default host and port" in { | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(), "test_default")) | ||
} | ||
"Define DB with default port" in { | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost("127.0.0.1"), "test_default")) | ||
} | ||
"Define DB with specified host and port" in { | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost("127.0.0.1", 27017), "test_default")) | ||
} | ||
"Define DB pair with DBAddress" in { | ||
val dba = new DBAddress("127.0.0.1", 27017, "test_a") | ||
val dbb = new DBAddress("127.0.0.1", 27018, "test_b") | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoPair(dba, dbb), "test_default")) | ||
} | ||
"Define DB with ServerAddress and MongoOptions" in { | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(new ServerAddress, new MongoOptions), "test_default")) | ||
} | ||
"Define DB with ServerAddress" in { | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(new ServerAddress), "test_default")) | ||
} | ||
"Define DB with MongoOptions" in { | ||
val mo = new MongoOptions | ||
mo.connectionsPerHost = 12 | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoHost(options=mo), "test_default")) | ||
} | ||
"Define DB pair with ServerAddress" in { | ||
val dba = new ServerAddress("127.0.0.1", 27017) | ||
val dbb = new ServerAddress("127.0.0.1", 27018) | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoPair(dba, dbb), "test_default")) | ||
} | ||
"Define DB set with ServerAddress" in { | ||
val dba = new ServerAddress("127.0.0.1", 27017) | ||
val dbb = new ServerAddress("127.0.0.1", 27018) | ||
val dbc = new ServerAddress("127.0.0.1", 27019) | ||
passDefinitionTests(TestMongoIdentifier, MongoAddress(MongoSet(List(dba, dbb, dbc)), "test_default")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters