Skip to content

Commit 34177db

Browse files
authored
Use assertThrows (#142)
Use assertThrows instead of try-fail-catch where possible.
1 parent 9d67960 commit 34177db

File tree

10 files changed

+148
-321
lines changed

10 files changed

+148
-321
lines changed

impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,17 @@ void testIntValue() {
111111
@Test
112112
void testAdd() {
113113
JsonArray array = Json.createArrayBuilder().build();
114-
try {
114+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
115115
array.add(JsonValue.FALSE);
116-
Assertions.fail("JsonArray#add() should throw UnsupportedOperationException");
117-
} catch(UnsupportedOperationException e) {
118-
// Expected
119-
}
116+
}, "JsonArray#add() should throw UnsupportedOperationException");
120117
}
121118

122119
@Test
123120
void testRemove() {
124121
JsonArray array = Json.createArrayBuilder().build();
125-
try {
122+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
126123
array.remove(0);
127-
Assertions.fail("JsonArray#remove() should throw UnsupportedOperationException");
128-
} catch(UnsupportedOperationException e) {
129-
// Expected
130-
}
124+
}, "JsonArray#remove() should throw UnsupportedOperationException");
131125
}
132126

133127
@Test
@@ -145,12 +139,9 @@ void testNumberView() {
145139

146140
@Test
147141
void testArrayBuilderNpe() {
148-
try {
142+
Assertions.assertThrows(NullPointerException.class, () -> {
149143
Json.createArrayBuilder().add((JsonValue)null).build();
150-
Assertions.fail("JsonArrayBuilder#add(null) should throw NullPointerException");
151-
} catch(NullPointerException e) {
152-
// Expected
153-
}
144+
}, "JsonArrayBuilder#add(null) should throw NullPointerException");
154145
}
155146

156147
@Test

impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ void testLargeBigDecimalBellowLimit() {
6161
@Test
6262
void testLargeBigDecimalAboveLimit() {
6363
JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_501));
64-
try {
65-
reader.readValue();
66-
Assertions.fail("No exception was thrown from BigDecimal parsing with source characters array length over limit");
67-
} catch (UnsupportedOperationException e) {
68-
// UnsupportedOperationException is expected to be thrown
69-
Assertions.assertEquals(
70-
"Number of BigDecimal source characters 501 exceeded maximal allowed value of 500",
71-
e.getMessage());
72-
}
64+
UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, reader::readValue,
65+
"No exception was thrown from BigDecimal parsing with source characters array length over limit");
66+
Assertions.assertEquals(
67+
"Number of BigDecimal source characters 501 exceeded maximal allowed value of 500",
68+
e.getMessage());
7369
}
7470

7571
}

impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ void testSystemPropertyBigIntegerScaleBellowLimit() {
6262
void testSystemPropertyBigIntegerScaleAboveLimit() {
6363
BigDecimal value = new BigDecimal("3.1415926535897932384626433")
6464
.setScale(50001, RoundingMode.HALF_UP);
65-
try {
66-
Json.createValue(value).bigIntegerValue();
67-
Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit");
68-
} catch (UnsupportedOperationException e) {
69-
// UnsupportedOperationException is expected to be thrown
70-
JsonNumberTest.assertExceptionMessageContainsNumber(e, 50001);
71-
JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE);
72-
}
65+
UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class,
66+
() -> Json.createValue(value).bigIntegerValue(),
67+
"No exception was thrown from bigIntegerValue with scale over limit");
68+
JsonNumberTest.assertExceptionMessageContainsNumber(e, 50001);
69+
JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE);
7370
System.clearProperty("org.eclipse.parsson.maxBigIntegerScale");
7471
}
7572

@@ -81,14 +78,11 @@ void testSystemPropertyBigIntegerScaleAboveLimit() {
8178
void testSystemPropertyBigIntegerNegScaleAboveLimit() {
8279
BigDecimal value = new BigDecimal("3.1415926535897932384626433")
8380
.setScale(-50001, RoundingMode.HALF_UP);
84-
try {
85-
Json.createValue(value).bigIntegerValue();
86-
Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit");
87-
} catch (UnsupportedOperationException e) {
88-
// UnsupportedOperationException is expected to be thrown
89-
JsonNumberTest.assertExceptionMessageContainsNumber(e, -50001);
90-
JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE);
91-
}
81+
UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class,
82+
() -> Json.createValue(value).bigIntegerValue(),
83+
"No exception was thrown from bigIntegerValue with scale over limit");
84+
JsonNumberTest.assertExceptionMessageContainsNumber(e, -50001);
85+
JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE);
9286
System.clearProperty("org.eclipse.parsson.maxBigIntegerScale");
9387
}
9488

impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,8 @@ void testJsonReaderDuplicateKey2() {
4646
String json = "{\"a\":\"b\",\"a\":\"c\"}";
4747
JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE));
4848
JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json));
49-
try {
50-
jsonReader.readObject();
51-
Assertions.fail();
52-
} catch (Exception e) {
53-
Assertions.assertTrue(e instanceof JsonParsingException);
54-
Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage());
55-
}
49+
JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject);
50+
Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage());
5651
}
5752

