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 2022/10/20 03:20:01 UTC
[logging-log4j2] 02/04: Adds a thread-bound `ExtensionContext`
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit da0b463fd54488949ed3978ff79f83714f593d98
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Mon Jun 20 21:44:43 2022 +0200
Adds a thread-bound `ExtensionContext`
The `ExtensionContextAnchor` extension can be auto-discovered by JUnit
5, if autodiscovery is enabled. It will be used whenever a static access
to per-test objects is needed.
---
.../log4j/test/junit/ExtensionContextAnchor.java | 90 ++++++++++++++++++++++
.../org.junit.jupiter.api.extension.Extension | 15 ++++
2 files changed, 105 insertions(+)
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ExtensionContextAnchor.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ExtensionContextAnchor.java
new file mode 100644
index 0000000000..0b4cc50d60
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ExtensionContextAnchor.java
@@ -0,0 +1,90 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
+
+public class ExtensionContextAnchor
+ implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {
+
+ public static Namespace LOG4J2_NAMESPACE = Namespace.create("org.apache.logging.log4j.junit");
+ private static final ThreadLocal<ExtensionContext> EXTENSION_CONTEXT = new InheritableThreadLocal<>();
+
+ private static void bind(ExtensionContext context) {
+ EXTENSION_CONTEXT.set(context);
+ }
+
+ private static void unbind(ExtensionContext context) {
+ EXTENSION_CONTEXT.set(context.getParent().orElse(null));
+ }
+
+ public static ExtensionContext getContext() {
+ return EXTENSION_CONTEXT.get();
+ }
+
+ public static ExtensionContext getContext(ExtensionContext context) {
+ return context != null ? context : EXTENSION_CONTEXT.get();
+ }
+
+ static <T> T getAttribute(Object key, Class<T> clazz, ExtensionContext context) {
+ final ExtensionContext actualContext = getContext(context);
+ assertNotNull(actualContext, "missing ExtensionContext");
+ return actualContext.getStore(LOG4J2_NAMESPACE).get(key, clazz);
+ }
+
+ static void setAttribute(Object key, Object value, ExtensionContext context) {
+ final ExtensionContext actualContext = getContext(context);
+ assertNotNull(actualContext, "missing ExtensionContext");
+ actualContext.getStore(LOG4J2_NAMESPACE).put(key, value);
+ }
+
+ static void removeAttribute(Object key, ExtensionContext context) {
+ final ExtensionContext actualContext = getContext(context);
+ if (actualContext != null) {
+ actualContext.getStore(LOG4J2_NAMESPACE).remove(key);
+ }
+ }
+
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ unbind(context);
+ }
+
+ @Override
+ public void afterAll(ExtensionContext context) throws Exception {
+ unbind(context);
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext context) throws Exception {
+ bind(context);
+ }
+
+ @Override
+ public void beforeAll(ExtensionContext context) throws Exception {
+ bind(context);
+ }
+
+}
diff --git a/log4j-api-test/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/log4j-api-test/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
new file mode 100644
index 0000000000..ca7ce84edd
--- /dev/null
+++ b/log4j-api-test/src/main/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
@@ -0,0 +1,15 @@
+# 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.
+org.apache.logging.log4j.test.junit.ExtensionContextAnchor
\ No newline at end of file