From b90dc0137d05085c0d2ce874405994e22f14291c Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sat, 11 Jan 2025 22:54:06 +0900 Subject: [PATCH 1/8] Add `testutil.failure` utils --- .../failure/JacksonTestFailureExpected.java | 38 ++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 43 ++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ .../failure/JacksonTestFailureExpected.java | 38 ++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 43 ++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ .../failure/JacksonTestFailureExpected.java | 38 ++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 43 ++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ pom.xml | 13 +++++- .../failure/JacksonTestFailureExpected.java | 38 ++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 43 ++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ .../failure/JacksonTestFailureExpected.java | 38 ++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 45 +++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ 16 files changed, 509 insertions(+), 1 deletion(-) create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpected.java create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestShouldFailException.java create mode 100644 cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpected.java create mode 100644 cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestShouldFailException.java create mode 100644 ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpected.java create mode 100644 ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestShouldFailException.java create mode 100644 protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/testutil/failure/JacksonTestFailureExpected.java create mode 100644 protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/testutil/failure/JacksonTestShouldFailException.java create mode 100644 smile/src/test/java/com/fasterxml/jackson/dataformat/smile/testutil/failure/JacksonTestFailureExpected.java create mode 100644 smile/src/test/java/com/fasterxml/jackson/dataformat/smile/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 smile/src/test/java/com/fasterxml/jackson/dataformat/smile/testutil/failure/JacksonTestShouldFailException.java diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpected.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 000000000..20c81274a --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,38 @@ +package com.fasterxml.jackson.dataformat.avro.testutil.failure; + +import java.lang.annotation.*; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + *

+ * Annotation used to indicate that a JUnit-5 based tests method is expected to fail. + * + *

+ * When a test method is annotated with {@code @JacksonTestFailureExpected}, the + * {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution. + * If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test, + * indicating that the test was expected to fail but didn't. + *

+ * + *

Usage Example:

+ * + *

+ *
+ *     @Test
+ *     @JacksonTestFailureExpected
+ *     @Test
+    public void testFeatureNotYetImplemented() {
+ *         // Test code that is expected to fail
+ *     }
+ * }
+ * 
+ * + *

