|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.bson.internal; |
| 17 | + |
| 18 | +import org.bson.BsonArray; |
| 19 | +import org.bson.BsonBinary; |
| 20 | +import org.bson.BsonDocument; |
| 21 | +import org.bson.BsonDocumentWrapper; |
| 22 | +import org.bson.BsonJavaScriptWithScope; |
| 23 | +import org.bson.BsonValue; |
| 24 | +import org.bson.RawBsonArray; |
| 25 | +import org.bson.RawBsonDocument; |
| 26 | +import org.bson.conversions.Bson; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.AbstractMap.SimpleImmutableEntry; |
| 31 | +import java.util.Map.Entry; |
| 32 | + |
| 33 | +import static java.util.Arrays.asList; |
| 34 | +import static java.util.Collections.singletonList; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 37 | + |
| 38 | +final class UtilTest { |
| 39 | + @Test |
| 40 | + public void mutableDeepCopy() { |
| 41 | + Entry<String, BsonBinary> originalBsonBinaryEntry = new SimpleImmutableEntry<>( |
| 42 | + "bsonBinary", |
| 43 | + new BsonBinary("bsonBinary".getBytes(StandardCharsets.UTF_8)) |
| 44 | + ); |
| 45 | + Entry<String, BsonJavaScriptWithScope> originalBsonJavaScriptWithScopeEntry = new SimpleImmutableEntry<>( |
| 46 | + "bsonJavaScriptWithScopeEntry", |
| 47 | + new BsonJavaScriptWithScope("\"use strict\";", new BsonDocument()) |
| 48 | + ); |
| 49 | + Entry<String, RawBsonDocument> originalRawBsonDocumentEntry = new SimpleImmutableEntry<>( |
| 50 | + "rawBsonDocument", |
| 51 | + RawBsonDocument.parse("{rawBsonDocument: 'rawBsonDocument_value'}") |
| 52 | + ); |
| 53 | + Entry<String, BsonDocumentWrapper<RawBsonDocument>> originalBsonDocumentWrapperEntry = new SimpleImmutableEntry<>( |
| 54 | + "bsonDocumentWrapper", |
| 55 | + new BsonDocumentWrapper<>(originalRawBsonDocumentEntry.getValue(), Bson.DEFAULT_CODEC_REGISTRY.get(RawBsonDocument.class)) |
| 56 | + ); |
| 57 | + Entry<String, BsonDocument> originalBsonDocumentEntry = new SimpleImmutableEntry<>( |
| 58 | + "bsonDocument", |
| 59 | + new BsonDocument() |
| 60 | + .append(originalBsonBinaryEntry.getKey(), originalBsonBinaryEntry.getValue()) |
| 61 | + .append(originalBsonJavaScriptWithScopeEntry.getKey(), originalBsonJavaScriptWithScopeEntry.getValue()) |
| 62 | + .append(originalRawBsonDocumentEntry.getKey(), originalRawBsonDocumentEntry.getValue()) |
| 63 | + .append(originalBsonDocumentWrapperEntry.getKey(), originalBsonDocumentWrapperEntry.getValue()) |
| 64 | + ); |
| 65 | + Entry<String, BsonArray> originalBsonArrayEntry = new SimpleImmutableEntry<>( |
| 66 | + "bsonArray", |
| 67 | + new BsonArray(singletonList(new BsonArray())) |
| 68 | + ); |
| 69 | + Entry<String, RawBsonArray> originalRawBsonArrayEntry = new SimpleImmutableEntry<>( |
| 70 | + "rawBsonArray", |
| 71 | + rawBsonArray( |
| 72 | + originalBsonBinaryEntry.getValue(), |
| 73 | + originalBsonJavaScriptWithScopeEntry.getValue(), |
| 74 | + originalRawBsonDocumentEntry.getValue(), |
| 75 | + originalBsonDocumentWrapperEntry.getValue(), |
| 76 | + originalBsonDocumentEntry.getValue(), |
| 77 | + originalBsonArrayEntry.getValue()) |
| 78 | + ); |
| 79 | + BsonDocument original = new BsonDocument() |
| 80 | + .append(originalBsonBinaryEntry.getKey(), originalBsonBinaryEntry.getValue()) |
| 81 | + .append(originalBsonJavaScriptWithScopeEntry.getKey(), originalBsonJavaScriptWithScopeEntry.getValue()) |
| 82 | + .append(originalRawBsonDocumentEntry.getKey(), originalRawBsonDocumentEntry.getValue()) |
| 83 | + .append(originalBsonDocumentWrapperEntry.getKey(), originalBsonDocumentWrapperEntry.getValue()) |
| 84 | + .append(originalBsonDocumentEntry.getKey(), originalBsonDocumentEntry.getValue()) |
| 85 | + .append(originalBsonArrayEntry.getKey(), originalBsonArrayEntry.getValue()) |
| 86 | + .append(originalRawBsonArrayEntry.getKey(), originalRawBsonArrayEntry.getValue()); |
| 87 | + BsonDocument copy = Util.mutableDeepCopy(original); |
| 88 | + assertEqualNotSameAndMutable(original, copy); |
| 89 | + original.forEach((key, value) -> assertEqualNotSameAndMutable(value, copy.get(key))); |
| 90 | + // check nested document |
| 91 | + String nestedDocumentKey = originalBsonDocumentEntry.getKey(); |
| 92 | + BsonDocument originalNestedDocument = original.getDocument(nestedDocumentKey); |
| 93 | + BsonDocument copyNestedDocument = copy.getDocument(nestedDocumentKey); |
| 94 | + assertEqualNotSameAndMutable(originalNestedDocument, copyNestedDocument); |
| 95 | + originalNestedDocument.forEach((key, value) -> assertEqualNotSameAndMutable(value, copyNestedDocument.get(key))); |
| 96 | + // check nested array |
| 97 | + String nestedArrayKey = originalRawBsonArrayEntry.getKey(); |
| 98 | + BsonArray originalNestedArray = original.getArray(nestedArrayKey); |
| 99 | + BsonArray copyNestedArray = copy.getArray(nestedArrayKey); |
| 100 | + assertEqualNotSameAndMutable(originalNestedArray, copyNestedArray); |
| 101 | + for (int i = 0; i < originalNestedArray.size(); i++) { |
| 102 | + assertEqualNotSameAndMutable(originalNestedArray.get(i), copyNestedArray.get(i)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static RawBsonArray rawBsonArray(final BsonValue... elements) { |
| 107 | + return (RawBsonArray) new RawBsonDocument( |
| 108 | + new BsonDocument("a", new BsonArray(asList(elements))), Bson.DEFAULT_CODEC_REGISTRY.get(BsonDocument.class)) |
| 109 | + .get("a"); |
| 110 | + } |
| 111 | + |
| 112 | + private static void assertEqualNotSameAndMutable(final Object expected, final Object actual) { |
| 113 | + assertEquals(expected, actual); |
| 114 | + assertNotSame(expected, actual); |
| 115 | + Class<?> actualClass = actual.getClass(); |
| 116 | + if (expected instanceof BsonDocument) { |
| 117 | + assertEquals(BsonDocument.class, actualClass); |
| 118 | + } else if (expected instanceof BsonArray) { |
| 119 | + assertEquals(BsonArray.class, actualClass); |
| 120 | + } else if (expected instanceof BsonBinary) { |
| 121 | + assertEquals(BsonBinary.class, actualClass); |
| 122 | + } else if (expected instanceof BsonJavaScriptWithScope) { |
| 123 | + assertEquals(BsonJavaScriptWithScope.class, actualClass); |
| 124 | + } else { |
| 125 | + org.bson.assertions.Assertions.fail("Unexpected " + expected.getClass().toString()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private UtilTest() { |
| 130 | + } |
| 131 | +} |
0 commit comments