Skip to content

Commit 8a6f9ab

Browse files
committed
Add unit tests
1 parent 26b15f3 commit 8a6f9ab

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

bson/src/test/unit/org/bson/codecs/IterableCodecSpecification.groovy

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
package org.bson.codecs
1818

19+
import org.bson.BsonArray
20+
import org.bson.BsonDateTime
1921
import org.bson.BsonDocument
2022
import org.bson.BsonDocumentReader
2123
import org.bson.BsonDocumentWriter
24+
import org.bson.codecs.jsr310.Jsr310CodecProvider
2225
import org.bson.types.Binary
2326
import spock.lang.Specification
2427
import spock.lang.Unroll
2528

29+
import java.lang.reflect.ParameterizedType
30+
import java.time.Instant
31+
2632
import static org.bson.BsonDocument.parse
2733
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
2834
import static org.bson.UuidRepresentation.JAVA_LEGACY
@@ -36,7 +42,8 @@ import static org.bson.codecs.configuration.CodecRegistries.fromRegistries
3642
class IterableCodecSpecification extends Specification {
3743

3844
static final REGISTRY = fromRegistries(fromCodecs(new UuidCodec(JAVA_LEGACY)),
39-
fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new IterableCodecProvider()))
45+
fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(),
46+
new IterableCodecProvider(), new MapCodecProvider()))
4047

4148
def 'should have Iterable encoding class'() {
4249
given:
@@ -159,4 +166,44 @@ class IterableCodecSpecification extends Specification {
159166
PYTHON_LEGACY | [new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[])] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
160167
UNSPECIFIED | [new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[])] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
161168
}
169+
170+
def 'should parameterize'() {
171+
given:
172+
def codec = new IterableCodec(REGISTRY, new BsonTypeClassMap())
173+
def writer = new BsonDocumentWriter(new BsonDocument())
174+
def reader = new BsonDocumentReader(writer.getDocument())
175+
def instants = [
176+
['firstMap': [Instant.ofEpochMilli(1), Instant.ofEpochMilli(2)]],
177+
['secondMap': [Instant.ofEpochMilli(3), Instant.ofEpochMilli(4)]]]
178+
when:
179+
codec = codec.parameterize(fromProviders(new Jsr310CodecProvider(), REGISTRY),
180+
Arrays.asList(((ParameterizedType) Container.getMethod('getInstants').genericReturnType).actualTypeArguments))
181+
writer.writeStartDocument()
182+
writer.writeName('instants')
183+
codec.encode(writer, instants, EncoderContext.builder().build())
184+
writer.writeEndDocument()
185+
186+
then:
187+
writer.getDocument() == new BsonDocument()
188+
.append('instants', new BsonArray(List.of(
189+
new BsonDocument('firstMap', new BsonArray(List.of(new BsonDateTime(1), new BsonDateTime(2)))),
190+
new BsonDocument('secondMap', new BsonArray(List.of(new BsonDateTime(3), new BsonDateTime(4)))))))
191+
192+
when:
193+
reader.readStartDocument()
194+
reader.readName('instants')
195+
def decodedInstants = codec.decode(reader, DecoderContext.builder().build())
196+
197+
then:
198+
decodedInstants == instants
199+
}
200+
201+
@SuppressWarnings('unused')
202+
static class Container {
203+
private final List<Map<String, List<Instant>>> instants = []
204+
205+
List<Map<String, List<Instant>>> getInstants() {
206+
instants
207+
}
208+
}
162209
}

bson/src/test/unit/org/bson/codecs/MapCodecSpecification.groovy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.bson.codecs
1818

19+
import org.bson.BsonArray
1920
import org.bson.BsonBinaryReader
2021
import org.bson.BsonBinaryWriter
22+
import org.bson.BsonDateTime
2123
import org.bson.BsonDbPointer
2224
import org.bson.BsonDocument
2325
import org.bson.BsonDocumentReader
@@ -30,6 +32,7 @@ import org.bson.BsonUndefined
3032
import org.bson.BsonWriter
3133
import org.bson.ByteBufNIO
3234
import org.bson.Document
35+
import org.bson.codecs.jsr310.Jsr310CodecProvider
3336
import org.bson.io.BasicOutputBuffer
3437
import org.bson.io.ByteBufferBsonInput
3538
import org.bson.json.JsonReader
@@ -44,7 +47,9 @@ import spock.lang.Shared
4447
import spock.lang.Specification
4548
import spock.lang.Unroll
4649

50+
import java.lang.reflect.ParameterizedType
4751
import java.nio.ByteBuffer
52+
import java.time.Instant
4853
import java.util.concurrent.atomic.AtomicBoolean
4954
import java.util.concurrent.atomic.AtomicInteger
5055
import java.util.concurrent.atomic.AtomicLong
@@ -223,4 +228,45 @@ class MapCodecSpecification extends Specification {
223228
then:
224229
doc['_id'] == 5
225230
}
231+
232+
233+
def 'should parameterize'() {
234+
given:
235+
def codec = new MapCodec(REGISTRY, new BsonTypeClassMap())
236+
def writer = new BsonDocumentWriter(new BsonDocument())
237+
def reader = new BsonDocumentReader(writer.getDocument())
238+
def instants =
239+
['firstMap': [Instant.ofEpochMilli(1), Instant.ofEpochMilli(2)],
240+
'secondMap': [Instant.ofEpochMilli(3), Instant.ofEpochMilli(4)]]
241+
when:
242+
codec = codec.parameterize(fromProviders(new Jsr310CodecProvider(), REGISTRY),
243+
asList(((ParameterizedType) Container.getMethod('getInstants').genericReturnType).actualTypeArguments))
244+
writer.writeStartDocument()
245+
writer.writeName('instants')
246+
codec.encode(writer, instants, EncoderContext.builder().build())
247+
writer.writeEndDocument()
248+
249+
then:
250+
writer.getDocument() == new BsonDocument()
251+
.append('instants',
252+
new BsonDocument('firstMap', new BsonArray(List.of(new BsonDateTime(1), new BsonDateTime(2))))
253+
.append('secondMap', new BsonArray(List.of(new BsonDateTime(3), new BsonDateTime(4)))))
254+
255+
when:
256+
reader.readStartDocument()
257+
reader.readName('instants')
258+
def decodedInstants = codec.decode(reader, DecoderContext.builder().build())
259+
260+
then:
261+
decodedInstants == instants
262+
}
263+
264+
@SuppressWarnings('unused')
265+
static class Container {
266+
private final Map<String, List<Instant>> instants = [:]
267+
268+
Map<String, List<Instant>> getInstants() {
269+
instants
270+
}
271+
}
226272
}

0 commit comments

Comments
 (0)