Skip to content

Commit

Permalink
Register a Config instance to bootstrap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Nov 15, 2024
1 parent 51c4bb0 commit bb0e4cb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,7 +79,6 @@ public interface TestConfig {
* is matched against the test class name (not the file name).
* <p>
* This is ignored if include-pattern has been set.
*
*/
@WithDefault(".*\\.IT[^.]+|.*IT|.*ITCase")
Optional<String> excludePattern();
Expand Down Expand Up @@ -241,7 +242,6 @@ public interface TestConfig {
* is matched against the module groupId:artifactId.
* <p>
* This is ignored if include-module-pattern has been set.
*
*/
Optional<String> excludeModulePattern();

Expand All @@ -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<List<String>> tags();
Optional<List<@WithConverter(TrimmedStringConverter.class) String>> tags();
}

interface Container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -87,34 +82,9 @@ public static void handleFailedStart() {

public static void handleFailedStart(RuntimeValue<Optional<Supplier<String>>> 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<String> getPropertyNames() {
Set<String> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<List<String>> tags = testConfig.profile().tags();
if (tags.isEmpty() || tags.get().isEmpty()) {
return ConditionEvaluationResult.enabled("No Quarkus Test Profile tags");
}
Class<? extends QuarkusTestProfile> testProfile = getQuarkusTestProfile(context);
Expand All @@ -295,8 +300,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
throw new RuntimeException(e);
}
Set<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.test.junit;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.quarkus.deployment.dev.testing.TestConfig;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigProviderResolver;

public class ConfigExtension implements Extension, BeforeAllCallback, AfterAllCallback {
private final SmallRyeConfig config;

public ConfigExtension() {
config = ConfigUtils.emptyConfigBuilder()
.addDiscoveredCustomizers()
.addDiscoveredSources()
.withMapping(TestConfig.class)
.build();
SmallRyeConfigProviderResolver.instance().registerConfig(config, Thread.currentThread().getContextClassLoader());
}

@Override
public void beforeAll(final ExtensionContext context) throws Exception {
IntegrationTestUtil.activateLogging();
}

@Override
public void afterAll(final ExtensionContext context) throws Exception {
SmallRyeConfigProviderResolver.instance().releaseConfig(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down Expand Up @@ -370,7 +372,6 @@ public void beforeTestExecution(ExtensionContext context) throws Exception {
}
} else {
throwBootFailureException();
return;
}
}

Expand Down Expand Up @@ -404,7 +405,6 @@ public void beforeEach(ExtensionContext context) throws Exception {
}
} else {
throwBootFailureException();
return;
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Iterable<ConfigSource> getConfigSources() {

@Override
public ConfigValue getConfigValue(final String propertyName) {
throw illegalStateException();
return runningQuarkusApplication.getConfigValue(propertyName, ConfigValue.class).get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.quarkus.test.junit.BasicLoggingEnabler
io.quarkus.test.junit.ConfigExtension
#io.quarkus.test.junit.BasicLoggingEnabler

0 comments on commit bb0e4cb

Please sign in to comment.