-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...mework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.test.junit; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
/** | ||
* 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}. If it is used on other test classes, it will have no effect. | ||
*/ | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(DisableIfBuiltWithGraalVMNewerThanCondition.class) | ||
public @interface DisableIfBuiltWithGraalVMNewerThan { | ||
GraalVMVersion value(); | ||
} |
70 changes: 70 additions & 0 deletions
70
...nit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThanCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.test.junit; | ||
|
||
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 DisableIfBuiltWithGraalVMNewerThanCondition 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(); | ||
Optional<DisableIfBuiltWithGraalVMNewerThan> optional = findAnnotation(element, | ||
DisableIfBuiltWithGraalVMNewerThan.class); | ||
if (!optional.isPresent()) { | ||
return ConditionEvaluationResult.enabled("@DisableIfBuiltWithGraalVMNewerThan was not found"); | ||
} | ||
if (!isIntegrationTest(context.getRequiredTestClass())) { | ||
return ConditionEvaluationResult.enabled("@DisableIfBuiltWithGraalVMNewerThan was added to an unsupported test"); | ||
} | ||
|
||
GraalVMVersion annotationValue = optional.get().value(); | ||
Properties quarkusArtifactProperties = readQuarkusArtifactProperties(context); | ||
try { | ||
org.graalvm.home.Version version = org.graalvm.home.Version | ||
.parse(quarkusArtifactProperties.getProperty("metadata.graalvm.version.version")); | ||
int comparison = annotationValue.compareTo(version); | ||
if (comparison < 0) { | ||
return ConditionEvaluationResult.disabled("Native binary was built with GraalVM{version=" + version.toString() | ||
+ "} but the test is disabled for GraalVM versions newer than " + annotationValue); | ||
} | ||
return ConditionEvaluationResult | ||
.enabled("Native binary was built with a GraalVM version compatible with the required version by the test"); | ||
} catch (NumberFormatException e) { | ||
return ConditionEvaluationResult | ||
.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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.test.junit; | ||
|
||
public enum GraalVMVersion { | ||
GRAALVM_21_0(org.graalvm.home.Version.create(21, 0)), | ||
GRAALVM_22_0(org.graalvm.home.Version.create(22, 0)), | ||
GRAALVM_22_1(org.graalvm.home.Version.create(22, 1)); | ||
|
||
private final org.graalvm.home.Version version; | ||
|
||
GraalVMVersion(org.graalvm.home.Version version) { | ||
this.version = version; | ||
} | ||
|
||
public org.graalvm.home.Version getVersion() { | ||
return version; | ||
} | ||
|
||
/** | ||
* Compares this version with another GraalVM version | ||
* | ||
* @return {@code -1} if this version is older than the other version, | ||
* {@code +1} if it's newer and {@code 0} if they represent the same version | ||
*/ | ||
public int compareTo(org.graalvm.home.Version version) { | ||
return this.version.compareTo(version); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "GraalVMVersion{" + | ||
"version=" + version.toString() + | ||
'}'; | ||
} | ||
} |