Skip to content

Commit b922075

Browse files
authored
Bson specification testing
Moved specifications submodule out of driver-core into a testing directory. Deleted old copy of bson specification tests and updated to use the submodule Fixed Json output for positive exponents to match the the extended json specification Added test exceptions to BinaryVectorGenericBsonTest Added BsonBinaryVector prose tests Added extra regression tests to ensure both explicit and implicit doubles with positive exponets parse as expected. JAVA-5877 JAVA-5779 JAVA-5782 JAVA-5652
1 parent ad43095 commit b922075

55 files changed

Lines changed: 241 additions & 5603 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "specifications"]
2-
path = driver-core/src/test/resources/specifications
2+
path = testing/resources/specifications
33
url = https://github.com/mongodb/specifications

bson/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ plugins {
2525

2626
base.archivesName.set("bson")
2727

28+
tasks.processTestResources {
29+
from("${rootProject.projectDir}/testing/resources")
30+
into("${layout.buildDirectory.get()}/resources/test")
31+
}
32+
2833
configureMavenPublication {
2934
pom {
3035
name.set("BSON")

bson/src/main/org/bson/BinaryVector.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
import org.bson.annotations.Beta;
2020
import org.bson.annotations.Reason;
21-
22-
import static org.bson.assertions.Assertions.isTrueArgument;
23-
import static org.bson.assertions.Assertions.notNull;
21+
import org.bson.diagnostics.Logger;
22+
import org.bson.diagnostics.Loggers;
2423

2524
/**
2625
* Binary Vectors are densely packed arrays of numbers, all the same type, which are stored and retrieved efficiently using the BSON Binary
@@ -33,6 +32,7 @@
3332
* @since 5.3
3433
*/
3534
public abstract class BinaryVector {
35+
protected static final Logger LOGGER = Loggers.getLogger("BinaryVector");
3636
private final DataType dataType;
3737

3838
BinaryVector(final DataType dataType) {
@@ -64,9 +64,6 @@ public abstract class BinaryVector {
6464
*/
6565
@Beta(Reason.SERVER)
6666
public static PackedBitBinaryVector packedBitVector(final byte[] data, final byte padding) {
67-
notNull("data", data);
68-
isTrueArgument("Padding must be between 0 and 7 bits. Provided padding: " + padding, padding >= 0 && padding <= 7);
69-
isTrueArgument("Padding must be 0 if vector is empty. Provided padding: " + padding, padding == 0 || data.length > 0);
7067
return new PackedBitBinaryVector(data, padding);
7168
}
7269

@@ -83,7 +80,6 @@ public static PackedBitBinaryVector packedBitVector(final byte[] data, final byt
8380
* @return A {@link Int8BinaryVector} instance with the {@link DataType#INT8} data type.
8481
*/
8582
public static Int8BinaryVector int8Vector(final byte[] data) {
86-
notNull("data", data);
8783
return new Int8BinaryVector(data);
8884
}
8985

@@ -99,7 +95,6 @@ public static Int8BinaryVector int8Vector(final byte[] data) {
9995
* @return A {@link Float32BinaryVector} instance with the {@link DataType#FLOAT32} data type.
10096
*/
10197
public static Float32BinaryVector floatVector(final float[] data) {
102-
notNull("data", data);
10398
return new Float32BinaryVector(data);
10499
}
105100

bson/src/main/org/bson/Float32BinaryVector.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.util.Arrays;
2020

21-
import static org.bson.assertions.Assertions.assertNotNull;
21+
import static org.bson.assertions.Assertions.notNull;
2222

2323
/**
2424
* Represents a vector of 32-bit floating-point numbers, where each element in the vector is a float.
@@ -35,9 +35,9 @@ public final class Float32BinaryVector extends BinaryVector {
3535

3636
private final float[] data;
3737

38-
Float32BinaryVector(final float[] vectorData) {
38+
Float32BinaryVector(final float[] data) {
3939
super(DataType.FLOAT32);
40-
this.data = assertNotNull(vectorData);
40+
this.data = notNull("data", data);
4141
}
4242

4343
/**
@@ -49,7 +49,7 @@ public final class Float32BinaryVector extends BinaryVector {
4949
* @return the underlying float array representing this {@link Float32BinaryVector} vector.
5050
*/
5151
public float[] getData() {
52-
return assertNotNull(data);
52+
return data;
5353
}
5454

5555
@Override

bson/src/main/org/bson/Int8BinaryVector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.Objects;
2121

22-
import static org.bson.assertions.Assertions.assertNotNull;
22+
import static org.bson.assertions.Assertions.notNull;
2323

2424
/**
2525
* Represents a vector of 8-bit signed integers, where each element in the vector is a byte.
@@ -38,7 +38,7 @@ public final class Int8BinaryVector extends BinaryVector {
3838

3939
Int8BinaryVector(final byte[] data) {
4040
super(DataType.INT8);
41-
this.data = assertNotNull(data);
41+
this.data = notNull("data", data);
4242
}
4343

4444
/**
@@ -50,7 +50,7 @@ public final class Int8BinaryVector extends BinaryVector {
5050
* @return the underlying byte array representing this {@link Int8BinaryVector} vector.
5151
*/
5252
public byte[] getData() {
53-
return assertNotNull(data);
53+
return data;
5454
}
5555

5656
@Override

bson/src/main/org/bson/PackedBitBinaryVector.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Objects;
2424

2525
import static org.bson.assertions.Assertions.assertNotNull;
26+
import static org.bson.assertions.Assertions.isTrueArgument;
27+
import static org.bson.assertions.Assertions.notNull;
2628

2729
/**
2830
* Represents a packed bit vector, where each element of the vector is represented by a single bit (0 or 1).
@@ -43,8 +45,17 @@ public final class PackedBitBinaryVector extends BinaryVector {
4345

4446
PackedBitBinaryVector(final byte[] data, final byte padding) {
4547
super(DataType.PACKED_BIT);
46-
this.data = assertNotNull(data);
48+
this.data = notNull("data", data);
4749
this.padding = padding;
50+
isTrueArgument("Padding must be between 0 and 7 bits. Provided padding: " + padding, padding >= 0 && padding <= 7);
51+
isTrueArgument("Padding must be 0 if vector is empty. Provided padding: " + padding, padding == 0 || data.length > 0);
52+
if (padding > 0) {
53+
int mask = (1 << padding) - 1;
54+
if ((data[data.length - 1] & mask) != 0) {
55+
// JAVA-5848 in version 6.0.0 will convert this logging into an IllegalArgumentException
56+
LOGGER.warn("The last " + padding + " padded bits should be zero in the final byte.");
57+
}
58+
}
4859
}
4960

5061
/**

bson/src/main/org/bson/json/ExtendedJsonDoubleConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ExtendedJsonDoubleConverter implements Converter<Double> {
2121
public void convert(final Double value, final StrictJsonWriter writer) {
2222
writer.writeStartObject();
2323
writer.writeName("$numberDouble");
24-
writer.writeString(Double.toString(value));
24+
writer.writeString(JsonDoubleHelper.toString(value));
2525
writer.writeEndObject();
2626

2727
}

bson/src/main/org/bson/json/JsonDoubleConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
class JsonDoubleConverter implements Converter<Double> {
2020
@Override
2121
public void convert(final Double value, final StrictJsonWriter writer) {
22-
writer.writeNumber(Double.toString(value));
22+
writer.writeNumber(JsonDoubleHelper.toString(value));
2323
}
2424
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.json;
17+
18+
import java.util.regex.Pattern;
19+
20+
final class JsonDoubleHelper {
21+
22+
private static final Pattern POSITIVE_EXPONENT_PATTERN = Pattern.compile("E(\\d+)");
23+
private static final String POSITIVE_EXPONENT_REPLACER = "E+$1";
24+
25+
static String toString(final double value) {
26+
String doubleString = Double.toString(value);
27+
return POSITIVE_EXPONENT_PATTERN.matcher(doubleString).replaceAll(POSITIVE_EXPONENT_REPLACER);
28+
}
29+
30+
private JsonDoubleHelper() {
31+
}
32+
}

bson/src/main/org/bson/json/RelaxedExtendedJsonDoubleConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void convert(final Double value, final StrictJsonWriter writer) {
2424
if (value.isNaN() || value.isInfinite()) {
2525
FALLBACK_CONVERTER.convert(value, writer);
2626
} else {
27-
writer.writeNumber(Double.toString(value));
27+
writer.writeNumber(JsonDoubleHelper.toString(value));
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)