Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of @DisableIfBuiltWithGraalVMOlderThan on test methods #16285

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}