/
MongoSpecs.scala
94 lines (86 loc) · 3.57 KB
/
MongoSpecs.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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"))
}
}
}