From ebf6534c2338330fa6b02b0f18e6a87c414d49d0 Mon Sep 17 00:00:00 2001 From: Anton Pinsky Date: Sun, 26 Nov 2023 00:22:26 +0100 Subject: [PATCH] Small improvements in the tests Signed-off-by: Anton Pinsky --- .../eclipse/parsson/tests/JsonArrayTest.java | 10 +-- .../parsson/tests/JsonBuilderTest.java | 8 +-- .../parsson/tests/JsonDuplicateKeyTest.java | 11 ++-- .../parsson/tests/JsonGeneratorTest.java | 32 +++++----- .../parsson/tests/JsonMergePatchDiffTest.java | 4 +- .../parsson/tests/JsonMergePatchTest.java | 4 +- .../eclipse/parsson/tests/JsonNumberTest.java | 14 ++-- .../eclipse/parsson/tests/JsonObjectTest.java | 14 ++-- .../eclipse/parsson/tests/JsonParserTest.java | 64 +++++++++---------- .../parsson/tests/JsonPatchBugsTest.java | 4 +- .../parsson/tests/JsonPatchDiffTest.java | 1 + .../parsson/tests/JsonPatchOperationTest.java | 4 +- .../eclipse/parsson/tests/JsonPatchTest.java | 1 + .../tests/JsonPointerRemoveOperationTest.java | 2 +- .../parsson/tests/JsonPointerTest.java | 9 +-- .../eclipse/parsson/tests/JsonReaderTest.java | 52 +++++++-------- .../parsson/tests/JsonSamplesParsingTest.java | 19 ++---- .../eclipse/parsson/tests/JsonStringTest.java | 6 +- .../eclipse/parsson/tests/JsonValueTest.java | 61 +++++------------- .../eclipse/parsson/tests/JsonWriterTest.java | 6 +- 20 files changed, 144 insertions(+), 182 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 79efec4..bab0801 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonArrayTest.java @@ -39,7 +39,7 @@ */ public class JsonArrayTest { @Test - void testArrayEquals() throws Exception { + void testArrayEquals() { JsonArray expected = Json.createArrayBuilder() .add(JsonValue.TRUE) .add(JsonValue.FALSE) @@ -96,7 +96,7 @@ void testArrayEqualsUsingCollection() { } @Test - void testStringValue() throws Exception { + void testStringValue() { JsonArray array = Json.createArrayBuilder() .add("John") .build(); @@ -104,7 +104,7 @@ void testStringValue() throws Exception { } @Test - void testIntValue() throws Exception { + void testIntValue() { JsonArray array = Json.createArrayBuilder() .add(20) .build(); @@ -134,7 +134,7 @@ void testRemove() { } @Test - void testNumberView() throws Exception { + void testNumberView() { JsonArray array = Json.createArrayBuilder().add(20).add(10).build(); List numberList = array.getValuesAs(JsonNumber.class); @@ -149,7 +149,7 @@ void testNumberView() throws Exception { @Test void testArrayBuilderNpe() { try { - JsonArray array = Json.createArrayBuilder().add((JsonValue)null).build(); + Json.createArrayBuilder().add((JsonValue)null).build(); fail("JsonArrayBuilder#add(null) should throw NullPointerException"); } catch(NullPointerException e) { // Expected diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonBuilderTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonBuilderTest.java index 79a00b5..3cbfad6 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonBuilderTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonBuilderTest.java @@ -39,7 +39,7 @@ public class JsonBuilderTest { @Test - void testEmptyObject() throws Exception { + void testEmptyObject() { JsonObject empty = Json.createObjectBuilder() .build(); @@ -47,7 +47,7 @@ void testEmptyObject() throws Exception { } @Test - void testEmptyArray() throws Exception { + void testEmptyArray() { JsonArray empty = Json.createArrayBuilder() .build(); @@ -55,13 +55,13 @@ void testEmptyArray() throws Exception { } @Test - void testObject() throws Exception { + void testObject() { JsonObject person = buildPerson(); JsonObjectTest.testPerson(person); } @Test - void testNumber() throws Exception { + void testNumber() { JsonObject person = buildPerson(); JsonNumber number = person.getJsonNumber("age"); assertEquals(25, number.intValueExact()); 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 8176500..caa2ac4 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java @@ -25,14 +25,13 @@ import jakarta.json.Json; import jakarta.json.JsonBuilderFactory; +import jakarta.json.JsonConfig; import jakarta.json.JsonObject; import jakarta.json.JsonObjectBuilder; import jakarta.json.JsonReader; import jakarta.json.JsonReaderFactory; import jakarta.json.stream.JsonParsingException; -import org.eclipse.parsson.api.JsonConfig; - import org.junit.jupiter.api.Test; @@ -48,7 +47,7 @@ void testJsonReaderDuplicateKey1() { @Test void testJsonReaderDuplicateKey2() { String json = "{\"a\":\"b\",\"a\":\"c\"}"; - JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.REJECT_DUPLICATE_KEYS, true)); + JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json)); try { jsonReader.readObject(); @@ -69,8 +68,8 @@ void testJsonReaderDuplicateKey3() { @Test void testJsonReaderDuplicateKey4() { - String json = "{\"a\":\"b\",\"b\":{\"c\":\"d\",\"c\":\"e\"}}";; - JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.REJECT_DUPLICATE_KEYS, true)); + 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(); @@ -90,7 +89,7 @@ void testJsonObjectBuilderDuplcateKey1() { @Test void testJsonObjectBuilderDuplcateKey2() { - JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(Collections.singletonMap(JsonConfig.REJECT_DUPLICATE_KEYS, true)); + 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(); 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 16ddb0f..8a34c40 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorTest.java @@ -81,7 +81,7 @@ void testObjectStream() throws Exception { in.close(); } - static void testObject(JsonGenerator generator) throws Exception { + static void testObject(JsonGenerator generator) { generator .writeStartObject() .write("firstName", "John") @@ -107,7 +107,7 @@ static void testObject(JsonGenerator generator) throws Exception { } @Test - void testArray() throws Exception { + void testArray() { Writer sw = new StringWriter(); JsonGenerator generator = Json.createGenerator(sw); generator @@ -185,7 +185,7 @@ void testEscapedString1() throws Exception { } @Test - void testGeneratorEquals() throws Exception { + void testGeneratorEquals() { StringWriter sw = new StringWriter(); JsonGenerator generator = Json.createGenerator(sw); generator.writeStartArray() @@ -283,7 +283,7 @@ void testPrettyObjectStream() throws Exception { } @Test - void testGenerationException1() throws Exception { + void testGenerationException1() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); @@ -296,7 +296,7 @@ void testGenerationException1() throws Exception { } @Test - void testGenerationException2() throws Exception { + void testGenerationException2() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); @@ -309,7 +309,7 @@ void testGenerationException2() throws Exception { } @Test - void testGenerationException3() throws Exception { + void testGenerationException3() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); try { @@ -321,7 +321,7 @@ void testGenerationException3() throws Exception { } @Test - void testGenerationException4() throws Exception { + void testGenerationException4() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray(); @@ -334,7 +334,7 @@ void testGenerationException4() throws Exception { } @Test - void testGenerationException5() throws Exception { + void testGenerationException5() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); @@ -347,7 +347,7 @@ void testGenerationException5() throws Exception { } @Test - void testGenerationException6() throws Exception { + void testGenerationException6() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject().writeEnd(); @@ -360,7 +360,7 @@ void testGenerationException6() throws Exception { } @Test - void testGenerationException7() throws Exception { + void testGenerationException7() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray().writeEnd(); @@ -374,7 +374,7 @@ void testGenerationException7() throws Exception { @Test - void testGenerationException8() throws Exception { + void testGenerationException8() { StringWriter sWriter = new StringWriter(); JsonGenerator generator = Json.createGenerator(sWriter); generator.writeStartObject(); @@ -387,7 +387,7 @@ void testGenerationException8() throws Exception { } @Test - void testGenerationException9() throws Exception { + void testGenerationException9() { StringWriter sWriter = new StringWriter(); JsonGenerator generator = Json.createGenerator(sWriter); generator.writeStartObject(); @@ -400,7 +400,7 @@ void testGenerationException9() throws Exception { } @Test - void testGeneratorArrayDouble() throws Exception { + void testGeneratorArrayDouble() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartArray(); @@ -427,7 +427,7 @@ void testGeneratorArrayDouble() throws Exception { } @Test - void testGeneratorObjectDouble() throws Exception { + void testGeneratorObjectDouble() { StringWriter writer = new StringWriter(); JsonGenerator generator = Json.createGenerator(writer); generator.writeStartObject(); @@ -454,7 +454,7 @@ void testGeneratorObjectDouble() throws Exception { } @Test - void testIntGenerator() throws Exception { + void testIntGenerator() { Random r = new Random(System.currentTimeMillis()); JsonGeneratorFactory gf = Json.createGeneratorFactory(null); JsonReaderFactory rf = Json.createReaderFactory(null); @@ -476,7 +476,7 @@ void testIntGenerator() throws Exception { } @Test - void testGeneratorBuf() throws Exception { + void testGeneratorBuf() { JsonGeneratorFactory gf = Json.createGeneratorFactory(null); JsonReaderFactory rf = Json.createReaderFactory(null); JsonBuilderFactory bf = Json.createBuilderFactory(null); diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchDiffTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchDiffTest.java index 5177ea0..a4bf5fe 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchDiffTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchDiffTest.java @@ -61,6 +61,7 @@ public static Iterable data() throws Exception { return examples; } + @SuppressWarnings("unchecked") private static Class createExceptionClass( JsonString exceptionClassName) throws ClassNotFoundException { if (exceptionClassName != null) { @@ -74,8 +75,7 @@ private static JsonArray loadData() { InputStream testData = JsonPatchTest.class .getResourceAsStream("/jsonmergepatchdiff.json"); JsonReader reader = Json.createReader(testData); - JsonArray data = (JsonArray) reader.read(); - return data; + return (JsonArray) reader.read(); } @MethodSource("data") diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchTest.java index fdd5622..ab9ce69 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchTest.java @@ -61,6 +61,7 @@ public static Iterable data() throws Exception { return examples; } + @SuppressWarnings("unchecked") private static Class createExceptionClass( JsonString exceptionClassName) throws ClassNotFoundException { if (exceptionClassName != null) { @@ -74,8 +75,7 @@ private static JsonArray loadData() { InputStream testData = JsonPatchTest.class .getResourceAsStream("/jsonmergepatch.json"); JsonReader reader = Json.createReader(testData); - JsonArray data = (JsonArray) reader.read(); - return data; + return (JsonArray) reader.read(); } @MethodSource("data") 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 e1ff458..2459a60 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonNumberTest.java @@ -72,7 +72,7 @@ public class JsonNumberTest { private static final int DEFAULT_MAX_BIGINTEGER_SCALE = 100000; @Test - void testFloating() throws Exception { + void testFloating() { JsonArray array1 = Json.createArrayBuilder().add(10.4).build(); JsonReader reader = Json.createReader(new StringReader("[10.4]")); JsonArray array2 = reader.readArray(); @@ -82,7 +82,7 @@ void testFloating() throws Exception { } @Test - void testBigDecimal() throws Exception { + void testBigDecimal() { JsonArray array1 = Json.createArrayBuilder().add(new BigDecimal("10.4")).build(); JsonReader reader = Json.createReader(new StringReader("[10.4]")); JsonArray array2 = reader.readArray(); @@ -92,14 +92,14 @@ void testBigDecimal() throws Exception { } @Test - void testIntNumberType() throws Exception { + void testIntNumberType() { JsonArray array1 = Json.createArrayBuilder() .add(Integer.MIN_VALUE) .add(Integer.MAX_VALUE) .add(Integer.MIN_VALUE + 1) .add(Integer.MAX_VALUE - 1) .add(12) - .add(12l) + .add(12L) .add(new BigInteger("0")) .build(); testNumberType(array1, true); @@ -128,7 +128,7 @@ private void testNumberType(JsonArray array, boolean integral) { } @Test - void testLongNumberType() throws Exception { + void testLongNumberType() { JsonArray array1 = Json.createArrayBuilder() .add(Long.MIN_VALUE) .add(Long.MAX_VALUE) @@ -180,7 +180,7 @@ void testLongNumberType() throws Exception { // } @Test - void testBigDecimalNumberType() throws Exception { + void testBigDecimalNumberType() { JsonArray array1 = Json.createArrayBuilder() .add(12d) .add(12.0d) @@ -206,7 +206,7 @@ void testBigDecimalNumberType() throws Exception { } @Test - void testMinMax() throws Exception { + void testMinMax() { JsonArray expected = Json.createArrayBuilder() .add(Integer.MIN_VALUE) .add(Integer.MAX_VALUE) 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 e2bd029..32bfd34 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java @@ -45,7 +45,7 @@ void test() { } @Test - void testEmptyObjectEquals() throws Exception { + void testEmptyObjectEquals() { JsonObject empty1 = Json.createObjectBuilder() .build(); @@ -56,7 +56,7 @@ void testEmptyObjectEquals() throws Exception { } @Test - void testPersonObjectEquals() throws Exception { + void testPersonObjectEquals() { JsonObject person1 = JsonBuilderTest.buildPerson(); JsonObject person2 = JsonReaderTest.readPerson(); @@ -64,7 +64,7 @@ void testPersonObjectEquals() throws Exception { } @Test - void testGetStringOrDefault() throws Exception { + void testGetStringOrDefault() { JsonObject object = Json.createObjectBuilder() .add("string", "value") .add("number", 25) @@ -76,7 +76,7 @@ void testGetStringOrDefault() throws Exception { } @Test - void testGetIntOrDefault() throws Exception { + void testGetIntOrDefault() { JsonObject object = Json.createObjectBuilder() .add("string", "value") .add("number", 25) @@ -88,7 +88,7 @@ void testGetIntOrDefault() throws Exception { } @Test - void testGetBooleanOrDefault() throws Exception { + void testGetBooleanOrDefault() { JsonObject object = Json.createObjectBuilder() .add("string", "value") .add("number", 25) @@ -227,7 +227,7 @@ void testObjectBuilderWithMap() { @Test void testObjectBuilderNpe() { try { - JsonObject obj = Json.createObjectBuilder().add(null, 1).build(); + Json.createObjectBuilder().add(null, 1).build(); fail("JsonObjectBuilder#add(null, 1) should throw NullPointerException"); } catch(NullPointerException e) { // Expected @@ -264,7 +264,7 @@ void testArrays() { m.put("floatArray", floatArr); JsonObject object = Json.createObjectBuilder(m).build(); assertEquals("b", object.get("stringArray").asJsonArray().getString(1)); - assertEquals(false, object.get("booleanArray").asJsonArray().getBoolean(1)); + assertFalse(object.get("booleanArray").asJsonArray().getBoolean(1)); assertEquals(2, object.get("intArray").asJsonArray().getInt(1)); assertEquals('b', object.get("charArray").asJsonArray().getInt(1)); assertEquals(2.0, object.get("floatArray").asJsonArray().getJsonNumber(1).doubleValue()); 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 4179893..35dcf6d 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonParserTest.java @@ -211,20 +211,20 @@ void testEmptyArrayStructureIterator() { } static void testEmptyArrayIterator(JsonParser parser) { - assertEquals(true, parser.hasNext()); - assertEquals(true, parser.hasNext()); + assertTrue(parser.hasNext()); + assertTrue(parser.hasNext()); assertEquals(Event.START_ARRAY, parser.next()); - assertEquals(true, parser.hasNext()); - assertEquals(true, parser.hasNext()); + assertTrue(parser.hasNext()); + assertTrue(parser.hasNext()); assertEquals(Event.END_ARRAY, parser.next()); - assertEquals(false, parser.hasNext()); - assertEquals(false, parser.hasNext()); + assertFalse(parser.hasNext()); + assertFalse(parser.hasNext()); try { parser.next(); fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { + } catch (NoSuchElementException ignored) { } } @@ -250,7 +250,7 @@ static void testEmptyArrayIterator2(JsonParser parser) { try { parser.next(); fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { + } catch (NoSuchElementException ignored) { } } @@ -272,11 +272,11 @@ void testEmptyArrayIterator3Structure() { static void testEmptyArrayIterator3(JsonParser parser) { assertEquals(Event.START_ARRAY, parser.next()); assertEquals(Event.END_ARRAY, parser.next()); - assertEquals(false, parser.hasNext()); + assertFalse(parser.hasNext()); try { parser.next(); fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { + } catch (NoSuchElementException ignored) { } } @@ -338,20 +338,20 @@ void testEmptyObjectIteratorStructure() { } static void testEmptyObjectIterator(JsonParser parser) { - assertEquals(true, parser.hasNext()); - assertEquals(true, parser.hasNext()); + assertTrue(parser.hasNext()); + assertTrue(parser.hasNext()); assertEquals(Event.START_OBJECT, parser.next()); - assertEquals(true, parser.hasNext()); - assertEquals(true, parser.hasNext()); + assertTrue(parser.hasNext()); + assertTrue(parser.hasNext()); assertEquals(Event.END_OBJECT, parser.next()); - assertEquals(false, parser.hasNext()); - assertEquals(false, parser.hasNext()); + assertFalse(parser.hasNext()); + assertFalse(parser.hasNext()); try { parser.next(); fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { + } catch (NoSuchElementException ignored) { } } @@ -377,7 +377,7 @@ static void testEmptyObjectIterator2(JsonParser parser) { try { parser.next(); fail("Should have thrown a NoSuchElementException"); - } catch (NoSuchElementException ne) { + } catch (NoSuchElementException ignored) { } } @@ -400,7 +400,7 @@ void testEmptyObjectIterator3Structure() { static void testEmptyObjectIterator3(JsonParser parser) { assertEquals(Event.START_OBJECT, parser.next()); assertEquals(Event.END_OBJECT, parser.next()); - assertEquals(false, parser.hasNext()); + assertFalse(parser.hasNext()); try { parser.next(); fail("Should have thrown a NoSuchElementException"); @@ -411,14 +411,14 @@ static void testEmptyObjectIterator3(JsonParser parser) { @Test - void testWikiIteratorReader() throws Exception { + void testWikiIteratorReader() { try (JsonParser parser = Json.createParser(wikiReader())) { testWikiIterator(parser); } } @Test - void testWikiIteratorStructure() throws Exception { + void testWikiIteratorStructure() { try (JsonParser parser = Json.createParserFactory(null).createParser( JsonBuilderTest.buildPerson())) { testWikiIterator(parser); @@ -426,21 +426,21 @@ void testWikiIteratorStructure() throws Exception { } @SuppressWarnings("UnusedDeclaration") - static void testWikiIterator(JsonParser parser) throws Exception { + static void testWikiIterator(JsonParser parser) { while (parser.hasNext()) { parser.next(); } } @Test - void testWikiInputStream() throws Exception { + void testWikiInputStream() { try (JsonParser parser = Json.createParser(wikiStream())) { testWiki(parser); } } @Test - void testWikiInputStreamUTF16LE() throws Exception { + void testWikiInputStreamUTF16LE() { ByteArrayInputStream bin = new ByteArrayInputStream(wikiString() .getBytes(StandardCharsets.UTF_16LE)); try (JsonParser parser = Json.createParser(bin)) { @@ -449,14 +449,14 @@ void testWikiInputStreamUTF16LE() throws Exception { } @Test - void testWikiReader() throws Exception { + void testWikiReader() { try (JsonParser parser = Json.createParser(wikiReader())) { testWiki(parser); } } @Test - void testWikiStructure() throws Exception { + void testWikiStructure() { try (JsonParser parser = Json.createParserFactory(null).createParser( JsonBuilderTest.buildPerson())) { testWiki(parser); @@ -562,19 +562,19 @@ static void testNestedArray(JsonParser parser) { assertEquals(Event.END_ARRAY, parser.next()); assertEquals(Event.END_ARRAY, parser.next()); assertEquals(Event.END_ARRAY, parser.next()); - assertEquals(false, parser.hasNext()); - assertEquals(false, parser.hasNext()); + assertFalse(parser.hasNext()); + assertFalse(parser.hasNext()); } @Test - void testExceptionsReader() throws Exception { + void testExceptionsReader() { try (JsonParser parser = Json.createParser(wikiReader())) { testExceptions(parser); } } @Test - void testExceptionsStructure() throws Exception { + void testExceptionsStructure() { try (JsonParser parser = Json.createParserFactory(null).createParser( JsonBuilderTest.buildPerson())) { testExceptions(parser); @@ -833,8 +833,8 @@ void testStringUsingBuffers() throws Throwable { parser.next(); assertEquals(name, parser.getString(), "value fails for buffer size=" + size + " name length=" + i); location = parser.getLocation(); - assertEquals(2 * name.length() + 6, location.getStreamOffset(), "Stream offset fails for buffer size=" + size + " name length=" + i); - assertEquals(2 * name.length() + 7, location.getColumnNumber(), "Column value fails for buffer size=" + size + " name length=" + i); + assertEquals(2L * name.length() + 6, location.getStreamOffset(), "Stream offset fails for buffer size=" + size + " name length=" + i); + assertEquals(2L * name.length() + 7, location.getColumnNumber(), "Column value fails for buffer size=" + size + " name length=" + i); assertEquals(1, location.getLineNumber(), "Line value fails for buffer size=" + size + " name length=" + i); } catch (Throwable e) { throw new Throwable("Failed for buffer size=" + size + " name length=" + i, e); diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBugsTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBugsTest.java index a190494..8202967 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBugsTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBugsTest.java @@ -48,7 +48,7 @@ void applyThrowsJsonException() { .replace("/0/name", "Bobek") .replace("/1/name", "Vila Amalka") .build(); - JsonArray result = patch.apply(array); + patch.apply(array); }); } @@ -78,7 +78,7 @@ void applyThrowsJsonException2() { // Applies the patch // It will throw a NullPointerException with no message - JsonStructure patched = patch.apply(target); + patch.apply(target); } }); } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchDiffTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchDiffTest.java index 2b8ff62..e7581d4 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchDiffTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchDiffTest.java @@ -63,6 +63,7 @@ public static Iterable data() throws Exception { return examples; } + @SuppressWarnings("unchecked") private static Class createExceptionClass( JsonString exceptionClassName) throws ClassNotFoundException { if (exceptionClassName != null) { diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchOperationTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchOperationTest.java index 40d407a..4a68eb2 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchOperationTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchOperationTest.java @@ -44,8 +44,6 @@ void fromOperationName() { @Test void fromInvalidOperationName() { - assertThrows(JsonException.class, () -> { - Operation.fromOperationName("undef"); - }); + assertThrows(JsonException.class, () -> Operation.fromOperationName("undef")); } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchTest.java index f3fac7a..f1ecf39 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPatchTest.java @@ -62,6 +62,7 @@ public static Iterable data() throws Exception { return examples; } + @SuppressWarnings("unchecked") private static Class createExceptionClass( JsonString exceptionClassName) throws ClassNotFoundException { if (exceptionClassName != null) { diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerRemoveOperationTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerRemoveOperationTest.java index 95759ae..d2c9660 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerRemoveOperationTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerRemoveOperationTest.java @@ -49,7 +49,7 @@ public static Iterable data() throws Exception { @ParameterizedTest(name = "{index}: ({0})={1}") void shouldRemoveElementsToExistingJsonDocument(JsonObject pathOperation, JsonObject target, JsonValue expectedResult) { JsonPointer pointer = Json.createPointer(pathOperation.getString("path")); - JsonObject modified = (JsonObject) pointer.remove(target); + JsonObject modified = pointer.remove(target); assertThat(modified, is(expectedResult)); } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerTest.java index 36bbcd3..af45ca5 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonPointerTest.java @@ -25,6 +25,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; +import java.util.Objects; import jakarta.json.Json; import jakarta.json.JsonException; @@ -84,11 +85,11 @@ void shouldEvaluateJsonPointerExpressions(JsonPointer pointer, JsonValue expecte } } - static JsonObject readRfc6901Example() throws Exception { - Reader rfc6901Reader = new InputStreamReader(JsonReaderTest.class.getResourceAsStream("/rfc6901.json")); + static JsonObject readRfc6901Example() { + Reader rfc6901Reader = new InputStreamReader(Objects.requireNonNull(JsonReaderTest.class.getResourceAsStream("/rfc6901.json"))); JsonReader reader = Json.createReader(rfc6901Reader); - JsonValue value = reader.readObject(); + JsonObject value = reader.readObject(); reader.close(); - return (JsonObject) value; + return value; } } 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 f0d0863..c24139e 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonReaderTest.java @@ -25,6 +25,7 @@ import java.math.BigInteger; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import jakarta.json.Json; import jakarta.json.JsonArray; @@ -33,7 +34,6 @@ import jakarta.json.JsonObject; import jakarta.json.JsonReader; import jakarta.json.JsonReaderFactory; -import jakarta.json.JsonValue; import org.eclipse.parsson.api.BufferPool; import org.eclipse.parsson.api.JsonConfig; @@ -47,13 +47,13 @@ public class JsonReaderTest { @Test - void testObject() throws Exception { + void testObject() { JsonObject person = readPerson(); JsonObjectTest.testPerson(person); } @Test - void testEscapedString() throws Exception { + void testEscapedString() { // u00ff is escaped once, not escaped once JsonReader reader = Json.createReader(new StringReader("[\"\\u0000\\u00ff\u00ff\"]")); JsonArray array = reader.readArray(); @@ -73,14 +73,11 @@ void testPrimitiveIntNumbers() { Long.toString(Integer.MIN_VALUE - 1L) }; for (String num : borderlineCases) { - JsonReader reader = Json.createReader(new StringReader("["+num+"]")); - try { - JsonArray array = reader.readArray(); - JsonNumber value = (JsonNumber) array.get(0); - assertEquals(new BigInteger(num).longValue(), value.longValue(), "Fails for num="+num); - } finally { - reader.close(); - } + try (JsonReader reader = Json.createReader(new StringReader("[" + num + "]"))) { + JsonArray array = reader.readArray(); + JsonNumber value = (JsonNumber) array.get(0); + assertEquals(new BigInteger(num).longValue(), value.longValue(), "Fails for num=" + num); + } } } @@ -95,31 +92,28 @@ void testPrimitiveLongNumbers() { new BigInteger(Long.toString(Long.MIN_VALUE)).subtract(BigInteger.ONE).toString() }; for (String num : borderlineCases) { - JsonReader reader = Json.createReader(new StringReader("["+num+"]")); - try { - JsonArray array = reader.readArray(); - JsonNumber value = (JsonNumber) array.get(0); - assertEquals(new BigInteger(num), value.bigIntegerValueExact(), "Fails for num="+num); - } finally { - reader.close(); - } + try (JsonReader reader = Json.createReader(new StringReader("[" + num + "]"))) { + JsonArray array = reader.readArray(); + JsonNumber value = (JsonNumber) array.get(0); + assertEquals(new BigInteger(num), value.bigIntegerValueExact(), "Fails for num=" + num); + } } } @Test - void testUnknownFeature() throws Exception { + void testUnknownFeature() { Map config = new HashMap<>(); config.put("foo", true); JsonReaderFactory factory = Json.createReaderFactory(config); factory.createReader(new StringReader("{}")); Map config1 = factory.getConfigInUse(); - if (config1.size() > 0) { + if (!config1.isEmpty()) { fail("Shouldn't have any config in use"); } } @Test - void testIllegalStateExcepton() throws Exception { + void testIllegalStateExcepton() { JsonReader reader = Json.createReader(new StringReader("{}")); reader.readObject(); try { @@ -148,12 +142,12 @@ void testIllegalStateExcepton() throws Exception { reader.close(); } - static JsonObject readPerson() throws Exception { - Reader wikiReader = new InputStreamReader(JsonReaderTest.class.getResourceAsStream("/wiki.json")); + static JsonObject readPerson() { + Reader wikiReader = new InputStreamReader(Objects.requireNonNull(JsonReaderTest.class.getResourceAsStream("/wiki.json"))); JsonReader reader = Json.createReader(wikiReader); - JsonValue value = reader.readObject(); + JsonObject value = reader.readObject(); reader.close(); - return (JsonObject) value; + return value; } // JSONP-23 cached empty string is not reset @@ -203,20 +197,20 @@ void testDuplicateKeysStrict() { try { reader.readObject(); fail("It is expected a JsonException"); - } catch (JsonException e) {} + } catch (JsonException ignored) {} } @Test void testDuplicateKeysStrictWithParssonConfig() { Map config = new HashMap<>(); - config.put(JsonConfig.REJECT_DUPLICATE_KEYS, "anything is valid here"); + config.put(jakarta.json.JsonConfig.KEY_STRATEGY, jakarta.json.JsonConfig.KeyStrategy.NONE); JsonReaderFactory factory = Json.createReaderFactory(config); String json = "{\"val1\":\"A\",\"val1\":\"B\"}"; JsonReader reader = factory.createReader(new StringReader(json)); try { reader.readObject(); fail("It is expected a JsonException"); - } catch (JsonException e) {} + } catch (JsonException ignored) {} } @Test diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonSamplesParsingTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonSamplesParsingTest.java index 126362f..93866d4 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonSamplesParsingTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonSamplesParsingTest.java @@ -19,6 +19,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.util.Objects; import jakarta.json.Json; import jakarta.json.JsonException; @@ -50,18 +51,12 @@ void testSampleFiles() { private void testSampleFile(String fileName) { Reader reader = new InputStreamReader( - JsonSamplesParsingTest.class.getResourceAsStream("/"+fileName), StandardCharsets.UTF_8); - JsonParser parser = null; - try { - parser = Json.createParser(reader); - while(parser.hasNext()) { - parser.next(); - } - } finally { - if (parser != null) { - parser.close(); - } - } + Objects.requireNonNull(JsonSamplesParsingTest.class.getResourceAsStream("/" + fileName)), StandardCharsets.UTF_8); + try (JsonParser parser = Json.createParser(reader)) { + while (parser.hasNext()) { + parser.next(); + } + } } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonStringTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonStringTest.java index 234cc44..5dfcf06 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonStringTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonStringTest.java @@ -35,7 +35,7 @@ public class JsonStringTest { // tests JsonString#toString() @Test - void testToString() throws Exception { + void testToString() { escapedString(""); escapedString("abc"); escapedString("abc\f"); @@ -52,7 +52,7 @@ void testToString() throws Exception { @Test void testHashCode() { - String string1 = new String("a"); + String string1 = "a"; JsonString jsonString1 = Json.createValue(string1); assertTrue(jsonString1.hashCode() == jsonString1.getString().hashCode()); @@ -63,7 +63,7 @@ void testHashCode() { assertTrue(jsonString1.hashCode() == jsonString2.hashCode()); } - void escapedString(String str) throws Exception { + void escapedString(String str) { JsonArray exp = Json.createArrayBuilder().add(str).build(); String parseStr = "["+exp.get(0).toString()+"]"; JsonReader jr = Json.createReader(new StringReader(parseStr)); diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonValueTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonValueTest.java index 944b702..2410a02 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonValueTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonValueTest.java @@ -40,58 +40,42 @@ public class JsonValueTest { @Test void arrayGetJsonObjectIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getJsonObject(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getJsonObject(0)); } @Test void arrayGetJsonArrayIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getJsonArray(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getJsonArray(0)); } @Test void arrayGetJsonNumberIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getJsonNumber(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getJsonNumber(0)); } @Test void arrayGetJsonStringIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getJsonString(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getJsonString(0)); } @Test void arrayGetStringIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getString(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getString(0)); } @Test void arrayGetIntIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getInt(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getInt(0)); } @Test void arrayGetBooleanIdx() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.getBoolean(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.getBoolean(0)); } @Test void arrayIsNull() { - assertThrows(IndexOutOfBoundsException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.isNull(0); - }); + assertThrows(IndexOutOfBoundsException.class, () -> JsonValue.EMPTY_JSON_ARRAY.isNull(0)); } @Test @@ -99,44 +83,34 @@ void arrayMethods() { Assertions.assertEquals(JsonValue.ValueType.ARRAY, JsonValue.EMPTY_JSON_ARRAY.getValueType()); Assertions.assertEquals(Collections.emptyList(), JsonValue.EMPTY_JSON_ARRAY.getValuesAs(JsonObject.class)); Assertions.assertEquals(Collections.emptyList(), JsonValue.EMPTY_JSON_ARRAY.getValuesAs(JsonString::getString)); - Assertions.assertEquals(true, JsonValue.EMPTY_JSON_ARRAY.getBoolean(0, true)); + Assertions.assertTrue(JsonValue.EMPTY_JSON_ARRAY.getBoolean(0, true)); Assertions.assertEquals(42, JsonValue.EMPTY_JSON_ARRAY.getInt(0, 42)); Assertions.assertEquals("Sasek", JsonValue.EMPTY_JSON_ARRAY.getString(0, "Sasek")); } @Test void arrayIsImmutable() { - assertThrows(UnsupportedOperationException.class, () -> { - JsonValue.EMPTY_JSON_ARRAY.add(JsonValue.EMPTY_JSON_OBJECT); - }); + assertThrows(UnsupportedOperationException.class, () -> JsonValue.EMPTY_JSON_ARRAY.add(JsonValue.EMPTY_JSON_OBJECT)); } @Test void objectGetString() { - assertThrows(NullPointerException.class, () -> { - JsonValue.EMPTY_JSON_OBJECT.getString("normalni string"); - }); + assertThrows(NullPointerException.class, () -> JsonValue.EMPTY_JSON_OBJECT.getString("normalni string")); } @Test void objectGetInt() { - assertThrows(NullPointerException.class, () -> { - JsonValue.EMPTY_JSON_OBJECT.getInt("hledej cislo"); - }); + assertThrows(NullPointerException.class, () -> JsonValue.EMPTY_JSON_OBJECT.getInt("hledej cislo")); } @Test void objectGetBoolean() { - assertThrows(NullPointerException.class, () -> { - JsonValue.EMPTY_JSON_OBJECT.getBoolean("booo"); - }); + assertThrows(NullPointerException.class, () -> JsonValue.EMPTY_JSON_OBJECT.getBoolean("booo")); } @Test void objectIsNull() { - assertThrows(NullPointerException.class, () -> { - JsonValue.EMPTY_JSON_OBJECT.isNull("???"); - }); + assertThrows(NullPointerException.class, () -> JsonValue.EMPTY_JSON_OBJECT.isNull("???")); } @Test @@ -147,16 +121,14 @@ void objectMethods() { Assertions.assertNull(JsonValue.EMPTY_JSON_OBJECT.getJsonString("divnej string")); Assertions.assertEquals("ja jo", JsonValue.EMPTY_JSON_OBJECT.getString("nejsem tu", "ja jo")); - Assertions.assertEquals(false, JsonValue.EMPTY_JSON_OBJECT.getBoolean("najdes mne", false)); + Assertions.assertFalse(JsonValue.EMPTY_JSON_OBJECT.getBoolean("najdes mne", false)); Assertions.assertEquals(98, JsonValue.EMPTY_JSON_OBJECT.getInt("spatnej dotaz", 98)); } @Test void objectImmutable() { - assertThrows(UnsupportedOperationException.class, () -> { - JsonValue.EMPTY_JSON_OBJECT.put("klauni", JsonValue.EMPTY_JSON_ARRAY); - }); + assertThrows(UnsupportedOperationException.class, () -> JsonValue.EMPTY_JSON_OBJECT.put("klauni", JsonValue.EMPTY_JSON_ARRAY)); } @Test @@ -192,6 +164,7 @@ private byte[] serialize(Object o) { return baos.toByteArray(); } + @SuppressWarnings("unchecked") private T deserialize(Class type, byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); try (ObjectInputStream ois = new ObjectInputStream(bais)) { diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonWriterTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonWriterTest.java index 01a1bd8..3194c83 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonWriterTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonWriterTest.java @@ -158,7 +158,7 @@ void testFalseValue() throws Exception { } @Test - void testIllegalStateExcepton() throws Exception { + void testIllegalStateExcepton() { JsonObject obj = Json.createObjectBuilder().build(); JsonArray array = Json.createArrayBuilder().build(); @@ -200,7 +200,7 @@ void testNoCloseWriteObjectToStream() throws Exception { } @Test - void testNoCloseWriteObjectToWriter() throws Exception { + void testNoCloseWriteObjectToWriter() { StringWriter sw = new StringWriter(); JsonWriter writer = Json.createWriter(sw); writer.write(Json.createObjectBuilder().build()); @@ -218,7 +218,7 @@ void testNoCloseWriteArrayToStream() throws Exception { } @Test - void testNoCloseWriteArrayToWriter() throws Exception { + void testNoCloseWriteArrayToWriter() { StringWriter sw = new StringWriter(); JsonWriter writer = Json.createWriter(sw); writer.write(Json.createArrayBuilder().build());