-
-
Notifications
You must be signed in to change notification settings - Fork 762
/
LocationOfError1173Test.java
301 lines (278 loc) · 8.52 KB
/
LocationOfError1173Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.fasterxml.jackson.core.read.loc;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
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 StreamReadException}
* due to invalid JSON points to the correct character.
*/
public class LocationOfError1173Test
{
static final JsonFactory JSON_F = new JsonFactory();
/** Represents the different parser backends */
public enum ParserVariant
{
BYTE_ARRAY(
(String input) -> JSON_F.createParser(input.getBytes(StandardCharsets.UTF_8)),
true, // supports byte offsets in reported location
false, // supports character offsets in reported location
true // supports column numbers in reported location
),
CHAR_ARRAY(
(String input) -> JSON_F.createParser(input.toCharArray()),
false,
true,
true
),
DATA_INPUT(
(String input) -> JSON_F.createParser((DataInput) new DataInputStream(new ByteArrayInputStream(
input.getBytes(StandardCharsets.UTF_8)
))),
false,
false,
false
),
ASYNC(
(String input) -> {
JsonParser parser = JSON_F.createNonBlockingByteArrayParser();
ByteArrayFeeder feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
assertTrue(feeder.needMoreInput());
byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
feeder.feedInput(inputBytes, 0, inputBytes.length);
feeder.endOfInput();
return parser;
},
true,
false,
true
)
;
ParserVariant(
ParserGenerator parserGenerator,
boolean supportsByteOffset,
boolean supportsCharOffset,
boolean supportsColumnNr
)
{
_parserGenerator = parserGenerator;
this.supportsByteOffset = supportsByteOffset;
this.supportsCharOffset = supportsCharOffset;
this.supportsColumnNr = supportsColumnNr;
}
public JsonParser createParser(String input) throws Exception
{
return _parserGenerator.createParser(input);
}
private final ParserGenerator _parserGenerator;
public final boolean supportsByteOffset;
public final boolean supportsCharOffset;
public final boolean supportsColumnNr;
}
/** Collection of differing invalid JSON input cases to test */
private static final List<InvalidJson> INVALID_JSON_CASES = Arrays.asList(
new InvalidJson(
"Object property missing colon",
a2q("{'invalid' 'json'}"),
11, // byte offset
11, // char offset
1, // line number
12 // column number
),
new InvalidJson(
"Comma after key in object property",
a2q("{'invalid', 'json'}"),
10,
10,
1,
11
),
new InvalidJson(
"Missing comma between object properties",
a2q("{'key1':'value1' 'key2':'value2'}"),
17,
17,
1,
18
),
new InvalidJson(
"Number as a property key",
"{1234: 5678}",
1,
1,
1,
2
),
new InvalidJson(
"false literal as property key",
"{false: true}",
1,
1,
1,
2
),
new InvalidJson(
"true literal as property key",
"{true: false}",
1,
1,
1,
2
),
new InvalidJson(
"null literal as property key",
"{null: \"content\"}",
1,
1,
1,
2
),
new InvalidJson(
"Missing comma between list elements",
"[\"no\" \"commas\"]",
6,
6,
1,
7
),
new InvalidJson(
"Property key/value delimiter in list",
"[\"still\":\"invalid\"]",
8,
8,
1,
9
),
new InvalidJson(
"Unexpected EOF",
"{",
1,
1,
1,
2
),
new InvalidJson(
"Close marker without matching open marker",
"}",
0,
0,
1,
1
),
new InvalidJson(
"Mismatched open/close tokens",
"{\"open\":\"close\"]",
15,
15,
1,
16
),
new InvalidJson(
"Bare strings in JSON",
"{missing: quotes}",
1,
1,
1,
2
),
new InvalidJson(
"Error in middle of line for multiline input",
// missing comma delimiter between properties two and three
"{\n \"one\": 1,\n \"two\": 2\n \"three\": 3\n}",
27,
27,
4,
3
),
new InvalidJson(
"Error at end of line for multiline input",
// double commas between keys
"{\n\"key1\":\"value1\",,\n\"key2\":\"value2\"\n}",
18,
18,
2,
17
)
);
@ParameterizedTest
@MethodSource("_generateTestData")
public void testParserBackendWithInvalidJson(ParserVariant variant, InvalidJson invalidJson)
throws Exception
{
try (JsonParser parser = variant.createParser(invalidJson.input))
{
StreamReadException e = Assertions.assertThrows(
StreamReadException.class,
() -> {
// Blindly advance the parser through the end of input
while (parser.nextToken() != null) {}
}
);
JsonLocation location = e.getLocation();
assertEquals(invalidJson.lineNr, location.getLineNr());
final String msg = e.getOriginalMessage();
if (variant.supportsByteOffset)
{
assertEquals("Incorrect byte offset (for '"+msg+"')",
invalidJson.byteOffset, location.getByteOffset());
}
if (variant.supportsCharOffset)
{
assertEquals("Incorrect char offset (for '"+msg+"')",
invalidJson.charOffset, location.getCharOffset());
}
if (variant.supportsColumnNr)
{
assertEquals("Incorrect column (for '"+msg+"')",
invalidJson.columnNr, location.getColumnNr());
}
}
}
private static Stream<Arguments> _generateTestData()
{
return Arrays.stream(ParserVariant.values())
.flatMap(parserVariant -> INVALID_JSON_CASES.stream().map(
invalidJson -> Arguments.of(parserVariant, invalidJson)
));
}
@FunctionalInterface
public interface ParserGenerator
{
JsonParser createParser(String input) throws Exception;
}
static class InvalidJson
{
InvalidJson(String name, String input, int byteOffset, int charOffset,
int lineNr, int columnNr)
{
_name = name;
this.input = input;
this.byteOffset = byteOffset;
this.charOffset = charOffset;
this.lineNr = lineNr;
this.columnNr = columnNr;
}
@Override
public String toString()
{
return _name;
}
private final String _name;
public final String input;
public final int byteOffset;
public final int charOffset;
public final int lineNr;
public final int columnNr;
}
}