Skip to content

Commit

Permalink
Allow the use of @DisableIfBuiltWithGraalVMOlderThan on test methods
Browse files Browse the repository at this point in the history
Relates to #16216 (comment)
  • Loading branch information
geoand committed Apr 7, 2021
1 parent a9c8cec commit 5d06a05
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Expand Up @@ -8,13 +8,13 @@
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Used to signal that a test class should be disabled if the version of GraalVM used to build the native binary
* Used to signal that a test class or method should be disabled if the version of GraalVM used to build the native binary
* under test was older than the supplied version.
*
* This annotation should only be used on a test classes annotated with {@link NativeImageTest} or
* {@link QuarkusIntegrationTest}
* {@link QuarkusIntegrationTest}. If it is used on other test classes, it will have no effect.
*/
@Target({ ElementType.TYPE })
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DisableIfBuiltWithGraalVMOlderThanCondition.class)
public @interface DisableIfBuiltWithGraalVMOlderThan {
Expand Down
Expand Up @@ -3,16 +3,26 @@
import static io.quarkus.test.junit.IntegrationTestUtil.readQuarkusArtifactProperties;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

public class DisableIfBuiltWithGraalVMOlderThanCondition implements ExecutionCondition {

private static final String QUARKUS_INTEGRATION_TEST_NAME = QuarkusIntegrationTest.class.getName();
private static final String NATIVE_IMAGE_TEST_NAME = NativeImageTest.class.getName();
private static final Set<String> SUPPORTED_INTEGRATION_TESTS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList(QUARKUS_INTEGRATION_TEST_NAME, NATIVE_IMAGE_TEST_NAME)));

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<AnnotatedElement> element = context.getElement();
Expand All @@ -21,6 +31,9 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
if (!optional.isPresent()) {
return ConditionEvaluationResult.enabled("@DisableIfBuiltWithGraalVMOlderThan was not found");
}
if (!isIntegrationTest(context.getRequiredTestClass())) {
return ConditionEvaluationResult.enabled("@DisableIfBuiltWithGraalVMOlderThan was added to an unsupported test");
}

DisableIfBuiltWithGraalVMOlderThan.GraalVMVersion annotationValue = optional.get().value();
Properties quarkusArtifactProperties = readQuarkusArtifactProperties(context);
Expand All @@ -39,4 +52,19 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
.disabled("Unable to determine the GraalVM version with which the native binary was built");
}
}

private boolean isIntegrationTest(Class<?> testClass) {
do {
Annotation[] annotations = testClass.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
String annotationTypeName = annotationType.getName();
if (SUPPORTED_INTEGRATION_TESTS.contains(annotationTypeName)) {
return true;
}
}
testClass = testClass.getSuperclass();
} while (testClass != Object.class);
return false;
}
}

0 comments on commit 5d06a05

Please sign in to comment.