Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JSTEP-10] Only add testutils to where needed #279

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fasterxml.jackson.module.afterburner.util.failure;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* <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,43 @@
package com.fasterxml.jackson.module.afterburner.util.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<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.module.afterburner.util.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,37 @@
package com.fasterxml.jackson.module.androidrecord.testutil.failure;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* <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,43 @@
package com.fasterxml.jackson.module.androidrecord.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<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.module.androidrecord.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,37 @@
package com.fasterxml.jackson.module.blackbird.testutil.failure;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* <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,43 @@
package com.fasterxml.jackson.module.blackbird.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<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.module.blackbird.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,37 @@
package com.fasterxml.jackson.module.jakarta.xmlbind.testutil.failure;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* <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,43 @@
package com.fasterxml.jackson.module.jakarta.xmlbind.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<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);
}

}
Loading