Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use assertThrows #142

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}
}
18 changes: 6 additions & 12 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand All @@ -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
Expand Down
127 changes: 37 additions & 90 deletions impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,89 +284,61 @@ 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
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
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
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
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
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");
}


Expand All @@ -375,50 +347,33 @@ 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
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
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();
}
Expand All @@ -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();
}
Expand Down
Loading
Loading