From 34177db697ee56f8e7a41f6b5998404681baf251 Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Thu, 21 Nov 2024 10:46:51 +0100 Subject: [PATCH] Use assertThrows (#142) Use assertThrows instead of try-fail-catch where possible. --- .../eclipse/parsson/tests/JsonArrayTest.java | 21 +-- .../tests/JsonBigDecimalLengthLimitTest.java | 14 +- .../tests/JsonBigDecimalScaleLimitTest.java | 26 ++-- .../parsson/tests/JsonDuplicateKeyTest.java | 28 +--- .../eclipse/parsson/tests/JsonFieldTest.java | 18 +-- .../parsson/tests/JsonGeneratorTest.java | 127 +++++------------- .../eclipse/parsson/tests/JsonNumberTest.java | 95 ++++++------- .../eclipse/parsson/tests/JsonObjectTest.java | 36 ++--- .../eclipse/parsson/tests/JsonParserTest.java | 88 +++--------- .../eclipse/parsson/tests/JsonReaderTest.java | 16 +-- 10 files changed, 148 insertions(+), 321 deletions(-) diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java index 814bb80..80f5b87 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java @@ -111,23 +111,17 @@ void testIntValue() { @Test void testAdd() { JsonArray array = Json.createArrayBuilder().build(); - try { + Assertions.assertThrows(UnsupportedOperationException.class, () -> { array.add(JsonValue.FALSE); - Assertions.fail("JsonArray#add() should throw UnsupportedOperationException"); - } catch(UnsupportedOperationException e) { - // Expected - } + }, "JsonArray#add() should throw UnsupportedOperationException"); } @Test void testRemove() { JsonArray array = Json.createArrayBuilder().build(); - try { + Assertions.assertThrows(UnsupportedOperationException.class, () -> { array.remove(0); - Assertions.fail("JsonArray#remove() should throw UnsupportedOperationException"); - } catch(UnsupportedOperationException e) { - // Expected - } + }, "JsonArray#remove() should throw UnsupportedOperationException"); } @Test @@ -145,12 +139,9 @@ void testNumberView() { @Test void testArrayBuilderNpe() { - try { + Assertions.assertThrows(NullPointerException.class, () -> { Json.createArrayBuilder().add((JsonValue)null).build(); - Assertions.fail("JsonArrayBuilder#add(null) should throw NullPointerException"); - } catch(NullPointerException e) { - // Expected - } + }, "JsonArrayBuilder#add(null) should throw NullPointerException"); } @Test diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java index 89c6a9f..10f4d4d 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java @@ -61,15 +61,11 @@ void testLargeBigDecimalBellowLimit() { @Test void testLargeBigDecimalAboveLimit() { JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_501)); - try { - reader.readValue(); - Assertions.fail("No exception was thrown from BigDecimal parsing with source characters array length over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - Assertions.assertEquals( - "Number of BigDecimal source characters 501 exceeded maximal allowed value of 500", - e.getMessage()); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, reader::readValue, + "No exception was thrown from BigDecimal parsing with source characters array length over limit"); + Assertions.assertEquals( + "Number of BigDecimal source characters 501 exceeded maximal allowed value of 500", + e.getMessage()); } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java index eba2666..52f9b24 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java @@ -62,14 +62,11 @@ void testSystemPropertyBigIntegerScaleBellowLimit() { void testSystemPropertyBigIntegerScaleAboveLimit() { BigDecimal value = new BigDecimal("3.1415926535897932384626433") .setScale(50001, RoundingMode.HALF_UP); - try { - Json.createValue(value).bigIntegerValue(); - Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - JsonNumberTest.assertExceptionMessageContainsNumber(e, 50001); - JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + () -> Json.createValue(value).bigIntegerValue(), + "No exception was thrown from bigIntegerValue with scale over limit"); + JsonNumberTest.assertExceptionMessageContainsNumber(e, 50001); + JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); System.clearProperty("org.eclipse.parsson.maxBigIntegerScale"); } @@ -81,14 +78,11 @@ void testSystemPropertyBigIntegerScaleAboveLimit() { void testSystemPropertyBigIntegerNegScaleAboveLimit() { BigDecimal value = new BigDecimal("3.1415926535897932384626433") .setScale(-50001, RoundingMode.HALF_UP); - try { - Json.createValue(value).bigIntegerValue(); - Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - JsonNumberTest.assertExceptionMessageContainsNumber(e, -50001); - JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + () -> Json.createValue(value).bigIntegerValue(), + "No exception was thrown from bigIntegerValue with scale over limit"); + JsonNumberTest.assertExceptionMessageContainsNumber(e, -50001); + JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); System.clearProperty("org.eclipse.parsson.maxBigIntegerScale"); } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java index 10ff23d..b8b9d03 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java @@ -46,13 +46,8 @@ void testJsonReaderDuplicateKey2() { String json = "{\"a\":\"b\",\"a\":\"c\"}"; JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json)); - try { - jsonReader.readObject(); - Assertions.fail(); - } catch (Exception e) { - Assertions.assertTrue(e instanceof JsonParsingException); - Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); - } + JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject); + Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); } @Test @@ -68,13 +63,8 @@ void testJsonReaderDuplicateKey4() { String json = "{\"a\":\"b\",\"b\":{\"c\":\"d\",\"c\":\"e\"}}"; JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json)); - try { - jsonReader.readObject(); - Assertions.fail(); - } catch (Exception e) { - Assertions.assertTrue(e instanceof JsonParsingException); - Assertions.assertEquals("Duplicate key 'c' is not allowed", e.getMessage()); - } + JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject); + Assertions.assertEquals("Duplicate key 'c' is not allowed", e.getMessage()); } @Test @@ -88,12 +78,8 @@ void testJsonObjectBuilderDuplcateKey1() { void testJsonObjectBuilderDuplcateKey2() { JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); JsonObjectBuilder objectBuilder = jsonBuilderFactory.createObjectBuilder(); - try { - objectBuilder.add("a", "b").add("a", "c").build(); - Assertions.fail(); - } catch (Exception e) { - Assertions.assertTrue(e instanceof IllegalStateException); - Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); - } + IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> + objectBuilder.add("a", "b").add("a", "c").build()); + Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java index 1f43218..b6aa9cf 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java @@ -124,12 +124,9 @@ void testFailFieldInField() { generator.writeStartObject(); generator.writeKey("f1Name"); - try { - generator.write("f2Name", "f2Value"); - Assertions.fail("Field value, start object/array expected"); - } catch (JsonGenerationException exception) { - //ok - } + Assertions.assertThrows(JsonGenerationException.class, + () -> generator.write("f2Name", "f2Value"), + "Field value, start object/array expected"); } @@ -140,12 +137,9 @@ void testFailFieldKeyInArray() { generator.writeStartArray(); - try { - generator.writeKey("f1Value"); - Assertions.fail("Not allowed in array ."); - } catch (JsonGenerationException exception) { - //ok - } + Assertions.assertThrows(JsonGenerationException.class, + () -> generator.writeKey("f1Value"), + "Not allowed in array ."); } @Test diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java index eca28b5..8cd212e 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java @@ -284,12 +284,8 @@ void testGenerationException1() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); - try { - generator.writeStartObject(); - Assertions.fail("Expected JsonGenerationException, writeStartObject() cannot be called more than once"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::writeStartObject, + "Expected JsonGenerationException, writeStartObject() cannot be called more than once"); } @Test @@ -297,24 +293,16 @@ void testGenerationException2() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); - try { - generator.writeStartArray(); - Assertions.fail("Expected JsonGenerationException, writeStartArray() is valid in no context"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::writeStartArray, + "Expected JsonGenerationException, writeStartArray() is valid in no context"); } @Test void testGenerationException3() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); - try { - generator.close(); - Assertions.fail("Expected JsonGenerationException, no JSON is generated"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::close, + "Expected JsonGenerationException, no JSON is generated"); } @Test @@ -322,12 +310,8 @@ void testGenerationException4() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray(); - try { - generator.close(); - Assertions.fail("Expected JsonGenerationException, writeEnd() is not called"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::close, + "Expected JsonGenerationException, writeEnd() is not called"); } @Test @@ -335,12 +319,8 @@ void testGenerationException5() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); - try { - generator.close(); - Assertions.fail("Expected JsonGenerationException, writeEnd() is not called"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::close, + "Expected JsonGenerationException, writeEnd() is not called"); } @Test @@ -348,12 +328,8 @@ void testGenerationException6() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject().writeEnd(); - try { - generator.writeStartObject(); - Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::writeStartObject, + "Expected JsonGenerationException, cannot generate one more JSON text"); } @Test @@ -361,12 +337,8 @@ void testGenerationException7() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray().writeEnd(); - try { - generator.writeStartArray(); - Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, generator::writeStartArray, + "Expected JsonGenerationException, cannot generate one more JSON text"); } @@ -375,12 +347,8 @@ void testGenerationException8() { StringWriter sWriter = new StringWriter(); JsonGenerator generator = Json.createGenerator(sWriter); generator.writeStartObject(); - try { - generator.write(JsonValue.TRUE); - Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, () -> generator.write(JsonValue.TRUE), + "Expected JsonGenerationException, cannot generate one more JSON text"); } @Test @@ -388,12 +356,8 @@ void testGenerationException9() { StringWriter sWriter = new StringWriter(); JsonGenerator generator = Json.createGenerator(sWriter); generator.writeStartObject(); - try { - generator.write("name"); - Assertions.fail("Expected JsonGenerationException, cannot generate one more JSON text"); - } catch (JsonGenerationException je) { - // Expected exception - } + Assertions.assertThrows(JsonGenerationException.class, () -> generator.write("name"), + "Expected JsonGenerationException, cannot generate one more JSON text"); } @Test @@ -401,24 +365,15 @@ void testGeneratorArrayDouble() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray(); - try { - generator.write(Double.NaN); - Assertions.fail("JsonGenerator.write(Double.NaN) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } - try { - generator.write(Double.POSITIVE_INFINITY); - Assertions.fail("JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } - try { - generator.write(Double.NEGATIVE_INFINITY); - Assertions.fail("JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } + Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.NaN), + "JsonGenerator.write(Double.NaN) should produce NumberFormatException"); + + Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.POSITIVE_INFINITY), + "JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException"); + + Assertions.assertThrows(NumberFormatException.class, () -> generator.write(Double.NEGATIVE_INFINITY), + "JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException"); + generator.writeEnd(); generator.close(); } @@ -428,24 +383,16 @@ void testGeneratorObjectDouble() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); - try { - generator.write("foo", Double.NaN); - Assertions.fail("JsonGenerator.write(String, Double.NaN) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } - try { - generator.write("foo", Double.POSITIVE_INFINITY); - Assertions.fail("JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } - try { - generator.write("foo", Double.NEGATIVE_INFINITY); - Assertions.fail("JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException"); - } catch (NumberFormatException ne) { - // expected - } + + Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.NaN), + "JsonGenerator.write(String, Double.NaN) should produce NumberFormatException"); + + Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.POSITIVE_INFINITY), + "JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException"); + + Assertions.assertThrows(NumberFormatException.class, () -> generator.write("foo", Double.NEGATIVE_INFINITY), + "JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException"); + generator.writeEnd(); generator.close(); } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java index 58f01da..d28edd3 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java @@ -241,13 +241,9 @@ void testLeadingZeroes() { @Test void testBigIntegerExact() { - try { - JsonArray array = Json.createArrayBuilder().add(12345.12345).build(); - array.getJsonNumber(0).bigIntegerValueExact(); - Assertions.fail("Expected Arithmetic exception"); - } catch (ArithmeticException expected) { - // no-op - } + JsonArray array = Json.createArrayBuilder().add(12345.12345).build(); + Assertions.assertThrows(ArithmeticException.class, () -> array.getJsonNumber(0).bigIntegerValueExact(), + "Expected Arithmetic exception"); } @Test @@ -293,14 +289,11 @@ void testDefaultBigIntegerScaleBellowLimit() { void testDefaultBigIntegerScaleAboveLimit() { BigDecimal value = new BigDecimal("3.1415926535897932384626433") .setScale(100001, RoundingMode.HALF_UP); - try { - Json.createValue(value).bigIntegerValue(); - Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - assertExceptionMessageContainsNumber(e, 100001); - assertExceptionMessageContainsNumber(e, DEFAULT_MAX_BIGINTEGER_SCALE); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + () -> Json.createValue(value).bigIntegerValue(), + "No exception was thrown from bigIntegerValue with scale over limit"); + assertExceptionMessageContainsNumber(e, 100001); + assertExceptionMessageContainsNumber(e, DEFAULT_MAX_BIGINTEGER_SCALE); } // Test default BigInteger scale value limit using negative value above limit. @@ -329,18 +322,15 @@ void testConfigBigIntegerScaleAboveLimit() { BigDecimal value = new BigDecimal("3.1415926535897932384626433") .setScale(50001, RoundingMode.HALF_UP); Map config = Map.of(JsonConfig.MAX_BIGINTEGER_SCALE, "50000"); - try { - JsonObject jsonObject = Json.createBuilderFactory(config) - .createObjectBuilder() - .add("bigDecimal", value) - .build(); - jsonObject.getJsonNumber("bigDecimal").bigIntegerValue(); - Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - assertExceptionMessageContainsNumber(e, 50001); - assertExceptionMessageContainsNumber(e, 50000); - } + JsonObject jsonObject = Json.createBuilderFactory(config) + .createObjectBuilder() + .add("bigDecimal", value) + .build(); + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + () -> jsonObject.getJsonNumber("bigDecimal").bigIntegerValue(), + "No exception was thrown from bigIntegerValue with scale over limit"); + assertExceptionMessageContainsNumber(e, 50001); + assertExceptionMessageContainsNumber(e, 50000); } // Test BigInteger scale value limit set from config Map using value above limit. @@ -353,18 +343,15 @@ void testConfigBigIntegerNegScaleAboveLimit() { BigDecimal value = new BigDecimal("3.1415926535897932384626433") .setScale(-50001, RoundingMode.HALF_UP); Map config = Map.of(JsonConfig.MAX_BIGINTEGER_SCALE, "50000"); - try { - JsonObject jsonObject = Json.createBuilderFactory(config) - .createObjectBuilder() - .add("bigDecimal", value) - .build(); - jsonObject.getJsonNumber("bigDecimal").bigIntegerValue(); - Assertions.fail("No exception was thrown from bigIntegerValue with scale over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - assertExceptionMessageContainsNumber(e, -50001); - assertExceptionMessageContainsNumber(e, 50000); - } + JsonObject jsonObject = Json.createBuilderFactory(config) + .createObjectBuilder() + .add("bigDecimal", value) + .build(); + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + () -> jsonObject.getJsonNumber("bigDecimal").bigIntegerValue(), + "No exception was thrown from bigIntegerValue with scale over limit"); + assertExceptionMessageContainsNumber(e, -50001); + assertExceptionMessageContainsNumber(e, 50000); } // Test BigDecimal max source characters array length using length equal to default limit of 1100. @@ -383,15 +370,12 @@ void testLargeBigDecimalBellowLimit() { @Test void testLargeBigDecimalAboveLimit() { JsonReader reader = Json.createReader(new StringReader(Π_1101)); - try { - reader.readValue(); - Assertions.fail("No exception was thrown from BigDecimal parsing with source characters array length over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - Assertions.assertEquals( - "Number of BigDecimal source characters 1101 exceeded maximal allowed value of 1100", - e.getMessage()); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + reader::readValue, + "No exception was thrown from BigDecimal parsing with source characters array length over limit"); + Assertions.assertEquals( + "Number of BigDecimal source characters 1101 exceeded maximal allowed value of 1100", + e.getMessage()); } // Test BigDecimal max source characters array length using length equal to custom limit of 500. @@ -412,15 +396,12 @@ void testLargeBigDecimalBellowCustomLimit() { void testLargeBigDecimalAboveCustomLimit() { Map config = Map.of(JsonConfig.MAX_BIGDECIMAL_LEN, "500"); JsonReader reader = Json.createReaderFactory(config).createReader(new StringReader(Π_501)); - try { - reader.readValue(); - Assertions.fail("No exception was thrown from BigDecimal parsing with source characters array length over limit"); - } catch (UnsupportedOperationException e) { - // UnsupportedOperationException is expected to be thrown - Assertions.assertEquals( - "Number of BigDecimal source characters 501 exceeded maximal allowed value of 500", - e.getMessage()); - } + UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, + reader::readValue, + "No exception was thrown from BigDecimal parsing with source characters array length over limit"); + Assertions.assertEquals( + "Number of BigDecimal source characters 501 exceeded maximal allowed value of 500", + e.getMessage()); } static void assertExceptionMessageContainsNumber(Exception e, int number) { diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java index cc27648..a9fe4db 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java @@ -133,34 +133,25 @@ static void testEmpty(JsonObject empty) { void testClassCastException() { JsonObject obj = Json.createObjectBuilder() .add("foo", JsonValue.FALSE).build(); - try { - obj.getJsonNumber("foo"); - Assertions.fail("Expected ClassCastException for casting JsonValue.FALSE to JsonNumber"); - } catch (ClassCastException ce) { - // Expected - } + Assertions.assertThrows(ClassCastException.class, + () -> obj.getJsonNumber("foo"), + "Expected ClassCastException for casting JsonValue.FALSE to JsonNumber"); } @Test void testPut() { JsonObject obj = Json.createObjectBuilder().add("foo", 1).build(); - try { - obj.put("bar", JsonValue.FALSE); - Assertions.fail("JsonObject#put() should throw UnsupportedOperationException"); - } catch(UnsupportedOperationException e) { - // Expected - } + Assertions.assertThrows(UnsupportedOperationException.class, + () -> obj.put("bar", JsonValue.FALSE), + "JsonObject#put() should throw UnsupportedOperationException"); } @Test void testRemove() { JsonObject obj = Json.createObjectBuilder().add("foo", 1).build(); - try { - obj.remove("foo"); - Assertions.fail("JsonObject#remove() should throw UnsupportedOperationException"); - } catch(UnsupportedOperationException e) { - // Expected - } + Assertions.assertThrows(UnsupportedOperationException.class, + () -> obj.remove("foo"), + "JsonObject#remove() should throw UnsupportedOperationException"); } @Test @@ -222,12 +213,9 @@ void testObjectBuilderWithMap() { @Test void testObjectBuilderNpe() { - try { - Json.createObjectBuilder().add(null, 1).build(); - Assertions.fail("JsonObjectBuilder#add(null, 1) should throw NullPointerException"); - } catch(NullPointerException e) { - // Expected - } + Assertions.assertThrows(NullPointerException.class, + () -> Json.createObjectBuilder().add(null, 1).build(), + "JsonObjectBuilder#add(null, 1) should throw NullPointerException"); } @Test diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java index d206851..822b763 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java @@ -16,10 +16,6 @@ package org.eclipse.parsson.tests; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; @@ -42,7 +38,6 @@ import java.util.Scanner; import jakarta.json.stream.JsonParsingException; -import org.eclipse.parsson.JsonParserFixture; import org.eclipse.parsson.api.BufferPool; import org.junit.jupiter.api.Assertions; @@ -190,11 +185,8 @@ static void testEmptyArrayIterator(JsonParser parser) { Assertions.assertFalse(parser.hasNext()); Assertions.assertFalse(parser.hasNext()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ignored) { - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @@ -218,11 +210,8 @@ static void testEmptyArrayIterator2(JsonParser parser) { Assertions.assertEquals(Event.START_ARRAY, parser.currentEvent()); Assertions.assertEquals(Event.END_ARRAY, parser.next()); Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ignored) { - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @Test @@ -246,11 +235,8 @@ static void testEmptyArrayIterator3(JsonParser parser) { Assertions.assertEquals(Event.END_ARRAY, parser.next()); Assertions.assertEquals(Event.END_ARRAY, parser.currentEvent()); Assertions.assertFalse(parser.hasNext()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ignored) { - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @@ -321,11 +307,8 @@ static void testEmptyObjectIterator(JsonParser parser) { Assertions.assertFalse(parser.hasNext()); Assertions.assertFalse(parser.hasNext()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ignored) { - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @@ -347,11 +330,8 @@ void testEmptyObjectIterator2Structure() { static void testEmptyObjectIterator2(JsonParser parser) { Assertions.assertEquals(Event.START_OBJECT, parser.next()); Assertions.assertEquals(Event.END_OBJECT, parser.next()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ignored) { - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @@ -374,12 +354,8 @@ static void testEmptyObjectIterator3(JsonParser parser) { Assertions.assertEquals(Event.START_OBJECT, parser.next()); Assertions.assertEquals(Event.END_OBJECT, parser.next()); Assertions.assertFalse(parser.hasNext()); - try { - parser.next(); - Assertions.fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { - // expected - } + Assertions.assertThrows(NoSuchElementException.class, parser::next, + "Should have thrown a NoSuchElementException"); } @@ -685,40 +661,20 @@ static void testExceptions(JsonParser parser) { Event event = parser.next(); Assertions.assertEquals(Event.START_OBJECT, event); - try { - parser.getString(); - Assertions.fail("JsonParser#getString() should have thrown exception in START_OBJECT state"); - } catch (IllegalStateException expected) { - // no-op - } + Assertions.assertThrows(IllegalStateException.class, parser::getString, + "JsonParser#getString() should have thrown exception in START_OBJECT state"); - try { - parser.isIntegralNumber(); - Assertions.fail("JsonParser#getNumberType() should have thrown exception in START_OBJECT state"); - } catch (IllegalStateException expected) { - // no-op - } + Assertions.assertThrows(IllegalStateException.class, parser::isIntegralNumber, + "JsonParser#getNumberType() should have thrown exception in START_OBJECT state"); - try { - parser.getInt(); - Assertions.fail("JsonParser#getInt() should have thrown exception in START_OBJECT state"); - } catch (IllegalStateException expected) { - // no-op - } + Assertions.assertThrows(IllegalStateException.class, parser::getInt, + "JsonParser#getInt() should have thrown exception in START_OBJECT state"); - try { - parser.getLong(); - Assertions.fail("JsonParser#getLong() should have thrown exception in START_OBJECT state"); - } catch (IllegalStateException expected) { - // no-op - } + Assertions.assertThrows(IllegalStateException.class, parser::getLong, + "JsonParser#getLong() should have thrown exception in START_OBJECT state"); - try { - parser.getBigDecimal(); - Assertions.fail("JsonParser#getBigDecimal() should have thrown exception in START_OBJECT state"); - } catch (IllegalStateException expected) { - // no-op - } + Assertions.assertThrows(IllegalStateException.class, parser::getBigDecimal, + "JsonParser#getBigDecimal() should have thrown exception in START_OBJECT state"); } static String wikiString() { diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java index c8ed5a1..a886344 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java @@ -105,9 +105,7 @@ void testUnknownFeature() { JsonReaderFactory factory = Json.createReaderFactory(config); factory.createReader(new StringReader("{}")); Map config1 = factory.getConfigInUse(); - if (!config1.isEmpty()) { - Assertions.fail("Shouldn't have any config in use"); - } + Assertions.assertTrue(config1.isEmpty(), "Shouldn't have any config in use"); } @Test @@ -192,10 +190,8 @@ void testDuplicateKeysStrict() { JsonReaderFactory factory = Json.createReaderFactory(config); String json = "{\"val1\":\"A\",\"val1\":\"B\"}"; JsonReader reader = factory.createReader(new StringReader(json)); - try { - reader.readObject(); - Assertions.fail("It is expected a JsonException"); - } catch (JsonException ignored) {} + Assertions.assertThrows(JsonException.class, reader::readObject, + "It is expected a JsonException"); } @Test @@ -205,10 +201,8 @@ void testDuplicateKeysStrictWithParssonConfig() { JsonReaderFactory factory = Json.createReaderFactory(config); String json = "{\"val1\":\"A\",\"val1\":\"B\"}"; JsonReader reader = factory.createReader(new StringReader(json)); - try { - reader.readObject(); - Assertions.fail("It is expected a JsonException"); - } catch (JsonException ignored) {} + Assertions.assertThrows(JsonException.class, reader::readObject, + "It is expected a JsonException"); } @Test