You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2022/10/16 06:33:43 UTC
[logging-log4j2] 04/04: Reset LoggerContextFactory after test class when using ContextSelectorType annotation
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit f1ffe2aa8d0f3ccd5b79ffb9b1ba771f51bbc6da
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Sun Oct 16 01:32:59 2022 -0500
Reset LoggerContextFactory after test class when using ContextSelectorType annotation
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../core/test/junit/ContextSelectorCallback.java | 28 ++++++++++++++--------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/ContextSelectorCallback.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/ContextSelectorCallback.java
index 14cff75071..75f339e981 100644
--- a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/ContextSelectorCallback.java
+++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/ContextSelectorCallback.java
@@ -22,20 +22,28 @@ import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.selector.ContextSelector;
import org.apache.logging.log4j.plugins.di.DI;
import org.apache.logging.log4j.plugins.di.Injector;
+import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.platform.commons.support.AnnotationSupport;
-public class ContextSelectorCallback implements BeforeAllCallback {
+public class ContextSelectorCallback implements BeforeAllCallback, AfterAllCallback {
@Override
public void beforeAll(final ExtensionContext context) throws Exception {
- final Class<?> testClass = context.getRequiredTestClass();
- final ContextSelectorType source = testClass.getAnnotation(ContextSelectorType.class);
- if (source != null) {
- final Injector injector = DI.createInjector();
- injector.registerBinding(ContextSelector.KEY, injector.getFactory(source.value()));
- injector.init();
- final Log4jContextFactory factory = injector.getInstance(Log4jContextFactory.class);
- LogManager.setFactory(factory);
- }
+ AnnotationSupport.findAnnotation(context.getTestClass(), ContextSelectorType.class)
+ .map(ContextSelectorType::value)
+ .ifPresent(contextSelectorClass -> {
+ final Injector injector = DI.createInjector();
+ injector.registerBinding(ContextSelector.KEY, injector.getFactory(contextSelectorClass));
+ injector.init();
+ final Log4jContextFactory factory = injector.getInstance(Log4jContextFactory.class);
+ LogManager.setFactory(factory);
+ });
+ }
+
+ @Override
+ public void afterAll(final ExtensionContext context) throws Exception {
+ AnnotationSupport.findAnnotation(context.getTestClass(), ContextSelectorType.class)
+ .ifPresent(ignored -> LogManager.setFactory(null));
}
}