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/13 18:34:38 UTC

[logging-log4j2] 02/02: Revert "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 release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit dca3b38e86d45cd73179a07809032b17c91b0f6e
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Sep 13 13:34:22 2020 -0500

    Revert "Introduce annotation for JUnit 5 LCF tests"
    
    This reverts commit 04fa3783ef5ae60eb17e68c706b6fb2b32623950.
---
 .../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, 22 insertions(+), 69 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 8fe4680..060e553 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 RegisterLoggerContextFactory} with JUnit 5
+ * @deprecated Use {@link LoggerContextFactoryExtension} with JUnit 5
  */
 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 7649c79..1131e1b 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,32 +22,25 @@ 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;
 
-import java.lang.reflect.Field;
-import java.util.List;
-
-class LoggerContextFactoryExtension implements BeforeAllCallback, AfterAllCallback {
+/**
+ * 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 {
 
     private static final String KEY = "previousFactory";
+    private final LoggerContextFactory loggerContextFactory;
+
+    public LoggerContextFactoryExtension(LoggerContextFactory loggerContextFactory) {
+        this.loggerContextFactory = loggerContextFactory;
+    }
 
     @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());
-        final LoggerContextFactory factory = (LoggerContextFactory) loggerContextFactories.get(0).get(null);
-        LogManager.setFactory(factory);
+        LogManager.setFactory(loggerContextFactory);
     }
 
     @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
deleted file mode 100644
index aa69b1e..0000000
--- a/log4j-api/src/test/java/org/apache/logging/log4j/junit/RegisterLoggerContextFactory.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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 529b811..ac42497 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,16 +18,17 @@ package org.apache.logging.log4j.simple;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.RegisterLoggerContextFactory;
-import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.apache.logging.log4j.junit.LoggerContextFactoryExtension;
 import org.junit.jupiter.api.Tag;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
 @Tag("smoke")
 public class SimpleLoggerTest {
 
-    @RegisterLoggerContextFactory
-    static final LoggerContextFactory FACTORY = new SimpleLoggerContextFactory();
+    @RegisterExtension
+    public static final LoggerContextFactoryExtension EXTENSION =
+            new LoggerContextFactoryExtension(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 efba175..4c5021c 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.RegisterLoggerContextFactory;
+import org.apache.logging.log4j.junit.LoggerContextFactoryExtension;
 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 {
 
-    @RegisterLoggerContextFactory
-    static final LoggerContextFactory FACTORY = new SimpleLoggerContextFactory();
+    @RegisterExtension
+    static final LoggerContextFactoryExtension extension = new LoggerContextFactoryExtension(new SimpleLoggerContextFactory());
 
     @Test
     public void testErrorNoClassLoader() throws Exception {