24
24
import org .bson .json .JsonParseException ;
25
25
import org .bson .json .JsonWriterSettings ;
26
26
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 ;
31
30
import util .Hex ;
32
31
import util .JsonPoweredTestHelper ;
33
32
40
39
import java .nio .charset .StandardCharsets ;
41
40
import java .util .ArrayList ;
42
41
import java .util .Arrays ;
43
- import java .util .Collection ;
44
42
import java .util .List ;
43
+ import java .util .stream .Stream ;
45
44
46
45
import static java .lang .String .format ;
47
46
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 ;
51
51
52
52
// BSON tests powered by language-agnostic JSON-based tests included in test resources
53
- @ RunWith (Parameterized .class )
54
53
public class GenericBsonTest {
55
54
56
55
private static final List <String > IGNORED_PARSE_ERRORS = Arrays .asList (
@@ -65,35 +64,26 @@ enum TestCaseType {
65
64
PARSE_ERROR
66
65
}
67
66
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 ,
73
70
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 () {
81
71
switch (testCaseType ) {
82
72
case VALID :
83
- runValid ();
73
+ runValid (testCase );
84
74
break ;
85
75
case DECODE_ERROR :
86
- runDecodeError ();
76
+ runDecodeError (testCase );
87
77
break ;
88
78
case PARSE_ERROR :
89
- runParseError ();
79
+ runParseError (testDefinition , testCase );
90
80
break ;
91
81
default :
92
82
throw new IllegalArgumentException (format ("Unsupported test case type %s" , testCaseType ));
93
83
}
94
84
}
95
85
96
- private void runValid () {
86
+ private void runValid (final BsonDocument testCase ) {
97
87
String description = testCase .getString ("description" ).getValue ();
98
88
String canonicalBsonHex = testCase .getString ("canonical_bson" ).getValue ().toUpperCase ();
99
89
String degenerateBsonHex = testCase .getString ("degenerate_bson" , new BsonString ("" )).getValue ().toUpperCase ();
@@ -105,50 +95,51 @@ private void runValid() {
105
95
BsonDocument decodedDocument = decodeToDocument (canonicalBsonHex , description );
106
96
107
97
// 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 ));
110
100
111
101
JsonWriterSettings canonicalJsonWriterSettings = JsonWriterSettings .builder ().outputMode (JsonMode .EXTENDED ).build ();
112
102
JsonWriterSettings relaxedJsonWriterSettings = JsonWriterSettings .builder ().outputMode (JsonMode .RELAXED ).build ();
113
103
114
104
if (!canonicalJson .isEmpty ()) {
115
105
// 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 ));
118
108
119
109
// native_to_canonical_extended_json( json_to_native(cEJ) ) = cEJ
120
110
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" );
123
113
124
114
if (!lossy ) {
125
115
// 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" );
128
118
}
129
119
}
130
120
131
121
if (!relaxedJson .isEmpty ()) {
132
122
// 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 ));
135
125
136
126
// 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" );
139
129
}
140
130
141
131
if (!degenerateJson .isEmpty ()) {
142
132
// 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" );
145
135
}
146
136
147
137
if (!degenerateBsonHex .isEmpty ()) {
148
138
BsonDocument decodedDegenerateDocument = decodeToDocument (degenerateBsonHex , description );
149
139
// 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 ));
152
143
}
153
144
}
154
145
@@ -223,7 +214,7 @@ private BsonDocument decodeToDocument(final String subjectHex, final String desc
223
214
224
215
if (byteBuffer .hasRemaining ()) {
225
216
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 " ,
227
218
description ));
228
219
}
229
220
return actualDecodedDocument ;
@@ -235,20 +226,20 @@ private String encodeToHex(final BsonDocument decodedDocument) {
235
226
return Hex .encode (outputBuffer .toByteArray ());
236
227
}
237
228
238
- private void runDecodeError () {
229
+ private void runDecodeError (final BsonDocument testCase ) {
239
230
try {
240
231
String description = testCase .getString ("description" ).getValue ();
241
- throwIfValueIsStringContainingReplacementCharacter (description );
232
+ throwIfValueIsStringContainingReplacementCharacter (testCase , description );
242
233
fail (format ("Should have failed parsing for subject with description '%s'" , description ));
243
234
} catch (BsonSerializationException e ) {
244
235
// all good
245
236
}
246
237
}
247
238
248
- private void runParseError () {
239
+ private void runParseError (final BsonDocument testDefinition , final BsonDocument testCase ) {
249
240
String description = testCase .getString ("description" ).getValue ();
250
241
251
- Assume . assumeFalse (IGNORED_PARSE_ERRORS .contains (description ));
242
+ assumeFalse (IGNORED_PARSE_ERRORS .contains (description ));
252
243
253
244
String str = testCase .getString ("string" ).getValue ();
254
245
@@ -290,7 +281,7 @@ private boolean isTestOfNullByteInCString(final String description) {
290
281
291
282
// Working around the fact that the Java driver doesn't report an error for invalid UTF-8, but rather replaces the invalid
292
283
// sequence with the replacement character
293
- private void throwIfValueIsStringContainingReplacementCharacter (final String description ) {
284
+ private void throwIfValueIsStringContainingReplacementCharacter (final BsonDocument testCase , final String description ) {
294
285
BsonDocument decodedDocument = decodeToDocument (testCase .getString ("bson" ).getValue (), description );
295
286
BsonValue value = decodedDocument .get (decodedDocument .getFirstKey ());
296
287
@@ -312,39 +303,40 @@ private void throwIfValueIsStringContainingReplacementCharacter(final String des
312
303
if (decodedString .contains (StandardCharsets .UTF_8 .newDecoder ().replacement ())) {
313
304
throw new BsonSerializationException ("String contains replacement character" );
314
305
}
315
- }
306
+ }
316
307
317
308
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 <>();
321
311
for (File file : JsonPoweredTestHelper .getTestFiles ("/bson" )) {
322
312
BsonDocument testDocument = JsonPoweredTestHelper .getTestDocument (file );
323
313
for (BsonValue curValue : testDocument .getArray ("valid" , new BsonArray ())) {
324
314
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 ));
327
318
}
328
319
329
320
for (BsonValue curValue : testDocument .getArray ("decodeErrors" , new BsonArray ())) {
330
321
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 ));
333
325
}
334
326
for (BsonValue curValue : testDocument .getArray ("parseErrors" , new BsonArray ())) {
335
327
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 ) );
338
330
}
339
331
}
340
- return data ;
332
+ return data . stream () ;
341
333
}
342
334
343
335
private static String createTestCaseDescription (final BsonDocument testDocument , final BsonDocument testCaseDocument ,
344
- final String testCaseType ) {
336
+ final String testCaseType ) {
345
337
return testDocument .getString ("description" ).getValue ()
346
- + "[" + testCaseType + "]"
347
- + ": " + testCaseDocument .getString ("description" ).getValue ();
338
+ + "[" + testCaseType + "]"
339
+ + ": " + testCaseDocument .getString ("description" ).getValue ();
348
340
}
349
341
350
342
private String stripWhiteSpace (final String json ) {
0 commit comments