1717package org.bson
1818
1919import com.mongodb.BasicDBObject
20+ import com.mongodb.DBObjectCodec
21+ import com.mongodb.LazyDBCallback
22+ import com.mongodb.MongoClient
23+ import org.bson.codecs.DecoderContext
2024import org.bson.types.BSONTimestamp
2125import org.bson.types.Binary
2226import org.bson.types.Code
@@ -31,6 +35,11 @@ import spock.lang.Unroll
3135
3236import java.util.regex.Pattern
3337
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+
3443@SuppressWarnings ([' LineLength' ])
3544class LazyBSONObjectSpecification extends Specification {
3645
@@ -93,6 +102,34 @@ class LazyBSONObjectSpecification extends Specification {
93102 type = BsonType . findByValue(bytes[4 ])
94103 }
95104
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+
96133 def ' should have nested items as lazy' () {
97134 given :
98135 byte [] bytes = [
0 commit comments