Skip to content

Commit

Permalink
Refactor to use shared jackson-core test-jar, JacksonTestUtilBase (#4901
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cowtowncoder authored Jan 16, 2025
1 parent d77ce7d commit 8cc14a2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 99 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@
</dependency>

<!-- Test dependencies -->

<!-- 13-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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jackson 3.x module-info for Tests
// Jackson 3.x module-info for jackson-databind Tests
module tools.jackson.databind
{
requires java.desktop;
Expand All @@ -14,6 +14,15 @@

// // Actual Test dependencies

// Shared Jackson test functionality

// 15-Jan-2025, tatu: missing module-info for `tools.jackson.core` can't yet add
// (but will be included in Class path just not Module path)
//
//requires tools.jackson.core.testutil;

// Test frameworks, libraries:

// Guava testlib needed by CLMH tests, alas; brings in junit4
requires guava.testlib;
// JUnit4 should NOT be needed but is transitively required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public QName deserialize(JsonParser p, DeserializationContext ctxt)
if (!p.hasToken(JsonToken.VALUE_STRING)) {
throw new IllegalArgumentException("Unexpected token "+p.currentToken());
}
return QName.valueOf(p.getText());
return QName.valueOf(p.getString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import tools.jackson.core.*;
import tools.jackson.core.json.JsonFactory;
import tools.jackson.core.testutil.JacksonTestUtilBase;
import tools.jackson.databind.*;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.introspect.AnnotatedMember;
Expand All @@ -22,6 +23,7 @@
* as part of JUnit 5 migration.
*/
public class DatabindTestUtil
extends JacksonTestUtilBase
{
// @since 2.18
// Helper annotation to work around lack of implicit name access with Jackson 2.x
Expand Down Expand Up @@ -407,37 +409,12 @@ protected String serializeAsString(Object value)
return serializeAsString(sharedMapper(), value);
}

/*
/**********************************************************************
/* Encoding or String representations
/**********************************************************************
*/

public static String a2q(String json) {
return json.replace("'", "\"");
}

public static String q(String str) {
return '"'+str+'"';
}

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

/*
/**********************************************************************
/* Additional assertion methods
/**********************************************************************
*/

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

public static void assertValidLocation(TokenStreamLocation location) {
assertNotNull(location, "Should have non-null location");
assertTrue(location.getLineNr() > 0, "Should have positive line number");
Expand Down Expand Up @@ -478,78 +455,6 @@ protected void assertStandardEquals(Object o)
o.hashCode();
}

/**
* @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.contains(lmatch)) {
return;
}
}
fail("Expected an exception with one of substrings ("
+ Arrays.asList(anyMatches)+"): got one (of type "+e.getClass().getName()
+") with message \""+msg+"\"");
}

/**
* Method that gets textual contents of the current token using
* available methods, and ensures results are consistent, before
* returning them
*/
protected static String getAndVerifyText(JsonParser jp)
{
// 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;
}

/*
/**********************************************************
/* JDK ser/deser
/**********************************************************
*/

public static byte[] jdkSerialize(Object o)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream(2000);
try (ObjectOutputStream obOut = new ObjectOutputStream(bytes)) {
obOut.writeObject(o);
obOut.close();
return bytes.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

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

/*
/**********************************************************************
/* Helper methods, other
Expand Down

0 comments on commit 8cc14a2

Please sign in to comment.