Skip to content
This repository
Browse code

Merge pull request #1277 from lift/tcn_issue_1277

Add configurable ObjectId tester in JObjectParser
  • Loading branch information...
commit 85b8293a76279365388ec11b5b8216be6c2f4f6a 2 parents 238de8a + bec9a49
David Pollak authored June 13, 2012
57  persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/MongoRecordSpec.scala
@@ -658,17 +658,34 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
658 658
         .optionalStringField("optional string")
659 659
         .save
660 660
 
661  
-      fttr.mandatoryBooleanField(false)
  661
+      fttr.mandatoryBooleanField(true)
  662
+      fttr.mandatoryBooleanField.dirty_? must_== true
  663
+
662 664
       fttr.mandatoryDecimalField(BigDecimal("3.14"))
  665
+      fttr.mandatoryDecimalField.dirty_? must_== true
  666
+
663 667
       fttr.mandatoryDoubleField(1999)
664  
-      fttr.mandatoryEnumField(MyTestEnum.ONE)
  668
+      fttr.mandatoryDoubleField.dirty_? must_== true
  669
+
  670
+      fttr.mandatoryEnumField(MyTestEnum.TWO)
  671
+      fttr.mandatoryEnumField.dirty_? must_== true
  672
+
665 673
       fttr.mandatoryIntField(99)
  674
+      fttr.mandatoryIntField.dirty_? must_== true
  675
+
666 676
       fttr.mandatoryLongField(100L)
  677
+      fttr.mandatoryLongField.dirty_? must_== true
  678
+
667 679
       fttr.mandatoryStringField("string")
  680
+      fttr.mandatoryStringField.dirty_? must_== true
  681
+
668 682
       fttr.optionalStringField(Empty)
  683
+      fttr.optionalStringField.dirty_? must_== true
  684
+
669 685
       fttr.legacyOptionalStringField(Empty)
  686
+      fttr.legacyOptionalStringField.dirty_? must_== true
670 687
 
671  
-      fttr.dirtyFields.length must_== 9
  688
+      fttr.dirtyFields.length must_== 7
672 689
       fttr.update
673 690
       fttr.dirtyFields.length must_== 0
674 691
 
@@ -682,7 +699,10 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
682 699
       val fttr2 = FieldTypeTestRecord.createRecord.save
683 700
 
684 701
       fttr2.legacyOptionalStringField("legacy optional string")
  702
+      fttr2.legacyOptionalStringField.dirty_? must_== true
  703
+
685 704
       fttr2.optionalStringField("optional string")
  705
+      fttr2.optionalStringField.dirty_? must_== true
686 706
 
687 707
       fttr2.dirtyFields.length must_== 2
688 708
       fttr2.update
@@ -704,13 +724,28 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
704 724
         .legacyOptionalObjectIdField(ObjectId.get)
705 725
         .save
706 726
 
  727
+      Thread.sleep(100) // sleep so dates will be different
  728
+
707 729
       mfttr.mandatoryDateField(new Date)
  730
+      mfttr.mandatoryDateField.dirty_? must_== true
  731
