17
17
package org.bson
18
18
19
19
import com.mongodb.BasicDBObject
20
+ import com.mongodb.DBObjectCodec
21
+ import com.mongodb.LazyDBCallback
22
+ import com.mongodb.MongoClient
23
+ import org.bson.codecs.DecoderContext
20
24
import org.bson.types.BSONTimestamp
21
25
import org.bson.types.Binary
22
26
import org.bson.types.Code
@@ -31,6 +35,11 @@ import spock.lang.Unroll
31
35
32
36
import java.util.regex.Pattern
33
37
38
+ import static org.bson.BsonHelper.toBson
39
+ import static org.bson.BsonHelper.valuesOfEveryType
40
+ import static org.bson.BsonType.SYMBOL
41
+ import static org.bson.BsonType.UNDEFINED
42
+
34
43
@SuppressWarnings ([' LineLength' ])
35
44
class LazyBSONObjectSpecification extends Specification {
36
45
@@ -93,6 +102,34 @@ class LazyBSONObjectSpecification extends Specification {
93
102
type = BsonType . findByValue(bytes[4 ])
94
103
}
95
104
105
+ @Unroll
106
+ def ' should read value of #value' () {
107
+ given :
108
+ def bsonDocument = new BsonDocument (' name' , value)
109
+ def dbObject = new DBObjectCodec (MongoClient . defaultCodecRegistry)
110
+ .decode(new BsonDocumentReader (bsonDocument), DecoderContext . builder(). build())
111
+ def lazyBSONObject = new LazyBSONObject (toBson(bsonDocument). array(), new LazyDBCallback ())
112
+
113
+ expect :
114
+ lazyBSONObject. keySet(). contains(' name' )
115
+
116
+ when :
117
+ def expectedValue
118
+ if (value. bsonType == UNDEFINED ) {
119
+ expectedValue = null
120
+ } else if (value. bsonType == SYMBOL ) {
121
+ expectedValue = new Symbol (((BsonSymbol ) value). getSymbol())
122
+ } else {
123
+ expectedValue = dbObject. get(' name' )
124
+ }
125
+
126
+ then :
127
+ expectedValue == lazyBSONObject. get(' name' )
128
+
129
+ where :
130
+ value << valuesOfEveryType()
131
+ }
132
+
96
133
def ' should have nested items as lazy' () {
97
134
given :
98
135
byte [] bytes = [
0 commit comments