Skip to content

Commit

Permalink
Verify that MetafixToDo extension is actually used. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Mar 22, 2022
1 parent 642f9b0 commit e449894
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion metafix/src/test/java/org/metafacture/metafix/MetafixToDo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.metafacture.metafix;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -25,13 +27,28 @@

String value();

class Extension implements InvocationInterceptor {
class Extension implements AfterAllCallback, InvocationInterceptor {

private static final boolean DISABLE_TO_DO = Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.disableToDo"));

private boolean annotationPresent;

private Extension() {
}

@Override
public void afterAll(final ExtensionContext context) {
if (!annotationPresent) {
for (final Method method : context.getTestClass().get().getDeclaredMethods()) {
if (method.isAnnotationPresent(Test.class) && method.isAnnotationPresent(MetafixToDo.class)) {
return;
}
}

Assertions.fail("Unused extension (no annotations present): " + Handler.EXTENSION_NAME);
}
}

@Override
public void interceptTestMethod(final InvocationInterceptor.Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable {
if (DISABLE_TO_DO) {
Expand Down Expand Up @@ -61,6 +78,7 @@ private void handleAnnotation(final ReflectiveInvocationContext<Method> invocati
final MetafixToDo annotation = invocationContext.getExecutable().getAnnotation(MetafixToDo.class);

if (annotation != null) {
annotationPresent = true;
consumer.accept(annotation);
}
}
Expand Down

0 comments on commit e449894

Please sign in to comment.