diff --git a/src/test/java/com/fasterxml/jackson/core/BaseTest.java b/src/test/java/com/fasterxml/jackson/core/BaseTest.java index ad5eeab8ae..f1313048fc 100644 --- a/src/test/java/com/fasterxml/jackson/core/BaseTest.java +++ b/src/test/java/com/fasterxml/jackson/core/BaseTest.java @@ -560,7 +560,7 @@ protected static String quote(String str) { return q(str); } - protected static String q(String str) { + public static String q(String str) { return '"'+str+'"'; } @@ -569,7 +569,7 @@ protected static String aposToQuotes(String json) { return a2q(json); } - protected static String a2q(String json) { + public static String a2q(String json) { return json.replace("'", "\""); } diff --git a/src/test/java/com/fasterxml/jackson/core/json/GeneratorFeaturesTest.java b/src/test/java/com/fasterxml/jackson/core/json/GeneratorFeaturesTest.java index cc5927b9f4..9d7622a7e6 100644 --- a/src/test/java/com/fasterxml/jackson/core/json/GeneratorFeaturesTest.java +++ b/src/test/java/com/fasterxml/jackson/core/json/GeneratorFeaturesTest.java @@ -296,7 +296,7 @@ public void testChangeOnGenerator() throws IOException g.enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS); g.writeNumber(123); g.close(); - assertEquals(quote("123"), w.toString()); + assertEquals(q("123"), w.toString()); // but also the opposite w = new StringWriter(); diff --git a/src/test/java/com/fasterxml/jackson/core/json/TestCustomEscaping.java b/src/test/java/com/fasterxml/jackson/core/json/TestCustomEscaping.java index 30b6f0bc84..29325f3667 100644 --- a/src/test/java/com/fasterxml/jackson/core/json/TestCustomEscaping.java +++ b/src/test/java/com/fasterxml/jackson/core/json/TestCustomEscaping.java @@ -169,7 +169,7 @@ private void _testEscapeAboveAscii(boolean useStream, boolean stringAsChars) thr g.close(); String json = bytes.toString("UTF-8"); - assertEquals("["+quote(VALUE)+"]", json); + assertEquals("["+q(VALUE)+"]", json); // And then with forced ASCII; first, values @@ -185,7 +185,7 @@ private void _testEscapeAboveAscii(boolean useStream, boolean stringAsChars) thr g.writeEndArray(); g.close(); json = bytes.toString("UTF-8"); - assertEquals("["+quote("chars: [\\u00A0]/[\\u1234]\\\\")+"]", json); + assertEquals("["+q("chars: [\\u00A0]/[\\u1234]\\\\")+"]", json); // and then keys bytes = new ByteArrayOutputStream(); @@ -201,7 +201,7 @@ private void _testEscapeAboveAscii(boolean useStream, boolean stringAsChars) thr g.writeEndObject(); g.close(); json = bytes.toString("UTF-8"); - assertEquals("{"+quote("fun:\\u0088:\\u3456\\\\")+":true}", json); + assertEquals("{"+q("fun:\\u0088:\\u3456\\\\")+":true}", json); } @SuppressWarnings("resource") diff --git a/src/test/java/com/fasterxml/jackson/failing/read/LocationOfError1173Test.java b/src/test/java/com/fasterxml/jackson/failing/read/LocationOfError1173Test.java index 19d503c746..786b10efb3 100644 --- a/src/test/java/com/fasterxml/jackson/failing/read/LocationOfError1173Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/read/LocationOfError1173Test.java @@ -1,9 +1,6 @@ package com.fasterxml.jackson.failing.read; -import java.io.ByteArrayInputStream; -import java.io.DataInput; -import java.io.DataInputStream; -import java.io.IOException; +import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; @@ -16,14 +13,20 @@ import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.async.ByteArrayFeeder; +import com.fasterxml.jackson.core.exc.StreamReadException; + +import static com.fasterxml.jackson.core.BaseTest.a2q; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Tests that the {@link JsonLocation} attached to a thrown {@link JsonProcessingException} due to invalid json points * to the correct character. */ -public class LocationOfError1173Test extends BaseTest +public class LocationOfError1173Test { - private static final JsonFactory JSON_F = new JsonFactory(); + static final JsonFactory JSON_F = new JsonFactory(); /** Represents the different parser backends */ public enum ParserVariant @@ -79,7 +82,7 @@ public enum ParserVariant this.supportsColumnNr = supportsColumnNr; } - public JsonParser createParser(String input) throws IOException + public JsonParser createParser(String input) throws Exception { return _parserGenerator.createParser(input); } @@ -94,7 +97,7 @@ public JsonParser createParser(String input) throws IOException private static final List INVALID_JSON_CASES = Arrays.asList( new InvalidJson( "Object property missing colon", - "{\"invalid\" \"json\"}", + a2q("{'invalid' 'json'}"), 11, // byte offset 11, // char offset 1, // line number @@ -251,34 +254,33 @@ public JsonParser createParser(String input) throws IOException @ParameterizedTest @MethodSource("_generateTestData") - public void testParserBackendWithInvalidJson(ParserVariant variant, InvalidJson invalidJson) throws IOException + public void testParserBackendWithInvalidJson(ParserVariant variant, InvalidJson invalidJson) + throws Exception { try (JsonParser parser = variant.createParser(invalidJson.input)) { - JsonProcessingException jpe = Assertions.assertThrows( - JsonProcessingException.class, + StreamReadException e = Assertions.assertThrows( + StreamReadException.class, () -> { // Blindly advance the parser through the end of input while (parser.nextToken() != null) {} } ); - JsonLocation location = jpe.getLocation(); + JsonLocation location = e.getLocation(); assertEquals(invalidJson.lineNr, location.getLineNr()); - if (variant.supportsColumnNr) - { - assertEquals(invalidJson.columnNr, location.getColumnNr()); - } - if (variant.supportsByteOffset) { - assertEquals(invalidJson.byteOffset, location.getByteOffset()); + assertEquals("Incorrect byte offset", invalidJson.byteOffset, location.getByteOffset()); } - if (variant.supportsCharOffset) { - assertEquals(invalidJson.charOffset, location.getCharOffset()); + assertEquals("Incorrect char offset",invalidJson.charOffset, location.getCharOffset()); + } + if (variant.supportsColumnNr) + { + assertEquals("Incorrect column", invalidJson.columnNr, location.getColumnNr()); } } } @@ -294,12 +296,13 @@ private static Stream _generateTestData() @FunctionalInterface public interface ParserGenerator { - JsonParser createParser(String input) throws IOException; + JsonParser createParser(String input) throws Exception; } - public static class InvalidJson + static class InvalidJson { - InvalidJson(String name, String input, int byteOffset, int charOffset, int lineNr, int columnNr) + InvalidJson(String name, String input, int byteOffset, int charOffset, + int lineNr, int columnNr) { _name = name;