Skip to content

Commit 26b5e2a

Browse files
authored
[ JSTEP-10 ] Add test dependencies (#548)
1 parent 163f423 commit 26b5e2a

File tree

16 files changed

+507
-1
lines changed

16 files changed

+507
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.dataformat.avro.testutil.failure;
2+
3+
import java.lang.annotation.*;
4+
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
7+
/**
8+
* <p>
9+
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
10+
*
11+
* <p>
12+
* When a test method is annotated with {@code @JacksonTestFailureExpected}, the
13+
* {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution.
14+
* If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test,
15+
* indicating that the test was expected to fail but didn't.
16+
* </p>
17+
*
18+
* <h3>Usage Example:</h3>
19+
*
20+
* <pre><code>
21+
*
22+
* &#64;Test
23+
* &#64;JacksonTestFailureExpected
24+
* @Test
25+
public void testFeatureNotYetImplemented() {
26+
* // Test code that is expected to fail
27+
* }
28+
* }
29+
* </code></pre>
30+
*
31+
* <p>
32+
*
33+
* @since 2.19
34+
*/
35+
@Target({ElementType.METHOD})
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
38+
public @interface JacksonTestFailureExpected { }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.dataformat.avro.testutil.failure;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.junit.jupiter.api.extension.*;
6+
7+
/**
8+
* Custom {@link InvocationInterceptor} that intercepts test method invocation.
9+
* To pass the test ***only if*** test fails with an exception, and fail the test otherwise.
10+
*
11+
* @since 2.19
12+
*/
13+
public class JacksonTestFailureExpectedInterceptor
14+
implements InvocationInterceptor
15+
{
16+
@Override
17+
public void interceptTestMethod(Invocation<Void> invocation,
18+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
19+
throws Throwable
20+
{
21+
try {
22+
invocation.proceed();
23+
} catch (Throwable t) {
24+
// do-nothing, we do expect an exception
25+
return;
26+
}
27+
handleUnexpectePassingTest(invocationContext);
28+
}
29+
30+
private void handleUnexpectePassingTest(ReflectiveInvocationContext<Method> invocationContext) {
31+
// Collect information we need
32+
Object targetClass = invocationContext.getTargetClass();
33+
Object testMethod = invocationContext.getExecutable().getName();
34+
//List<Object> arguments = invocationContext.getArguments();
35+
36+
// Create message
37+
String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod);
38+
39+
// throw exception
40+
throw new JacksonTestShouldFailException(message);
41+
}
42+
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.dataformat.avro.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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.dataformat.cbor.testutil.failure;
2+
3+
import java.lang.annotation.*;
4+
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
7+
/**
8+
* <p>
9+
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
10+
*
11+
* <p>
12+
* When a test method is annotated with {@code @JacksonTestFailureExpected}, the
13+
* {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution.
14+
* If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test,
15+
* indicating that the test was expected to fail but didn't.
16+
* </p>
17+
*
18+
* <h3>Usage Example:</h3>
19+
*
20+
* <pre><code>
21+
*
22+
* &#64;Test
23+
* &#64;JacksonTestFailureExpected
24+
* @Test
25+
public void testFeatureNotYetImplemented() {
26+
* // Test code that is expected to fail
27+
* }
28+
* }
29+
* </code></pre>
30+
*
31+
* <p>
32+
*
33+
* @since 2.19
34+
*/
35+
@Target({ElementType.METHOD})
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
38+
public @interface JacksonTestFailureExpected { }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.dataformat.cbor.testutil.failure;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.junit.jupiter.api.extension.*;
6+
7+
/**
8+
* Custom {@link InvocationInterceptor} that intercepts test method invocation.
9+
* To pass the test ***only if*** test fails with an exception, and fail the test otherwise.
10+
*
11+
* @since 2.19
12+
*/
13+
public class JacksonTestFailureExpectedInterceptor
14+
implements InvocationInterceptor
15+
{
16+
@Override
17+
public void interceptTestMethod(Invocation<Void> invocation,
18+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
19+
throws Throwable
20+
{
21+
try {
22+
invocation.proceed();
23+
} catch (Throwable t) {
24+
// do-nothing, we do expect an exception
25+
return;
26+
}
27+
handleUnexpectePassingTest(invocationContext);
28+
}
29+
30+
private void handleUnexpectePassingTest(ReflectiveInvocationContext<Method> invocationContext) {
31+
// Collect information we need
32+
Object targetClass = invocationContext.getTargetClass();
33+
Object testMethod = invocationContext.getExecutable().getName();
34+
//List<Object> arguments = invocationContext.getArguments();
35+
36+
// Create message
37+
String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod);
38+
39+
// throw exception
40+
throw new JacksonTestShouldFailException(message);
41+
}
42+
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.dataformat.cbor.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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.dataformat.ion.testutil.failure;
2+
3+
import java.lang.annotation.*;
4+
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
7+
/**
8+
* <p>
9+
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
10+
*
11+
* <p>
12+
* When a test method is annotated with {@code @JacksonTestFailureExpected}, the
13+
* {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution.
14+
* If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test,
15+
* indicating that the test was expected to fail but didn't.
16+
* </p>
17+
*
18+
* <h3>Usage Example:</h3>
19+
*
20+
* <pre><code>
21+
*
22+
* &#64;Test
23+
* &#64;JacksonTestFailureExpected
24+
* @Test
25+
public void testFeatureNotYetImplemented() {
26+
* // Test code that is expected to fail
27+
* }
28+
* }
29+
* </code></pre>
30+
*
31+
* <p>
32+
*
33+
* @since 2.19
34+
*/
35+
@Target({ElementType.METHOD})
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
38+
public @interface JacksonTestFailureExpected { }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.dataformat.ion.testutil.failure;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.junit.jupiter.api.extension.*;
6+
7+
/**
8+
* Custom {@link InvocationInterceptor} that intercepts test method invocation.
9+
* To pass the test ***only if*** test fails with an exception, and fail the test otherwise.
10+
*
11+
* @since 2.19
12+
*/
13+
public class JacksonTestFailureExpectedInterceptor
14+
implements InvocationInterceptor
15+
{
16+
@Override
17+
public void interceptTestMethod(Invocation<Void> invocation,
18+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
19+
throws Throwable
20+
{
21+
try {
22+
invocation.proceed();
23+
} catch (Throwable t) {
24+
// do-nothing, we do expect an exception
25+
return;
26+
}
27+
handleUnexpectePassingTest(invocationContext);
28+
}
29+
30+
private void handleUnexpectePassingTest(ReflectiveInvocationContext<Method> invocationContext) {
31+
// Collect information we need
32+
Object targetClass = invocationContext.getTargetClass();
33+
Object testMethod = invocationContext.getExecutable().getName();
34+
//List<Object> arguments = invocationContext.getArguments();
35+
36+
// Create message
37+
String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod);
38+
39+
// throw exception
40+
throw new JacksonTestShouldFailException(message);
41+
}
42+
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.dataformat.ion.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+
}

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@
6767
<groupId>junit</groupId>
6868
<artifactId>junit</artifactId>
6969
<scope>test</scope>
70-
</dependency>
70+
</dependency>
71+
<!-- 11-Jan-2025, joohyukkim: For JSTEP-10, migrate to JUnit5 -->
72+
<dependency>
73+
<groupId>org.junit.jupiter</groupId>
74+
<artifactId>junit-jupiter</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.junit.jupiter</groupId>
79+
<artifactId>junit-jupiter-api</artifactId>
80+
<scope>test</scope>
81+
</dependency>
7182
</dependencies>
7283

7384
<!-- Alas, need to include snapshot reference since otherwise can not find

0 commit comments

Comments
 (0)