-
-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport part of junit5 changes to 2.16
- Loading branch information
1 parent
a1c1205
commit 7df89c4
Showing
2 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
src/test/java/com/fasterxml/jackson/core/JUnit5TestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package com.fasterxml.jackson.core; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInput; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import com.fasterxml.jackson.core.testsupport.MockDataInput; | ||
import com.fasterxml.jackson.core.testsupport.ThrottledInputStream; | ||
import com.fasterxml.jackson.core.testsupport.ThrottledReader; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* Intended replacement for {@link BaseTest} | ||
*/ | ||
public class JUnit5TestBase | ||
{ | ||
protected final static int MODE_INPUT_STREAM = 0; | ||
protected final static int MODE_INPUT_STREAM_THROTTLED = 1; | ||
protected final static int MODE_READER = 2; | ||
protected final static int MODE_READER_THROTTLED = 3; | ||
protected final static int MODE_DATA_INPUT = 4; | ||
|
||
protected final static int[] ALL_MODES = new int[] { | ||
MODE_INPUT_STREAM, | ||
MODE_INPUT_STREAM_THROTTLED, | ||
MODE_READER, | ||
MODE_READER_THROTTLED, | ||
MODE_DATA_INPUT | ||
}; | ||
|
||
protected final static int[] ALL_BINARY_MODES = new int[] { | ||
MODE_INPUT_STREAM, | ||
MODE_INPUT_STREAM_THROTTLED, | ||
MODE_DATA_INPUT | ||
}; | ||
|
||
protected final static int[] ALL_TEXT_MODES = new int[] { | ||
MODE_READER, | ||
MODE_READER_THROTTLED | ||
}; | ||
|
||
// DataInput not streaming | ||
protected final static int[] ALL_STREAMING_MODES = new int[] { | ||
MODE_INPUT_STREAM, | ||
MODE_INPUT_STREAM_THROTTLED, | ||
MODE_READER, | ||
MODE_READER_THROTTLED | ||
}; | ||
|
||
/* | ||
/********************************************************************** | ||
/* Factory methods | ||
/********************************************************************** | ||
*/ | ||
|
||
protected JsonFactory newStreamFactory() { | ||
return new JsonFactory(); | ||
} | ||
|
||
protected JsonFactoryBuilder streamFactoryBuilder() { | ||
return (JsonFactoryBuilder) JsonFactory.builder(); | ||
} | ||
|
||
protected JsonParser createParser(TokenStreamFactory f, int mode, String doc) throws IOException | ||
{ | ||
switch (mode) { | ||
case MODE_INPUT_STREAM: | ||
return createParserUsingStream(f, doc, "UTF-8"); | ||
case MODE_INPUT_STREAM_THROTTLED: | ||
return f.createParser(new ThrottledInputStream(utf8Bytes(doc), 1)); | ||
case MODE_READER: | ||
return createParserUsingReader(f, doc); | ||
case MODE_READER_THROTTLED: | ||
return f.createParser(new ThrottledReader(doc, 1)); | ||
case MODE_DATA_INPUT: | ||
return createParserForDataInput(f, new MockDataInput(doc)); | ||
default: | ||
} | ||
throw new RuntimeException("internal error"); | ||
} | ||
|
||
protected JsonParser createParser(TokenStreamFactory f, int mode, byte[] doc) throws IOException | ||
{ | ||
switch (mode) { | ||
case MODE_INPUT_STREAM: | ||
return f.createParser(new ByteArrayInputStream(doc)); | ||
case MODE_INPUT_STREAM_THROTTLED: | ||
return f.createParser(new ThrottledInputStream(doc, 1)); | ||
case MODE_READER: | ||
return f.createParser(new StringReader(new String(doc, "UTF-8"))); | ||
case MODE_READER_THROTTLED: | ||
return f.createParser(new ThrottledReader(new String(doc, "UTF-8"), 1)); | ||
case MODE_DATA_INPUT: | ||
return createParserForDataInput(f, new MockDataInput(doc)); | ||
default: | ||
} | ||
throw new RuntimeException("internal error"); | ||
} | ||
|
||
protected JsonParser createParserUsingReader(TokenStreamFactory f, String input) | ||
throws IOException | ||
{ | ||
return f.createParser(new StringReader(input)); | ||
} | ||
|
||
protected JsonParser createParserUsingStream(TokenStreamFactory f, | ||
String input, String encoding) | ||
throws IOException | ||
{ | ||
|
||
/* 23-Apr-2008, tatus: UTF-32 is not supported by JDK, have to | ||
* use our own codec too (which is not optimal since there's | ||
* a chance both encoder and decoder might have bugs, but ones | ||
* that cancel each other out or such) | ||
*/ | ||
byte[] data; | ||
if (encoding.equalsIgnoreCase("UTF-32")) { | ||
data = encodeInUTF32BE(input); | ||
} else { | ||
data = input.getBytes(encoding); | ||
} | ||
InputStream is = new ByteArrayInputStream(data); | ||
return f.createParser(is); | ||
} | ||
|
||
protected JsonParser createParserForDataInput(TokenStreamFactory f, | ||
DataInput input) | ||
throws IOException | ||
{ | ||
return f.createParser(input); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Assertions | ||
/********************************************************************** | ||
*/ | ||
|
||
protected void assertToken(JsonToken expToken, JsonToken actToken) | ||
{ | ||
if (actToken != expToken) { | ||
fail("Expected token "+expToken+", current token "+actToken); | ||
} | ||
} | ||
|
||
protected void assertToken(JsonToken expToken, JsonParser p) | ||
{ | ||
assertToken(expToken, p.currentToken()); | ||
} | ||
|
||
/** | ||
* @param e Exception to check | ||
* @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included | ||
* in {@code e.getMessage()} -- using case-INSENSITIVE comparison | ||
*/ | ||
public static void verifyException(Throwable e, String... anyMatches) | ||
{ | ||
String msg = e.getMessage(); | ||
String lmsg = (msg == null) ? "" : msg.toLowerCase(); | ||
for (String match : anyMatches) { | ||
String lmatch = match.toLowerCase(); | ||
if (lmsg.indexOf(lmatch) >= 0) { | ||
return; | ||
} | ||
} | ||
fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\""); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Escaping/quoting | ||
/********************************************************************** | ||
*/ | ||
|
||
protected String q(String str) { | ||
return '"'+str+'"'; | ||
} | ||
|
||
protected String a2q(String json) { | ||
return json.replace("'", "\""); | ||
} | ||
|
||
protected byte[] utf8Bytes(String str) { | ||
return str.getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
protected String utf8String(ByteArrayOutputStream bytes) { | ||
return new String(bytes.toByteArray(), StandardCharsets.UTF_8); | ||
} | ||
|
||
public static byte[] encodeInUTF32BE(String input) | ||
{ | ||
int len = input.length(); | ||
byte[] result = new byte[len * 4]; | ||
int ptr = 0; | ||
for (int i = 0; i < len; ++i, ptr += 4) { | ||
char c = input.charAt(i); | ||
result[ptr] = result[ptr+1] = (byte) 0; | ||
result[ptr+2] = (byte) (c >> 8); | ||
result[ptr+3] = (byte) c; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters