Skip to content

Commit

Permalink
JAVA-2298: Use BsonHelper in LazyBsonObjectSpecification to ensure co…
Browse files Browse the repository at this point in the history
…verage of all BSON types
  • Loading branch information
jyemin committed Nov 8, 2016
1 parent d96f1c7 commit 64e9f8a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions driver/src/test/unit/org/bson/LazyBSONObjectSpecification.groovy
Expand Up @@ -17,6 +17,10 @@
package org.bson

import com.mongodb.BasicDBObject
import com.mongodb.DBObjectCodec
import com.mongodb.LazyDBCallback
import com.mongodb.MongoClient
import org.bson.codecs.DecoderContext
import org.bson.types.BSONTimestamp
import org.bson.types.Binary
import org.bson.types.Code
Expand All @@ -31,6 +35,11 @@ import spock.lang.Unroll

import java.util.regex.Pattern

import static org.bson.BsonHelper.toBson
import static org.bson.BsonHelper.valuesOfEveryType
import static org.bson.BsonType.SYMBOL
import static org.bson.BsonType.UNDEFINED

@SuppressWarnings(['LineLength'])
class LazyBSONObjectSpecification extends Specification {

Expand Down Expand Up @@ -93,6 +102,34 @@ class LazyBSONObjectSpecification extends Specification {
type = BsonType.findByValue(bytes[4])
}

@Unroll
def 'should read value of #value'() {
given:
def bsonDocument = new BsonDocument('name', value)
def dbObject = new DBObjectCodec(MongoClient.defaultCodecRegistry)
.decode(new BsonDocumentReader(bsonDocument), DecoderContext.builder().build())
def lazyBSONObject = new LazyBSONObject(toBson(bsonDocument).array(), new LazyDBCallback())

expect:
lazyBSONObject.keySet().contains('name')

when:
def expectedValue
if (value.bsonType == UNDEFINED) {
expectedValue = null
} else if (value.bsonType == SYMBOL) {
expectedValue = new Symbol(((BsonSymbol) value).getSymbol())
} else {
expectedValue = dbObject.get('name')
}

then:
expectedValue == lazyBSONObject.get('name')

where:
value << valuesOfEveryType()
}

def 'should have nested items as lazy'() {
given:
byte[] bytes = [
Expand Down

0 comments on commit 64e9f8a

Please sign in to comment.