Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using shared test util classes #707

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,19 @@ alternative support for serializing POJOs as XML and deserializing XML as pojos.
</dependency>

<!-- Test dependencies -->


<!-- 16-Jan-2025, tatu: Use the new (in 3.0) shared core test-jar
for some shared test functionality.
-->
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version.core}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<!-- 11-Jan-2025, joohyukkim: For JSTEP-10, migrate to JUnit5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -100,7 +112,7 @@ alternative support for serializing POJOs as XML and deserializing XML as pojos.
<scope>test</scope>
</dependency>

<!-- Jakarta XMLBind (nee javax/jaxb( annotation introspector is needed BUT ONLY
<!-- Jakarta XMLBind (nee javax/jaxb) annotation introspector is needed BUT ONLY
for tests (starting 2.13: previously compile/runtime)
-->
<dependency>
Expand Down
89 changes: 7 additions & 82 deletions src/test/java/tools/jackson/dataformat/xml/XmlTestUtil.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package tools.jackson.dataformat.xml;

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

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import tools.jackson.core.*;

import tools.jackson.core.JsonParser;
import tools.jackson.core.testutil.JacksonTestUtilBase;

import tools.jackson.databind.AnnotationIntrospector;

import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector;

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

public abstract class XmlTestUtil
extends JacksonTestUtilBase
{
protected static final String DEFAULT_NEW_LINE;

Expand Down Expand Up @@ -249,75 +253,19 @@ protected AnnotationIntrospector jakartaXMLBindAnnotationIntrospector() {
/**********************************************************
*/

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

protected void assertToken(JsonToken expToken, JsonParser jp)
{
assertToken(expToken, jp.currentToken());
}

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

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

return str;
}

protected void verifyFieldName(JsonParser jp, String expName)
throws IOException
{
assertEquals(expName, jp.getString());
assertEquals(expName, jp.currentName());
}

protected void verifyException(Throwable e, String... matches)
{
String msg = e.getMessage();
String lmsg = (msg == null) ? "" : msg.toLowerCase();
for (String match : matches) {
String lmatch = match.toLowerCase();
if (lmsg.indexOf(lmatch) >= 0) {
return;
}
}
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one ("+
e.getClass().getName()+") with message \""+msg+"\"");
}

/*
/**********************************************************
/* Helper methods, other
/**********************************************************
*/

protected static String a2q(String content) {
return content.replace("'", "\"");
}

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

/**
* Helper method that tries to remove unnecessary namespace
* declaration that default JDK XML parser (SJSXP) sees fit
Expand Down Expand Up @@ -346,29 +294,6 @@ protected String readAll(File f) throws IOException
return sb.toString();
}

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

InputStream in = getClass().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 String jaxbSerialized(Object ob, Class<?>... classes) throws Exception
{
StringWriter sw = new StringWriter();
Expand Down
Loading