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/12 17:42:22 UTC
[logging-log4j2] branch master updated: 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 master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 916c684 Revert "Introduce annotation for JUnit 5 LCF tests"
916c684 is described below
commit 916c68411b6b6966d47c10e31d2b69c6c46f88c3
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sat Sep 12 12:40:41 2020 -0500
Revert "Introduce annotation for JUnit 5 LCF tests"
This reverts commit 771dc35f6de90c4fb24337c6971b82c869bf20b3.
---
.../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 b9847d3..6fcdbd0 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
*/
@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 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 {