diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/CLITestExtension.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/CLITestExtension.java index e26dc70a3b..7ae757713d 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/CLITestExtension.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/CLITestExtension.java @@ -97,12 +97,12 @@ public class CLITestExtension extends QuarkusMainTestExtension { infinispanContainer = configureExternalInfinispan(context); if (distConfig != null) { - onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class)); - if (dist == null) { dist = createDistribution(distConfig, getStoreConfig(context), getDatabaseConfig(context)); } + onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class), true); + copyTestProvider(context.getRequiredTestClass().getAnnotation(TestProvider.class)); copyTestProvider(context.getRequiredTestMethod().getAnnotation(TestProvider.class)); onBeforeStartDistribution(context.getRequiredTestClass().getAnnotation(BeforeStartDistribution.class)); @@ -163,10 +163,10 @@ public class CLITestExtension extends QuarkusMainTestExtension { } } - private void onKeepServerAlive(KeepServerAlive annotation) { + private void onKeepServerAlive(KeepServerAlive annotation, boolean setting) { if(annotation != null && dist != null) { try { - dist.setManualStop(true); + dist.setManualStop(setting); } catch (Exception cause) { throw new RuntimeException("Error when invoking " + annotation, cause); } @@ -177,12 +177,11 @@ public class CLITestExtension extends QuarkusMainTestExtension { public void afterEach(ExtensionContext context) throws Exception { DistributionTest distConfig = getDistributionConfig(context); - if (distConfig != null) { - if (distConfig.keepAlive()) { - dist.stop(); - } + if (dist != null) { + onKeepServerAlive(context.getRequiredTestMethod().getAnnotation(KeepServerAlive.class), false); + dist.stop(); - if (DistributionTest.ReInstall.BEFORE_TEST.equals(distConfig.reInstall())) { + if (distConfig != null && DistributionTest.ReInstall.BEFORE_TEST.equals(distConfig.reInstall())) { dist = null; } } diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DistributionTest.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DistributionTest.java index 5ddbd0d562..2c3cf6b400 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DistributionTest.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/DistributionTest.java @@ -29,6 +29,9 @@ import org.junit.jupiter.api.extension.ExtendWith; public @interface DistributionTest { boolean debug() default false; + /** + * If the distribution should be left running after the launch. + */ boolean keepAlive() default false; boolean enableTls() default false; diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/KeepServerAlive.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/KeepServerAlive.java index c7da32d714..364f0e7e75 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/KeepServerAlive.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/junit5/extension/KeepServerAlive.java @@ -17,13 +17,10 @@ package org.keycloak.it.junit5.extension; -import org.keycloak.it.utils.KeycloakDistribution; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.util.function.Consumer; /** * {@link KeepServerAlive} is used in a distributiontest to keep the server alive on test / method level. diff --git a/quarkus/tests/junit5/src/main/java/org/keycloak/it/utils/DockerKeycloakDistribution.java b/quarkus/tests/junit5/src/main/java/org/keycloak/it/utils/DockerKeycloakDistribution.java index 12c1dd19a2..782ad990af 100644 --- a/quarkus/tests/junit5/src/main/java/org/keycloak/it/utils/DockerKeycloakDistribution.java +++ b/quarkus/tests/junit5/src/main/java/org/keycloak/it/utils/DockerKeycloakDistribution.java @@ -52,7 +52,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution { this.envVars.put(name, value); } - private GenericContainer getKeycloakContainer() { + private GenericContainer getKeycloakContainer() { File distributionFile = new File("../../dist/" + File.separator + "target" + File.separator + "keycloak-" + Version.VERSION + ".tar.gz"); if (!distributionFile.exists()) { @@ -77,7 +77,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution { image = new RemoteDockerImage(DockerImageName.parse("quay.io/keycloak/keycloak")); } - return new GenericContainer(image) + return new GenericContainer<>(image) .withEnv(envVars) .withExposedPorts(8080) .withStartupAttempts(1) @@ -113,6 +113,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution { LOGGER.warn("Failed to start Keycloak container", cause); } finally { if (!manualStop) { + stop(); envVars.clear(); } } @@ -181,7 +182,6 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution { private void cleanupContainer() { if (containerId != null) { try { - final String finalContainerId = containerId; Runnable reaper = new Runnable() { @Override public void run() { @@ -253,7 +253,7 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution { } if (type.isInstance(this)) { - return (D) this; + return type.cast(this); } throw new IllegalArgumentException("Not a " + type + " type");