You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/01/20 06:54:46 UTC

[logging-log4j2] 04/05: Add JUnit 5 extension to store per-test status messages

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 26b55db8909ac44669b7d5de1907e56947b20931
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Sat Jan 14 15:16:57 2023 +0100

    Add JUnit 5 extension to store per-test status messages
    
    The `@UsingStatusListener` annotation on a test or class resolves
    parameters of type `ListStatusListener`. These can be used to retrieve
    status messages that were generated by a single test.
---
 .../logging/log4j/test/ListStatusListener.java     |  39 +++++++
 .../log4j/test/junit/StatusLoggerExtension.java    | 122 +++++++++++++++++++++
 .../log4j/test/junit/UsingStatusListener.java      |  45 ++++++++
 3 files changed, 206 insertions(+)

diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/ListStatusListener.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/ListStatusListener.java
new file mode 100644
index 0000000000..08d68b4f7b
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/ListStatusListener.java
@@ -0,0 +1,39 @@
+/*
+ * 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.test;
+
+import java.util.stream.Stream;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.status.StatusData;
+import org.apache.logging.log4j.status.StatusListener;
+
+/**
+ * A {@link StatusListener}
+ */
+public interface ListStatusListener extends StatusListener {
+
+    Stream<StatusData> getStatusData();
+
+    default Stream<StatusData> findStatusData(Level level) {
+        return getStatusData().filter(data -> level.isLessSpecificThan(data.getLevel()));
+    }
+
+    default Stream<StatusData> findStatusData(Level level, String regex) {
+        return findStatusData(level).filter(data -> data.getMessage().getFormattedMessage().matches(regex));
+    }
+}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/StatusLoggerExtension.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/StatusLoggerExtension.java
new file mode 100644
index 0000000000..2ad80bc33a
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/StatusLoggerExtension.java
@@ -0,0 +1,122 @@
+/*
+ * 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.test.junit;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.status.StatusConsoleListener;
+import org.apache.logging.log4j.status.StatusData;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
+
+class StatusLoggerExtension extends TypeBasedParameterResolver<ListStatusListener>
+        implements BeforeEachCallback, TestExecutionExceptionHandler {
+
+    private static final StatusLogger LOGGER = StatusLogger.getLogger();
+    private static final StatusConsoleListener CONSOLE_LISTENER = new StatusConsoleListener(Level.ALL);
+    private static final Object KEY = ListStatusListener.class;
+
+    public StatusLoggerExtension() {
+        super(ListStatusListener.class);
+    }
+
+    @Override
+    public void beforeEach(ExtensionContext context) throws Exception {
+        final ListStatusListenerHolder holder = new ListStatusListenerHolder(context);
+        ExtensionContextAnchor.setAttribute(KEY, holder, context);
+    }
+
+    @Override
+    public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
+        final ListStatusListener statusListener = resolveParameter(null, context);
+        statusListener.getStatusData().forEach(CONSOLE_LISTENER::log);
+        throw throwable;
+    }
+
+    @Override
+    public ListStatusListener resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
+            throws ParameterResolutionException {
+        final ListStatusListenerHolder holder = ExtensionContextAnchor.getAttribute(KEY, ListStatusListenerHolder.class,
+                extensionContext);
+        return holder.getStatusListener();
+    }
+
+    private static class ListStatusListenerHolder implements CloseableResource {
+
+        private final ListStatusListener statusListener;
+
+        public ListStatusListenerHolder(ExtensionContext context) {
+            this.statusListener = new JUnitListStatusListener(context);
+            LOGGER.registerListener(statusListener);
+        }
+
+        public ListStatusListener getStatusListener() {
+            return statusListener;
+        }
+
+        @Override
+        public void close() throws Throwable {
+            LOGGER.removeListener(statusListener);
+        }
+
+    }
+
+    private static class JUnitListStatusListener implements ListStatusListener {
+
+        private final ExtensionContext context;
+        private final List<StatusData> statusData = Collections.synchronizedList(new ArrayList<StatusData>());
+
+        public JUnitListStatusListener(ExtensionContext context) {
+            this.context = context;
+        }
+
+        @Override
+        public void log(StatusData data) {
+            if (context.equals(ExtensionContextAnchor.getContext())) {
+                statusData.add(data);
+            }
+        }
+
+        @Override
+        public Level getStatusLevel() {
+            return Level.DEBUG;
+        }
+
+        @Override
+        public void close() throws IOException {
+            // NOP
+        }
+
+        @Override
+        public Stream<StatusData> getStatusData() {
+            return statusData.stream();
+        }
+
+    }
+}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingStatusListener.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingStatusListener.java
new file mode 100644
index 0000000000..c06fb67515
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingStatusListener.java
@@ -0,0 +1,45 @@
+/*
+ * 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.test.junit;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.Properties;
+
+import org.apache.logging.log4j.status.StatusListener;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Configures and injects a {@link StatusListener} of type
+ * {@link ListStatusListener}, that will collect status messages for the test
+ * context.
+ */
+@Retention(RUNTIME)
+@Target({ FIELD, METHOD })
+@Inherited
+@Documented
+@ExtendWith(ExtensionContextAnchor.class)
+@ExtendWith(StatusLoggerExtension.class)
+public @interface UsingStatusListener {
+}