+ * + * @since 2.19 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(JacksonTestFailureExpectedInterceptor.class) +public @interface JacksonTestFailureExpected { } diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 000000000..1284841ca --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,43 @@ +package com.fasterxml.jackson.dataformat.avro.testutil.failure; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.extension.*; + +/** + * Custom {@link InvocationInterceptor} that intercepts test method invocation. + * To pass the test ***only if*** test fails with an exception, and fail the test otherwise. + * + * @since 2.19 + */ +public class JacksonTestFailureExpectedInterceptor + implements InvocationInterceptor +{ + @Override + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, ExtensionContext extensionContext) + throws Throwable + { + try { + invocation.proceed(); + } catch (Throwable t) { + // do-nothing, we do expect an exception + return; + } + handleUnexpectePassingTest(invocationContext); + } + + private void handleUnexpectePassingTest(ReflectiveInvocationContext invocationContext) { + // Collect information we need + Object targetClass = invocationContext.getTargetClass(); + Object testMethod = invocationContext.getExecutable().getName(); + //List arguments = invocationContext.getArguments(); + + // Create message + String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod); + + // throw exception + throw new JacksonTestShouldFailException(message); + } + +} diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestShouldFailException.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 000000000..ec35d7899 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.dataformat.avro.testutil.failure; + +/** + * Exception used to alert that a test is passing, but should be failing. + * + * WARNING : This only for test code, and should never be thrown from production code. + * + * @since 2.19 + */ +public class JacksonTestShouldFailException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public JacksonTestShouldFailException(String msg) { + super(msg); + } +} diff --git a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpected.java b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 000000000..571b28b5d --- /dev/null +++ b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,38 @@ +package com.fasterxml.jackson.dataformat.cbor.testutil.failure; + +import java.lang.annotation.*; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + *

+ * Annotation used to indicate that a JUnit-5 based tests method is expected to fail. + * + *

+ * When a test method is annotated with {@code @JacksonTestFailureExpected}, the + * {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution. + * If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test, + * indicating that the test was expected to fail but didn't. + *

+ * + *

Usage Example:

+ * + *

+ *
+ *     @Test
+ *     @JacksonTestFailureExpected
+ *     @Test
+    public void testFeatureNotYetImplemented() {
+ *         // Test code that is expected to fail
+ *     }
+ * }
+ * 
+ * + *

+ * + * @since 2.19 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(JacksonTestFailureExpectedInterceptor.class) +public @interface JacksonTestFailureExpected { } diff --git a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 000000000..47d9f23a7 --- /dev/null +++ b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,43 @@ +package com.fasterxml.jackson.dataformat.cbor.testutil.failure; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.extension.*; + +/** + * Custom {@link InvocationInterceptor} that intercepts test method invocation. + * To pass the test ***only if*** test fails with an exception, and fail the test otherwise. + * + * @since 2.19 + */ +public class JacksonTestFailureExpectedInterceptor + implements InvocationInterceptor +{ + @Override + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, ExtensionContext extensionContext) + throws Throwable + { + try { + invocation.proceed(); + } catch (Throwable t) { + // do-nothing, we do expect an exception + return; + } + handleUnexpectePassingTest(invocationContext); + } + + private void handleUnexpectePassingTest(ReflectiveInvocationContext invocationContext) { + // Collect information we need + Object targetClass = invocationContext.getTargetClass(); + Object testMethod = invocationContext.getExecutable().getName(); + //List arguments = invocationContext.getArguments(); + + // Create message + String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod); + + // throw exception + throw new JacksonTestShouldFailException(message); + } + +} diff --git a/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestShouldFailException.java b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 000000000..e74611237 --- /dev/null +++ b/cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.dataformat.cbor.testutil.failure; + +/** + * Exception used to alert that a test is passing, but should be failing. + * + * WARNING : This only for test code, and should never be thrown from production code. + * + * @since 2.19 + */ +public class JacksonTestShouldFailException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public JacksonTestShouldFailException(String msg) { + super(msg); + } +} diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpected.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 000000000..86839a72f --- /dev/null +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,38 @@ +package com.fasterxml.jackson.dataformat.ion.testutil.failure; + +import java.lang.annotation.*; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + *

+ * Annotation used to indicate that a JUnit-5 based tests method is expected to fail. + * + *

+ * When a test method is annotated with {@code @JacksonTestFailureExpected}, the + * {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution. + * If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test, + * indicating that the test was expected to fail but didn't. + *

+ * + *

Usage Example:

+ * + *

+ *
+ *     @Test
+ *     @JacksonTestFailureExpected
+ *     @Test
+    public void testFeatureNotYetImplemented() {
+ *         // Test code that is expected to fail
+ *     }
+ * }
+ * 
+ * + *

+ * + * @since 2.19 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(JacksonTestFailureExpectedInterceptor.class) +public @interface JacksonTestFailureExpected { } diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 000000000..9724649e5 --- /dev/null +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,43 @@ +package com.fasterxml.jackson.dataformat.ion.testutil.failure; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.extension.*; + +/** + * Custom {@link InvocationInterceptor} that intercepts test method invocation. + * To pass the test ***only if*** test fails with an exception, and fail the test otherwise. + * + * @since 2.19 + */ +public class JacksonTestFailureExpectedInterceptor + implements InvocationInterceptor +{ + @Override + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, ExtensionContext extensionContext) + throws Throwable + { + try { + invocation.proceed(); + } catch (Throwable t) { + // do-nothing, we do expect an exception + return; + } + handleUnexpectePassingTest(invocationContext); + } + + private void handleUnexpectePassingTest(ReflectiveInvocationContext invocationContext) { + // Collect information we need + Object targetClass = invocationContext.getTargetClass(); + Object testMethod = invocationContext.getExecutable().getName(); + //List arguments = invocationContext.getArguments(); + + // Create message + String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod); + + // throw exception + throw new JacksonTestShouldFailException(message); + } + +} diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestShouldFailException.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 000000000..3d1ef99de --- /dev/null +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.dataformat.ion.testutil.failure; + +/** + * Exception used to alert that a test is passing, but should be failing. + * + * WARNING : This only for test code, and should never be thrown from production code. + * + * @since 2.19 + */ +public class JacksonTestShouldFailException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public JacksonTestShouldFailException(String msg) { + super(msg); + } +} diff --git a/pom.xml b/pom.xml index f9cb18d33..9b50e0f52 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,18 @@ junit junit test - + + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.jupiter + junit-jupiter-api + test +