|
14 | 14 | import java.lang.annotation.RetentionPolicy; |
15 | 15 | import java.lang.annotation.Target; |
16 | 16 | import java.lang.reflect.Method; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
17 | 19 | import java.util.function.Consumer; |
18 | 20 |
|
19 | 21 | @Target(ElementType.METHOD) |
|
25 | 27 |
|
26 | 28 | class Extension implements InvocationInterceptor { |
27 | 29 |
|
| 30 | + private static final boolean DISABLE_TO_DO = Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.disableToDo")); |
| 31 | + |
28 | 32 | private Extension() { |
29 | 33 | } |
30 | 34 |
|
31 | 35 | @Override |
32 | 36 | public void interceptTestMethod(final InvocationInterceptor.Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
33 | | - if (Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.disableToDo"))) { |
| 37 | + if (DISABLE_TO_DO) { |
34 | 38 | handleAnnotation(invocationContext, a -> { |
35 | 39 | throw new TestAbortedException(a.value()); |
36 | 40 | }); |
@@ -68,21 +72,32 @@ class Handler implements ExecutionCondition { |
68 | 72 | private static final Class<Extension> EXTENSION_CLASS = Extension.class; |
69 | 73 | private static final String EXTENSION_NAME = EXTENSION_CLASS.getTypeName(); |
70 | 74 |
|
| 75 | + private static final Map<Class<?>, Boolean> EXTENSION_PRESENT = new HashMap<>(); |
| 76 | + |
71 | 77 | private Handler() { |
72 | 78 | } |
73 | 79 |
|
74 | 80 | @Override |
75 | 81 | public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { |
76 | | - for (final ExtendWith annotation : context.getTestClass().get().getAnnotationsByType(ExtendWith.class)) { |
77 | | - for (final Class<? extends org.junit.jupiter.api.extension.Extension> extensionClass : annotation.value()) { |
78 | | - if (extensionClass.isAssignableFrom(EXTENSION_CLASS)) { |
79 | | - return ConditionEvaluationResult.enabled("Extension present: " + EXTENSION_NAME); |
| 82 | + final boolean extensionPresent = EXTENSION_PRESENT.computeIfAbsent(context.getTestClass().get(), k -> { |
| 83 | + for (final ExtendWith annotation : k.getAnnotationsByType(ExtendWith.class)) { |
| 84 | + for (final Class<? extends org.junit.jupiter.api.extension.Extension> extensionClass : annotation.value()) { |
| 85 | + if (extensionClass.isAssignableFrom(EXTENSION_CLASS)) { |
| 86 | + return true; |
| 87 | + } |
80 | 88 | } |
81 | 89 | } |
82 | | - } |
83 | 90 |
|
84 | | - Assertions.fail("Extension missing: " + EXTENSION_NAME); |
85 | | - return null; // not reached |
| 91 | + return false; |
| 92 | + }); |
| 93 | + |
| 94 | + if (extensionPresent) { |
| 95 | + return ConditionEvaluationResult.enabled("Extension present: " + EXTENSION_NAME); |
| 96 | + } |
| 97 | + else { |
| 98 | + Assertions.fail("Extension missing: " + EXTENSION_NAME); |
| 99 | + return null; // not reached |
| 100 | + } |
86 | 101 | } |
87 | 102 |
|
88 | 103 | } |
|
0 commit comments