+
708 732
       mfttr.mandatoryJsonObjectField(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")))
  733
+      mfttr.mandatoryJsonObjectField.dirty_? must_== true
  734
+
709 735
       mfttr.mandatoryObjectIdField(ObjectId.get)
  736
+      mfttr.mandatoryObjectIdField.dirty_? must_== true
  737
+
710 738
       mfttr.mandatoryPatternField(Pattern.compile("^Mon", Pattern.CASE_INSENSITIVE))
  739
+      mfttr.mandatoryPatternField.dirty_? must_== true
  740
+
711 741
       mfttr.mandatoryUUIDField(UUID.randomUUID)
  742
+      mfttr.mandatoryUUIDField.dirty_? must_== true
  743
+
712 744
       mfttr.legacyOptionalDateField(Empty)
  745
+      mfttr.legacyOptionalDateField.dirty_? must_== true
  746
+
713 747
       mfttr.legacyOptionalObjectIdField(Empty)
  748
+      mfttr.legacyOptionalObjectIdField.dirty_? must_== true
714 749
 
715 750
       mfttr.dirtyFields.length must_== 7
716 751
       mfttr.update
@@ -726,7 +761,10 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
726 761
       val mfttr2 = MongoFieldTypeTestRecord.createRecord.save
727 762
 
728 763
       mfttr2.legacyOptionalDateField(new Date)
  764
+      mfttr2.legacyOptionalDateField.dirty_? must_== true
  765
+
729 766
       mfttr2.legacyOptionalObjectIdField(ObjectId.get)
  767
+      mfttr2.legacyOptionalObjectIdField.dirty_? must_== true
730 768
 
731 769
       mfttr2.dirtyFields.length must_== 2
732 770
       mfttr2.update
@@ -746,9 +784,16 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
746 784
       val ltr = ListTestRecord.createRecord.save
747 785
 
748 786
       ltr.mandatoryStringListField(List("abc", "def", "ghi"))
  787
+      ltr.mandatoryStringListField.dirty_? must_== true
  788
+
749 789
       ltr.mandatoryIntListField(List(4, 5, 6))
  790
+      ltr.mandatoryIntListField.dirty_? must_== true
  791
+
750 792
       ltr.mandatoryMongoJsonObjectListField(List(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")), TypeTestJsonObject(2, "jsonobj2", Map("x" -> "2"))))
  793
+      ltr.mandatoryMongoJsonObjectListField.dirty_? must_== true
  794
+
751 795
       ltr.mongoCaseClassListField(List(MongoCaseClassTestObject(1,"str")))
  796
+      ltr.mongoCaseClassListField.dirty_? must_== true
752 797
 
753 798
       ltr.dirtyFields.length must_== 4
754 799
       ltr.update
@@ -768,7 +813,10 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
768 813
       val mtr = MapTestRecord.save
769 814
 
770 815
       mtr.mandatoryStringMapField(Map("a" -> "abc", "b" -> "def", "c" -> "ghi"))
  816
+      mtr.mandatoryStringMapField.dirty_? must_== true
  817
+
771 818
       mtr.mandatoryIntMapField(Map("a" -> 4, "b" -> 5, "c" -> 6))
  819
+      mtr.mandatoryIntMapField.dirty_? must_== true
772 820
 
773 821
       mtr.dirtyFields.length must_== 2
774 822
       mtr.update
@@ -801,7 +849,10 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
801 849
       val sr2 = SubRecord.createRecord.name("SubRecord2")
802 850
 
803 851
       srtr.mandatoryBsonRecordField(sr1)
  852
+      srtr.mandatoryBsonRecordField.dirty_? must_== true
  853
+
804 854
       srtr.mandatoryBsonRecordListField(List(sr1,sr2))
  855
+      srtr.mandatoryBsonRecordListField.dirty_? must_== true
805 856
 
806 857
       srtr.dirtyFields.length must_== 2
807 858
       srtr.update
16  persistence/mongodb/src/main/scala/net/liftweb/mongodb/JObjectParser.scala
@@ -24,11 +24,22 @@ import java.util.regex.Pattern
24 24
 
25 25
 import net.liftweb.json._
26 26
 import net.liftweb.common.Box
  27
+import net.liftweb.util.SimpleInjector
27 28
 
28 29
 import com.mongodb.{BasicDBObject, BasicDBList, DBObject}
29 30
 import org.bson.types.ObjectId
30 31
 
31  
-object JObjectParser {
  32
+object JObjectParser extends SimpleInjector {
  33
+  /**
  34
+    * Set this to override JObjectParser turning strings that are valid
  35
+    * ObjectIds into actual ObjectIds. For example, place the following in Boot.boot:
  36
+    *
  37
+    * <code>JObjectParser.stringProcessor.default.set((s: String) => s)</code>
  38
+    */
  39
+  val stringProcessor = new Inject(() => (s: String) => {
  40
+    if (ObjectId.isValid(s)) new ObjectId(s)
  41
+    else s
  42
+  }) {}
32 43
 
33 44
   /*
34 45
   * Parse a JObject into a DBObject
@@ -113,8 +124,7 @@ object JObjectParser {
113 124
       case JNull => null
114 125
       case JNothing => error("can't render 'nothing'")
115 126
       case JString(null) => "null"
116  
-      case JString(s) if (ObjectId.isValid(s)) => new ObjectId(s)
117  
-      case JString(s) => s
  127
+      case JString(s) => stringProcessor.vend(s)
118 128
       case _ =>  ""
119 129
     }
120 130
 
59  persistence/mongodb/src/test/scala/net/liftweb/mongodb/JObjectParserSpec.scala
... ...
@@ -0,0 +1,59 @@
  1
+/*
  2
+ * Copyright 2012 WorldWide Conferencing, LLC
  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 net.liftweb
  18
+package mongodb
  19
+
  20
+import json._
  21
+import JsonDSL._
  22
+import util.Helpers._
  23
+
  24
+import org.bson.types.ObjectId
  25
+import org.specs.Specification
  26
+
  27
+import com.mongodb.DBObject
  28
+
  29
+object JObjectParserSpec extends Specification("JObjectParser Specification") {
  30
+
  31
+  def buildTestData: (ObjectId, DBObject) = {
  32
+    val oid = ObjectId.get
  33
+    val dbo = JObjectParser.parse(("x" -> oid.toString))(DefaultFormats)
  34
+    (oid, dbo)
  35
+  }
  36
+
  37
+  "JObjectParser" should {
  38
+    "convert strings to ObjectId by default" in {
  39
+      val (oid, dbo) = buildTestData
  40
+      val xval = tryo(dbo.get("x").asInstanceOf[ObjectId])
  41
+
  42
+      xval must notBeEmpty
  43
+      xval.foreach { x =>
  44
+        x must_== oid
  45
+      }
  46
+    }
  47
+    "not convert strings to ObjectId when configured not to" in {
  48
+      JObjectParser.stringProcessor.default.set((s: String) => s)
  49
+
  50
+      val (oid, dbo) = buildTestData
  51
+      val xval = tryo(dbo.get("x").asInstanceOf[String])
  52
+
  53
+      xval must notBeEmpty
  54
+      xval.foreach { x =>
  55
+        x must_== oid.toString
  56
+      }
  57
+    }
  58
+  }
  59
+}
2  project/Build.scala
@@ -147,7 +147,7 @@ object BuildDef extends Build {
147 147
 
148 148
   lazy val mongodb =
149 149
     persistenceProject("mongodb")
150  
-        .dependsOn(json_ext)
  150
+        .dependsOn(json_ext, util)
151 151
         .settings(parallelExecution in Test := false,
152 152
                   libraryDependencies += mongo_driver,
153 153
                   initialize in Test <<= (resourceDirectory in Test) { rd =>

0 notes on commit 85b8293

Please sign in to comment.
Something went wrong with that request. Please try again.