Skip to content

Commit 750f7a0

Browse files
committed
Optimize MetafixToDo a little. (f4a7523, f4a7523) (#158)
1 parent f053099 commit 750f7a0

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

metafix/src/test/java/org/metafacture/metafix/MetafixToDo.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.lang.annotation.RetentionPolicy;
1515
import java.lang.annotation.Target;
1616
import java.lang.reflect.Method;
17+
import java.util.HashMap;
18+
import java.util.Map;
1719
import java.util.function.Consumer;
1820

1921
@Target(ElementType.METHOD)
@@ -25,12 +27,14 @@
2527

2628
class Extension implements InvocationInterceptor {
2729

30+
private static final boolean DISABLE_TO_DO = Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.disableToDo"));
31+
2832
private Extension() {
2933
}
3034

3135
@Override
3236
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) {
3438
handleAnnotation(invocationContext, a -> {
3539
throw new TestAbortedException(a.value());
3640
});
@@ -68,21 +72,32 @@ class Handler implements ExecutionCondition {
6872
private static final Class<Extension> EXTENSION_CLASS = Extension.class;
6973
private static final String EXTENSION_NAME = EXTENSION_CLASS.getTypeName();
7074

75+
private static final Map<Class<?>, Boolean> EXTENSION_PRESENT = new HashMap<>();
76+
7177
private Handler() {
7278
}
7379

7480
@Override
7581
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+
}
8088
}
8189
}
82-
}
8390

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+
}
86101
}
87102

88103
}

0 commit comments

Comments
 (0)