1
1
package com .fasterxml .jackson .failing .read ;
2
2
3
- import java .io .ByteArrayInputStream ;
4
- import java .io .DataInput ;
5
- import java .io .DataInputStream ;
6
- import java .io .IOException ;
3
+ import java .io .*;
7
4
import java .nio .charset .StandardCharsets ;
8
5
import java .util .Arrays ;
9
6
import java .util .List ;
16
13
17
14
import com .fasterxml .jackson .core .*;
18
15
import com .fasterxml .jackson .core .async .ByteArrayFeeder ;
16
+ import com .fasterxml .jackson .core .exc .StreamReadException ;
17
+
18
+ import static com .fasterxml .jackson .core .BaseTest .a2q ;
19
+
20
+ import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .assertTrue ;
19
22
20
23
/**
21
24
* Tests that the {@link JsonLocation} attached to a thrown {@link JsonProcessingException} due to invalid json points
22
25
* to the correct character.
23
26
*/
24
- public class LocationOfError1173Test extends BaseTest
27
+ public class LocationOfError1173Test
25
28
{
26
- private static final JsonFactory JSON_F = new JsonFactory ();
29
+ static final JsonFactory JSON_F = new JsonFactory ();
27
30
28
31
/** Represents the different parser backends */
29
32
public enum ParserVariant
@@ -79,7 +82,7 @@ public enum ParserVariant
79
82
this .supportsColumnNr = supportsColumnNr ;
80
83
}
81
84
82
- public JsonParser createParser (String input ) throws IOException
85
+ public JsonParser createParser (String input ) throws Exception
83
86
{
84
87
return _parserGenerator .createParser (input );
85
88
}
@@ -94,7 +97,7 @@ public JsonParser createParser(String input) throws IOException
94
97
private static final List <InvalidJson > INVALID_JSON_CASES = Arrays .asList (
95
98
new InvalidJson (
96
99
"Object property missing colon" ,
97
- "{ \" invalid\" \" json\" }" ,
100
+ a2q ( "{' invalid' ' json'}" ) ,
98
101
11 , // byte offset
99
102
11 , // char offset
100
103
1 , // line number
@@ -251,34 +254,33 @@ public JsonParser createParser(String input) throws IOException
251
254
252
255
@ ParameterizedTest
253
256
@ MethodSource ("_generateTestData" )
254
- public void testParserBackendWithInvalidJson (ParserVariant variant , InvalidJson invalidJson ) throws IOException
257
+ public void testParserBackendWithInvalidJson (ParserVariant variant , InvalidJson invalidJson )
258
+ throws Exception
255
259
{
256
260
try (JsonParser parser = variant .createParser (invalidJson .input ))
257
261
{
258
- JsonProcessingException jpe = Assertions .assertThrows (
259
- JsonProcessingException .class ,
262
+ StreamReadException e = Assertions .assertThrows (
263
+ StreamReadException .class ,
260
264
() -> {
261
265
// Blindly advance the parser through the end of input
262
266
while (parser .nextToken () != null ) {}
263
267
}
264
268
);
265
269
266
- JsonLocation location = jpe .getLocation ();
270
+ JsonLocation location = e .getLocation ();
267
271
assertEquals (invalidJson .lineNr , location .getLineNr ());
268
272
269
- if (variant .supportsColumnNr )
270
- {
271
- assertEquals (invalidJson .columnNr , location .getColumnNr ());
272
- }
273
-
274
273
if (variant .supportsByteOffset )
275
274
{
276
- assertEquals (invalidJson .byteOffset , location .getByteOffset ());
275
+ assertEquals ("Incorrect byte offset" , invalidJson .byteOffset , location .getByteOffset ());
277
276
}
278
-
279
277
if (variant .supportsCharOffset )
280
278
{
281
- assertEquals (invalidJson .charOffset , location .getCharOffset ());
279
+ assertEquals ("Incorrect char offset" ,invalidJson .charOffset , location .getCharOffset ());
280
+ }
281
+ if (variant .supportsColumnNr )
282
+ {
283
+ assertEquals ("Incorrect column" , invalidJson .columnNr , location .getColumnNr ());
282
284
}
283
285
}
284
286
}
@@ -294,12 +296,13 @@ private static Stream<Arguments> _generateTestData()
294
296
@ FunctionalInterface
295
297
public interface ParserGenerator
296
298
{
297
- JsonParser createParser (String input ) throws IOException ;
299
+ JsonParser createParser (String input ) throws Exception ;
298
300
}
299
301
300
- public static class InvalidJson
302
+ static class InvalidJson
301
303
{
302
- InvalidJson (String name , String input , int byteOffset , int charOffset , int lineNr , int columnNr )
304
+ InvalidJson (String name , String input , int byteOffset , int charOffset ,
305
+ int lineNr , int columnNr )
303
306
{
304
307
_name = name ;
305
308
0 commit comments