Skip to content

Commit

Permalink
Enable logging from integration tests
Browse files Browse the repository at this point in the history
Fixes: #20303
  • Loading branch information
geoand committed Sep 23, 2021
1 parent d3ed65b commit e751640
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;

/**
Expand Down
5 changes: 5 additions & 0 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,34 @@
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;

import io.quarkus.bootstrap.BootstrapConstants;
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;
Expand Down Expand Up @@ -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<String, String> properties;
private final String networkId;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<? extends QuarkusTestProfile> selectedProfile = IntegrationTestUtil.findProfile(testClass);
Class<? extends QuarkusTestProfile> 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)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -148,6 +149,7 @@ private ArtifactLauncher.LaunchResult doProcessStart(ExtensionContext context, S
}

launcher.includeAsSysProps(additionalProperties);
activateLogging();
return launcher.runToCompletion(args);

} finally {
Expand Down

0 comments on commit e751640

Please sign in to comment.