/casbah-core/src/test/scala/CasbahDBTestSpecification.scala

http://github.com/mongodb/casbah · Scala · 133 lines · 89 code · 23 blank · 21 comment · 10 complexity · 4cc440660f43e169275ad7c3132f43ce MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package com.mongodb.casbah.test.core
  18. import scala.collection.JavaConverters._
  19. import scala.util.Properties
  20. import com.mongodb.casbah.Imports._
  21. import com.mongodb.casbah.commons.test.CasbahMutableSpecification
  22. import org.specs2.execute.{ AsResult, Result }
  23. import org.specs2.specification.Around
  24. trait CasbahDBTestSpecification extends CasbahMutableSpecification with Around {
  25. sequential
  26. val DEFAULT_URI: String = "mongodb://localhost:27017"
  27. val MONGODB_URI_SYSTEM_PROPERTY_NAME: String = "org.mongodb.test.uri"
  28. val TEST_DB = "mongo-casbah-driver-test"
  29. def around[R: AsResult](r: => R): Result = {
  30. initialSetUp()
  31. try AsResult(r)
  32. finally finalTeardown()
  33. }
  34. def initialSetUp(): Unit = {}
  35. def finalTeardown(): Unit = {
  36. if (mongoDbOnline) {
  37. database.dropDatabase()
  38. mongoClient.close()
  39. }
  40. }
  41. val databaseName: String = TEST_DB
  42. val className = getClass.getName
  43. val mongoClientURI = {
  44. val mongoURIString = Properties.propOrElse(MONGODB_URI_SYSTEM_PROPERTY_NAME, DEFAULT_URI)
  45. MongoClientURI(mongoURIString)
  46. }
  47. val mongoClient = MongoClient(mongoClientURI)
  48. lazy val database = mongoClient.getDB(databaseName)
  49. lazy val collection = database(className)
  50. val mongoDbOnline = {
  51. try {
  52. mongoClient.databaseNames()
  53. true
  54. } catch {
  55. case t: Throwable => false
  56. }
  57. }
  58. lazy val mongoDbIsOnline = mongoDbOnline
  59. lazy val versionArray = mongoClient.getDB("admin")
  60. .command("buildInfo")
  61. .getAs[MongoDBList]("versionArray")
  62. .get
  63. /**
  64. *
  65. * @param minVersion version Array
  66. * @return true if server is at least specified version
  67. */
  68. def serverIsAtLeastVersion(minVersion: Int*): Boolean = {
  69. var retVal = 0
  70. for ((v, i) <- minVersion.zipWithIndex) {
  71. if (retVal == 0) retVal = versionArray(i).asInstanceOf[Int].compareTo(v)
  72. }
  73. retVal >= 0
  74. }
  75. def enableMaxTimeFailPoint() {
  76. if (serverIsAtLeastVersion(2, 5)) {
  77. mongoClient.getDB("admin").command(
  78. MongoDBObject("configureFailPoint" -> "maxTimeAlwaysTimeOut", "mode" -> "alwaysOn"),
  79. ReadPreference.Primary
  80. )
  81. }
  82. }
  83. def disableMaxTimeFailPoint() {
  84. if (serverIsAtLeastVersion(2, 5)) {
  85. mongoClient.getDB("admin").command(
  86. MongoDBObject("configureFailPoint" -> "maxTimeAlwaysTimeOut", "mode" -> "off"),
  87. ReadPreference.Primary
  88. )
  89. }
  90. }
  91. lazy val isStandalone = !runReplicaSetStatusCommand
  92. lazy val isReplicaSet = runReplicaSetStatusCommand
  93. lazy val isSharded: Boolean = {
  94. val isMasterResult = mongoClient.getDB("admin").command(MongoDBObject("ismaster" -> 1))
  95. Option(isMasterResult.get("msg")) match {
  96. case Some("isdbgrid") => true
  97. case _ => false
  98. }
  99. }
  100. def runReplicaSetStatusCommand: Boolean = {
  101. val result = mongoClient.getDB("admin").command(MongoDBObject("replSetGetStatus" -> 1))
  102. result.getErrorMessage match {
  103. case errorMsg if errorMsg != null && errorMsg.indexOf("--replSet") != -1 => false
  104. case _ => true
  105. }
  106. }
  107. lazy val getCommandLine = mongoClient.getDB("admin").command("getCmdLineOpts").asScala
  108. lazy val hasTestCommand: Boolean = getCommandLine("argv").asInstanceOf[BasicDBList] contains "enableTestCommands=1"
  109. skipAllUnless(mongoDbOnline)
  110. }