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 2020/09/07 16:30:22 UTC
[logging-log4j2] 01/02: Introduce annotation for JUnit 5 LCF tests
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 771dc35f6de90c4fb24337c6971b82c869bf20b3
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Mon Sep 7 11:28:52 2020 -0500
Introduce annotation for JUnit 5 LCF tests
This is less awkward than the previous syntax.
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../junit/LogManagerLoggerContextFactoryRule.java | 2 +-
.../log4j/junit/LoggerContextFactoryExtension.java | 31 +++++++++-------
.../log4j/junit/RegisterLoggerContextFactory.java | 41 ++++++++++++++++++++++
.../logging/log4j/simple/SimpleLoggerTest.java | 9 +++--
.../log4j/core/config/TestConfiguratorError.java | 8 ++---
5 files changed, 69 insertions(+), 22 deletions(-)
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/LogManagerLoggerContextFactoryRule.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/LogManagerLoggerContextFactoryRule.java
index 6fcdbd0..b9847d3 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/junit/LogManagerLoggerContextFactoryRule.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/LogManagerLoggerContextFactoryRule.java
@@ -24,7 +24,7 @@ import org.junit.rules.ExternalResource;
* Sets the {@link LogManager}'s {@link LoggerContextFactory} to the given instance before the test and restores it to
* the original value after the test.
*
- * @deprecated Use {@link LoggerContextFactoryExtension} with JUnit 5
+ * @deprecated Use {@link RegisterLoggerContextFactory} with JUnit 5
*/
@Deprecated
public class LogManagerLoggerContextFactoryRule extends ExternalResource {
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/LoggerContextFactoryExtension.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/LoggerContextFactoryExtension.java
index 1131e1b..7649c79 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/junit/LoggerContextFactoryExtension.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/LoggerContextFactoryExtension.java
@@ -22,25 +22,32 @@ import org.apache.logging.log4j.spi.LoggerContextFactory;
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.HierarchyTraversalMode;
+import org.junit.platform.commons.support.ModifierSupport;
+import org.junit.platform.commons.support.ReflectionSupport;
-/**
- * JUnit 5 extension that sets a particular {@link LoggerContextFactory} for the entire run of tests in a class.
- *
- * @since 2.14.0
- */
-public class LoggerContextFactoryExtension implements BeforeAllCallback, AfterAllCallback {
+import java.lang.reflect.Field;
+import java.util.List;
- private static final String KEY = "previousFactory";
- private final LoggerContextFactory loggerContextFactory;
+class LoggerContextFactoryExtension implements BeforeAllCallback, AfterAllCallback {
- public LoggerContextFactoryExtension(LoggerContextFactory loggerContextFactory) {
- this.loggerContextFactory = loggerContextFactory;
- }
+ private static final String KEY = "previousFactory";
@Override
public void beforeAll(ExtensionContext context) throws Exception {
+ final Class<?> testClass = context.getRequiredTestClass();
+ final List<Field> loggerContextFactories = ReflectionSupport.findFields(testClass,
+ f -> ModifierSupport.isStatic(f) && f.isAnnotationPresent(RegisterLoggerContextFactory.class),
+ HierarchyTraversalMode.BOTTOM_UP);
+ if (loggerContextFactories.isEmpty()) {
+ return;
+ }
+ if (loggerContextFactories.size() > 1) {
+ throw new IllegalArgumentException("More than one static LoggerContextFactory specified in " + testClass.getName());
+ }
getStore(context).put(KEY, LogManager.getFactory());
- LogManager.setFactory(loggerContextFactory);
+ final LoggerContextFactory factory = (LoggerContextFactory) loggerContextFactories.get(0).get(null);
+ LogManager.setFactory(factory);
}
@Override
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/RegisterLoggerContextFactory.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/RegisterLoggerContextFactory.java
new file mode 100644
index 0000000..aa69b1e
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/RegisterLoggerContextFactory.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.junit;
+
+import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * JUnit 5 extension that sets a particular {@link LoggerContextFactory} instance for the entire run of tests in a class.
+ *
+ * @since 2.14.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+@Documented
+@Inherited
+@ExtendWith(LoggerContextFactoryExtension.class)
+public @interface RegisterLoggerContextFactory {
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/simple/SimpleLoggerTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/simple/SimpleLoggerTest.java
index ac42497..529b811 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/simple/SimpleLoggerTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/simple/SimpleLoggerTest.java
@@ -18,17 +18,16 @@ package org.apache.logging.log4j.simple;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.LoggerContextFactoryExtension;
+import org.apache.logging.log4j.junit.RegisterLoggerContextFactory;
+import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
@Tag("smoke")
public class SimpleLoggerTest {
- @RegisterExtension
- public static final LoggerContextFactoryExtension EXTENSION =
- new LoggerContextFactoryExtension(new SimpleLoggerContextFactory());
+ @RegisterLoggerContextFactory
+ static final LoggerContextFactory FACTORY = new SimpleLoggerContextFactory();
private final Logger logger = LogManager.getLogger("TestError");
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
index 4c5021c..efba175 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfiguratorError.java
@@ -17,17 +17,17 @@
package org.apache.logging.log4j.core.config;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.junit.LoggerContextFactoryExtension;
+import org.apache.logging.log4j.junit.RegisterLoggerContextFactory;
import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
+import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
import static org.junit.jupiter.api.Assertions.assertNull;
public class TestConfiguratorError {
- @RegisterExtension
- static final LoggerContextFactoryExtension extension = new LoggerContextFactoryExtension(new SimpleLoggerContextFactory());
+ @RegisterLoggerContextFactory
+ static final LoggerContextFactory FACTORY = new SimpleLoggerContextFactory();
@Test
public void testErrorNoClassLoader() throws Exception {