Skip to content

Commit 3e7401b

Browse files
authored
Migrate bson tests from JUnit 4 to 5 (#1229)
JAVA-5215
1 parent a42f1c8 commit 3e7401b

Some content is hidden

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

43 files changed

+609
-554
lines changed

bson/src/test/unit/org/bson/BsonBinaryReaderTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
import org.bson.io.ByteBufferBsonInput;
2020
import org.bson.types.ObjectId;
21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
import java.nio.ByteBuffer;
2424

2525
import static org.hamcrest.CoreMatchers.is;
26-
import static org.junit.Assert.assertEquals;
2726
import static org.hamcrest.MatcherAssert.assertThat;
28-
import static org.junit.Assert.fail;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.fail;
2929

3030
public class BsonBinaryReaderTest {
3131

@@ -53,8 +53,7 @@ public void testInvalidBsonType() {
5353
reader.readBsonType();
5454
fail("Should have thrown BsonSerializationException");
5555
} catch (BsonSerializationException e) {
56-
assertEquals("Detected unknown BSON type \"\\x16\" for fieldname \"a\". Are you using the latest driver version?",
57-
e.getMessage());
56+
assertEquals("Detected unknown BSON type \"\\x16\" for fieldname \"a\". Are you using the latest driver version?", e.getMessage());
5857
}
5958
}
6059

bson/src/test/unit/org/bson/BsonBinaryWriterTest.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import org.bson.io.BasicOutputBuffer;
2020
import org.bson.io.ByteBufferBsonInput;
2121
import org.bson.types.ObjectId;
22-
import org.junit.After;
23-
import org.junit.Before;
24-
import org.junit.Test;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
2525

2626
import java.io.ByteArrayOutputStream;
2727
import java.io.IOException;
@@ -31,25 +31,25 @@
3131
import static java.util.Arrays.asList;
3232
import static org.hamcrest.CoreMatchers.is;
3333
import static org.hamcrest.MatcherAssert.assertThat;
34-
import static org.junit.Assert.assertArrayEquals;
35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertFalse;
37-
import static org.junit.Assert.assertThrows;
38-
import static org.junit.Assert.assertTrue;
39-
import static org.junit.Assert.fail;
34+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertThrows;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
import static org.junit.jupiter.api.Assertions.fail;
4040

4141
public class BsonBinaryWriterTest {
4242

4343
private BsonBinaryWriter writer;
4444
private BasicOutputBuffer buffer;
4545

46-
@Before
46+
@BeforeEach
4747
public void setup() {
4848
buffer = new BasicOutputBuffer();
4949
writer = new BsonBinaryWriter(new BsonWriterSettings(100), new BsonBinaryWriterSettings(1024), buffer);
5050
}
5151

52-
@After
52+
@AfterEach
5353
public void tearDown() {
5454
writer.close();
5555
}

bson/src/test/unit/org/bson/BsonDocumentTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import org.bson.json.JsonReader;
2424
import org.bson.json.JsonWriter;
2525
import org.bson.json.JsonWriterSettings;
26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727

2828
import java.io.StringWriter;
2929
import java.util.Arrays;
3030

31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertNotEquals;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3333

3434
// Don't convert to Spock, as Groovy intercepts equals/hashCode methods that we are trying to test
3535
public class BsonDocumentTest {
@@ -78,8 +78,7 @@ public void shouldHaveSameHashCodeAsEquivalentBsonDocument() {
7878

7979
@Test
8080
public void toJsonShouldReturnEquivalent() {
81-
assertEquals(new BsonDocumentCodec().decode(new JsonReader(document.toJson()), DecoderContext.builder().build()),
82-
document);
81+
assertEquals(new BsonDocumentCodec().decode(new JsonReader(document.toJson()), DecoderContext.builder().build()), document);
8382
}
8483

8584
@Test

bson/src/test/unit/org/bson/DocumentTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.bson.codecs.configuration.CodecRegistry;
2929
import org.bson.conversions.Bson;
3030
import org.bson.json.JsonReader;
31-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
3232

3333
import java.util.Collections;
3434
import java.util.List;
@@ -38,9 +38,9 @@
3838
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs;
3939
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
4040
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
41-
import static org.junit.Assert.assertEquals;
42-
import static org.junit.Assert.assertNotEquals;
43-
import static org.junit.Assert.fail;
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
43+
import static org.junit.jupiter.api.Assertions.fail;
4444

4545
// Don't convert to Spock, as Groovy intercepts equals/hashCode methods that we are trying to test
4646
public class DocumentTest {
@@ -75,8 +75,7 @@ public void shouldHaveSameHashCodeAsEquivalentBsonDocument() {
7575

7676
@Test
7777
public void toJsonShouldReturnEquivalent() {
78-
assertEquals(new DocumentCodec().decode(new JsonReader(document.toJson()), DecoderContext.builder().build()),
79-
document);
78+
assertEquals(new DocumentCodec().decode(new JsonReader(document.toJson()), DecoderContext.builder().build()), document);
8079
}
8180

8281
// Test to ensure that toJson does not reorder _id field

bson/src/test/unit/org/bson/GenericBsonTest.java

+53-61
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
import org.bson.json.JsonParseException;
2525
import org.bson.json.JsonWriterSettings;
2626
import org.bson.types.Decimal128;
27-
import org.junit.Assume;
28-
import org.junit.Test;
29-
import org.junit.runner.RunWith;
30-
import org.junit.runners.Parameterized;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.Arguments;
29+
import org.junit.jupiter.params.provider.MethodSource;
3130
import util.Hex;
3231
import util.JsonPoweredTestHelper;
3332

@@ -40,17 +39,17 @@
4039
import java.nio.charset.StandardCharsets;
4140
import java.util.ArrayList;
4241
import java.util.Arrays;
43-
import java.util.Collection;
4442
import java.util.List;
43+
import java.util.stream.Stream;
4544

4645
import static java.lang.String.format;
4746
import static org.bson.BsonDocument.parse;
48-
import static org.junit.Assert.assertEquals;
49-
import static org.junit.Assert.assertTrue;
50-
import static org.junit.Assert.fail;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
import static org.junit.jupiter.api.Assertions.assertTrue;
49+
import static org.junit.jupiter.api.Assertions.fail;
50+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
5151

5252
// BSON tests powered by language-agnostic JSON-based tests included in test resources
53-
@RunWith(Parameterized.class)
5453
public class GenericBsonTest {
5554

5655
private static final List<String> IGNORED_PARSE_ERRORS = Arrays.asList(
@@ -65,35 +64,26 @@ enum TestCaseType {
6564
PARSE_ERROR
6665
}
6766

68-
private final BsonDocument testDefinition;
69-
private final BsonDocument testCase;
70-
private final TestCaseType testCaseType;
71-
72-
public GenericBsonTest(@SuppressWarnings("unused") final String description,
67+
@ParameterizedTest(name = "{0}")
68+
@MethodSource("data")
69+
public void shouldPassAllOutcomes(@SuppressWarnings("unused") final String description,
7370
final BsonDocument testDefinition, final BsonDocument testCase, final TestCaseType testCaseType) {
74-
this.testDefinition = testDefinition;
75-
this.testCase = testCase;
76-
this.testCaseType = testCaseType;
77-
}
78-
79-
@Test
80-
public void shouldPassAllOutcomes() {
8171
switch (testCaseType) {
8272
case VALID:
83-
runValid();
73+
runValid(testCase);
8474
break;
8575
case DECODE_ERROR:
86-
runDecodeError();
76+
runDecodeError(testCase);
8777
break;
8878
case PARSE_ERROR:
89-
runParseError();
79+
runParseError(testDefinition, testCase);
9080
break;
9181
default:
9282
throw new IllegalArgumentException(format("Unsupported test case type %s", testCaseType));
9383
}
9484
}
9585

96-
private void runValid() {
86+
private void runValid(final BsonDocument testCase) {
9787
String description = testCase.getString("description").getValue();
9888
String canonicalBsonHex = testCase.getString("canonical_bson").getValue().toUpperCase();
9989
String degenerateBsonHex = testCase.getString("degenerate_bson", new BsonString("")).getValue().toUpperCase();
@@ -105,50 +95,51 @@ private void runValid() {
10595
BsonDocument decodedDocument = decodeToDocument(canonicalBsonHex, description);
10696

10797
// native_to_bson( bson_to_native(cB) ) = cB
108-
assertEquals(format("Failed to create expected BSON for document with description '%s'", description),
109-
canonicalBsonHex, encodeToHex(decodedDocument));
98+
assertEquals(canonicalBsonHex, encodeToHex(decodedDocument),
99+
format("Failed to create expected BSON for document with description '%s'", description));
110100

111101
JsonWriterSettings canonicalJsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
112102
JsonWriterSettings relaxedJsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
113103

114104
if (!canonicalJson.isEmpty()) {
115105
// native_to_canonical_extended_json( bson_to_native(cB) ) = cEJ
116-
assertEquals(format("Failed to create expected canonical JSON for document with description '%s'", description),
117-
stripWhiteSpace(canonicalJson), stripWhiteSpace(decodedDocument.toJson(canonicalJsonWriterSettings)));
106+
assertEquals(stripWhiteSpace(canonicalJson), stripWhiteSpace(decodedDocument.toJson(canonicalJsonWriterSettings)),
107+
format("Failed to create expected canonical JSON for document with description '%s'", description));
118108

119109
// native_to_canonical_extended_json( json_to_native(cEJ) ) = cEJ
120110
BsonDocument parsedCanonicalJsonDocument = parse(canonicalJson);
121-
assertEquals("Failed to create expected canonical JSON from parsing canonical JSON",
122-
stripWhiteSpace(canonicalJson), stripWhiteSpace(parsedCanonicalJsonDocument.toJson(canonicalJsonWriterSettings)));
111+
assertEquals(stripWhiteSpace(canonicalJson), stripWhiteSpace(parsedCanonicalJsonDocument.toJson(canonicalJsonWriterSettings)),
112+
"Failed to create expected canonical JSON from parsing canonical JSON");
123113

124114
if (!lossy) {
125115
// native_to_bson( json_to_native(cEJ) ) = cB
126-
assertEquals("Failed to create expected canonical BSON from parsing canonical JSON",
127-
canonicalBsonHex, encodeToHex(parsedCanonicalJsonDocument));
116+
assertEquals(canonicalBsonHex, encodeToHex(parsedCanonicalJsonDocument),
117+
"Failed to create expected canonical BSON from parsing canonical JSON");
128118
}
129119
}
130120

131121
if (!relaxedJson.isEmpty()) {
132122
// native_to_relaxed_extended_json( bson_to_native(cB) ) = rEJ
133-
assertEquals(format("Failed to create expected relaxed JSON for document with description '%s'", description),
134-
stripWhiteSpace(relaxedJson), stripWhiteSpace(decodedDocument.toJson(relaxedJsonWriterSettings)));
123+
assertEquals(stripWhiteSpace(relaxedJson), stripWhiteSpace(decodedDocument.toJson(relaxedJsonWriterSettings)),
124+
format("Failed to create expected relaxed JSON for document with description '%s'", description));
135125

136126
// native_to_relaxed_extended_json( json_to_native(rEJ) ) = rEJ
137-
assertEquals("Failed to create expected relaxed JSON from parsing relaxed JSON", stripWhiteSpace(relaxedJson),
138-
stripWhiteSpace(parse(relaxedJson).toJson(relaxedJsonWriterSettings)));
127+
assertEquals(stripWhiteSpace(relaxedJson), stripWhiteSpace(parse(relaxedJson).toJson(relaxedJsonWriterSettings)),
128+
"Failed to create expected relaxed JSON from parsing relaxed JSON");
139129
}
140130

141131
if (!degenerateJson.isEmpty()) {
142132
// native_to_bson( json_to_native(dEJ) ) = cB
143-
assertEquals("Failed to create expected canonical BSON from parsing canonical JSON",
144-
canonicalBsonHex, encodeToHex(parse(degenerateJson)));
133+
assertEquals(canonicalBsonHex, encodeToHex(parse(degenerateJson)),
134+
"Failed to create expected canonical BSON from parsing canonical JSON");
145135
}
146136

147137
if (!degenerateBsonHex.isEmpty()) {
148138
BsonDocument decodedDegenerateDocument = decodeToDocument(degenerateBsonHex, description);
149139
// native_to_bson( bson_to_native(dB) ) = cB
150-
assertEquals(format("Failed to create expected canonical BSON from degenerate BSON for document with description "
151-
+ "'%s'", description), canonicalBsonHex, encodeToHex(decodedDegenerateDocument));
140+
assertEquals(canonicalBsonHex, encodeToHex(decodedDegenerateDocument),
141+
format("Failed to create expected canonical BSON from degenerate BSON for document with description '%s'",
142+
description));
152143
}
153144
}
154145

@@ -223,7 +214,7 @@ private BsonDocument decodeToDocument(final String subjectHex, final String desc
223214

224215
if (byteBuffer.hasRemaining()) {
225216
throw new BsonSerializationException(format("Should have consumed all bytes, but " + byteBuffer.remaining()
226-
+ " still remain in the buffer for document with description ",
217+
+ " still remain in the buffer for document with description ",
227218
description));
228219
}
229220
return actualDecodedDocument;
@@ -235,20 +226,20 @@ private String encodeToHex(final BsonDocument decodedDocument) {
235226
return Hex.encode(outputBuffer.toByteArray());
236227
}
237228

238-
private void runDecodeError() {
229+
private void runDecodeError(final BsonDocument testCase) {
239230
try {
240231
String description = testCase.getString("description").getValue();
241-
throwIfValueIsStringContainingReplacementCharacter(description);
232+
throwIfValueIsStringContainingReplacementCharacter(testCase, description);
242233
fail(format("Should have failed parsing for subject with description '%s'", description));
243234
} catch (BsonSerializationException e) {
244235
// all good
245236
}
246237
}
247238

248-
private void runParseError() {
239+
private void runParseError(final BsonDocument testDefinition, final BsonDocument testCase) {
249240
String description = testCase.getString("description").getValue();
250241

251-
Assume.assumeFalse(IGNORED_PARSE_ERRORS.contains(description));
242+
assumeFalse(IGNORED_PARSE_ERRORS.contains(description));
252243

253244
String str = testCase.getString("string").getValue();
254245

@@ -290,7 +281,7 @@ private boolean isTestOfNullByteInCString(final String description) {
290281

291282
// Working around the fact that the Java driver doesn't report an error for invalid UTF-8, but rather replaces the invalid
292283
// sequence with the replacement character
293-
private void throwIfValueIsStringContainingReplacementCharacter(final String description) {
284+
private void throwIfValueIsStringContainingReplacementCharacter(final BsonDocument testCase, final String description) {
294285
BsonDocument decodedDocument = decodeToDocument(testCase.getString("bson").getValue(), description);
295286
BsonValue value = decodedDocument.get(decodedDocument.getFirstKey());
296287

@@ -312,39 +303,40 @@ private void throwIfValueIsStringContainingReplacementCharacter(final String des
312303
if (decodedString.contains(StandardCharsets.UTF_8.newDecoder().replacement())) {
313304
throw new BsonSerializationException("String contains replacement character");
314305
}
315-
}
306+
}
316307

317308

318-
@Parameterized.Parameters(name = "{0}")
319-
public static Collection<Object[]> data() throws URISyntaxException, IOException {
320-
List<Object[]> data = new ArrayList<>();
309+
private static Stream<Arguments> data() throws URISyntaxException, IOException {
310+
List<Arguments> data = new ArrayList<>();
321311
for (File file : JsonPoweredTestHelper.getTestFiles("/bson")) {
322312
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
323313
for (BsonValue curValue : testDocument.getArray("valid", new BsonArray())) {
324314
BsonDocument testCaseDocument = curValue.asDocument();
325-
data.add(new Object[]{createTestCaseDescription(testDocument, testCaseDocument, "valid"), testDocument, testCaseDocument,
326-
TestCaseType.VALID});
315+
data.add(Arguments.of(
316+
createTestCaseDescription(testDocument, testCaseDocument, "valid"), testDocument, testCaseDocument,
317+
TestCaseType.VALID));
327318
}
328319

329320
for (BsonValue curValue : testDocument.getArray("decodeErrors", new BsonArray())) {
330321
BsonDocument testCaseDocument = curValue.asDocument();
331-
data.add(new Object[]{createTestCaseDescription(testDocument, testCaseDocument, "decodeError"), testDocument,
332-
testCaseDocument, TestCaseType.DECODE_ERROR});
322+
data.add(Arguments.of(
323+
createTestCaseDescription(testDocument, testCaseDocument, "decodeError"), testDocument, testCaseDocument,
324+
TestCaseType.DECODE_ERROR));
333325
}
334326
for (BsonValue curValue : testDocument.getArray("parseErrors", new BsonArray())) {
335327
BsonDocument testCaseDocument = curValue.asDocument();
336-
data.add(new Object[]{createTestCaseDescription(testDocument, testCaseDocument, "parseError"), testDocument,
337-
testCaseDocument, TestCaseType.PARSE_ERROR});
328+
data.add(Arguments.of(createTestCaseDescription(testDocument, testCaseDocument, "parseError"), testDocument,
329+
testCaseDocument, TestCaseType.PARSE_ERROR));
338330
}
339331
}
340-
return data;
332+
return data.stream();
341333
}
342334

343335
private static String createTestCaseDescription(final BsonDocument testDocument, final BsonDocument testCaseDocument,
344-
final String testCaseType) {
336+
final String testCaseType) {
345337
return testDocument.getString("description").getValue()
346-
+ "[" + testCaseType + "]"
347-
+ ": " + testCaseDocument.getString("description").getValue();
338+
+ "[" + testCaseType + "]"
339+
+ ": " + testCaseDocument.getString("description").getValue();
348340
}
349341

350342
private String stripWhiteSpace(final String json) {

0 commit comments

Comments
 (0)