|
1 | | -/******************************************************************************* |
2 | | - * Copyright (c) 2025 Avaloq Group AG and others. |
3 | | - * All rights reserved. This program and the accompanying materials |
4 | | - * are made available under the terms of the Eclipse Public License v1.0 |
5 | | - * which accompanies this distribution, and is available at |
6 | | - * http://www.eclipse.org/legal/epl-v10.html |
7 | | - * |
8 | | - * Contributors: |
9 | | - * Avaloq Group AG - initial API and implementation |
10 | | - *******************************************************************************/ |
11 | | -package com.avaloq.tools.ddk.test.core.jupiter; |
12 | | - |
13 | | -import java.lang.reflect.Method; |
14 | | - |
15 | | -import org.junit.jupiter.api.extension.ExtensionContext; |
16 | | -import org.junit.jupiter.api.extension.InvocationInterceptor; |
17 | | -import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
18 | | - |
19 | | -import com.avaloq.tools.ddk.test.core.BugTest; |
20 | | - |
21 | | - |
22 | | -/** |
23 | | - * This {@link InvocationInterceptor} implementation changes the behavior for unresolved bug tests. |
24 | | - * <p> |
25 | | - * The behavior for at test that is annotated with {@link BugTest(unresolved=true)} is the following: |
26 | | - * <ul> |
27 | | - * <li>Test evaluation <code>OK</code> results in <code>FAIL</code> ({@link AssertionError})</li> |
28 | | - * <li>Test evaluation <code>FAIL</code> results in <code>OK</code></li> |
29 | | - * <li>Test evaluation <code>ERROR</code> results in <code>ERROR</code></li> |
30 | | - * </ul> |
31 | | - * </p> |
32 | | - * <p> |
33 | | - * Example for a bug test: |
34 | | - * |
35 | | - * <pre> |
36 | | - * public class TestClass { |
37 | | - * |
38 | | - * @Rule |
39 | | - * public BugTestAwareRule rule = BugTestAwareRule.getInstance(); |
40 | | - * |
41 | | - * @org.junit.Test |
42 | | - * @com.avaloq.tools.ddk.test.core.BugTest(value = "BUG-42", unresolved = true) |
43 | | - * public void testMethod() { |
44 | | - * org.junit.Assert.fail(); |
45 | | - * } |
46 | | - * } |
47 | | - * </pre> |
48 | | - * </p> |
49 | | - * |
50 | | - * @see BugTest |
51 | | - */ |
52 | | -public final class BugTestAwareRule implements InvocationInterceptor { |
53 | | - |
54 | | - private static final String ERROR_TEST_MUST_FAIL = "The unresolved bug test must fail:"; //$NON-NLS-1$ |
55 | | - /** The singleton instance, or {@code null} if not cached. */ |
56 | | - private static BugTestAwareRule instance; |
57 | | - private static final Object LOCK = new Object(); |
58 | | - |
59 | | - /** |
60 | | - * Creates a new instance of {@link BugTestAwareRule}. |
61 | | - */ |
62 | | - private BugTestAwareRule() { |
63 | | - // prevent instantiation |
64 | | - } |
65 | | - |
66 | | - /** |
67 | | - * Returns a shared singleton instance. |
68 | | - * |
69 | | - * @return a shared instance, never {@code null} |
70 | | - */ |
71 | | - public static BugTestAwareRule getInstance() { |
72 | | - synchronized (LOCK) { |
73 | | - if (instance == null) { |
74 | | - instance = new BugTestAwareRule(); |
75 | | - } |
76 | | - return instance; |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - @SuppressWarnings("nls") |
81 | | - @Override |
82 | | - public void interceptTestMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
83 | | - BugTest bugTestAnnotation = extensionContext.getRequiredTestMethod().getAnnotation(BugTest.class); |
84 | | - if (bugTestAnnotation == null && extensionContext.getRequiredTestClass() != null) { |
85 | | - bugTestAnnotation = extensionContext.getRequiredTestClass().getAnnotation(BugTest.class); |
86 | | - } |
87 | | - if (bugTestAnnotation != null && bugTestAnnotation.unresolved()) { |
88 | | - try { |
89 | | - invocation.proceed(); |
90 | | - } catch (AssertionError exception) { |
91 | | - return; |
92 | | - } |
93 | | - String testCase = extensionContext.getRequiredTestClass().getSimpleName() + "." + extensionContext.getRequiredTestMethod().getName(); |
94 | | - throw new AssertionError(ERROR_TEST_MUST_FAIL + " " + testCase); |
95 | | - } else { |
96 | | - invocation.proceed(); |
97 | | - } |
98 | | - } |
99 | | - |
100 | | -} |
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Avaloq Group AG and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Avaloq Group AG - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package com.avaloq.tools.ddk.test.core.jupiter; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 16 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 17 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 18 | + |
| 19 | +import com.avaloq.tools.ddk.test.core.BugTest; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * This {@link InvocationInterceptor} implementation changes the behavior for unresolved bug tests. |
| 24 | + * <p> |
| 25 | + * The behavior for at test that is annotated with {@link BugTest(unresolved=true)} is the following: |
| 26 | + * <ul> |
| 27 | + * <li>Test evaluation <code>OK</code> results in <code>FAIL</code> ({@link AssertionError})</li> |
| 28 | + * <li>Test evaluation <code>FAIL</code> results in <code>OK</code></li> |
| 29 | + * <li>Test evaluation <code>ERROR</code> results in <code>ERROR</code></li> |
| 30 | + * </ul> |
| 31 | + * </p> |
| 32 | + * <p> |
| 33 | + * Example for a bug test: |
| 34 | + * |
| 35 | + * <pre> |
| 36 | + * public class TestClass { |
| 37 | + * |
| 38 | + * @Rule |
| 39 | + * public BugTestAwareRule rule = BugTestAwareRule.getInstance(); |
| 40 | + * |
| 41 | + * @org.junit.Test |
| 42 | + * @com.avaloq.tools.ddk.test.core.BugTest(value = "BUG-42", unresolved = true) |
| 43 | + * public void testMethod() { |
| 44 | + * org.junit.Assert.fail(); |
| 45 | + * } |
| 46 | + * } |
| 47 | + * </pre> |
| 48 | + * </p> |
| 49 | + * |
| 50 | + * @see BugTest |
| 51 | + */ |
| 52 | +public final class BugTestAwareRule implements InvocationInterceptor { |
| 53 | + |
| 54 | + private static final String ERROR_TEST_MUST_FAIL = "The unresolved bug test must fail:"; //$NON-NLS-1$ |
| 55 | + /** The singleton instance, or {@code null} if not cached. */ |
| 56 | + private static BugTestAwareRule instance; |
| 57 | + private static final Object LOCK = new Object(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a new instance of {@link BugTestAwareRule}. |
| 61 | + */ |
| 62 | + private BugTestAwareRule() { |
| 63 | + // prevent instantiation |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns a shared singleton instance. |
| 68 | + * |
| 69 | + * @return a shared instance, never {@code null} |
| 70 | + */ |
| 71 | + public static BugTestAwareRule getInstance() { |
| 72 | + synchronized (LOCK) { |
| 73 | + if (instance == null) { |
| 74 | + instance = new BugTestAwareRule(); |
| 75 | + } |
| 76 | + return instance; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @SuppressWarnings("nls") |
| 81 | + @Override |
| 82 | + public void interceptTestMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 83 | + BugTest bugTestAnnotation = extensionContext.getRequiredTestMethod().getAnnotation(BugTest.class); |
| 84 | + if (bugTestAnnotation == null && extensionContext.getRequiredTestClass() != null) { |
| 85 | + bugTestAnnotation = extensionContext.getRequiredTestClass().getAnnotation(BugTest.class); |
| 86 | + } |
| 87 | + if (bugTestAnnotation != null && bugTestAnnotation.unresolved()) { |
| 88 | + try { |
| 89 | + invocation.proceed(); |
| 90 | + } catch (AssertionError exception) { |
| 91 | + return; |
| 92 | + } |
| 93 | + String testCase = extensionContext.getRequiredTestClass().getSimpleName() + "." + extensionContext.getRequiredTestMethod().getName(); |
| 94 | + throw new AssertionError(ERROR_TEST_MUST_FAIL + " " + testCase); |
| 95 | + } else { |
| 96 | + invocation.proceed(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments