16
16
17
17
package org .mongodb .scala
18
18
19
- import com .mongodb .ClusterFixture .getServerApi
20
19
import com .mongodb .connection .ServerVersion
21
20
import org .mongodb .scala .bson .BsonString
22
21
import org .scalatest ._
23
22
24
23
import scala .collection .JavaConverters ._
25
24
import scala .concurrent .duration .{ Duration , _ }
26
25
import scala .concurrent .{ Await , ExecutionContext }
27
- import scala .util .{ Properties , Try }
28
26
29
27
trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
30
28
31
29
implicit val ec : ExecutionContext = ExecutionContext .Implicits .global
32
30
33
- private val DEFAULT_URI : String = " mongodb://localhost:27017/"
34
- private val MONGODB_URI_SYSTEM_PROPERTY_NAME : String = " org.mongodb.test.uri"
35
31
val WAIT_DURATION : Duration = 60 .seconds
36
32
private val DB_PREFIX = " mongo-scala-"
37
33
private var _currentTestName : Option [String ] = None
38
- private var mongoDBOnline : Boolean = false
39
34
40
35
protected override def runTest (testName : String , args : Args ): Status = {
41
36
_currentTestName = Some (testName.split(" should" )(1 ))
42
- mongoDBOnline = isMongoDBOnline()
43
37
super .runTest(testName, args)
44
38
}
45
39
@@ -53,48 +47,33 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
53
47
*/
54
48
def collectionName : String = _currentTestName.getOrElse(suiteName).filter(_.isLetterOrDigit)
55
49
56
- val mongoClientURI : String = {
57
- val uri = Properties .propOrElse(MONGODB_URI_SYSTEM_PROPERTY_NAME , DEFAULT_URI )
58
- if (! uri.isBlank) uri else DEFAULT_URI
59
- }
60
- val connectionString : ConnectionString = ConnectionString (mongoClientURI)
50
+ def mongoClientSettingsBuilder : MongoClientSettings .Builder = TestMongoClientHelper .mongoClientSettingsBuilder
61
51
62
- def mongoClientSettingsBuilder : MongoClientSettings .Builder = {
63
- val builder = MongoClientSettings .builder().applyConnectionString(connectionString)
64
- if (getServerApi != null ) {
65
- builder.serverApi(getServerApi)
66
- }
67
- builder
68
- }
52
+ val mongoClientSettings : MongoClientSettings = TestMongoClientHelper .mongoClientSettings
69
53
70
- val mongoClientSettings : MongoClientSettings = mongoClientSettingsBuilder.build()
54
+ def mongoClient () : MongoClient = TestMongoClientHelper .mongoClient
71
55
72
- def mongoClient (): MongoClient = MongoClient (mongoClientSettings)
73
-
74
- def isMongoDBOnline (): Boolean = {
75
- Try (Await .result(MongoClient (mongoClientSettings).listDatabaseNames().toFuture(), WAIT_DURATION )).isSuccess
76
- }
77
-
78
- def hasSingleHost (): Boolean = {
79
- new ConnectionString (mongoClientURI).getHosts.size() == 1
56
+ def checkMongoDB (): Unit = {
57
+ if (! TestMongoClientHelper .isMongoDBOnline) {
58
+ cancel(" No Available Database" )
59
+ }
80
60
}
81
61
82
- def checkMongoDB () {
83
- if (! mongoDBOnline) {
84
- cancel(" No Available Database" )
62
+ def withTempClient (mongoClientSettings : MongoClientSettings , testCode : MongoClient => Any ): Unit = {
63
+ val client = MongoClient (mongoClientSettings)
64
+ try {
65
+ testCode(client)
66
+ } finally {
67
+ client.close()
85
68
}
86
69
}
87
70
88
71
def withClient (testCode : MongoClient => Any ): Unit = {
89
72
checkMongoDB()
90
- val client = mongoClient()
91
- try testCode(client) // loan the client
92
- finally {
93
- client.close()
94
- }
73
+ testCode(TestMongoClientHelper .mongoClient) // loan the client
95
74
}
96
75
97
- def withDatabase (dbName : String )(testCode : MongoDatabase => Any ) {
76
+ def withDatabase (dbName : String )(testCode : MongoDatabase => Any ): Unit = {
98
77
withClient { client =>
99
78
val databaseName = if (dbName.startsWith(DB_PREFIX )) dbName.take(63 ) else s " $DB_PREFIX$dbName" .take(63 ) // scalastyle:ignore
100
79
val mongoDatabase = client.getDatabase(databaseName)
@@ -108,7 +87,7 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
108
87
109
88
def withDatabase (testCode : MongoDatabase => Any ): Unit = withDatabase(databaseName)(testCode : MongoDatabase => Any )
110
89
111
- def withCollection (testCode : MongoCollection [Document ] => Any ) {
90
+ def withCollection (testCode : MongoCollection [Document ] => Any ): Unit = {
112
91
withDatabase(databaseName) { mongoDatabase =>
113
92
val mongoCollection = mongoDatabase.getCollection(collectionName)
114
93
try testCode(mongoCollection) // "loan" the fixture to the test
@@ -119,19 +98,25 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
119
98
}
120
99
}
121
100
122
- lazy val isSharded : Boolean = if (! mongoDBOnline ) {
101
+ lazy val isSharded : Boolean = if (! TestMongoClientHelper .isMongoDBOnline ) {
123
102
false
124
103
} else {
125
104
Await
126
- .result(mongoClient().getDatabase(" admin" ).runCommand(Document (" isMaster" -> 1 )).toFuture(), WAIT_DURATION )
105
+ .result(
106
+ mongoClient().getDatabase(" admin" ).runCommand(Document (" isMaster" -> 1 )).toFuture(),
107
+ WAIT_DURATION
108
+ )
127
109
.getOrElse(" msg" , BsonString (" " ))
128
110
.asString()
129
111
.getValue == " isdbgrid"
130
112
}
131
113
132
114
lazy val buildInfo : Document = {
133
- if (mongoDBOnline) {
134
- Await .result(mongoClient().getDatabase(" admin" ).runCommand(Document (" buildInfo" -> 1 )).toFuture(), WAIT_DURATION )
115
+ if (TestMongoClientHelper .isMongoDBOnline) {
116
+ Await .result(
117
+ mongoClient().getDatabase(" admin" ).runCommand(Document (" buildInfo" -> 1 )).toFuture(),
118
+ WAIT_DURATION
119
+ )
135
120
} else {
136
121
Document ()
137
122
}
@@ -158,26 +143,14 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
158
143
}
159
144
160
145
override def beforeAll () {
161
- if (mongoDBOnline) {
162
- val client = mongoClient()
163
- Await .result(client.getDatabase(databaseName).drop().toFuture(), WAIT_DURATION )
164
- client.close()
146
+ if (TestMongoClientHelper .isMongoDBOnline) {
147
+ Await .result(TestMongoClientHelper .mongoClient.getDatabase(databaseName).drop().toFuture(), WAIT_DURATION )
165
148
}
166
149
}
167
150
168
151
override def afterAll () {
169
- if (mongoDBOnline) {
170
- val client = mongoClient()
171
- Await .result(client.getDatabase(databaseName).drop().toFuture(), WAIT_DURATION )
172
- client.close()
173
- }
174
- }
175
-
176
- Runtime .getRuntime.addShutdownHook(new ShutdownHook ())
177
-
178
- private [mongodb] class ShutdownHook extends Thread {
179
- override def run () {
180
- mongoClient().getDatabase(databaseName).drop()
152
+ if (TestMongoClientHelper .isMongoDBOnline) {
153
+ Await .result(TestMongoClientHelper .mongoClient.getDatabase(databaseName).drop().toFuture(), WAIT_DURATION )
181
154
}
182
155
}
183
156
0 commit comments