|
| 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 | + |
| 17 | +package org.bson; |
| 18 | + |
| 19 | +import org.bson.codecs.BsonDocumentCodec; |
| 20 | +import org.bson.codecs.EncoderContext; |
| 21 | +import org.bson.io.BasicOutputBuffer; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.api.Named; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | + |
| 33 | +class RawBsonDocumentTest { |
| 34 | + |
| 35 | + private static final BsonDocument DOCUMENT = new BsonDocument() |
| 36 | + .append("a", new BsonInt32(1)) |
| 37 | + .append("b", new BsonInt32(2)) |
| 38 | + .append("c", new BsonDocument("x", BsonBoolean.TRUE)) |
| 39 | + .append("d", new BsonArray(Arrays.asList( |
| 40 | + new BsonDocument("y", BsonBoolean.FALSE), |
| 41 | + new BsonArray(Arrays.asList(new BsonInt32(1)))))); |
| 42 | + |
| 43 | + private static final byte[] DOCUMENT_BYTES = encodeDocument(); |
| 44 | + |
| 45 | + static Stream<Arguments> backingArrayAccessors() { |
| 46 | + int documentLength = DOCUMENT_BYTES.length; |
| 47 | + |
| 48 | + Stream.Builder<Arguments> builder = Stream.builder(); |
| 49 | + builder.add(Arguments.of(createFromDocument(), 0, documentLength)); |
| 50 | + builder.add(Arguments.of(createFromByteArray(), 0, documentLength)); |
| 51 | + |
| 52 | + for (int padding = 1; padding <= 2; padding++) { |
| 53 | + builder.add(Arguments.of(createPaddedBefore(padding), padding, documentLength)); |
| 54 | + builder.add(Arguments.of(createPaddedAfter(padding), 0, documentLength)); |
| 55 | + builder.add(Arguments.of(createPaddedBoth(padding), padding, documentLength)); |
| 56 | + } |
| 57 | + |
| 58 | + return builder.build(); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "{0}, expectedOffset={1}, expectedLength={2}") |
| 62 | + @MethodSource("backingArrayAccessors") |
| 63 | + void shouldExposeBackingArrayOffsetAndLength(final RawBsonDocument rawDocument, |
| 64 | + final int expectedOffset, |
| 65 | + final int expectedLength) { |
| 66 | + assertEquals(expectedOffset, rawDocument.getByteOffset()); |
| 67 | + assertEquals(expectedLength, rawDocument.getByteLength()); |
| 68 | + assertArrayEquals(DOCUMENT_BYTES, |
| 69 | + Arrays.copyOfRange( |
| 70 | + rawDocument.getBackingArray(), |
| 71 | + rawDocument.getByteOffset(), |
| 72 | + rawDocument.getByteOffset() + rawDocument.getByteLength())); |
| 73 | + } |
| 74 | + |
| 75 | + private static Named<RawBsonDocument> createFromDocument() { |
| 76 | + return Named.of("from document", new RawBsonDocument(DOCUMENT, new BsonDocumentCodec())); |
| 77 | + } |
| 78 | + |
| 79 | + private static Named<RawBsonDocument> createFromByteArray() { |
| 80 | + return Named.of("from byte array", new RawBsonDocument(DOCUMENT_BYTES)); |
| 81 | + } |
| 82 | + |
| 83 | + private static Named<RawBsonDocument> createPaddedBefore(final int padding) { |
| 84 | + byte[] padded = new byte[DOCUMENT_BYTES.length + padding]; |
| 85 | + System.arraycopy(DOCUMENT_BYTES, 0, padded, padding, DOCUMENT_BYTES.length); |
| 86 | + return Named.of("padded before " + padding, new RawBsonDocument(padded, padding, DOCUMENT_BYTES.length)); |
| 87 | + } |
| 88 | + |
| 89 | + private static Named<RawBsonDocument> createPaddedAfter(final int padding) { |
| 90 | + byte[] padded = new byte[DOCUMENT_BYTES.length + padding]; |
| 91 | + System.arraycopy(DOCUMENT_BYTES, 0, padded, 0, DOCUMENT_BYTES.length); |
| 92 | + return Named.of("padded after " + padding, new RawBsonDocument(padded, 0, DOCUMENT_BYTES.length)); |
| 93 | + } |
| 94 | + |
| 95 | + private static Named<RawBsonDocument> createPaddedBoth(final int padding) { |
| 96 | + byte[] padded = new byte[DOCUMENT_BYTES.length + padding * 2]; |
| 97 | + System.arraycopy(DOCUMENT_BYTES, 0, padded, padding, DOCUMENT_BYTES.length); |
| 98 | + return Named.of("padded both " + padding, new RawBsonDocument(padded, padding, DOCUMENT_BYTES.length)); |
| 99 | + } |
| 100 | + |
| 101 | + private static byte[] encodeDocument() { |
| 102 | + BasicOutputBuffer buffer = new BasicOutputBuffer(); |
| 103 | + new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), DOCUMENT, EncoderContext.builder().build()); |
| 104 | + return Arrays.copyOf(buffer.getInternalBuffer(), buffer.getPosition()); |
| 105 | + } |
| 106 | +} |
0 commit comments