diff --git a/integration-tests/main/src/main/java/io/quarkus/it/corestuff/serialization/SerializationConfig.java b/integration-tests/main/src/main/java/io/quarkus/it/corestuff/serialization/SerializationConfig.java index f2124ea4135ef..e508f0d19da97 100644 --- a/integration-tests/main/src/main/java/io/quarkus/it/corestuff/serialization/SerializationConfig.java +++ b/integration-tests/main/src/main/java/io/quarkus/it/corestuff/serialization/SerializationConfig.java @@ -1,11 +1,10 @@ package io.quarkus.it.corestuff.serialization; import java.util.ArrayList; -import java.util.List; import io.quarkus.runtime.annotations.RegisterForReflection; -@RegisterForReflection(targets = { List.class, ArrayList.class, String.class }, serialization = true) +@RegisterForReflection(targets = { ArrayList.class, String.class }, serialization = true) public class SerializationConfig { } diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/RegisterForReflectionITCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/RegisterForReflectionITCase.java index 480c7e7392be2..0a029bb77e394 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/RegisterForReflectionITCase.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/RegisterForReflectionITCase.java @@ -4,6 +4,9 @@ import org.junit.jupiter.api.Test; +import io.quarkus.test.junit.DisableIfBuiltWithGraalVMNewerThan; +import io.quarkus.test.junit.DisableIfBuiltWithGraalVMOlderThan; +import io.quarkus.test.junit.GraalVMVersion; import io.quarkus.test.junit.QuarkusIntegrationTest; import io.restassured.RestAssured; @@ -35,7 +38,20 @@ public void testSelfWithNested() { } @Test - public void testTargetWithNested() { + @DisableIfBuiltWithGraalVMOlderThan(GraalVMVersion.GRAALVM_22_1) + public void testTargetWithNestedPost22_1() { + final String resourceC = BASE_PKG + ".ResourceC"; + + // Starting with GraalVM 22.1 ResourceC implicitly gets registered by GraalVM + // (see https://github.com/oracle/graal/pull/4414) + assertRegistration("ResourceC", resourceC); + assertRegistration("InaccessibleClassOfC", resourceC + "$InaccessibleClassOfC"); + assertRegistration("OtherInaccessibleClassOfC", resourceC + "$InaccessibleClassOfC$OtherInaccessibleClassOfC"); + } + + @Test + @DisableIfBuiltWithGraalVMNewerThan(GraalVMVersion.GRAALVM_22_0) + public void testTargetWithNestedPre22_1() { final String resourceC = BASE_PKG + ".ResourceC"; assertRegistration("FAILED", resourceC); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThan.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThan.java new file mode 100644 index 0000000000000..ccc876e1d9aaa --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThan.java @@ -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(); +} diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThanCondition.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThanCondition.java new file mode 100644 index 0000000000000..c7a9bbfaf687d --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMNewerThanCondition.java @@ -0,0 +1,67 @@ +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.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 SUPPORTED_INTEGRATION_TESTS = Set.of(QUARKUS_INTEGRATION_TEST_NAME, + NATIVE_IMAGE_TEST_NAME); + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { + Optional element = context.getElement(); + Optional 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 annotationType = annotation.annotationType(); + String annotationTypeName = annotationType.getName(); + if (SUPPORTED_INTEGRATION_TESTS.contains(annotationTypeName)) { + return true; + } + } + testClass = testClass.getSuperclass(); + } while (testClass != Object.class); + return false; + } +} diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThan.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThan.java index f4ca6efbb120b..2e466f73202f3 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThan.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThan.java @@ -18,37 +18,5 @@ @Retention(RetentionPolicy.RUNTIME) @ExtendWith(DisableIfBuiltWithGraalVMOlderThanCondition.class) public @interface DisableIfBuiltWithGraalVMOlderThan { - GraalVMVersion value(); - - enum GraalVMVersion { - GRAALVM_21_0(org.graalvm.home.Version.create(21, 0)); - - 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() + - '}'; - } - } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThanCondition.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThanCondition.java index 4e3fdce2a885f..f5af1f92aa767 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThanCondition.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisableIfBuiltWithGraalVMOlderThanCondition.java @@ -5,9 +5,6 @@ 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; @@ -20,8 +17,8 @@ public class DisableIfBuiltWithGraalVMOlderThanCondition implements ExecutionCon 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 SUPPORTED_INTEGRATION_TESTS = Collections - .unmodifiableSet(new HashSet<>(Arrays.asList(QUARKUS_INTEGRATION_TEST_NAME, NATIVE_IMAGE_TEST_NAME))); + private static final Set SUPPORTED_INTEGRATION_TESTS = Set.of(QUARKUS_INTEGRATION_TEST_NAME, + NATIVE_IMAGE_TEST_NAME); @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { @@ -35,7 +32,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con return ConditionEvaluationResult.enabled("@DisableIfBuiltWithGraalVMOlderThan was added to an unsupported test"); } - DisableIfBuiltWithGraalVMOlderThan.GraalVMVersion annotationValue = optional.get().value(); + GraalVMVersion annotationValue = optional.get().value(); Properties quarkusArtifactProperties = readQuarkusArtifactProperties(context); try { org.graalvm.home.Version version = org.graalvm.home.Version diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java new file mode 100644 index 0000000000000..8ef1e0b52273f --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java @@ -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() + + '}'; + } +}