5853
@Test
@@ -68,13 +63,8 @@ void testJsonReaderDuplicateKey4() {
6863
String json = "{\"a\":\"b\",\"b\":{\"c\":\"d\",\"c\":\"e\"}}";
6964
JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE));
7065
JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json));
71-
try {
72-
jsonReader.readObject();
73-
Assertions.fail();
74-
} catch (Exception e) {
75-
Assertions.assertTrue(e instanceof JsonParsingException);
76-
Assertions.assertEquals("Duplicate key 'c' is not allowed", e.getMessage());
77-
}
66+
JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject);
67+
Assertions.assertEquals("Duplicate key 'c' is not allowed", e.getMessage());
7868
}
7969

8070
@Test
@@ -88,12 +78,8 @@ void testJsonObjectBuilderDuplcateKey1() {
8878
void testJsonObjectBuilderDuplcateKey2() {
8979
JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE));
9080
JsonObjectBuilder objectBuilder = jsonBuilderFactory.createObjectBuilder();
91-
try {
92-
objectBuilder.add("a", "b").add("a", "c").build();
93-
Assertions.fail();
94-
} catch (Exception e) {
95-
Assertions.assertTrue(e instanceof IllegalStateException);
96-
Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage());
97-
}
81+
IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () ->
82+
objectBuilder.add("a", "b").add("a", "c").build());
83+
Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage());
9884
}
9985
}

impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,9 @@ void testFailFieldInField() {
124124
generator.writeStartObject();
125125
generator.writeKey("f1Name");
126126

127-
try {
128-
generator.write("f2Name", "f2Value");
129-
Assertions.fail("Field value, start object/array expected");
130-
} catch (JsonGenerationException exception) {
131-
//ok
132-
}
127+
Assertions.assertThrows(JsonGenerationException.class,
128+
() -> generator.write("f2Name", "f2Value"),
129+
"Field value, start object/array expected");
133130
}
134131

135132

@@ -140,12 +137,9 @@ void testFailFieldKeyInArray() {
140137

141138
generator.writeStartArray();
142139

143-
try {
144-
generator.writeKey("f1Value");
145-
Assertions.fail("Not allowed in array .");
146-
} catch (JsonGenerationException exception) {
147-
//ok
148-
}
140+
Assertions.assertThrows(JsonGenerationException.class,
141+
() -> generator.writeKey("f1Value"),
142+
"Not allowed in array .");
149143
}
150144

151145
@Test

impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java

Lines changed: 37 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -284,89 +284,61 @@ void testGenerationException1() {
284284
StringWriter writer = new StringWriter();
285285
JsonGenerator generator = Json.createGenerator(writer);
286286
generator.writeStartObject();
287-
try {
288-
generator.writeStartObject();
289-
Assertions.fail("Expected JsonGenerationException, writeStartObject() cannot be called more than once");
290-
} catch (JsonGenerationException je) {
291-
// Expected exception
292-
}
287+
Assertions.assertThrows(JsonGenerationException.class, generator::writeStartObject,
288+
"Expected JsonGenerationException, writeStartObject() cannot be called more than once");
293289
}
294290

295291
@Test
296292
void testGenerationException2() {
297293
StringWriter writer = new StringWriter();
298294
JsonGenerator generator = Json.createGenerator(writer);
299295
generator.writeStartObject();
300-
try {
301-
generator.writeStartArray();
302-
Assertions.fail("Expected JsonGenerationException, writeStartArray() is valid in no context");
303-
} catch (JsonGenerationException je) {
304-
// Expected exception
305-
}
296+
Assertions.assertThrows(JsonGenerationException.class, generator::writeStartArray,
297+
"Expected JsonGenerationException, writeStartArray() is valid in no context");
306298
}
307299

308300
@Test
309301
void testGenerationException3() {
310302
StringWriter writer = new StringWriter();
311303
JsonGenerator generator = Json.createGenerator(writer);
312-
try {
313-
generator.close();
314-
Assertions.fail("Expected JsonGenerationException, no JSON is generated");
315-
} catch (JsonGenerationException je) {
316-
// Expected exception
317-
}
304+
Assertions.assertThrows(JsonGenerationException.class, generator::close,
305+
"Expected JsonGenerationException, no JSON is generated");
318306
}
319307

320308
@Test
321309
void testGenerationException4() {
322310
StringWriter writer = new StringWriter();
323311
JsonGenerator generator = Json.createGenerator(writer);
324312
generator.writeStartArray();
325-
try {
326-
generator.close();
327-
Assertions.fail("Expected JsonGenerationException, writeEnd() is not called");
328-
} catch (JsonGenerationException je) {
329-
// Expected exception
330-
}
313+
Assertions.assertThrows(JsonGenerationException.class, generator::close,
314+
"Expected JsonGenerationException, writeEnd() is not called");
331315
}
332316

