-
-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start converting some unit tests to JUnit5; add new test base class t…
…o help (#1212)
- Loading branch information
1 parent
e5f2ca4
commit ca81a1f
Showing
3 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
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
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
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
package com.fasterxml.jackson.core; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* Intended replacement for {@link BaseTest} | ||
*/ | ||
public class JUnit5TestBase | ||
{ | ||
/* | ||
/********************************************************************** | ||
/* Factory methods | ||
/********************************************************************** | ||
*/ | ||
|
||
protected JsonFactory newStreamFactory() { | ||
return new JsonFactory(); | ||
} | ||
|
||
protected JsonFactoryBuilder streamFactoryBuilder() { | ||
return (JsonFactoryBuilder) JsonFactory.builder(); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* 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); | ||
} | ||
|
||
} |