PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/etorreborre/casbah
Scala | 192 lines | 111 code | 43 blank | 38 comment | 0 complexity | ec73b0519dd08eafc93749bf5aef1380 MD5 | raw file
  1. /**
  2. * Copyright (c) 2010 10gen, Inc. <http://10gen.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah.commons
  23. package conversions
  24. import com.mongodb.casbah.Imports._
  25. import com.mongodb.casbah.commons.conversions.scala._
  26. import org.scala_tools.time.Imports._
  27. import org.specs._
  28. import org.specs.specification.PendingUntilFixed
  29. class ConversionsSpec extends Specification with PendingUntilFixed {
  30. type JDKDate = java.util.Date
  31. def clearConversions = beforeContext {
  32. DeregisterConversionHelpers()
  33. DeregisterJodaTimeConversionHelpers()
  34. }
  35. "Casbah's Conversion Helpers" ->- (clearConversions) should {
  36. shareVariables
  37. implicit val mongoDB = MongoConnection()("casbahTest")
  38. mongoDB.dropDatabase()
  39. "Properly save Option[_] to MongoDB" in {
  40. mongoDB must notBeNull
  41. val mongo = mongoDB("optionSerialization")
  42. mongo.dropCollection()
  43. mongo += MongoDBObject("some" -> Some("foo"), "none" -> None)
  44. val optDoc = mongo.findOne().getOrElse(
  45. throw new IllegalArgumentException("No document"))
  46. optDoc.getAs[String]("some") must beSome("foo")
  47. optDoc.getAs[String]("none") must beNone
  48. }
  49. val jodaDate: DateTime = DateTime.now
  50. val jdkDate: JDKDate = new JDKDate(jodaDate.getMillis)
  51. jodaDate.getMillis must beEqualTo(jdkDate.getTime)
  52. "Fail to serialize Joda DateTime Objects unless explicitly loaded." in {
  53. mongoDB must notBeNull
  54. val mongo = mongoDB("dateFail")
  55. mongo.dropCollection()
  56. lazy val saveDate = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  57. saveDate must throwA[IllegalArgumentException]
  58. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  59. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  60. MongoDBObject("date" -> 1))
  61. jdkEntry.get
  62. jdkEntry must beSomething
  63. jdkEntry.get must notBeNull
  64. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  65. }
  66. "Successfully serialize & deserialize Joda DateTime Objects when convertors are loaded." in {
  67. mongoDB must notBeNull
  68. val mongo = mongoDB("jodaSerDeser")
  69. mongo.dropCollection()
  70. RegisterConversionHelpers()
  71. RegisterJodaTimeConversionHelpers()
  72. // TODO - Matcher verification of these being loaded
  73. mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  74. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "joda"),
  75. MongoDBObject("date" -> 1))
  76. jodaEntry must beSomething
  77. jodaEntry.get must notBeNull
  78. //jodaEntry.get.get("date") must beSome[DateTime]
  79. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  80. // Casting it as something it isn't will fail
  81. lazy val getDate = { jodaEntry.get.getAs[JDKDate]("date") }
  82. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  83. getDate.get must throwA[ClassCastException]
  84. }
  85. "Be successfully deregistered." in {
  86. mongoDB must notBeNull
  87. val mongo = mongoDB("conversionDeReg")
  88. mongo.dropCollection()
  89. RegisterConversionHelpers()
  90. RegisterJodaTimeConversionHelpers()
  91. DeregisterConversionHelpers()
  92. DeregisterJodaTimeConversionHelpers()
  93. lazy val testJodaInsert = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  94. testJodaInsert must throwA[IllegalArgumentException]
  95. // Normal JDK Date should work
  96. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  97. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  98. MongoDBObject("date" -> 1))
  99. jdkEntry must beSomething
  100. jdkEntry.get must notBeNull
  101. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  102. // Casting it as something it isn't will fail
  103. lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  104. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  105. getDate.get must throwA[ClassCastException]
  106. }
  107. "Inserting a JDKDate should still allow retrieval as JodaTime after Conversions load" in {
  108. mongoDB must notBeNull
  109. val mongo = mongoDB("conversionConversion")
  110. mongo.dropCollection()
  111. RegisterConversionHelpers()
  112. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  113. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  114. MongoDBObject("date" -> 1))
  115. jdkEntry must beSomething
  116. jdkEntry.get must notBeNull
  117. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  118. // Casting it as something it isn't will fail
  119. lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  120. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  121. getDate.get must throwA[ClassCastException]
  122. RegisterJodaTimeConversionHelpers()
  123. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  124. MongoDBObject("date" -> 1))
  125. jodaEntry must beSomething
  126. jodaEntry.get must notBeNull
  127. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  128. // Casting it as something it isn't will fail
  129. lazy val getConvertedDate = { jodaEntry.get.getAs[JDKDate]("date") }
  130. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  131. getConvertedDate.get must throwA[ClassCastException]
  132. }
  133. "toString-ing a JODA Date with JODA Conversions loaded doesn't choke horribly." in {
  134. RegisterConversionHelpers()
  135. val jodaEntry: DBObject = MongoDBObject("type" -> "jdk",
  136. "date" -> jdkDate)
  137. jodaEntry must notBeNull
  138. /*jodaEntry.getAs[DateTime]("date") must beSome(jdkDate)
  139. // Casting it as something it isn't will fail
  140. lazy val getDate = { jodaEntry.getAs[JDKDate]("date") }
  141. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  142. getDate.get must throwA[ClassCastException] */
  143. RegisterJodaTimeConversionHelpers()
  144. val json = jodaEntry.toString
  145. json must notBeNull
  146. }
  147. }
  148. }
  149. // vim: set ts=2 sw=2 sts=2 et: