PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/ymasory/scala-corpus
Scala | 180 lines | 101 code | 41 blank | 38 comment | 0 complexity | 842c307c92563e50fc9a55ad109616f1 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, AGPL-3.0, BSD-3-Clause, MIT
  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. val jodaDate: DateTime = DateTime.now
  40. val jdkDate: JDKDate = new JDKDate(jodaDate.getMillis)
  41. jodaDate.getMillis must beEqualTo(jdkDate.getTime)
  42. "Fail to serialize Joda DateTime Objects unless explicitly loaded." in {
  43. mongoDB must notBeNull
  44. val mongo = mongoDB("dateFail")
  45. mongo.dropCollection()
  46. lazy val saveDate = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  47. saveDate must throwA[IllegalArgumentException]
  48. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  49. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  50. MongoDBObject("date" -> 1))
  51. jdkEntry.get
  52. jdkEntry must beSomething
  53. jdkEntry.get must notBeNull
  54. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  55. }
  56. "Successfully serialize & deserialize Joda DateTime Objects when convertors are loaded." in {
  57. mongoDB must notBeNull
  58. val mongo = mongoDB("jodaSerDeser")
  59. mongo.dropCollection()
  60. RegisterConversionHelpers()
  61. RegisterJodaTimeConversionHelpers()
  62. // TODO - Matcher verification of these being loaded
  63. mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  64. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "joda"),
  65. MongoDBObject("date" -> 1))
  66. jodaEntry must beSomething
  67. jodaEntry.get must notBeNull
  68. //jodaEntry.get.get("date") must beSome[DateTime]
  69. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  70. // Casting it as something it isn't will fail
  71. lazy val getDate = { jodaEntry.get.getAs[JDKDate]("date") }
  72. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  73. getDate.get must throwA[ClassCastException]
  74. }
  75. "Be successfully deregistered." in {
  76. mongoDB must notBeNull
  77. val mongo = mongoDB("conversionDeReg")
  78. mongo.dropCollection()
  79. RegisterConversionHelpers()
  80. RegisterJodaTimeConversionHelpers()
  81. DeregisterConversionHelpers()
  82. DeregisterJodaTimeConversionHelpers()
  83. lazy val testJodaInsert = { mongo += MongoDBObject("date" -> jodaDate, "type" -> "joda") }
  84. testJodaInsert must throwA[IllegalArgumentException]
  85. // Normal JDK Date should work
  86. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  87. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  88. MongoDBObject("date" -> 1))
  89. jdkEntry must beSomething
  90. jdkEntry.get must notBeNull
  91. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  92. // Casting it as something it isn't will fail
  93. lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  94. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  95. getDate.get must throwA[ClassCastException]
  96. }
  97. "Inserting a JDKDate should still allow retrieval as JodaTime after Conversions load" in {
  98. mongoDB must notBeNull
  99. val mongo = mongoDB("conversionConversion")
  100. mongo.dropCollection()
  101. RegisterConversionHelpers()
  102. mongo += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  103. val jdkEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  104. MongoDBObject("date" -> 1))
  105. jdkEntry must beSomething
  106. jdkEntry.get must notBeNull
  107. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  108. // Casting it as something it isn't will fail
  109. lazy val getDate = { jdkEntry.get.getAs[DateTime]("date") }
  110. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  111. getDate.get must throwA[ClassCastException]
  112. RegisterJodaTimeConversionHelpers()
  113. val jodaEntry = mongo.findOne(MongoDBObject("type" -> "jdk"),
  114. MongoDBObject("date" -> 1))
  115. jodaEntry must beSomething
  116. jodaEntry.get must notBeNull
  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 must notBeNull
  128. /*jodaEntry.getAs[DateTime]("date") must beSome(jdkDate)
  129. // Casting it as something it isn't will fail
  130. lazy val getDate = { jodaEntry.getAs[JDKDate]("date") }
  131. // Note - exceptions are wrapped by Some() and won't be thrown until you .get
  132. getDate.get must throwA[ClassCastException] */
  133. RegisterJodaTimeConversionHelpers()
  134. val json = jodaEntry.toString
  135. json must notBeNull
  136. }
  137. }
  138. }
  139. // vim: set ts=2 sw=2 sts=2 et: