Skip to content

Commit 8cc14a2

Browse files
authored
Refactor to use shared jackson-core test-jar, JacksonTestUtilBase (#4901)
1 parent d77ce7d commit 8cc14a2

File tree

4 files changed

+25
-99
lines changed

4 files changed

+25
-99
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@
108108
</dependency>
109109

110110
<!-- Test dependencies -->
111+
112+
<!-- 13-Jan-2025, tatu: Use the new (in 3.0) shared core test-jar
113+
for some shared test functionality.
114+
-->
115+
<dependency>
116+
<groupId>tools.jackson.core</groupId>
117+
<artifactId>jackson-core</artifactId>
118+
<version>${jackson.version.core}</version>
119+
<classifier>tests</classifier>
120+
<type>test-jar</type>
121+
<scope>test</scope>
122+
</dependency>
111123
<dependency>
112124
<groupId>org.junit.jupiter</groupId>
113125
<artifactId>junit-jupiter</artifactId>

src/test/java/module-info.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jackson 3.x module-info for Tests
1+
// Jackson 3.x module-info for jackson-databind Tests
22
module tools.jackson.databind
33
{
44
requires java.desktop;
@@ -14,6 +14,15 @@
1414

1515
// // Actual Test dependencies
1616

17+
// Shared Jackson test functionality
18+
19+
// 15-Jan-2025, tatu: missing module-info for `tools.jackson.core` can't yet add
20+
// (but will be included in Class path just not Module path)
21+
//
22+
//requires tools.jackson.core.testutil;
23+
24+
// Test frameworks, libraries:
25+
1726
// Guava testlib needed by CLMH tests, alas; brings in junit4
1827
requires guava.testlib;
1928
// JUnit4 should NOT be needed but is transitively required

src/test/java/tools/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public QName deserialize(JsonParser p, DeserializationContext ctxt)
109109
if (!p.hasToken(JsonToken.VALUE_STRING)) {
110110
throw new IllegalArgumentException("Unexpected token "+p.currentToken());
111111
}
112-
return QName.valueOf(p.getText());
112+
return QName.valueOf(p.getString());
113113
}
114114
}
115115

src/test/java/tools/jackson/databind/testutil/DatabindTestUtil.java

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import tools.jackson.core.*;
99
import tools.jackson.core.json.JsonFactory;
10+
import tools.jackson.core.testutil.JacksonTestUtilBase;
1011
import tools.jackson.databind.*;
1112
import tools.jackson.databind.cfg.MapperConfig;
1213
import tools.jackson.databind.introspect.AnnotatedMember;
@@ -22,6 +23,7 @@
2223
* as part of JUnit 5 migration.
2324
*/
2425
public class DatabindTestUtil
26+
extends JacksonTestUtilBase
2527
{
2628
// @since 2.18
2729
// Helper annotation to work around lack of implicit name access with Jackson 2.x
@@ -407,37 +409,12 @@ protected String serializeAsString(Object value)
407409
return serializeAsString(sharedMapper(), value);
408410
}
409411

410-
/*
411-
/**********************************************************************
412-
/* Encoding or String representations
413-
/**********************************************************************
414-
*/
415-
416-
public static String a2q(String json) {
417-
return json.replace("'", "\"");
418-
}
419-
420-
public static String q(String str) {
421-
return '"'+str+'"';
422-
}
423-
424-
public static byte[] utf8Bytes(String str) {
425-
return str.getBytes(StandardCharsets.UTF_8);
426-
}
427-
428412
/*
429413
/**********************************************************************
430414
/* Additional assertion methods
431415
/**********************************************************************
432416
*/
433417

434-
public static void assertToken(JsonToken expToken, JsonToken actToken)
435-
{
436-
if (actToken != expToken) {
437-
fail("Expected token "+expToken+", current token "+actToken);
438-
}
439-
}
440-
441418
public static void assertValidLocation(TokenStreamLocation location) {
442419
assertNotNull(location, "Should have non-null location");
443420
assertTrue(location.getLineNr() > 0, "Should have positive line number");
@@ -478,78 +455,6 @@ protected void assertStandardEquals(Object o)
478455
o.hashCode();
479456
}
480457

481-
/**
482-
* @param e Exception to check
483-
* @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included
484-
* in {@code e.getMessage()} -- using case-INSENSITIVE comparison
485-
*/
486-
public static void verifyException(Throwable e, String... anyMatches)
487-
{
488-
String msg = e.getMessage();
489-
String lmsg = (msg == null) ? "" : msg.toLowerCase();
490-
for (String match : anyMatches) {
491-
String lmatch = match.toLowerCase();
492-
if (lmsg.contains(lmatch)) {
493-
return;
494-
}
495-
}
496-
fail("Expected an exception with one of substrings ("
497-
+ Arrays.asList(anyMatches)+"): got one (of type "+e.getClass().getName()
498-
+") with message \""+msg+"\"");
499-
}
500-
501-
/**
502-
* Method that gets textual contents of the current token using
503-
* available methods, and ensures results are consistent, before
504-
* returning them
505-
*/
506-
protected static String getAndVerifyText(JsonParser jp)
507-
{
508-
// Ok, let's verify other accessors
509-
int actLen = jp.getStringLength();
510-
char[] ch = jp.getStringCharacters();
511-
String str2 = new String(ch, jp.getStringOffset(), actLen);
512-
String str = jp.getString();
513-
514-
if (str.length() != actLen) {
515-
fail("Internal problem (jp.token == "+jp.currentToken()+"): jp.getText().length() ['"+str+"'] == "+str.length()+"; jp.getTextLength() == "+actLen);
516-
}
517-
assertEquals(str, str2, "String access via getText(), getTextXxx() must be the same");
518-
519-
return str;
520-
}
521-
522-
/*
523-
/**********************************************************
524-
/* JDK ser/deser
525-
/**********************************************************
526-
*/
527-
528-
public static byte[] jdkSerialize(Object o)
529-
{
530-
ByteArrayOutputStream bytes = new ByteArrayOutputStream(2000);
531-
try (ObjectOutputStream obOut = new ObjectOutputStream(bytes)) {
532-
obOut.writeObject(o);
533-
obOut.close();
534-
return bytes.toByteArray();
535-
} catch (IOException e) {
536-
throw new UncheckedIOException(e);
537-
}
538-
}
539-
540-
@SuppressWarnings("unchecked")
541-
public static <T> T jdkDeserialize(byte[] raw)
542-
{
543-
try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw))) {
544-
return (T) objIn.readObject();
545-
} catch (ClassNotFoundException e) {
546-
fail("Missing class: "+e.getMessage());
547-
return null;
548-
} catch (IOException e) {
549-
throw new UncheckedIOException(e);
550-
}
551-
}
552-
553458
/*
554459
/**********************************************************************
555460
/* Helper methods, other

0 commit comments

Comments
 (0)