Skip to content

Commit f54a998

Browse files
authored
Revert use of shared jackson-core test-jar due to problems (#4942)
1 parent 6be6f47 commit f54a998

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+352
-46
lines changed

src/test/java/module-info.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
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-
2417
// Test frameworks, libraries:
2518

2619
// Guava testlib needed by CLMH tests, alas; brings in junit4
@@ -95,6 +88,7 @@
9588
opens tools.jackson.databind.ser.filter;
9689
opens tools.jackson.databind.seq;
9790
opens tools.jackson.databind.struct;
91+
opens tools.jackson.databind.testutil.failure;
9892
opens tools.jackson.databind.tofix;
9993
opens tools.jackson.databind.util.internal;
10094
opens tools.jackson.databind.views;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import tools.jackson.core.*;
88
import tools.jackson.core.json.JsonFactory;
9-
import tools.jackson.core.testutil.JacksonTestUtilBase;
109
import tools.jackson.databind.*;
1110
import tools.jackson.databind.cfg.MapperConfig;
1211
import tools.jackson.databind.introspect.AnnotatedMember;
@@ -24,15 +23,13 @@
2423
public class DatabindTestUtil
2524
extends JacksonTestUtilBase
2625
{
27-
// @since 2.18
2826
// Helper annotation to work around lack of implicit name access with Jackson 2.x
2927
@Target(ElementType.PARAMETER)
3028
@Retention(RetentionPolicy.RUNTIME)
3129
protected @interface ImplicitName {
3230
String value();
3331
}
3432

35-
// @since 2.18
3633
@SuppressWarnings("serial")
3734
static public class ImplicitNameIntrospector extends JacksonAnnotationIntrospector
3835
{
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package tools.jackson.databind.testutil;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import java.io.*;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.Arrays;
9+
10+
import tools.jackson.core.ErrorReportConfiguration;
11+
import tools.jackson.core.JsonEncoding;
12+
import tools.jackson.core.JsonParser;
13+
import tools.jackson.core.JsonToken;
14+
import tools.jackson.core.ObjectReadContext;
15+
import tools.jackson.core.StreamReadConstraints;
16+
import tools.jackson.core.StreamWriteConstraints;
17+
import tools.jackson.core.io.ContentReference;
18+
import tools.jackson.core.io.IOContext;
19+
import tools.jackson.core.util.BufferRecycler;
20+
21+
/**
22+
* Container for various factories needed by (unit) tests.
23+
*/
24+
public class JacksonTestUtilBase
25+
{
26+
/*
27+
/**********************************************************************
28+
/* Factory methods for test contexts
29+
/**********************************************************************
30+
*/
31+
32+
/**
33+
* Factory method for creating {@link IOContext}s for tests
34+
*/
35+
public static IOContext testIOContext() {
36+
return testIOContext(StreamReadConstraints.defaults(),
37+
StreamWriteConstraints.defaults(),
38+
ErrorReportConfiguration.defaults());
39+
}
40+
41+
protected static IOContext testIOContext(StreamReadConstraints src,
42+
StreamWriteConstraints swc,
43+
ErrorReportConfiguration erc) {
44+
return new IOContext(src, swc, erc,
45+
new BufferRecycler(), ContentReference.unknown(), false,
46+
JsonEncoding.UTF8);
47+
}
48+
49+
50+
public static ObjectReadContext testObjectReadContext() {
51+
return ObjectReadContext.empty();
52+
}
53+
54+
/*
55+
/**********************************************************************
56+
/* Escaping/quoting
57+
/**********************************************************************
58+
*/
59+
60+
public static String q(String str) {
61+
return '"'+str+'"';
62+
}
63+
64+
public static String a2q(String json) {
65+
return json.replace('\'', '"');
66+
}
67+
68+
/*
69+
/**********************************************************************
70+
/* Assertions
71+
/**********************************************************************
72+
*/
73+
74+
public static void assertToken(JsonToken expToken, JsonToken actToken)
75+
{
76+
if (actToken != expToken) {
77+
fail("Expected token "+expToken+", current token "+actToken);
78+
}
79+
}
80+
81+
public static void assertToken(JsonToken expToken, JsonParser p)
82+
{
83+
assertToken(expToken, p.currentToken());
84+
}
85+
86+
/**
87+
* @param e Exception to check
88+
* @param anyMatches Array of Strings of which AT LEAST ONE ("any") has to be included
89+
* in {@code e.getMessage()} -- using case-INSENSITIVE comparison
90+
*/
91+
public static void verifyException(Throwable e, String... anyMatches)
92+
{
93+
String msg = e.getMessage();
94+
String lmsg = (msg == null) ? "" : msg.toLowerCase();
95+
for (String match : anyMatches) {
96+
String lmatch = match.toLowerCase();
97+
if (lmsg.indexOf(lmatch) >= 0) {
98+
return;
99+
}
100+
}
101+
fail("Expected an exception with one of substrings ("+Arrays.asList(anyMatches)+"): got one with message \""+msg+"\"");
102+
}
103+
104+
/**
105+
* Method that gets textual contents of the current token using
106+
* available methods, and ensures results are consistent, before
107+
* returning them
108+
*/
109+
public static String getAndVerifyText(JsonParser p)
110+
{
111+
// Ok, let's verify other accessors
112+
int actLen = p.getStringLength();
113+
char[] ch = p.getStringCharacters();
114+
String str2 = new String(ch, p.getStringOffset(), actLen);
115+
String str = p.getString();
116+
117+
if (str.length() != actLen) {
118+
fail("Internal problem (p.token == "+p.currentToken()+"): p.getText().length() ['"+str+"'] == "+str.length()+"; p.getTextLength() == "+actLen);
119+
}
120+
assertEquals(str, str2, "String access via getText(), getTextXxx() must be the same");
121+
122+
return str;
123+
}
124+
125+
/*
126+
/**********************************************************************
127+
/* Character encoding support
128+
/**********************************************************************
129+
*/
130+
131+
public static byte[] encodeInUTF32BE(String input)
132+
{
133+
int len = input.length();
134+
byte[] result = new byte[len * 4];
135+
int ptr = 0;
136+
for (int i = 0; i < len; ++i, ptr += 4) {
137+
char c = input.charAt(i);
138+
result[ptr] = result[ptr+1] = (byte) 0;
139+
result[ptr+2] = (byte) (c >> 8);
140+
result[ptr+3] = (byte) c;
141+
}
142+
return result;
143+
}
144+
145+
public static byte[] utf8Bytes(String str) {
146+
return str.getBytes(StandardCharsets.UTF_8);
147+
}
148+
149+
public String utf8String(ByteArrayOutputStream bytes) {
150+
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
151+
}
152+
153+
/*
154+
/**********************************************************************
155+
/* Resource reading helpers
156+
/**********************************************************************
157+
*/
158+
159+
public static byte[] readResource(String ref)
160+
{
161+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
162+
final byte[] buf = new byte[4000];
163+
164+
InputStream in = JacksonTestUtilBase.class.getResourceAsStream(ref);
165+
if (in != null) {
166+
try {
167+
int len;
168+
while ((len = in.read(buf)) > 0) {
169+
bytes.write(buf, 0, len);
170+
}
171+
in.close();
172+
} catch (IOException e) {
173+
throw new RuntimeException("Failed to read resource '"+ref+"': "+e);
174+
}
175+
}
176+
if (bytes.size() == 0) {
177+
throw new IllegalArgumentException("Failed to read resource '"+ref+"': empty resource?");
178+
}
179+
return bytes.toByteArray();
180+
}
181+
182+
/*
183+
/**********************************************************************
184+
/* JDK serialization helpers
185+
/**********************************************************************
186+
*/
187+
188+
public static byte[] jdkSerialize(Object o) throws IOException
189+
{
190+
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
191+
ObjectOutputStream obOut = new ObjectOutputStream(bytes);
192+
obOut.writeObject(o);
193+
obOut.close();
194+
return bytes.toByteArray();
195+
}
196+
197+
@SuppressWarnings("unchecked")
198+
public static <T> T jdkDeserialize(byte[] raw) throws IOException
199+
{
200+
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw));
201+
try {
202+
return (T) objIn.readObject();
203+
} catch (ClassNotFoundException e) {
204+
fail("Missing class: "+e.getMessage());
205+
return null;
206+
} finally {
207+
objIn.close();
208+
}
209+
}
210+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tools.jackson.databind.testutil.failure;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
10+
/**
11+
* <p>
12+
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
13+
*
14+
* <p>
15+
* When a test method is annotated with {@code @JacksonTestFailureExpected}, the
16+
* {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution.
17+
* If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test,
18+
* indicating that the test was expected to fail but didn't.
19+
* </p>
20+
*
21+
* <h3>Usage Example:</h3>
22+
*
23+
* <pre><code>
24+
*
25+
* &#64;Test
26+
* &#64;JacksonTestFailureExpected
27+
* public void testFeatureNotYetImplemented() {
28+
* // Test code that is expected to fail
29+
* }
30+
* }
31+
* </code></pre>
32+
*
33+
* <p>
34+
*
35+
* @since 2.19
36+
*/
37+
@Target({ElementType.METHOD})
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
40+
public @interface JacksonTestFailureExpected { }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tools.jackson.databind.testutil.failure;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.InvocationInterceptor;
7+
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
8+
9+
/**
10+
* Custom {@link InvocationInterceptor} that intercepts test method invocation.
11+
* To pass the test ***only if*** test fails with an exception, and fail the test otherwise.
12+
*
13+
* @since 2.19
14+
*/
15+
public class JacksonTestFailureExpectedInterceptor
16+
implements InvocationInterceptor
17+
{
18+
@Override
19+
public void interceptTestMethod(Invocation<Void> invocation,
20+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
21+
throws Throwable
22+
{
23+
try {
24+
invocation.proceed();
25+
} catch (Throwable t) {
26+
// do-nothing, we do expect an exception
27+
return;
28+
}
29+
handleUnexpectePassingTest(invocationContext);
30+
}
31+
32+
private void handleUnexpectePassingTest(ReflectiveInvocationContext<Method> invocationContext) {
33+
// Collect information we need
34+
Object targetClass = invocationContext.getTargetClass();
35+
Object testMethod = invocationContext.getExecutable().getName();
36+
//List<Object> arguments = invocationContext.getArguments();
37+
38+
// Create message
39+
String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod);
40+
41+
// throw exception
42+
throw new JacksonTestShouldFailException(message);
43+
}
44+
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tools.jackson.databind.testutil.failure;
2+
3+
/**
4+
* Exception used to alert that a test is passing, but should be failing.
5+
*
6+
* WARNING : This only for test code, and should never be thrown from production code.
7+
*
8+
* @since 2.19
9+
*/
10+
public class JacksonTestShouldFailException
11+
extends RuntimeException
12+
{
13+
private static final long serialVersionUID = 1L;
14+
15+
public JacksonTestShouldFailException(String msg) {
16+
super(msg);
17+
}
18+
}

src/test/java/tools/jackson/databind/tofix/AnyGetterSorting518Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import java.util.Map;
66

77
import org.junit.jupiter.api.Test;
8-
import tools.jackson.core.testutil.failure.JacksonTestFailureExpected;
98

109
import com.fasterxml.jackson.annotation.JsonAnyGetter;
1110
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1211

1312
import tools.jackson.databind.*;
1413
import tools.jackson.databind.testutil.DatabindTestUtil;
14+
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
1515

1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717

src/test/java/tools/jackson/databind/tofix/AnySetterAsCreatorFallback1401Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package tools.jackson.databind.tofix;
22

33
import org.junit.jupiter.api.Test;
4-
import tools.jackson.core.testutil.failure.JacksonTestFailureExpected;
54

65
import com.fasterxml.jackson.annotation.JsonAnySetter;
76
import com.fasterxml.jackson.annotation.JsonCreator;
87
import com.fasterxml.jackson.annotation.JsonProperty;
98

109
import tools.jackson.databind.*;
1110
import tools.jackson.databind.testutil.DatabindTestUtil;
11+
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414

0 commit comments

Comments
 (0)