-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register a Config instance to bootstrap tests
- Loading branch information
Showing
25 changed files
with
217 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-test-framework</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-junit5-config</artifactId> | ||
<name>Quarkus - Test Framework - JUnit 5 Config</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.smallrye.config</groupId> | ||
<artifactId>smallrye-config</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core-deployment</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
test-framework/junit5-config/src/main/java/io/quarkus/test/config/LoggingSetupExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkus.test.config; | ||
|
||
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.logging.LoggingSetupRecorder; | ||
|
||
public class LoggingSetupExtension implements Extension, BeforeAllCallback { | ||
@Override | ||
public void beforeAll(final ExtensionContext context) throws Exception { | ||
LoggingSetupRecorder.handleFailedStart(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
test-framework/junit5-config/src/main/java/io/quarkus/test/config/QuarkusClassOrderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.test.config; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigProviderResolver; | ||
import org.junit.jupiter.api.ClassOrderer; | ||
import org.junit.jupiter.api.ClassOrdererContext; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
|
||
import io.quarkus.deployment.dev.testing.TestConfig; | ||
import io.quarkus.runtime.LaunchMode; | ||
import io.smallrye.config.SmallRyeConfig; | ||
|
||
public class QuarkusClassOrderer implements ClassOrderer { | ||
private final ClassOrderer delegate; | ||
|
||
static { | ||
TestConfigProviderResolver resolver = new TestConfigProviderResolver(); | ||
ConfigProviderResolver.setInstance(resolver); | ||
resolver.getConfig(LaunchMode.TEST); | ||
} | ||
|
||
public QuarkusClassOrderer() { | ||
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class); | ||
TestConfig testConfig = config.getConfigMapping(TestConfig.class); | ||
|
||
delegate = testConfig.classOrderer() | ||
.map(klass -> ReflectionUtils.tryToLoadClass(klass) | ||
.andThenTry(ReflectionUtils::newInstance) | ||
.andThenTry(instance -> (ClassOrderer) instance) | ||
.toOptional().orElse(EMPTY)) | ||
.orElse(EMPTY); | ||
} | ||
|
||
@Override | ||
public void orderClasses(final ClassOrdererContext context) { | ||
delegate.orderClasses(context); | ||
} | ||
|
||
private static final ClassOrderer EMPTY = new ClassOrderer() { | ||
@Override | ||
public void orderClasses(final ClassOrdererContext context) { | ||
|
||
} | ||
}; | ||
} |
74 changes: 74 additions & 0 deletions
74
...mework/junit5-config/src/main/java/io/quarkus/test/config/TestConfigProviderResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.quarkus.test.config; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
import io.quarkus.deployment.dev.testing.TestConfig; | ||
import io.quarkus.runtime.LaunchMode; | ||
import io.quarkus.runtime.configuration.ConfigUtils; | ||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.SmallRyeConfigProviderResolver; | ||
|
||
public class TestConfigProviderResolver extends SmallRyeConfigProviderResolver { | ||
private static final SmallRyeConfigProviderResolver resolver = (SmallRyeConfigProviderResolver) SmallRyeConfigProviderResolver | ||
.instance(); | ||
|
||
private static final Map<LaunchMode, SmallRyeConfig> configs = new ConcurrentHashMap<>(); | ||
private static final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
|
||
@Override | ||
public Config getConfig() { | ||
return resolver.getConfig(); | ||
} | ||
|
||
public Config getConfig(final LaunchMode mode) { | ||
if (classLoader.equals(Thread.currentThread().getContextClassLoader())) { | ||
resolver.releaseConfig(classLoader); | ||
SmallRyeConfig config = configs.computeIfAbsent(mode, new Function<LaunchMode, SmallRyeConfig>() { | ||
@Override | ||
public SmallRyeConfig apply(final LaunchMode launchMode) { | ||
return ConfigUtils.configBuilder(false, true, mode) | ||
.withProfile(mode.getDefaultProfile()) | ||
.withMapping(TestConfig.class, "quarkus.test") | ||
.build(); | ||
} | ||
}); | ||
resolver.registerConfig(config, classLoader); | ||
return config; | ||
} | ||
throw new IllegalStateException(); | ||
} | ||
|
||
public void restoreConfig() { | ||
if (classLoader.equals(Thread.currentThread().getContextClassLoader())) { | ||
resolver.releaseConfig(classLoader); | ||
resolver.registerConfig(configs.get(LaunchMode.TEST), classLoader); | ||
} else { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
@Override | ||
public Config getConfig(final ClassLoader loader) { | ||
return resolver.getConfig(loader); | ||
} | ||
|
||
@Override | ||
public SmallRyeConfigBuilder getBuilder() { | ||
return resolver.getBuilder(); | ||
} | ||
|
||
@Override | ||
public void registerConfig(final Config config, final ClassLoader classLoader) { | ||
resolver.registerConfig(config, classLoader); | ||
} | ||
|
||
@Override | ||
public void releaseConfig(final Config config) { | ||
resolver.releaseConfig(config); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...it5-config/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.test.config.LoggingSetupExtension |
2 changes: 2 additions & 0 deletions
2
test-framework/junit5-config/src/main/resources/junit-platform.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
junit.jupiter.extensions.autodetection.enabled=true | ||
junit.jupiter.testclass.order.default=io.quarkus.test.config.QuarkusClassOrderer |
Oops, something went wrong.