Skip to content

Commit

Permalink
[ JSTEP-10 ] Add test dependencies (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Jan 11, 2025
1 parent bb65a01 commit baf42c3
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
*
* <p>
* 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.
* </p>
*
* <h3>Usage Example:</h3>
*
* <pre><code>
*
* &#64;Test
* &#64;JacksonTestFailureExpected
* public void testFeatureNotYetImplemented() {
* // Test code that is expected to fail
* }
* }
* </code></pre>
*
* <p>
*
* @since 2.19
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
public @interface JacksonTestFailureExpected { }
Original file line number Diff line number Diff line change
@@ -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<Void> invocation,
ReflectiveInvocationContext<Method> 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<Method> invocationContext) {
// Collect information we need
Object targetClass = invocationContext.getTargetClass();
Object testMethod = invocationContext.getExecutable().getName();
//List<Object> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 11-Jan-2025, joohyukkim: JSTEP-10, part 1, migrate to JUnit5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- Alas, need to include snapshot reference since otherwise can not find
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.dataformat.javaprop.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;

/**
* <p>
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
*
* <p>
* 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.
* </p>
*
* <h3>Usage Example:</h3>
*
* <pre><code>
*
* &#64;Test
* &#64;JacksonTestFailureExpected
* public void testFeatureNotYetImplemented() {
* // Test code that is expected to fail
* }
* }
* </code></pre>
*
* <p>
*
* @since 2.19
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
public @interface JacksonTestFailureExpected { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.dataformat.javaprop.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<Void> invocation,
ReflectiveInvocationContext<Method> 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<Method> invocationContext) {
// Collect information we need
Object targetClass = invocationContext.getTargetClass();
Object testMethod = invocationContext.getExecutable().getName();
//List<Object> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.dataformat.javaprop.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.dataformat.toml.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;

/**
* <p>
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
*
* <p>
* 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.
* </p>
*
* <h3>Usage Example:</h3>
*
* <pre><code>
*
* &#64;Test
* &#64;JacksonTestFailureExpected
* public void testFeatureNotYetImplemented() {
* // Test code that is expected to fail
* }
* }
* </code></pre>
*
* <p>
*
* @since 2.19
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
public @interface JacksonTestFailureExpected { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.dataformat.toml.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<Void> invocation,
ReflectiveInvocationContext<Method> 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<Method> invocationContext) {
// Collect information we need
Object targetClass = invocationContext.getTargetClass();
Object testMethod = invocationContext.getExecutable().getName();
//List<Object> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.dataformat.toml.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.dataformat.yaml.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;

/**
* <p>
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
*
* <p>
* 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.
* </p>
*
* <h3>Usage Example:</h3>
*
* <pre><code>
*
* &#64;Test
* &#64;JacksonTestFailureExpected
* public void testFeatureNotYetImplemented() {
* // Test code that is expected to fail
* }
* }
* </code></pre>
*
* <p>
*
* @since 2.19
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
public @interface JacksonTestFailureExpected { }
Loading

0 comments on commit baf42c3

Please sign in to comment.