|
29 | 29 | import org.bson.json.JsonReader;
|
30 | 30 | import org.junit.Test;
|
31 | 31 |
|
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | + |
32 | 36 | import static java.util.Arrays.asList;
|
33 | 37 | import static org.bson.codecs.configuration.CodecRegistries.fromCodecs;
|
34 | 38 | import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
|
@@ -75,6 +79,35 @@ public void toJsonShouldReturnEquivalent() {
|
75 | 79 | document);
|
76 | 80 | }
|
77 | 81 |
|
| 82 | + // Test in Java to make sure none of the casts result in compiler warnings or class cast exceptions |
| 83 | + @Test |
| 84 | + public void shouldGetWithDefaultValue() { |
| 85 | + // given |
| 86 | + Document d = new Document("x", 1) |
| 87 | + .append("y", Collections.singletonList("one")) |
| 88 | + .append("z", "foo"); |
| 89 | + |
| 90 | + // when the key is found |
| 91 | + int x = d.get("x", 2); |
| 92 | + List<String> y = d.get("y", Arrays.asList("three", "four")); |
| 93 | + String z = d.get("z", "bar"); |
| 94 | + |
| 95 | + // then it returns the value |
| 96 | + assertEquals(1, x); |
| 97 | + assertEquals(Arrays.asList("one"), y); |
| 98 | + assertEquals("foo", z); |
| 99 | + |
| 100 | + // when the key is not found |
| 101 | + int x2 = d.get("x2", 2); |
| 102 | + List<String> y2 = d.get("y2", Arrays.asList("three", "four")); |
| 103 | + String z2 = d.get("z2", "bar"); |
| 104 | + |
| 105 | + // then it returns the default value |
| 106 | + assertEquals(2, x2); |
| 107 | + assertEquals(Arrays.asList("three", "four"), y2); |
| 108 | + assertEquals("bar", z2); |
| 109 | + } |
| 110 | + |
78 | 111 | @Test
|
79 | 112 | public void toJsonShouldTakeACustomDocumentCodec() {
|
80 | 113 |
|
|
0 commit comments