Skip to content

Commit

Permalink
Merge test-related ConfigUtils into a single class
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Oct 26, 2023
1 parent 9721889 commit 4b70a5a
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package io.quarkus.test.junit.launcher;

import static io.quarkus.test.junit.IntegrationTestUtil.DEFAULT_WAIT_TIME_SECONDS;
package io.quarkus.test.common;

import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -10,11 +8,14 @@

import org.eclipse.microprofile.config.Config;

import io.quarkus.runtime.configuration.QuarkusConfigFactory;
import io.smallrye.config.SmallRyeConfig;

public final class ConfigUtil {
public final class TestConfigUtil {

public static final long DEFAULT_WAIT_TIME_SECONDS = 60;

private ConfigUtil() {
private TestConfigUtil() {
}

public static List<String> argLineValue(Config config) {
Expand Down Expand Up @@ -57,4 +58,26 @@ public static String runTarget(Config config) {
return config.getOptionalValue("quarkus.run.target", String.class)
.orElse(null);
}

/**
* Clean up config left over from badly-behaving previous tests.
* <p>
* Tests may call ConfigProvider.getConfig() directly or indirectly,
* triggering lazy initialization in QuarkusConfigFactory#getConfigFor,
* and if they don't call QuarkusConfigFactory.setConfig(null) afterward,
* they may leak that config.
* As it's quite hard to guarantee that all tests clean up after them,
* especially if they don't use any Quarkus*Test extensions,
* we call this cleanup here in Quarkus*Test extensions as an additional safeguard.
* <p>
* Note this must be called quite early in extensions in order to be effective:
* things like TestHTTPResourceManager#getUri make use of config and may be called very early,
* upon test instantiation, so cleaning up in beforeEach() won't cut it for example.
*/
public static void cleanUp() {
try {
QuarkusConfigFactory.setConfig(null);
} catch (Throwable ignored) {
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import io.quarkus.test.common.GroovyClassValue;
import io.quarkus.test.common.PathTestHelper;
import io.quarkus.test.common.PropertyTestUtil;
import io.quarkus.test.common.TestConfigUtil;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.test.common.http.TestHTTPResourceManager;

Expand Down Expand Up @@ -228,7 +229,7 @@ public Object createTestInstance(TestInstanceFactoryContext factoryContext, Exte

@Override
public void beforeAll(ExtensionContext context) throws Exception {
ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
GroovyClassValue.disable();
//set the right launch mode in the outer CL, used by the HTTP host config source
ProfileManager.setLaunchMode(LaunchMode.DEVELOPMENT);
Expand Down Expand Up @@ -325,7 +326,7 @@ public void afterAll(ExtensionContext context) throws Exception {
inMemoryLogHandler.clearRecords();
inMemoryLogHandler.setFilter(null);
ClearCache.clearAnnotationCache();
ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.test.common.PathTestHelper;
import io.quarkus.test.common.RestAssuredURLManager;
import io.quarkus.test.common.TestConfigUtil;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.utilities.JavaBinFinder;

Expand Down Expand Up @@ -365,7 +366,7 @@ private JavaArchive getArchiveProducerOrDefault() {

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
ensureNoInjectAnnotationIsUsed(extensionContext.getRequiredTestClass());
ExclusivityChecker.checkTestType(extensionContext, QuarkusProdModeTest.class);

Expand Down Expand Up @@ -759,7 +760,7 @@ public void afterAll(ExtensionContext extensionContext) throws Exception {
FileUtil.deleteDirectory(outputDir);
}

ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import io.quarkus.test.common.PathTestHelper;
import io.quarkus.test.common.PropertyTestUtil;
import io.quarkus.test.common.RestAssuredURLManager;
import io.quarkus.test.common.TestConfigUtil;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.test.common.http.TestHTTPResourceManager;

Expand Down Expand Up @@ -507,7 +508,7 @@ private void runExtensionMethod(ReflectiveInvocationContext<Method> invocationCo

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
GroovyClassValue.disable();
//set the right launch mode in the outer CL, used by the HTTP host config source
ProfileManager.setLaunchMode(LaunchMode.TEST);
Expand Down Expand Up @@ -767,7 +768,7 @@ public void afterAll(ExtensionContext extensionContext) throws Exception {
afterAllCustomizer.run();
}
ClearCache.clearAnnotationCache();
ConfigUtil.cleanUp();
TestConfigUtil.cleanUp();
}
if (records != null) {
assertLogRecords.accept(records);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public final class IntegrationTestUtil {

public static final int DEFAULT_PORT = 8081;
public static final int DEFAULT_HTTPS_PORT = 8444;
public static final long DEFAULT_WAIT_TIME_SECONDS = 60;

private IntegrationTestUtil() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
import io.quarkus.test.common.PropertyTestUtil;
import io.quarkus.test.common.RestAssuredURLManager;
import io.quarkus.test.common.RunCommandLauncher;
import io.quarkus.test.common.TestConfigUtil;
import io.quarkus.test.common.TestHostLauncher;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.test.common.TestScopeManager;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
import io.quarkus.test.junit.launcher.ArtifactLauncherProvider;
import io.quarkus.test.junit.launcher.ConfigUtil;

public class QuarkusIntegrationTestExtension extends AbstractQuarkusTestWithContextExtension
implements BeforeTestExecutionCallback, AfterTestExecutionCallback, BeforeEachCallback, AfterEachCallback,
Expand Down Expand Up @@ -267,8 +267,8 @@ public void close() throws Throwable {
launcher = new TestHostLauncher();
} else {
Config config = LauncherUtil.installAndGetSomeConfig();
Duration waitDuration = ConfigUtil.waitTimeValue(config);
String target = ConfigUtil.runTarget(config);
Duration waitDuration = TestConfigUtil.waitTimeValue(config);
String target = TestConfigUtil.runTarget(config);
// try to execute a run command published by an extension if it exists. We do this so that extensions that have a custom run don't have to create any special artifact type
launcher = RunCommandLauncher.tryLauncher(devServicesLaunchResult.getCuratedApplication().getQuarkusBootstrap(),
target, waitDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.quarkus.test.common.DefaultDockerContainerLauncher;
import io.quarkus.test.common.DockerContainerArtifactLauncher;
import io.quarkus.test.common.LauncherUtil;
import io.quarkus.test.common.TestConfigUtil;
import io.smallrye.config.SmallRyeConfig;

public class DockerContainerLauncherProvider implements ArtifactLauncherProvider {
Expand Down Expand Up @@ -44,10 +45,10 @@ public DockerContainerArtifactLauncher create(CreateContext context) {
launcher.init(new DefaultDockerInitContext(
config.getValue("quarkus.http.test-port", OptionalInt.class).orElse(DEFAULT_PORT),
config.getValue("quarkus.http.test-ssl-port", OptionalInt.class).orElse(DEFAULT_HTTPS_PORT),
ConfigUtil.waitTimeValue(config),
ConfigUtil.integrationTestProfile(config),
ConfigUtil.argLineValue(config),
ConfigUtil.env(config),
TestConfigUtil.waitTimeValue(config),
TestConfigUtil.integrationTestProfile(config),
TestConfigUtil.argLineValue(config),
TestConfigUtil.env(config),
context.devServicesLaunchResult(),
containerImage,
pullRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.quarkus.test.common.DefaultJarLauncher;
import io.quarkus.test.common.JarArtifactLauncher;
import io.quarkus.test.common.LauncherUtil;
import io.quarkus.test.common.TestConfigUtil;

public class JarLauncherProvider implements ArtifactLauncherProvider {

Expand All @@ -43,10 +44,10 @@ public JarArtifactLauncher create(CreateContext context) {
launcher.init(new DefaultJarInitContext(
config.getValue("quarkus.http.test-port", OptionalInt.class).orElse(DEFAULT_PORT),
config.getValue("quarkus.http.test-ssl-port", OptionalInt.class).orElse(DEFAULT_HTTPS_PORT),
ConfigUtil.waitTimeValue(config),
ConfigUtil.integrationTestProfile(config),
ConfigUtil.argLineValue(config),
ConfigUtil.env(config),
TestConfigUtil.waitTimeValue(config),
TestConfigUtil.integrationTestProfile(config),
TestConfigUtil.argLineValue(config),
TestConfigUtil.env(config),
context.devServicesLaunchResult(),
context.buildOutputDirectory().resolve(pathStr)));
return launcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.quarkus.test.common.DefaultNativeImageLauncher;
import io.quarkus.test.common.LauncherUtil;
import io.quarkus.test.common.NativeImageLauncher;
import io.quarkus.test.common.TestConfigUtil;

public class NativeImageLauncherProvider implements ArtifactLauncherProvider {
@Override
Expand All @@ -41,10 +42,10 @@ public NativeImageLauncher create(CreateContext context) {
launcher.init(new NativeImageLauncherProvider.DefaultNativeImageInitContext(
config.getValue("quarkus.http.test-port", OptionalInt.class).orElse(DEFAULT_PORT),
config.getValue("quarkus.http.test-ssl-port", OptionalInt.class).orElse(DEFAULT_HTTPS_PORT),
ConfigUtil.waitTimeValue(config),
ConfigUtil.integrationTestProfile(config),
ConfigUtil.argLineValue(config),
ConfigUtil.env(config),
TestConfigUtil.waitTimeValue(config),
TestConfigUtil.integrationTestProfile(config),
TestConfigUtil.argLineValue(config),
TestConfigUtil.env(config),
context.devServicesLaunchResult(),
System.getProperty("native.image.path"),
config.getOptionalValue("quarkus.package.output-directory", String.class).orElse(null),
Expand Down

0 comments on commit 4b70a5a

Please sign in to comment.