From e75164087c0391bead65a9a87a9bf17236938a87 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 23 Sep 2021 14:55:51 +0300 Subject: [PATCH] Enable logging from integration tests Fixes: #20303 --- .../runtime/logging/ConsoleConfig.java | 3 ++- .../asciidoc/getting-started-testing.adoc | 5 ++++ .../test/junit/IntegrationTestUtil.java | 26 +++++++++++++++++++ .../QuarkusIntegrationTestExtension.java | 4 ++- .../QuarkusMainIntegrationTestExtension.java | 2 ++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java index e888f45159bc7c..a3ceae52db4928 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/ConsoleConfig.java @@ -9,6 +9,7 @@ @ConfigGroup public class ConsoleConfig { + public static final String DEFAULT_CONSOLE_CONFIG_FORMAT = "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n"; /** * If console logging should be enabled */ @@ -25,7 +26,7 @@ public class ConsoleConfig { * The log format. Note that this value will be ignored if an extension is present that takes * control of console formatting (e.g. an XML or JSON-format extension). */ - @ConfigItem(defaultValue = "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n") + @ConfigItem(defaultValue = DEFAULT_CONSOLE_CONFIG_FORMAT) String format; /** diff --git a/docs/src/main/asciidoc/getting-started-testing.adoc b/docs/src/main/asciidoc/getting-started-testing.adoc index c96b16f49e8715..53c022fec34e75 100644 --- a/docs/src/main/asciidoc/getting-started-testing.adoc +++ b/docs/src/main/asciidoc/getting-started-testing.adoc @@ -1151,6 +1151,11 @@ As a test annotated with `@QuarkusIntegrationTest` tests the result of the build These tests will **not** work if run in the same phase as `@QuarkusTest` as Quarkus has not yet created the final artifact. ==== +[NOTE] +==== +As is the case with `@QuarkusTest`, logging statements can be added to the test method of a `@QuarkusIntegrationTest` as well. However, the logging configuration for the application is currently not taken into account in this case. Please open an issue if this feature is important. +==== + == Mixing `@QuarkusTest` with other type of tests Mixing tests annotated with `@QuarkusTest` with tests annotated with either `@QuarkusDevModeTest`, `@QuarkusProdModeTest` or `@QuarkusUnitTest` diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java index 33c9cb3d4a18d4..b89d2ab9daab16 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java @@ -26,12 +26,17 @@ import java.util.Properties; import java.util.Set; import java.util.function.BiConsumer; +import java.util.logging.Handler; +import java.util.logging.Level; import java.util.stream.Collectors; import javax.enterprise.inject.Alternative; import javax.inject.Inject; import org.jboss.jandex.Index; +import org.jboss.logmanager.formatters.ColorPatternFormatter; +import org.jboss.logmanager.formatters.PatternFormatter; +import org.jboss.logmanager.handlers.ConsoleHandler; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.platform.commons.JUnitException; @@ -39,12 +44,16 @@ import io.quarkus.bootstrap.app.AugmentAction; import io.quarkus.bootstrap.app.CuratedApplication; import io.quarkus.bootstrap.app.QuarkusBootstrap; +import io.quarkus.bootstrap.logging.InitialConfigurator; +import io.quarkus.bootstrap.logging.QuarkusDelayedHandler; import io.quarkus.bootstrap.model.PathsCollection; import io.quarkus.bootstrap.model.gradle.QuarkusModel; import io.quarkus.bootstrap.util.PathsUtils; import io.quarkus.bootstrap.utils.BuildToolHelper; import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem; +import io.quarkus.dev.console.QuarkusConsole; import io.quarkus.runtime.configuration.ProfileManager; +import io.quarkus.runtime.logging.ConsoleConfig; import io.quarkus.test.common.ArtifactLauncher; import io.quarkus.test.common.PathTestHelper; import io.quarkus.test.common.TestClassIndexer; @@ -296,6 +305,23 @@ public void accept(String s, String s2) { return new DefaultDevServicesLaunchResult(propertyMap, networkId, curatedApplication); } + /** + * Responsible for activating logging of the tests themselves. + * This is a very basic implementation that does not take into account the Quarkus configuration and instead + * just uses the same defaults as Quarkus. + */ + static void activateLogging() { + QuarkusDelayedHandler delayedHandler = InitialConfigurator.DELAYED_HANDLER; + if (!delayedHandler.isActivated()) { + Handler handler = new ConsoleHandler(ConsoleHandler.Target.SYSTEM_OUT, + QuarkusConsole.hasColorSupport() ? new ColorPatternFormatter(ConsoleConfig.DEFAULT_CONSOLE_CONFIG_FORMAT) + : new PatternFormatter(ConsoleConfig.DEFAULT_CONSOLE_CONFIG_FORMAT)); + handler.setLevel(Level.INFO); + delayedHandler.setHandlers(new Handler[] { + handler }); + } + } + static class DefaultDevServicesLaunchResult implements ArtifactLauncher.InitContext.DevServicesLaunchResult { private final Map properties; private final String networkId; diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java index 4aee12aea1add8..0395143536cfaf 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit; +import static io.quarkus.test.junit.IntegrationTestUtil.*; import static io.quarkus.test.junit.IntegrationTestUtil.determineBuildOutputDirectory; import static io.quarkus.test.junit.IntegrationTestUtil.determineTestProfileAndProperties; import static io.quarkus.test.junit.IntegrationTestUtil.doProcessTestInstance; @@ -84,7 +85,7 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo ExtensionContext.Store store = root.getStore(ExtensionContext.Namespace.GLOBAL); IntegrationTestExtensionState state = store.get(IntegrationTestExtensionState.class.getName(), IntegrationTestExtensionState.class); - Class selectedProfile = IntegrationTestUtil.findProfile(testClass); + Class selectedProfile = findProfile(testClass); boolean wrongProfile = !Objects.equals(selectedProfile, quarkusTestProfile); // we reload the test resources if we changed test class and if we had or will have per-test test resources boolean reloadTestResources = !Objects.equals(extensionContext.getRequiredTestClass(), currentJUnitTestClass) @@ -184,6 +185,7 @@ public void close() throws Throwable { "Artifact type + '" + artifactType + "' is not supported by @QuarkusIntegrationTest"); } + activateLogging(); startLauncher(launcher, additionalProperties, () -> ssl = true); IntegrationTestExtensionState state = new IntegrationTestExtensionState(testResourceManager, launcher, diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainIntegrationTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainIntegrationTestExtension.java index 05520715721a7c..e50da9059ac40d 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainIntegrationTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainIntegrationTestExtension.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit; +import static io.quarkus.test.junit.IntegrationTestUtil.activateLogging; import static io.quarkus.test.junit.IntegrationTestUtil.determineBuildOutputDirectory; import static io.quarkus.test.junit.IntegrationTestUtil.determineTestProfileAndProperties; import static io.quarkus.test.junit.IntegrationTestUtil.getAdditionalTestResources; @@ -148,6 +149,7 @@ private ArtifactLauncher.LaunchResult doProcessStart(ExtensionContext context, S } launcher.includeAsSysProps(additionalProperties); + activateLogging(); return launcher.runToCompletion(args); } finally {