Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 12, 2025
1 parent 7d46dbb commit a515a3c
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 136 deletions.
138 changes: 3 additions & 135 deletions src/test/java/tools/jackson/core/JUnit5TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import tools.jackson.core.io.IOContext;
import tools.jackson.core.json.JsonFactory;
import tools.jackson.core.json.JsonFactoryBuilder;
import tools.jackson.core.testutil.MockDataInput;
import tools.jackson.core.testutil.JacksonTestUtilBase;
import tools.jackson.core.testutil.ThrottledInputStream;
import tools.jackson.core.testutil.ThrottledReader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Replacement of JUnit4-based {@code BaseTest}
* Base class for Jackson-core unit tests, using JUnit 5.
*<p>
* NOTE: replacement of Jackson 2.x JUnit4-based {@code BaseTest}
*/
public class JUnit5TestBase
extends JacksonTestUtilBase
Expand Down Expand Up @@ -97,9 +94,6 @@ public class JUnit5TestBase

protected final static JsonFactory JSON_FACTORY = new JsonFactory();




/*
/**********************************************************************
/* Factory methods
Expand Down Expand Up @@ -247,10 +241,6 @@ public static JsonGenerator createGenerator(TokenStreamFactory f, Writer w) thro
/**********************************************************************
*/

public static IOContext testIOContext() {
return JacksonTestUtilBase.testIOContext();
}

protected void writeJsonDoc(JsonFactory f, String doc, JsonGenerator g) throws IOException
{
try (JsonParser p = f.createParser(ObjectReadContext.empty(), a2q(doc))) {
Expand All @@ -261,81 +251,12 @@ protected void writeJsonDoc(JsonFactory f, String doc, JsonGenerator g) throws I
}
}

/*
/**********************************************************************
/* 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+"\"");
}

/**
* Method that gets textual contents of the current token using
* available methods, and ensures results are consistent, before
* returning them
*/
public static String getAndVerifyText(JsonParser p)
{
// Ok, let's verify other accessors
int actLen = p.getStringLength();
char[] ch = p.getStringCharacters();
String str2 = new String(ch, p.getStringOffset(), actLen);
String str = p.getString();

if (str.length() != actLen) {
fail("Internal problem (p.token == "+p.currentToken()+"): p.getText().length() ['"+str+"'] == "+str.length()+"; p.getTextLength() == "+actLen);
}
assertEquals(str, str2, "String access via getText(), getTextXxx() must be the same");

return str;
}

/*
/**********************************************************************
/* Misc other
/**********************************************************************
*/

protected ObjectReadContext testObjectReadContext() {
return ObjectReadContext.empty();
}

protected static 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 String fieldNameFor(int index)
{
StringBuilder sb = new StringBuilder(16);
Expand Down Expand Up @@ -387,57 +308,4 @@ protected int[] calcQuads(byte[] wordBytes) {
}
return result;
}

/*
/**********************************************************************
/* Content reading, serialization
/**********************************************************************
*/

public static byte[] readResource(String ref)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final byte[] buf = new byte[4000];

InputStream in = JUnit5TestBase.class.getResourceAsStream(ref);
if (in != null) {
try {
int len;
while ((len = in.read(buf)) > 0) {
bytes.write(buf, 0, len);
}
in.close();
} catch (IOException e) {
throw new RuntimeException("Failed to read resource '"+ref+"': "+e);
}
}
if (bytes.size() == 0) {
throw new IllegalArgumentException("Failed to read resource '"+ref+"': empty resource?");
}
return bytes.toByteArray();
}

public static byte[] jdkSerialize(Object o) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
ObjectOutputStream obOut = new ObjectOutputStream(bytes);
obOut.writeObject(o);
obOut.close();
return bytes.toByteArray();
}

@SuppressWarnings("unchecked")
public static <T> T jdkDeserialize(byte[] raw) throws IOException
{
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw));
try {
return (T) objIn.readObject();
} catch (ClassNotFoundException e) {
fail("Missing class: "+e.getMessage());
return null;
} finally {
objIn.close();
}
}

}
142 changes: 141 additions & 1 deletion src/test/java/tools/jackson/core/testutil/JacksonTestUtilBase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package tools.jackson.core.testutil;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import tools.jackson.core.*;
import tools.jackson.core.io.ContentReference;
import tools.jackson.core.io.IOContext;
Expand All @@ -12,7 +19,7 @@ public class JacksonTestUtilBase
{
/*
/**********************************************************************
/* Factory methods
/* Factory methods for test contexts
/**********************************************************************
*/

Expand All @@ -33,6 +40,11 @@ private static IOContext testIOContext(StreamReadConstraints src,
JsonEncoding.UTF8);
}


public static ObjectReadContext testObjectReadContext() {
return ObjectReadContext.empty();
}

/*
/**********************************************************************
/* Escaping/quoting
Expand All @@ -47,6 +59,69 @@ public static String a2q(String json) {
return json.replace('\'', '"');
}

/*
/**********************************************************************
/* Assertions
/**********************************************************************
*/

public void assertToken(JsonToken expToken, JsonToken actToken)
{
if (actToken != expToken) {
fail("Expected token "+expToken+", current token "+actToken);
}
}

public 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+"\"");
}

/**
* Method that gets textual contents of the current token using
* available methods, and ensures results are consistent, before
* returning them
*/
public static String getAndVerifyText(JsonParser p)
{
// Ok, let's verify other accessors
int actLen = p.getStringLength();
char[] ch = p.getStringCharacters();
String str2 = new String(ch, p.getStringOffset(), actLen);
String str = p.getString();

if (str.length() != actLen) {
fail("Internal problem (p.token == "+p.currentToken()+"): p.getText().length() ['"+str+"'] == "+str.length()+"; p.getTextLength() == "+actLen);
}
assertEquals(str, str2, "String access via getText(), getTextXxx() must be the same");

return str;
}

/*
/**********************************************************************
/* Character encoding support
/**********************************************************************
*/

public static byte[] encodeInUTF32BE(String input)
{
int len = input.length();
Expand All @@ -61,4 +136,69 @@ public static byte[] encodeInUTF32BE(String input)
return result;
}

protected static byte[] utf8Bytes(String str) {
return str.getBytes(StandardCharsets.UTF_8);
}

protected String utf8String(ByteArrayOutputStream bytes) {
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
}

/*
/**********************************************************************
/* Resource reading helpers
/**********************************************************************
*/

public static byte[] readResource(String ref)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final byte[] buf = new byte[4000];

InputStream in = JUnit5TestBase.class.getResourceAsStream(ref);
if (in != null) {
try {
int len;
while ((len = in.read(buf)) > 0) {
bytes.write(buf, 0, len);
}
in.close();
} catch (IOException e) {
throw new RuntimeException("Failed to read resource '"+ref+"': "+e);
}
}
if (bytes.size() == 0) {
throw new IllegalArgumentException("Failed to read resource '"+ref+"': empty resource?");
}
return bytes.toByteArray();
}

/*
/**********************************************************************
/* JDK serialization helpers
/**********************************************************************
*/

public static byte[] jdkSerialize(Object o) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
ObjectOutputStream obOut = new ObjectOutputStream(bytes);
obOut.writeObject(o);
obOut.close();
return bytes.toByteArray();
}

@SuppressWarnings("unchecked")
public static <T> T jdkDeserialize(byte[] raw) throws IOException
{
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw));
try {
return (T) objIn.readObject();
} catch (ClassNotFoundException e) {
fail("Missing class: "+e.getMessage());
return null;
} finally {
objIn.close();
}
}
}

0 comments on commit a515a3c

Please sign in to comment.