333317
@Test
334318
void testGenerationException5() {
335319
StringWriter writer = new StringWriter();
336320
JsonGenerator generator = Json.createGenerator(writer);
337321
generator.writeStartObject();
338-
try {
339-
generator.close();
340-
Assertions.fail("Expected JsonGenerationException, writeEnd() is not called");
341-
} catch (JsonGenerationException je) {
342-
// Expected exception
343-
}
322+
Assertions.assertThrows(JsonGenerationException.class, generator::close,
323+
"Expected JsonGenerationException, writeEnd() is not called");
344324
}
345325

346326
@Test
347327
void testGenerationException6() {
348328
StringWriter writer = new StringWriter();
349329
JsonGenerator generator = Json.createGenerator(writer);
350330
generator.writeStartObject().writeEnd();
351-
try {
352-
generator.writeStartObject();
353-
Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text");
354-
} catch (JsonGenerationException je) {
355-
// Expected exception
356-
}
331+
Assertions.assertThrows(JsonGenerationException.class, generator::writeStartObject,
332+
"Expected JsonGenerationException, cannot generate one more JSON text");
357333
}
358334

359335
@Test
360336
void testGenerationException7() {
361337
StringWriter writer = new StringWriter();
362338
JsonGenerator generator = Json.createGenerator(writer);
363339
generator.writeStartArray().writeEnd();
364-
try {
365-
generator.writeStartArray();
366-
Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text");
367-
} catch (JsonGenerationException je) {
368-
// Expected exception
369-
}
340+
Assertions.assertThrows(JsonGenerationException.class, generator::writeStartArray,
341+
"Expected JsonGenerationException, cannot generate one more JSON text");
370342
}
371343

372344

@@ -375,50 +347,33 @@ void testGenerationException8() {
375347
StringWriter sWriter = new StringWriter();
376348
JsonGenerator generator = Json.createGenerator(sWriter);
377349
generator.writeStartObject();
378-
try {
379-
generator.write(JsonValue.TRUE);
380-
Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text");
381-
} catch (JsonGenerationException je) {
382-
// Expected exception
383-
}
350+
Assertions.assertThrows(JsonGenerationException.class, () -> generator.write(JsonValue.TRUE),
351+
"Expected JsonGenerationException, cannot generate one more JSON text");
384352
}
385353

386354
@Test
387355
void testGenerationException9() {
388356
StringWriter sWriter = new StringWriter();
389357
JsonGenerator generator = Json.createGenerator(sWriter);
390358
generator.writeStartObject();
391-
try {
392-
generator.write("name");
393-
Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text");
394-
} catch (JsonGenerationException je) {
395-
// Expected exception
396-
}
359+
Assertions.assertThrows(JsonGenerationException.class, () -> generator.write("name"),
360+
"Expected JsonGenerationException, cannot generate one more JSON text");
397361
}
398362

399363
@Test
400364
void testGeneratorArrayDouble() {
401365
StringWriter writer = new StringWriter();
402366
JsonGenerator generator = Json.createGenerator(writer);
403367
generator.writeStartArray();
404-
try {
405-
generator.write(Double.NaN);
406-
Assertions.fail("JsonGenerator.write(Double.NaN) should produce NumberFormatException");
407-
} catch (NumberFormatException ne) {
408-
// expected
409-
}
410-
try {
411-
generator.write(Double.POSITIVE_INFINITY);
412-
Assertions.fail("JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException");
413-
} catch (NumberFormatException ne) {
414-
// expected
415-
}
416-
try {
417-
generator.write(Double.NEGATIVE_INFINITY);
418-
Assertions.fail("JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException");
419-
} catch (NumberFormatException ne) {
420-
// expected
421-
}
368+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.NaN),
369+
"JsonGenerator.write(Double.NaN) should produce NumberFormatException");
370+
371+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.POSITIVE_INFINITY),
372+
"JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException");
373+
374+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.NEGATIVE_INFINITY),
375+
"JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException");
376+
422377
generator.writeEnd();
423378
generator.close();
424379
}
@@ -428,24 +383,16 @@ void testGeneratorObjectDouble() {
428383
StringWriter writer = new StringWriter();
429384
JsonGenerator generator = Json.createGenerator(writer);
430385
generator.writeStartObject();
431-
try {
432-
generator.write("foo", Double.NaN);
433-
Assertions.fail("JsonGenerator.write(String, Double.NaN) should produce NumberFormatException");
434-
} catch (NumberFormatException ne) {
435-
// expected
436-
}
437-
try {
438-
generator.write("foo", Double.POSITIVE_INFINITY);
439-
Assertions.fail("JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException");
440-
} catch (NumberFormatException ne) {
441-
// expected
442-
}
443-
try {
444-
generator.write("foo", Double.NEGATIVE_INFINITY);
445-
Assertions.fail("JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException");
446-
} catch (NumberFormatException ne) {
447-
// expected
448-
}
386+
387+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.NaN),
388+
"JsonGenerator.write(String, Double.NaN) should produce NumberFormatException");
389+
390+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.POSITIVE_INFINITY),
391+
"JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException");
392+
393+
Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.NEGATIVE_INFINITY),
394+
"JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException");
395+
449396
generator.writeEnd();
450397
generator.close();
451398
}

0 commit comments

Comments
 (0)