Skip to content

Commit 7df89c4

Browse files
committed
Backport part of junit5 changes to 2.16
1 parent a1c1205 commit 7df89c4

File tree

2 files changed

+227
-8
lines changed

2 files changed

+227
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package com.fasterxml.jackson.core;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.DataInput;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.StringReader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Arrays;
11+
12+
import com.fasterxml.jackson.core.testsupport.MockDataInput;
13+
import com.fasterxml.jackson.core.testsupport.ThrottledInputStream;
14+
import com.fasterxml.jackson.core.testsupport.ThrottledReader;
15+
16+
import static org.junit.jupiter.api.Assertions.fail;
17+
18+
/**
19+
* Intended replacement for {@link BaseTest}
20+
*/
21+
public class JUnit5TestBase
22+
{
23+
protected final static int MODE_INPUT_STREAM = 0;
24+
protected final static int MODE_INPUT_STREAM_THROTTLED = 1;
25+
protected final static int MODE_READER = 2;
26+
protected final static int MODE_READER_THROTTLED = 3;
27+
protected final static int MODE_DATA_INPUT = 4;
28+
29+
protected final static int[] ALL_MODES = new int[] {
30+
MODE_INPUT_STREAM,
31+
MODE_INPUT_STREAM_THROTTLED,
32+
MODE_READER,
33+
MODE_READER_THROTTLED,
34+
MODE_DATA_INPUT
35+
};
36+
37+
protected final static int[] ALL_BINARY_MODES = new int[] {
38+
MODE_INPUT_STREAM,
39+
MODE_INPUT_STREAM_THROTTLED,
40+
MODE_DATA_INPUT
41+
};
42+
43+
protected final static int[] ALL_TEXT_MODES = new int[] {
44+
MODE_READER,
45+
MODE_READER_THROTTLED
46+
};
47+
48+
// DataInput not streaming
49+
protected final static int[] ALL_STREAMING_MODES = new int[] {
50+
MODE_INPUT_STREAM,
51+
MODE_INPUT_STREAM_THROTTLED,
52+
MODE_READER,
53+
MODE_READER_THROTTLED
54+
};
55+
56+
/*
57+
/**********************************************************************
58+
/* Factory methods
59+
/**********************************************************************
60+
*/
61+
62+
protected JsonFactory newStreamFactory() {
63+
return new JsonFactory();
64+
}
65+
66+
protected JsonFactoryBuilder streamFactoryBuilder() {
67+
return (JsonFactoryBuilder) JsonFactory.builder();
68+
}
69+
70+
protected JsonParser createParser(TokenStreamFactory f, int mode, String doc) throws IOException
71+
{
72+
switch (mode) {
73+
case MODE_INPUT_STREAM:
74+
return createParserUsingStream(f, doc, "UTF-8");
75+
case MODE_INPUT_STREAM_THROTTLED:
76+
return f.createParser(new ThrottledInputStream(utf8Bytes(doc), 1));
77+
case MODE_READER:
78+
return createParserUsingReader(f, doc);
79+
case MODE_READER_THROTTLED:
80+
return f.createParser(new ThrottledReader(doc, 1));
81+
case MODE_DATA_INPUT:
82+
return createParserForDataInput(f, new MockDataInput(doc));
83+
default:
84+
}
85+
throw new RuntimeException("internal error");
86+
}
87+
88+
protected JsonParser createParser(TokenStreamFactory f, int mode, byte[] doc) throws IOException
89+
{
90+
switch (mode) {
91+
case MODE_INPUT_STREAM:
92+
return f.createParser(new ByteArrayInputStream(doc));
93+
case MODE_INPUT_STREAM_THROTTLED:
94+
return f.createParser(new ThrottledInputStream(doc, 1));
95+
case MODE_READER:
96+
return f.createParser(new StringReader(new String(doc, "UTF-8")));
97+
case MODE_READER_THROTTLED:
98+
return f.createParser(new ThrottledReader(new String(doc, "UTF-8"), 1));
99+
case MODE_DATA_INPUT:
100+
return createParserForDataInput(f, new MockDataInput(doc));
101+
default:
102+
}
103+
throw new RuntimeException("internal error");
104+
}
105+
106+
protected JsonParser createParserUsingReader(TokenStreamFactory f, String input)
107+
throws IOException
108+
{
109+
return f.createParser(new StringReader(input));
110+
}
111+
112+
protected JsonParser createParserUsingStream(TokenStreamFactory f,
113+
String input, String encoding)
114+
throws IOException
115+
{
116+
117+
/* 23-Apr-2008, tatus: UTF-32 is not supported by JDK, have to
118+
* use our own codec too (which is not optimal since there's
119+
* a chance both encoder and decoder might have bugs, but ones
120+
* that cancel each other out or such)
121+
*/
122+
byte[] data;
123+
if (encoding.equalsIgnoreCase("UTF-32")) {
124+
data = encodeInUTF32BE(input);
125+
} else {
126+
data = input.getBytes(encoding);
127+
}
128+
InputStream is = new ByteArrayInputStream(data);
129+
return f.createParser(is);
130+
}
131+
132+
protected JsonParser createParserForDataInput(TokenStreamFactory f,
133+
DataInput input)
134+
throws IOException
135+
{
136+
return f.createParser(input);
137+
}
138+
139+
/*
140+
/**********************************************************************
141+
/* Assertions
142+
/**********************************************************************
143+
*/
144+
145+
protected void assertToken(JsonToken expToken, JsonToken actToken)
146+
{
147+
if (actToken != expToken) {
148+
fail("Expected token "+expToken+", current token "+actToken);
149+
}
150+
}
151+
152+
protected void assertToken(JsonToken expToken, JsonParser p)
153+
{
154+
assertToken(expToken, p.currentToken());
155+
}
156+
157+
/**
158+
* @param e Exception to check
159+
* @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included
160+
* in {@code e.getMessage()} -- using case-INSENSITIVE comparison
161+
*/
162+
public static void verifyException(Throwable e, String... anyMatches)
163+
{
164+
String msg = e.getMessage();
165+
String lmsg = (msg == null) ? "" : msg.toLowerCase();
166+
for (String match : anyMatches) {
167+
String lmatch = match.toLowerCase();
168+
if (lmsg.indexOf(lmatch) >= 0) {
169+
return;
170+
}
171+
}
172+
fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\"");
173+
}
174+
175+
/*
176+
/**********************************************************************
177+
/* Escaping/quoting
178+
/**********************************************************************
179+
*/
180+
181+
protected String q(String str) {
182+
return '"'+str+'"';
183+
}
184+
185+
protected String a2q(String json) {
186+
return json.replace("'", "\"");
187+
}
188+
189+
protected byte[] utf8Bytes(String str) {
190+
return str.getBytes(StandardCharsets.UTF_8);
191+
}
192+
193+
protected String utf8String(ByteArrayOutputStream bytes) {
194+
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
195+
}
196+
197+
public static byte[] encodeInUTF32BE(String input)
198+
{
199+
int len = input.length();
200+
byte[] result = new byte[len * 4];
201+
int ptr = 0;
202+
for (int i = 0; i < len; ++i, ptr += 4) {
203+
char c = input.charAt(i);
204+
result[ptr] = result[ptr+1] = (byte) 0;
205+
result[ptr+2] = (byte) (c >> 8);
206+
result[ptr+3] = (byte) c;
207+
}
208+
return result;
209+
}
210+
}

