From 158d6a4c680981b84d1b736b49a936db87cd90aa Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 14 Nov 2024 23:29:32 +0000 Subject: [PATCH] Register a Config instance to bootstrap tests --- .../deployment/dev/testing/TestConfig.java | 6 ++-- .../runtime/logging/LoggingSetupRecorder.java | 36 ++----------------- .../AbstractJvmQuarkusTestExtension.java | 12 ++++--- .../quarkus/test/junit/ConfigExtension.java | 24 +++++++++++++ .../test/junit/QuarkusTestExtension.java | 9 ++--- .../test/junit/RunningAppConfigResolver.java | 2 +- .../junit/TestConfigBuilderCustomizer.java | 12 +++++++ ...rye.config.SmallRyeConfigBuilderCustomizer | 1 + .../org.junit.jupiter.api.extension.Extension | 3 +- 9 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 test-framework/junit5/src/main/java/io/quarkus/test/junit/ConfigExtension.java create mode 100644 test-framework/junit5/src/main/java/io/quarkus/test/junit/TestConfigBuilderCustomizer.java create mode 100644 test-framework/junit5/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java index da1a31e3f43d9..50a295bbd16a5 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java @@ -8,7 +8,9 @@ import io.quarkus.runtime.annotations.ConfigDocMapKey; import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; +import io.quarkus.runtime.configuration.TrimmedStringConverter; import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithConverter; import io.smallrye.config.WithDefault; import io.smallrye.config.WithParentName; @@ -77,7 +79,6 @@ public interface TestConfig { * is matched against the test class name (not the file name). *

* This is ignored if include-pattern has been set. - * */ @WithDefault(".*\\.IT[^.]+|.*IT|.*ITCase") Optional excludePattern(); @@ -241,7 +242,6 @@ public interface TestConfig { * is matched against the module groupId:artifactId. *

* This is ignored if include-module-pattern has been set. - * */ Optional excludeModulePattern(); @@ -265,7 +265,7 @@ interface Profile { * then Quarkus will only execute tests that are annotated with a {@code @TestProfile} that has at least one of the * supplied (via the aforementioned system property) tags. */ - Optional> tags(); + Optional> tags(); } interface Container { diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java index 2d390ab877db0..16238c5f45eac 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java @@ -12,12 +12,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -31,7 +29,6 @@ import java.util.logging.LogRecord; import org.eclipse.microprofile.config.ConfigProvider; -import org.eclipse.microprofile.config.spi.ConfigSource; import org.jboss.logmanager.ExtFormatter; import org.jboss.logmanager.LogContext; import org.jboss.logmanager.LogContextInitializer; @@ -57,7 +54,6 @@ import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; -import io.quarkus.runtime.configuration.QuarkusConfigBuilderCustomizer; import io.quarkus.runtime.console.ConsoleRuntimeConfig; import io.quarkus.runtime.logging.LogBuildTimeConfig.CategoryBuildTimeConfig; import io.quarkus.runtime.logging.LogRuntimeConfig.CategoryConfig; @@ -67,7 +63,6 @@ import io.quarkus.runtime.logging.LogRuntimeConfig.SocketConfig; import io.quarkus.runtime.shutdown.ShutdownListener; import io.smallrye.config.SmallRyeConfig; -import io.smallrye.config.SmallRyeConfigBuilder; @Recorder public class LoggingSetupRecorder { @@ -87,34 +82,9 @@ public static void handleFailedStart() { public static void handleFailedStart(RuntimeValue>> banner) { SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); - // There may be cases where a Config with the mappings is already available, but we can't be sure, so we wrap - // the original Config and map the logging classes. - SmallRyeConfig loggingConfig = new SmallRyeConfigBuilder() - .withCustomizers(new QuarkusConfigBuilderCustomizer()) - .withMapping(LogBuildTimeConfig.class) - .withMapping(LogRuntimeConfig.class) - .withMapping(ConsoleRuntimeConfig.class) - .withSources(new ConfigSource() { - @Override - public Set getPropertyNames() { - Set properties = new HashSet<>(); - config.getPropertyNames().forEach(properties::add); - return properties; - } - - @Override - public String getValue(final String propertyName) { - return config.getRawValue(propertyName); - } - - @Override - public String getName() { - return "Logging Config"; - } - }).build(); - LogRuntimeConfig logRuntimeConfig = loggingConfig.getConfigMapping(LogRuntimeConfig.class); - LogBuildTimeConfig logBuildTimeConfig = loggingConfig.getConfigMapping(LogBuildTimeConfig.class); - ConsoleRuntimeConfig consoleRuntimeConfig = loggingConfig.getConfigMapping(ConsoleRuntimeConfig.class); + LogRuntimeConfig logRuntimeConfig = config.getConfigMapping(LogRuntimeConfig.class); + LogBuildTimeConfig logBuildTimeConfig = config.getConfigMapping(LogBuildTimeConfig.class); + ConsoleRuntimeConfig consoleRuntimeConfig = config.getConfigMapping(ConsoleRuntimeConfig.class); new LoggingSetupRecorder(new RuntimeValue<>(consoleRuntimeConfig)).initializeLogging(logRuntimeConfig, logBuildTimeConfig, DiscoveredLogComponents.ofEmpty(), emptyMap(), false, null, emptyList(), emptyList(), emptyList(), emptyList(), diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java index a36ee0cc8ac64..5c13c88c862d3 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java @@ -12,6 +12,7 @@ import java.util.Collection; import java.util.Deque; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -20,6 +21,7 @@ import jakarta.enterprise.inject.Alternative; +import org.eclipse.microprofile.config.ConfigProvider; import org.jboss.jandex.Index; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.extension.ConditionEvaluationResult; @@ -38,11 +40,13 @@ import io.quarkus.bootstrap.workspace.SourceDir; import io.quarkus.bootstrap.workspace.WorkspaceModule; import io.quarkus.deployment.dev.testing.CurrentTestApplication; +import io.quarkus.deployment.dev.testing.TestConfig; import io.quarkus.paths.PathList; import io.quarkus.runtime.LaunchMode; import io.quarkus.test.common.PathTestHelper; import io.quarkus.test.common.RestorableSystemProperties; import io.quarkus.test.common.TestClassIndexer; +import io.smallrye.config.SmallRyeConfig; public class AbstractJvmQuarkusTestExtension extends AbstractQuarkusTestWithContextExtension implements ExecutionCondition { @@ -279,8 +283,9 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con if (context.getTestInstance().isPresent()) { return ConditionEvaluationResult.enabled("Quarkus Test Profile tags only affect classes"); } - String tagsStr = System.getProperty("quarkus.test.profile.tags"); - if ((tagsStr == null) || tagsStr.isEmpty()) { + TestConfig testConfig = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(TestConfig.class); + Optional> tags = testConfig.profile().tags(); + if (tags.isEmpty() || tags.get().isEmpty()) { return ConditionEvaluationResult.enabled("No Quarkus Test Profile tags"); } Class testProfile = getQuarkusTestProfile(context); @@ -295,8 +300,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con throw new RuntimeException(e); } Set testProfileTags = profileInstance.tags(); - String[] tags = tagsStr.split(","); - for (String tag : tags) { + for (String tag : tags.get()) { String trimmedTag = tag.trim(); if (testProfileTags.contains(trimmedTag)) { return ConditionEvaluationResult.enabled("Tag '" + trimmedTag + "' is present on '" + testProfile diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/ConfigExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/ConfigExtension.java new file mode 100644 index 0000000000000..d5d9d37960587 --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/ConfigExtension.java @@ -0,0 +1,24 @@ +package io.quarkus.test.junit; + +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.Extension; +import org.junit.jupiter.api.extension.ExtensionContext; + +import io.quarkus.runtime.configuration.ConfigUtils; +import io.smallrye.config.SmallRyeConfig; +import io.smallrye.config.SmallRyeConfigProviderResolver; + +public class ConfigExtension implements Extension, BeforeAllCallback { + static { + SmallRyeConfig config = ConfigUtils.emptyConfigBuilder() + .addDiscoveredCustomizers() + .addDiscoveredSources() + .build(); + SmallRyeConfigProviderResolver.instance().registerConfig(config, Thread.currentThread().getContextClassLoader()); + } + + @Override + public void beforeAll(final ExtensionContext context) throws Exception { + IntegrationTestUtil.activateLogging(); + } +} diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index 91da01372bb78..3435204aca227 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -262,7 +262,9 @@ public Thread newThread(Runnable r) { .orElse(Duration.of(10, ChronoUnit.MINUTES)); hangTaskKey = hangDetectionExecutor.schedule(hangDetectionTask, hangTimeout.toMillis(), TimeUnit.MILLISECONDS); } - ConfigProviderResolver.setInstance(new RunningAppConfigResolver(runningQuarkusApplication)); + ConfigProviderResolver.instance().registerConfig( + new RunningAppConfigResolver(runningQuarkusApplication).getConfig(), + runningQuarkusApplication.getClassLoader()); RestorableSystemProperties restorableSystemProperties = RestorableSystemProperties.setProperties( Collections.singletonMap("test.url", TestHTTPResourceManager.getUri(runningQuarkusApplication))); @@ -370,7 +372,6 @@ public void beforeTestExecution(ExtensionContext context) throws Exception { } } else { throwBootFailureException(); - return; } } @@ -404,7 +405,6 @@ public void beforeEach(ExtensionContext context) throws Exception { } } else { throwBootFailureException(); - return; } } @@ -1153,19 +1153,16 @@ protected void doClose() { } finally { runningQuarkusApplication = null; Thread.currentThread().setContextClassLoader(old); - ConfigProviderResolver.setInstance(null); } } } class FailedCleanup implements ExtensionContext.Store.CloseableResource { - @Override public void close() { shutdownHangDetection(); firstException = null; failedBoot = false; - ConfigProviderResolver.setInstance(null); } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/RunningAppConfigResolver.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/RunningAppConfigResolver.java index e3ec91cd40b6a..54580344058a4 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/RunningAppConfigResolver.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/RunningAppConfigResolver.java @@ -44,7 +44,7 @@ public Iterable getConfigSources() { @Override public ConfigValue getConfigValue(final String propertyName) { - throw illegalStateException(); + return runningQuarkusApplication.getConfigValue(propertyName, ConfigValue.class).get(); } @Override diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestConfigBuilderCustomizer.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestConfigBuilderCustomizer.java new file mode 100644 index 0000000000000..365b07b56b752 --- /dev/null +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/TestConfigBuilderCustomizer.java @@ -0,0 +1,12 @@ +package io.quarkus.test.junit; + +import io.quarkus.deployment.dev.testing.TestConfig; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +public class TestConfigBuilderCustomizer implements SmallRyeConfigBuilderCustomizer { + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + builder.withMapping(TestConfig.class); + } +} diff --git a/test-framework/junit5/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer b/test-framework/junit5/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer new file mode 100644 index 0000000000000..bee0bb19b713a --- /dev/null +++ b/test-framework/junit5/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer @@ -0,0 +1 @@ +io.quarkus.test.junit.TestConfigBuilderCustomizer diff --git a/test-framework/junit5/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/test-framework/junit5/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension index 3d466fc6d2db5..a7e1dd109a41f 100644 --- a/test-framework/junit5/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension +++ b/test-framework/junit5/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -1 +1,2 @@ -io.quarkus.test.junit.BasicLoggingEnabler +io.quarkus.test.junit.ConfigExtension +#io.quarkus.test.junit.BasicLoggingEnabler