-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22920 from famod/test-basic-logging
Make sure (basic) logging is set up before each test class
- Loading branch information
Showing
5 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
test-framework/junit5-properties/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 |
---|---|---|
@@ -1 +1,2 @@ | ||
junit.jupiter.testclass.order.default=io.quarkus.test.junit.util.QuarkusTestProfileAwareClassOrderer | ||
junit.jupiter.extensions.autodetection.enabled=true | ||
junit.jupiter.testclass.order.default=io.quarkus.test.junit.util.QuarkusTestProfileAwareClassOrderer |
54 changes: 54 additions & 0 deletions
54
test-framework/junit5/src/main/java/io/quarkus/test/junit/BasicLoggingEnabler.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,54 @@ | ||
package io.quarkus.test.junit; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigProviderResolver; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import io.quarkus.bootstrap.logging.InitialConfigurator; | ||
|
||
/** | ||
* A (global) JUnit callback that enables/sets up basic logging if logging has not already been set up. | ||
* <p/> | ||
* This is useful for getting log output from non-Quarkus tests (if executed separately or before the first Quarkus test), | ||
* but also for getting instant log output from {@code QuarkusTestResourceLifecycleManagers} etc. | ||
* <p/> | ||
* This callback can be disabled via {@link #CFGKEY_ENABLED} in {@code junit-platform.properties} or via system property. | ||
*/ | ||
public class BasicLoggingEnabler implements BeforeAllCallback { | ||
|
||
private static final String CFGKEY_ENABLED = "junit.quarkus.enable-basic-logging"; | ||
private static Boolean enabled; | ||
|
||
// to speed things up a little, eager async loading of the config that will be looked up in LoggingSetupRecorder | ||
// downside: doesn't obey CFGKEY_ENABLED, but that should be bearable | ||
static { | ||
// e.g. continuous testing has everything set up already (DELAYED_HANDLER is active) | ||
if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) { | ||
new Thread(() -> ConfigProvider.getConfig()).start(); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
if (enabled == null) { | ||
enabled = context.getConfigurationParameter(CFGKEY_ENABLED).map(Boolean::valueOf).orElse(Boolean.TRUE); | ||
} | ||
if (!enabled || InitialConfigurator.DELAYED_HANDLER.isActivated()) { | ||
return; | ||
} | ||
try { | ||
IntegrationTestUtil.activateLogging(); | ||
} finally { | ||
// release the config that was retrieved by above call so that tests that try to register their own config | ||
// don't fail with: | ||
// "IllegalStateException: SRCFG00017: Configuration already registered for the given class loader" | ||
// also, a possible recreation of basically the same config for a later test class will consume far less time | ||
var configProviderResolver = ConfigProviderResolver.instance(); | ||
var config = configProviderResolver.getConfig(); | ||
if (config != null) { // probably never null, but be safe | ||
configProviderResolver.releaseConfig(config); | ||
} | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ork/junit5/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.junit.BasicLoggingEnabler |
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