1616
1717package org .mongodb .scala
1818
19- import com .mongodb .ClusterFixture .getServerApi
2019import com .mongodb .connection .ServerVersion
2120import org .mongodb .scala .bson .BsonString
2221import org .scalatest ._
2322
2423import scala .collection .JavaConverters ._
2524import scala .concurrent .duration .{ Duration , _ }
2625import scala .concurrent .{ Await , ExecutionContext }
27- import scala .util .{ Properties , Try }
2826
2927trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
3028
3129 implicit val ec : ExecutionContext = ExecutionContext .Implicits .global
3230
33- private val DEFAULT_URI : String = " mongodb://localhost:27017/"
34- private val MONGODB_URI_SYSTEM_PROPERTY_NAME : String = " org.mongodb.test.uri"
3531 val WAIT_DURATION : Duration = 60 .seconds
3632 private val DB_PREFIX = " mongo-scala-"
3733 private var _currentTestName : Option [String ] = None
38- private var mongoDBOnline : Boolean = false
3934
4035 protected override def runTest (testName : String , args : Args ): Status = {
4136 _currentTestName = Some (testName.split(" should" )(1 ))
42- mongoDBOnline = isMongoDBOnline()
4337 super .runTest(testName, args)
4438 }
4539
@@ -53,48 +47,33 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
5347 */
5448 def collectionName : String = _currentTestName.getOrElse(suiteName).filter(_.isLetterOrDigit)
5549
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
6151
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
6953
70- val mongoClientSettings : MongoClientSettings = mongoClientSettingsBuilder.build()
54+ def mongoClient () : MongoClient = TestMongoClientHelper .mongoClient
7155
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+ }
8060 }
8161
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()
8568 }
8669 }
8770
8871 def withClient (testCode : MongoClient => Any ): Unit = {
8972 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
9574 }
9675
97- def withDatabase (dbName : String )(testCode : MongoDatabase => Any ) {
76+ def withDatabase (dbName : String )(testCode : MongoDatabase => Any ): Unit = {
9877 withClient { client =>
9978 val databaseName = if (dbName.startsWith(DB_PREFIX )) dbName.take(63 ) else s " $DB_PREFIX$dbName" .take(63 ) // scalastyle:ignore
10079 val mongoDatabase = client.getDatabase(databaseName)
@@ -108,7 +87,7 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
10887
10988 def withDatabase (testCode : MongoDatabase => Any ): Unit = withDatabase(databaseName)(testCode : MongoDatabase => Any )
11089
111- def withCollection (testCode : MongoCollection [Document ] => Any ) {
90+ def withCollection (testCode : MongoCollection [Document ] => Any ): Unit = {
11291 withDatabase(databaseName) { mongoDatabase =>
11392 val mongoCollection = mongoDatabase.getCollection(collectionName)
11493 try testCode(mongoCollection) // "loan" the fixture to the test
@@ -119,19 +98,25 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
11998 }
12099 }
121100
122- lazy val isSharded : Boolean = if (! mongoDBOnline ) {
101+ lazy val isSharded : Boolean = if (! TestMongoClientHelper .isMongoDBOnline ) {
123102 false
124103 } else {
125104 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+ )
127109 .getOrElse(" msg" , BsonString (" " ))
128110 .asString()
129111 .getValue == " isdbgrid"
130112 }
131113
132114 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+ )
135120 } else {
136121 Document ()
137122 }
@@ -158,26 +143,14 @@ trait RequiresMongoDBISpec extends BaseSpec with BeforeAndAfterAll {
158143 }
159144
160145 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 )
165148 }
166149 }
167150
168151 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 )
181154 }
182155 }
183156
0 commit comments