|
23 | 23 | import org.bson.json.JsonReader;
|
24 | 24 | import org.bson.json.JsonWriter;
|
25 | 25 | import org.bson.json.JsonWriterSettings;
|
26 |
| -import org.junit.Test; |
| 26 | +import org.junit.jupiter.api.Test; |
27 | 27 |
|
28 | 28 | import java.io.StringWriter;
|
29 | 29 | import java.util.Arrays;
|
| 30 | +import java.util.function.Consumer; |
30 | 31 |
|
31 |
| -import static org.junit.Assert.assertEquals; |
32 |
| -import static org.junit.Assert.assertNotEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
33 | 36 |
|
34 | 37 | // Don't convert to Spock, as Groovy intercepts equals/hashCode methods that we are trying to test
|
35 | 38 | public class BsonDocumentTest {
|
@@ -106,4 +109,28 @@ public void toStringShouldEqualToJson() {
|
106 | 109 | public void shouldParseJson() {
|
107 | 110 | assertEquals(new BsonDocument("a", new BsonInt32(1)), BsonDocument.parse("{\"a\" : 1}"));
|
108 | 111 | }
|
| 112 | + |
| 113 | + @Test |
| 114 | + public void cloneIsDeepCopyAndMutable() { |
| 115 | + Consumer<BsonDocument> assertCloneDeepCopyMutable = original -> { |
| 116 | + BsonDocument clone = original.clone(); |
| 117 | + assertNotSame(original, clone); |
| 118 | + assertEquals(original, clone); |
| 119 | + // check that mutating `clone` does not mutate `original` |
| 120 | + clone.getDocument("k1").put("k2", new BsonString("clone")); |
| 121 | + assertEquals(new BsonString("clone"), clone.getDocument("k1").get("k2")); |
| 122 | + assertEquals(BsonNull.VALUE, original.getDocument("k1").get("k2")); |
| 123 | + // check that mutating `original` (if it is mutable) does not mutate `clone` |
| 124 | + if (!(original instanceof RawBsonDocument)) { |
| 125 | + original.put("k1", new BsonDocument("k2", new BsonString("original"))); |
| 126 | + assertEquals(new BsonString("original"), original.getDocument("k1").get("k2")); |
| 127 | + assertEquals(new BsonString("clone"), clone.getDocument("k1").get("k2")); |
| 128 | + } |
| 129 | + }; |
| 130 | + assertAll( |
| 131 | + () -> assertCloneDeepCopyMutable.accept(new BsonDocument("k1", new BsonDocument("k2", BsonNull.VALUE))), |
| 132 | + () -> assertCloneDeepCopyMutable.accept(new BsonDocument("k1", RawBsonDocument.parse("{'k2': null}"))), |
| 133 | + () -> assertCloneDeepCopyMutable.accept(RawBsonDocument.parse("{'k1': {'k2': null}}")) |
| 134 | + ); |
| 135 | + } |
109 | 136 | }
|
0 commit comments