Skip to content

Commit 64e9f8a

Browse files
committed
JAVA-2298: Use BsonHelper in LazyBsonObjectSpecification to ensure coverage of all BSON types
1 parent d96f1c7 commit 64e9f8a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

driver/src/test/unit/org/bson/LazyBSONObjectSpecification.groovy

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
package org.bson
1818

1919
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
2024
import org.bson.types.BSONTimestamp
2125
import org.bson.types.Binary
2226
import org.bson.types.Code
@@ -31,6 +35,11 @@ import spock.lang.Unroll
3135

3236
import 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'])
3544
class 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

Comments
 (0)