From baf42c305cf72f915a3f4aa691848a3b64e59629 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 12 Jan 2025 02:18:42 +0900 Subject: [PATCH] [ JSTEP-10 ] Add test dependencies (#524) --- .../failure/JacksonTestFailureExpected.java | 40 +++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 45 +++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ pom.xml | 16 +++++++ .../failure/JacksonTestFailureExpected.java | 40 +++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 45 +++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ .../failure/JacksonTestFailureExpected.java | 40 +++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 45 +++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ .../failure/JacksonTestFailureExpected.java | 40 +++++++++++++++++ ...JacksonTestFailureExpectedInterceptor.java | 45 +++++++++++++++++++ .../JacksonTestShouldFailException.java | 18 ++++++++ 13 files changed, 428 insertions(+) create mode 100644 csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpected.java create mode 100644 csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestShouldFailException.java create mode 100644 properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/testutil/failure/JacksonTestFailureExpected.java create mode 100644 properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/testutil/failure/JacksonTestShouldFailException.java create mode 100644 toml/src/test/java/com/fasterxml/jackson/dataformat/toml/testutil/failure/JacksonTestFailureExpected.java create mode 100644 toml/src/test/java/com/fasterxml/jackson/dataformat/toml/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 toml/src/test/java/com/fasterxml/jackson/dataformat/toml/testutil/failure/JacksonTestShouldFailException.java create mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/testutil/failure/JacksonTestFailureExpected.java create mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/testutil/failure/JacksonTestFailureExpectedInterceptor.java create mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/testutil/failure/JacksonTestShouldFailException.java diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpected.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 000000000..3fb2f3fc6 --- /dev/null +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,40 @@ +package com.fasterxml.jackson.dataformat.csv.testutil.failure; + +import org.junit.jupiter.api.extension.ExtendWith; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + *

+ * 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
+ *     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/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 000000000..354a1493c --- /dev/null +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,45 @@ +package com.fasterxml.jackson.dataformat.csv.testutil.failure; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.InvocationInterceptor; +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; + +import java.lang.reflect.Method; + +/** + * 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/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestShouldFailException.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 000000000..153d5a7ce --- /dev/null +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.dataformat.csv.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 ce8d95c92..0703d210c 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,22 @@ junit test + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.assertj + assertj-core + test +