src/test/java/com/fasterxml/jackson/core/read/ParserErrorHandlingTest.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,38 @@
22

33
import java.io.IOException;
44

5-
import com.fasterxml.jackson.core.JsonParseException;
6-
import com.fasterxml.jackson.core.JsonParser;
7-
import com.fasterxml.jackson.core.JsonToken;
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.fasterxml.jackson.core.*;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
810

911
public class ParserErrorHandlingTest
10-
extends com.fasterxml.jackson.core.BaseTest
12+
extends JUnit5TestBase
1113
{
14+
private final JsonFactory JSON_F = newStreamFactory();
15+
16+
@Test
1217
public void testInvalidKeywordsBytes() throws Exception {
1318
_testInvalidKeywords(MODE_INPUT_STREAM);
1419
_testInvalidKeywords(MODE_INPUT_STREAM_THROTTLED);
1520
_testInvalidKeywords(MODE_DATA_INPUT);
1621
}
1722

23+
@Test
1824
public void testInvalidKeywordsChars() throws Exception {
1925
_testInvalidKeywords(MODE_READER);
2026
}
2127

2228
// Tests for [core#105] ("eager number parsing misses errors")
29+
@Test
2330
public void testMangledIntsBytes() throws Exception {
2431
_testMangledNumbersInt(MODE_INPUT_STREAM);
2532
_testMangledNumbersInt(MODE_INPUT_STREAM_THROTTLED);
2633
_testMangledNumbersInt(MODE_DATA_INPUT);
2734
}
2835

36+
@Test
2937
public void testMangledFloatsBytes() throws Exception {
3038
_testMangledNumbersFloat(MODE_INPUT_STREAM);
3139
_testMangledNumbersFloat(MODE_INPUT_STREAM_THROTTLED);
@@ -34,6 +42,7 @@ public void testMangledFloatsBytes() throws Exception {
3442
_testMangledNumbersFloat(MODE_DATA_INPUT);
3543
}
3644

45+
@Test
3746
public void testMangledNumbersChars() throws Exception {
3847
_testMangledNumbersInt(MODE_READER);
3948
_testMangledNumbersFloat(MODE_READER);
@@ -65,7 +74,7 @@ private void doTestInvalidKeyword1(int mode, String value)
6574
throws IOException
6675
{
6776
String doc = "{ \"key1\" : "+value+" }";
68-
JsonParser p = createParser(mode, doc);
77+
JsonParser p = createParser(JSON_F, mode, doc);
6978
assertToken(JsonToken.START_OBJECT, p.nextToken());
7079
// Note that depending on parser impl, we may
7180
// get the exception early or late...
@@ -82,7 +91,7 @@ private void doTestInvalidKeyword1(int mode, String value)
8291

8392
// Try as root-level value as well:
8493
doc = value + " "; // may need space after for DataInput
85-
p = createParser(mode, doc);
94+
p = createParser(JSON_F, mode, doc);
8695
try {
8796
p.nextToken();
8897
fail("Expected an exception for malformed value keyword");
@@ -96,7 +105,7 @@ private void doTestInvalidKeyword1(int mode, String value)
96105

97106
private void _testMangledNumbersInt(int mode) throws Exception
98107
{
99-
JsonParser p = createParser(mode, "123true");
108+
JsonParser p = createParser(JSON_F, mode, "123true");
100109
try {
101110
JsonToken t = p.nextToken();
102111
fail("Should have gotten an exception; instead got token: "+t);
@@ -109,7 +118,7 @@ private void _testMangledNumbersInt(int mode) throws Exception
109118
private void _testMangledNumbersFloat(int mode) throws Exception
110119
{
111120
// Also test with floats
112-
JsonParser p = createParser(mode, "1.5false");
121+
JsonParser p = createParser(JSON_F, mode, "1.5false");
113122
try {
114123
JsonToken t = p.nextToken();
115124
fail("Should have gotten an exception; instead got token: "+t);

0 commit comments

Comments
 (0)