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

https://github.com/derzzle/casbah · Scala · 179 lines · 53 code · 32 blank · 94 comment · 0 complexity · b0dbe8b3971009f017ad871c741ed7fc 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 com.mongodb.casbah.commons.test.CasbahSpecification
  28. import org.specs2.specification.BeforeExample
  29. import org.bson.BSON
  30. class ConversionsSpec extends CasbahSpecification with BeforeExample {
  31. type JDKDate = java.util.Date
  32. def before = {
  33. DeregisterConversionHelpers()
  34. DeregisterJodaTimeConversionHelpers()
  35. }
  36. "Casbah's Conversion Helpers" should {
  37. implicit val mongoDB = MongoConnection()("casbahTest")
  38. mongoDB.dropDatabase()
  39. "Properly save Option[_] to MongoDB" in {
  40. val mongo = mongoDB("optionSerialization")
  41. mongo.dropCollection()
  42. mongo += MongoDBObject("some" -> Some("foo"), "none" -> None)
  43. val optDoc = mongo.findOne().getOrElse(
  44. throw new IllegalArgumentException("No document"))
  45. optDoc.getAs[String]("some") must beSome("foo")
  46. optDoc.getAs[String]("none") must beNone
  47. }
  48. val jodaDate: DateTime = DateTime.now
  49. val jdkDate: JDKDate = new JDKDate(jodaDate.getMillis)
  50. jodaDate.getMillis must beEqualTo(jdkDate.getTime)
  51. /* "Fail to serialize Joda DateTime Objects unless explicitly loaded." in {
  52. val mongo = mongoDB("dateFail")
  53. mongo.dropCollection()
  54. lazy val saveDate = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  55. log.info("Save Date: %s", saveDate)
  56. saveDate must throwA[Exception]
  57. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  58. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  59. MongoDBObject("date" -> 1))
  60. jdkEntry must beSome
  61. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  62. }*/
  63. /* "Successfully serialize & deserialize Joda DateTime Objects when convertors are loaded." in {
  64. val mongo = mongoDB("jodaSerDeser")
  65. mongo.dropCollection()
  66. RegisterConversionHelpers()
  67. RegisterJodaTimeConversionHelpers()
  68. // TODO - Matcher verification of these being loaded
  69. mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  70. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "joda"),
  71. MongoDBObject("date" -> 1))
  72. jodaEntry must beSome
  73. //jodaEntry.get.get("date") must beSome[DateTime]
  74. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  75. // Casting it as something it isn't will fail
  76. lazy val getDate = { jodaEntry.get.getAs[JDKDate]("date") }
  77. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  78. getDate.get must throwA[ClassCastException]
  79. }*/
  80. //"Be successfully deregistered." in {
  81. //val mongo = mongoDB("conversionDeReg")
  82. //mongo.dropCollection()
  83. //RegisterConversionHelpers()
  84. //RegisterJodaTimeConversionHelpers()
  85. //DeregisterConversionHelpers()
  86. //DeregisterJodaTimeConversionHelpers()
  87. //lazy val testJodaInsert = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  88. //testJodaInsert must throwA[IllegalArgumentException]
  89. //// Normal JDK Date should work
  90. //mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  91. //val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  92. //MongoDBObject("date" -> 1))
  93. //jdkEntry must beSome
  94. //jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  95. //// Casting it as something it isn't will fail
  96. //lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  97. //// Note - exceptions are wrapped by Some() and won't be thrown until you .get
  98. //getDate.get must throwA[ClassCastException]
  99. //}
  100. "Inserting a JDKDate should still allow retrieval as JodaTime after Conversions load" in {
  101. val mongo = mongoDB("conversionConversion")
  102. mongo.dropCollection()
  103. RegisterConversionHelpers()
  104. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  105. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  106. MongoDBObject("date" -> 1))
  107. jdkEntry must beSome
  108. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  109. // Casting it as something it isn't will fail
  110. lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  111. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  112. getDate.get must throwA[ClassCastException]
  113. RegisterJodaTimeConversionHelpers()
  114. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  115. MongoDBObject("date" -> 1))
  116. jodaEntry must beSome
  117. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  118. // Casting it as something it isn't will fail
  119. lazy val getConvertedDate = { jodaEntry.get.getAs[JDKDate]("date") }
  120. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  121. getConvertedDate.get must throwA[ClassCastException]
  122. }
  123. /* "toString-ing a JODA Date with JODA Conversions loaded doesn't choke horribly." in {
  124. RegisterConversionHelpers()
  125. val jodaEntry: DBObject = MongoDBObject("type" -> "jdk",
  126. "date" -> jdkDate)
  127. /*jodaEntry.getAs[DateTime]("date") must beSome(jdkDate)
  128. // Casting it as something it isn't will fail
  129. lazy val getDate = { jodaEntry.getAs[JDKDate]("date") }
  130. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  131. getDate.get must throwA[ClassCastException] */
  132. RegisterJodaTimeConversionHelpers()
  133. val json = jodaEntry.toString
  134. }*/
  135. }
  136. }
  137. // vim: set ts=2 sw=2 sts=2 et: