PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/casbah
Scala | 328 lines | 231 code | 64 blank | 33 comment | 2 complexity | dd67ad461dbd5a1271d3b5a6a32fefaa MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.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.test.core
  23. import scala.collection.mutable
  24. import scala.collection.JavaConverters._
  25. import org.bson.codecs.configuration.CodecConfigurationException
  26. import com.mongodb.casbah.Imports._
  27. import com.mongodb.casbah.commons.conversions.scala._
  28. import com.github.nscala_time.time.Imports._
  29. import org.specs2.specification.{ BeforeEach, BeforeExample }
  30. class ConversionsSpec extends CasbahDBTestSpecification with BeforeEach {
  31. sequential
  32. type JDKDate = java.util.Date
  33. def before = {
  34. DeregisterConversionHelpers()
  35. DeregisterJodaTimeConversionHelpers()
  36. DeregisterJodaLocalDateTimeConversionHelpers()
  37. }
  38. "Casbah's Conversion Helpers" should {
  39. "Properly save Option[_] to MongoDB" in {
  40. collection.dropCollection()
  41. collection += MongoDBObject("some" -> Some("foo"), "none" -> None)
  42. val optDoc = collection.findOne().getOrElse(
  43. throw new IllegalArgumentException("No document")
  44. )
  45. optDoc.getAs[String]("some") must beSome("foo")
  46. optDoc.getAs[String]("none") must beNone
  47. }
  48. val jodaDate: DateTime = DateTime.now
  49. val localJodaDate: LocalDateTime = jodaDate.toLocalDateTime
  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. collection.dropCollection()
  54. lazy val saveDate = {
  55. collection += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  56. }
  57. saveDate must throwA[CodecConfigurationException]
  58. collection += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  59. val jdkEntry = collection.findOne(
  60. MongoDBObject("type" -> "jdk"),
  61. MongoDBObject("date" -> 1)
  62. )
  63. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  64. }
  65. "Successfully serialize & deserialize Joda DateTime Objects when DateTime convertors are loaded." in {
  66. collection.dropCollection()
  67. RegisterConversionHelpers()
  68. RegisterJodaTimeConversionHelpers()
  69. // TODO - Matcher verification of these being loaded
  70. collection += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  71. val jodaEntry = collection.findOne(
  72. MongoDBObject("type" -> "joda"),
  73. MongoDBObject("date" -> 1)
  74. )
  75. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  76. // Casting it as something it isn't will fail
  77. jodaEntry.get.getAs[JDKDate]("date") must beNone
  78. }
  79. "Successfully serialize & deserialize Joda Local/DateTime Objects when DateTime convertors are loaded." in {
  80. collection.dropCollection()
  81. RegisterConversionHelpers()
  82. RegisterJodaTimeConversionHelpers()
  83. // TODO - Matcher verification of these being loaded
  84. collection += MongoDBObject("date" -> localJodaDate, "type" -> "joda")
  85. val jodaEntry = collection.findOne(
  86. MongoDBObject("type" -> "joda"),
  87. MongoDBObject("date" -> 1)
  88. )
  89. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  90. // Casting it as something it isn't will fail
  91. jodaEntry.get.getAs[JDKDate]("date") must beNone
  92. }
  93. "Successfully serialize & deserialize Joda LocalDateTime Objects when LocalDateTime convertors are loaded." in {
  94. collection.dropCollection()
  95. RegisterConversionHelpers()
  96. RegisterJodaLocalDateTimeConversionHelpers()
  97. // TODO - Matcher verification of these being loaded
  98. collection += MongoDBObject("date" -> localJodaDate, "type" -> "joda")
  99. val jodaEntry = collection.findOne(
  100. MongoDBObject("type" -> "joda"),
  101. MongoDBObject("date" -> 1)
  102. )
  103. jodaEntry.get.getAs[LocalDateTime]("date") must beSome(localJodaDate)
  104. // Casting it as something it isn't will fail
  105. jodaEntry.get.getAs[JDKDate]("date") must beNone
  106. }
  107. "Successfully serialize & deserialize Joda Local/DateTime Objects when LocalDateTime convertors are loaded." in {
  108. collection.dropCollection()
  109. RegisterConversionHelpers()
  110. RegisterJodaLocalDateTimeConversionHelpers()
  111. // TODO - Matcher verification of these being loaded
  112. collection += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  113. val jodaEntry = collection.findOne(
  114. MongoDBObject("type" -> "joda"),
  115. MongoDBObject("date" -> 1)
  116. )
  117. jodaEntry.get.getAs[LocalDateTime]("date") must beSome(localJodaDate)
  118. // Casting it as something it isn't will fail
  119. jodaEntry.get.getAs[JDKDate]("date") must beNone
  120. }
  121. "Be successfully deregistered." in {
  122. collection.dropCollection()
  123. RegisterConversionHelpers()
  124. RegisterJodaTimeConversionHelpers()
  125. RegisterJodaLocalDateTimeConversionHelpers()
  126. DeregisterConversionHelpers()
  127. DeregisterJodaTimeConversionHelpers()
  128. DeregisterJodaLocalDateTimeConversionHelpers()
  129. lazy val testJodaInsert = {
  130. collection += MongoDBObject("date" -> jodaDate, "type" -> "joda")
  131. }
  132. testJodaInsert must throwA[CodecConfigurationException]
  133. // Normal JDK Date should work
  134. collection += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  135. val jdkEntry = collection.findOne(
  136. MongoDBObject("type" -> "jdk"),
  137. MongoDBObject("date" -> 1)
  138. )
  139. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  140. // Casting it as something it isn't will fail
  141. jdkEntry.get.getAs[DateTime]("date") must beNone
  142. }
  143. "Inserting a JDKDate should still allow retrieval as JodaTime after Conversions load" in {
  144. collection.dropCollection()
  145. RegisterConversionHelpers()
  146. collection += MongoDBObject("date" -> jdkDate, "type" -> "jdk")
  147. val jdkEntry = collection.findOne(
  148. MongoDBObject("type" -> "jdk"),
  149. MongoDBObject("date" -> 1)
  150. )
  151. jdkEntry must beSome
  152. jdkEntry.get.getAs[JDKDate]("date") must beSome(jdkDate)
  153. // Casting it as something it isn't will fail
  154. jdkEntry.get.getAs[DateTime]("date") must beNone
  155. RegisterJodaTimeConversionHelpers()
  156. val jodaEntry = collection.findOne(
  157. MongoDBObject("type" -> "jdk"),
  158. MongoDBObject("date" -> 1)
  159. )
  160. jodaEntry must beSome
  161. jodaEntry.get.getAs[DateTime]("date") must beSome(jodaDate)
  162. // Casting it as something it isn't will fail
  163. jodaEntry.get.getAs[JDKDate]("date") must beNone
  164. }
  165. "toString-ing a JODA Date with JODA Conversions loaded doesn't choke horribly." in {
  166. RegisterConversionHelpers()
  167. val jodaEntry: DBObject = MongoDBObject(
  168. "type" -> "jdk",
  169. "date" -> jdkDate
  170. )
  171. RegisterJodaTimeConversionHelpers()
  172. val json = jodaEntry.toString
  173. json must not beNull
  174. }
  175. "Handle scala collection types correctly" in {
  176. "Allow Maps to convert correctly" in {
  177. collection.dropCollection()
  178. collection += Map("a" -> "b", "c" -> "d")
  179. collection.findOne().get must haveEntry("a" -> "b")
  180. }
  181. "Allow mutable Maps to convert correctly" in {
  182. collection.dropCollection()
  183. collection += mutable.Map("a" -> "b", "c" -> "d")
  184. collection.findOne().get must haveEntry("a" -> "b")
  185. }
  186. "Allow Sets to convert correctly" in {
  187. collection.dropCollection()
  188. collection += MongoDBObject("a" -> Set("a", "b", "c"))
  189. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  190. }
  191. "Allow mutable Sets to convert correctly" in {
  192. collection.dropCollection()
  193. collection += MongoDBObject("a" -> mutable.Set("a", "b", "c"))
  194. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  195. }
  196. "Allow Seqs to convert correctly" in {
  197. collection.dropCollection()
  198. collection += MongoDBObject("a" -> mutable.Seq("a", "b", "c"))
  199. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  200. }
  201. "Allow mutable Seqs to convert correctly" in {
  202. collection.dropCollection()
  203. collection += MongoDBObject("a" -> mutable.Seq("a", "b", "c"))
  204. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  205. }
  206. "Allow mutable Buffers to convert correctly" in {
  207. collection.dropCollection()
  208. collection += MongoDBObject("a" -> mutable.Buffer("a", "b", "c"))
  209. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  210. }
  211. "Allow Tuples (Products) to convert correctly" in {
  212. collection.dropCollection()
  213. collection += MongoDBObject("a" -> ("a", "b", "c"))
  214. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  215. }
  216. "Allow Lists to convert correctly" in {
  217. collection.dropCollection()
  218. collection += MongoDBObject("a" -> List("a", "b", "c"))
  219. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  220. }
  221. "Allow converted Lists to convert correctly" in {
  222. collection.dropCollection()
  223. collection += MongoDBObject("a" -> List("a", "b", "c").asJava)
  224. collection.findOne().get.as[MongoDBList]("a") must containTheSameElementsAs(List("a", "b", "c"))
  225. }
  226. }
  227. }
  228. "Casbah and Java Driver custom type encoding" should {
  229. val encoder = new com.mongodb.DefaultDBEncoder()
  230. def encode(doc: DBObject): Long = {
  231. val start = System.currentTimeMillis()
  232. val encoded = encoder.encode(doc)
  233. val end = System.currentTimeMillis()
  234. end - start
  235. }
  236. DeregisterConversionHelpers()
  237. DeregisterJodaTimeConversionHelpers()
  238. "Produce viable performance numbers to test off of " >> {
  239. "Encoding DateTimes without any custom encoders registered " in {
  240. var total = 0.0
  241. val x = 10000
  242. for (n <- 1 to x) {
  243. val doc = MongoDBObject("date1" -> new JDKDate, "date2" -> new JDKDate, "foo" -> "bar", "x" -> 5.2)
  244. total += encode(doc)
  245. }
  246. val avg = total / x
  247. log.error("[Basic] Average encoding time over %s tries: %f [%s]", x, avg, total)
  248. avg must beGreaterThan(0.0)
  249. }
  250. "Encoding Joda DateTimes with custom encoders registered " in {
  251. RegisterJodaTimeConversionHelpers()
  252. var total = 0.0
  253. val x = 10000
  254. for (n <- 1 to x) {
  255. val doc = MongoDBObject("date1" -> DateTime.now, "date2" -> DateTime.now, "foo" -> "bar", "x" -> 5.2)
  256. total += encode(doc)
  257. }
  258. val avg = total / x
  259. log.error("[Custom Types] Average encoding time over %s tries: %f [%s]", x, avg, total)
  260. avg must beGreaterThan(0.0)
  261. }
  262. }
  263. }
  264. }