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/15 18:57:57 UTC
[logging-log4j2] 04/10: Introduce StatusLoggerLevel test annotation
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 5699d05e42141e52569e7b17ed91936cdfedef1b
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Sep 13 16:41:32 2020 -0500
Introduce StatusLoggerLevel test annotation
Backported from 3.x.
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../apache/logging/log4j/AbstractLoggerTest.java | 15 ++++----
.../logging/log4j/junit/StatusLoggerLevel.java | 42 ++++++++++++++++++++++
.../log4j/junit/StatusLoggerLevelExtension.java | 20 ++++-------
.../logging/log4j/junit/StatusLoggerRule.java | 2 +-
.../validators/ValidHostValidatorTest.java | 8 ++---
5 files changed, 58 insertions(+), 29 deletions(-)
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/AbstractLoggerTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/AbstractLoggerTest.java
index 9c13006..1e18453 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/AbstractLoggerTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/AbstractLoggerTest.java
@@ -16,11 +16,7 @@
*/
package org.apache.logging.log4j;
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.MatcherAssert.*;
-import static org.junit.jupiter.api.Assertions.*;
-
-import org.apache.logging.log4j.junit.StatusLoggerLevelExtension;
+import org.apache.logging.log4j.junit.StatusLoggerLevel;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
import org.apache.logging.log4j.message.ParameterizedMessage;
@@ -33,10 +29,14 @@ import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.MessageSupplier;
import org.apache.logging.log4j.util.Supplier;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
import java.util.List;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.*;
+
+@StatusLoggerLevel("WARN")
public class AbstractLoggerTest {
private static final StringBuilder CHAR_SEQ = new StringBuilder("CharSeq");
@@ -60,9 +60,6 @@ public class AbstractLoggerTest {
private static final Marker MARKER = MarkerManager.getMarker("TEST");
private static final String MARKER_NAME = "TEST";
- @RegisterExtension
- public StatusLoggerLevelExtension status = new StatusLoggerLevelExtension(Level.WARN);
-
private static final LogEvent[] EVENTS = new LogEvent[] {
new LogEvent(null, simple, null),
new LogEvent(MARKER_NAME, simple, null),
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevel.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevel.java
new file mode 100644
index 0000000..9edf7b0
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevel.java
@@ -0,0 +1,42 @@
+/*
+ * 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.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 test extension that sets a specific StatusLogger logging level for each test.
+ *
+ * @since 2.14.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@Documented
+@Inherited
+@ExtendWith(StatusLoggerLevelExtension.class)
+public @interface StatusLoggerLevel {
+ /** Name of {@link org.apache.logging.log4j.Level} to use for status logger. */
+ String value();
+}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevelExtension.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevelExtension.java
index 5eb7de2..2b0a5cd 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevelExtension.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerLevelExtension.java
@@ -23,25 +23,19 @@ import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
-/**
- * JUnit 5 test extension that sets a specific StatusLogger logging level for each test.
- *
- * @since 2.14.0
- */
-public class StatusLoggerLevelExtension implements BeforeEachCallback, AfterEachCallback {
+class StatusLoggerLevelExtension implements BeforeEachCallback, AfterEachCallback {
private static final String KEY = "previousLevel";
- private final Level level;
-
- public StatusLoggerLevelExtension(Level level) {
- this.level = level;
- }
@Override
public void beforeEach(ExtensionContext context) throws Exception {
+ final StatusLoggerLevel annotation = context.getRequiredTestClass().getAnnotation(StatusLoggerLevel.class);
+ if (annotation == null) {
+ return;
+ }
final StatusLogger logger = StatusLogger.getLogger();
getStore(context).put(KEY, logger.getLevel());
- logger.setLevel(level);
+ logger.setLevel(Level.valueOf(annotation.value()));
}
@Override
@@ -51,6 +45,6 @@ public class StatusLoggerLevelExtension implements BeforeEachCallback, AfterEach
private ExtensionContext.Store getStore(ExtensionContext context) {
return context.getStore(ExtensionContext.Namespace
- .create(getClass(), context.getRequiredTestInstance(), context.getRequiredTestMethod()));
+ .create(getClass(), context.getRequiredTestInstance()));
}
}
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
index a14114b..33b2e58 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
@@ -25,7 +25,7 @@ import org.junit.rules.ExternalResource;
* Log4j configuration file.
*
* @since 2.8
- * @deprecated Use {@link StatusLoggerLevelExtension} with JUnit 5
+ * @deprecated Use {@link StatusLoggerLevel} with JUnit 5
*/
public class StatusLoggerRule extends ExternalResource {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
index fc280eb..9fe67cf 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
@@ -16,25 +16,21 @@
*/
package org.apache.logging.log4j.core.config.plugins.validation.validators;
-import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
-import org.apache.logging.log4j.junit.StatusLoggerLevelExtension;
+import org.apache.logging.log4j.junit.StatusLoggerLevel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
import static org.junit.jupiter.api.Assertions.*;
+@StatusLoggerLevel("FATAL")
public class ValidHostValidatorTest {
- @RegisterExtension
- static StatusLoggerLevelExtension extension = new StatusLoggerLevelExtension(Level.FATAL);
-
private PluginType<HostAndPort> plugin;
private Node node;