diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/ArtifactTypeUtil.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/ArtifactTypeUtil.java new file mode 100644 index 0000000000000..93d76448907a9 --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/ArtifactTypeUtil.java @@ -0,0 +1,19 @@ +package io.quarkus.test.junit; + +public final class ArtifactTypeUtil { + + private ArtifactTypeUtil() { + } + + public static boolean isContainer(String artifactType) { + return "jar-container".equals(artifactType) || "native-container".equals(artifactType); + } + + public static boolean isNativeBinary(String artifactType) { + return "native".equals(artifactType); + } + + public static boolean isJar(String artifactType) { + return "jar".equals(artifactType); + } +} diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTest.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTest.java index 28077cf6f2b99..04cb367e91e8d 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTest.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTest.java @@ -36,4 +36,18 @@ * Reason for disabling this test */ String value() default ""; + + /** + * The types of Quarkus application produced by the build for which this test is disabled. + * For example, if the tests are supposed to be disabled for built containers only, + * {@code ArtifactType.CONTAINER} would be set. + */ + ArtifactType[] forArtifactTypes() default { ArtifactType.ALL }; + + enum ArtifactType { + ALL, + JAR, + CONTAINER, + NATIVE_BINARY, + } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTestCondition.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTestCondition.java index bd01ad32cd1d9..586ddcb394a75 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTestCondition.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnIntegrationTestCondition.java @@ -1,10 +1,20 @@ package io.quarkus.test.junit; +import static io.quarkus.test.junit.ArtifactTypeUtil.isContainer; +import static io.quarkus.test.junit.ArtifactTypeUtil.isJar; +import static io.quarkus.test.junit.ArtifactTypeUtil.isNativeBinary; +import static io.quarkus.test.junit.DisabledOnIntegrationTest.ArtifactType.ALL; +import static io.quarkus.test.junit.DisabledOnIntegrationTest.ArtifactType.CONTAINER; +import static io.quarkus.test.junit.DisabledOnIntegrationTest.ArtifactType.JAR; +import static io.quarkus.test.junit.DisabledOnIntegrationTest.ArtifactType.NATIVE_BINARY; +import static io.quarkus.test.junit.IntegrationTestUtil.getArtifactType; +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.function.BiFunction; import java.util.function.Function; import org.junit.jupiter.api.extension.ConditionEvaluationResult; @@ -27,13 +37,33 @@ public class DisabledOnIntegrationTestCondition implements ExecutionCondition { public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional element = context.getElement(); ConditionEvaluationResult disabledOnIntegrationTestReason = check(context, element, DisabledOnIntegrationTest.class, - DisabledOnIntegrationTest::value); + DisabledOnIntegrationTest::value, + // check and see if the artifact type of the build has been disabled + ((ec, an) -> { + DisabledOnIntegrationTest.ArtifactType[] disabledArtifactTypes = an.forArtifactTypes(); + String artifactType = getArtifactType(readQuarkusArtifactProperties(ec)); + for (DisabledOnIntegrationTest.ArtifactType disabledArtifactType : disabledArtifactTypes) { + if (disabledArtifactType == ALL) { + return true; + } + if ((disabledArtifactType == CONTAINER) && isContainer(artifactType)) { + return true; + } + if ((disabledArtifactType == NATIVE_BINARY) && isNativeBinary(artifactType)) { + return true; + } + if ((disabledArtifactType == JAR) && isJar(artifactType)) { + return true; + } + } + return false; + })); if (disabledOnIntegrationTestReason != null) { return disabledOnIntegrationTestReason; } // support DisabledOnNativeImage for backward compatibility ConditionEvaluationResult disabledOnNativeImageReason = check(context, element, DisabledOnNativeImage.class, - DisabledOnNativeImage::value); + DisabledOnNativeImage::value, null); if (disabledOnNativeImageReason != null) { return disabledOnNativeImageReason; } @@ -41,17 +71,19 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con } private ConditionEvaluationResult check(ExtensionContext context, Optional element, - Class annotationClass, Function valueExtractor) { + Class annotationClass, Function valueExtractor, BiFunction predicate) { Optional disabled = findAnnotation(element, annotationClass); if (disabled.isPresent()) { // Cannot use ExtensionState here because this condition needs to be evaluated before QuarkusTestExtension boolean it = findAnnotation(context.getTestClass(), QuarkusIntegrationTest.class).isPresent() || findAnnotation(context.getTestClass(), QuarkusMainIntegrationTest.class).isPresent(); if (it) { - String reason = disabled.map(valueExtractor) - .filter(StringUtils::isNotBlank) - .orElseGet(() -> element.get() + " is @DisabledOnIntegrationTest"); - return ConditionEvaluationResult.disabled(reason); + if ((predicate == null) || predicate.apply(context, disabled.get())) { + String reason = disabled.map(valueExtractor) + .filter(StringUtils::isNotBlank) + .orElseGet(() -> element.get() + " is @DisabledOnIntegrationTest"); + return ConditionEvaluationResult.disabled(reason); + } } } return null; diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnNativeImage.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnNativeImage.java index 21929f2cc1e05..2ee78feaa4e9a 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnNativeImage.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/DisabledOnNativeImage.java @@ -26,11 +26,14 @@ * {@code @BeforeEach} methods, {@code @AfterEach} methods, and corresponding * extension APIs. * + * @deprecated Use {@link @DisabledOnIntegrationTest} instead. + * */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited +@Deprecated(since = "2.9.0.Final", forRemoval = true) public @interface DisabledOnNativeImage { /** * Reason for disabling this test diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java index 454ded5d53195..56d01c2c7c0ea 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java @@ -439,6 +439,14 @@ static Properties readQuarkusArtifactProperties(ExtensionContext context) { } } + static String getArtifactType(Properties quarkusArtifactProperties) { + String artifactType = quarkusArtifactProperties.getProperty("type"); + if (artifactType == null) { + throw new IllegalStateException("Unable to determine the type of artifact created by the Quarkus build"); + } + return artifactType; + } + private static TestLauncher determineTestLauncher() { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); int i = stackTrace.length - 1; diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java index 1fc6cb758d631..863edbaff2a40 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit; +import static io.quarkus.test.junit.ArtifactTypeUtil.isContainer; import static io.quarkus.test.junit.IntegrationTestUtil.activateLogging; import static io.quarkus.test.junit.IntegrationTestUtil.determineBuildOutputDirectory; import static io.quarkus.test.junit.IntegrationTestUtil.determineTestProfileAndProperties; @@ -7,6 +8,7 @@ import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed; import static io.quarkus.test.junit.IntegrationTestUtil.findProfile; import static io.quarkus.test.junit.IntegrationTestUtil.getAdditionalTestResources; +import static io.quarkus.test.junit.IntegrationTestUtil.getArtifactType; import static io.quarkus.test.junit.IntegrationTestUtil.getSysPropsToRestore; import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices; import static io.quarkus.test.junit.IntegrationTestUtil.readQuarkusArtifactProperties; @@ -132,12 +134,9 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactProperties, Class profile, ExtensionContext context) throws Throwable { - String artifactType = quarkusArtifactProperties.getProperty("type"); - if (artifactType == null) { - throw new IllegalStateException("Unable to determine the type of artifact created by the Quarkus build"); - } + String artifactType = getArtifactType(quarkusArtifactProperties); - boolean isDockerLaunch = "jar-container".equals(artifactType) || "native-container".equals(artifactType); + boolean isDockerLaunch = isContainer(artifactType); ArtifactLauncher.InitContext.DevServicesLaunchResult devServicesLaunchResult = handleDevServices(context, isDockerLaunch); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/DockerContainerLauncherProvider.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/DockerContainerLauncherProvider.java index 1f2ec5b0aa7a6..039cf407c76d4 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/DockerContainerLauncherProvider.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/DockerContainerLauncherProvider.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit.launcher; +import static io.quarkus.test.junit.ArtifactTypeUtil.isContainer; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_HTTPS_PORT; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_PORT; @@ -22,7 +23,7 @@ public class DockerContainerLauncherProvider implements ArtifactLauncherProvider @Override public boolean supportsArtifactType(String type) { - return "jar-container".equals(type) || "native-container".equals(type); + return isContainer(type); } @Override diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/JarLauncherProvider.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/JarLauncherProvider.java index 4f84518732afa..2b90dc993e063 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/JarLauncherProvider.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/JarLauncherProvider.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit.launcher; +import static io.quarkus.test.junit.ArtifactTypeUtil.isJar; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_HTTPS_PORT; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_PORT; @@ -21,7 +22,7 @@ public class JarLauncherProvider implements ArtifactLauncherProvider { @Override public boolean supportsArtifactType(String type) { - return "jar".equals(type); + return isJar(type); } @Override diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/NativeImageLauncherProvider.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/NativeImageLauncherProvider.java index 16cd147bc282d..5a829c7f3c07a 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/NativeImageLauncherProvider.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/launcher/NativeImageLauncherProvider.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit.launcher; +import static io.quarkus.test.junit.ArtifactTypeUtil.isNativeBinary; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_HTTPS_PORT; import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_PORT; @@ -19,7 +20,7 @@ public class NativeImageLauncherProvider implements ArtifactLauncherProvider { @Override public boolean supportsArtifactType(String type) { - return "native".equals(type); + return isNativeBinary(type); } @Override