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 2022/06/05 00:06:05 UTC
[logging-log4j2] 06/08: Split ThreadContext extensions and simplify lifecycle management
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
commit 1a8d191b91885c91369467418ccdac5c909e8451
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Sat Jun 4 19:02:31 2022 -0500
Split ThreadContext extensions and simplify lifecycle management
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../log4j/test/junit/ThreadContextExtension.java | 102 ---------------------
.../test/junit/ThreadContextMapExtension.java | 48 ++++++++++
.../test/junit/ThreadContextStackExtension.java | 47 ++++++++++
.../log4j/test/junit/UsingAnyThreadContext.java | 3 -
.../log4j/test/junit/UsingThreadContextMap.java | 2 +-
.../log4j/test/junit/UsingThreadContextStack.java | 2 +-
.../logging/log4j/CloseableThreadContextTest.java | 8 --
7 files changed, 97 insertions(+), 115 deletions(-)
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextExtension.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextExtension.java
deleted file mode 100644
index becd681b9c..0000000000
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextExtension.java
+++ /dev/null
@@ -1,102 +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.test.junit;
-
-import org.apache.logging.log4j.ThreadContext;
-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;
-import org.junit.jupiter.api.extension.ExtensionContext.Store;
-import org.junit.platform.commons.util.AnnotationUtils;
-
-import java.lang.reflect.Method;
-import java.util.Collection;
-import java.util.Map;
-
-class ThreadContextExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback {
- private static class ThreadContextMapStore implements Store.CloseableResource {
- private final Map<String, String> previousMap = ThreadContext.getImmutableContext();
-
- private ThreadContextMapStore() {
- ThreadContext.clearMap();
- }
-
- @Override
- public void close() throws Throwable {
- // TODO LOG4J2-1517 Add ThreadContext.setContext(Map<String, String>)
- ThreadContext.clearMap();
- ThreadContext.putAll(previousMap);
- }
- }
-
- private static class ThreadContextStackStore implements Store.CloseableResource {
- private final Collection<String> previousStack = ThreadContext.getImmutableStack();
-
- private ThreadContextStackStore() {
- ThreadContext.clearStack();
- }
-
- @Override
- public void close() throws Throwable {
- ThreadContext.setStack(previousStack);
- }
- }
-
- @Override
- public void beforeAll(final ExtensionContext context) throws Exception {
- final Class<?> testClass = context.getRequiredTestClass();
- final Store store = getTestStore(context);
- if (AnnotationUtils.isAnnotated(testClass, UsingThreadContextMap.class)) {
- store.put(ThreadContextMapStore.class, new ThreadContextMapStore());
- }
- if (AnnotationUtils.isAnnotated(testClass, UsingThreadContextStack.class)) {
- store.put(ThreadContextStackStore.class, new ThreadContextStackStore());
- }
- }
-
- @Override
- public void beforeEach(ExtensionContext context) throws Exception {
- final Store store = getTestStore(context);
- final Method testMethod = context.getRequiredTestMethod();
- if (AnnotationUtils.isAnnotated(testMethod, UsingThreadContextMap.class)) {
- store.put(ThreadContextMapStore.class, new ThreadContextMapStore());
- }
- if (AnnotationUtils.isAnnotated(testMethod, UsingThreadContextStack.class)) {
- store.put(ThreadContextStackStore.class, new ThreadContextStackStore());
- }
- }
-
- @Override
- public void afterEach(final ExtensionContext context) throws Exception {
- final Class<?> testClass = context.getRequiredTestClass();
- if (AnnotationUtils.isAnnotated(testClass, UsingThreadContextMap.class)) {
- ThreadContext.clearMap();
- }
- if (AnnotationUtils.isAnnotated(testClass, UsingThreadContextStack.class)) {
- ThreadContext.clearStack();
- }
- }
-
- private static Store getTestStore(final ExtensionContext context) {
- final Namespace baseNamespace = Namespace.create(ThreadContext.class, context.getRequiredTestClass());
- final Namespace namespace = context.getTestInstance().map(baseNamespace::append).orElse(baseNamespace);
- return context.getStore(namespace);
- }
-}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextMapExtension.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextMapExtension.java
new file mode 100644
index 0000000000..35089acf02
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextMapExtension.java
@@ -0,0 +1,48 @@
+/*
+ * 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 org.apache.logging.log4j.ThreadContext;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
+
+import java.util.Map;
+
+class ThreadContextMapExtension implements BeforeEachCallback {
+ private static class ThreadContextMapStore implements ExtensionContext.Store.CloseableResource {
+ private final Map<String, String> previousMap = ThreadContext.getImmutableContext();
+
+ private ThreadContextMapStore() {
+ ThreadContext.clearMap();
+ }
+
+ @Override
+ public void close() throws Throwable {
+ // TODO LOG4J2-1517 Add ThreadContext.setContext(Map<String, String>)
+ ThreadContext.clearMap();
+ ThreadContext.putAll(previousMap);
+ }
+ }
+
+ @Override
+ public void beforeEach(final ExtensionContext context) throws Exception {
+ context.getStore(Namespace.create(ThreadContext.class, context.getRequiredTestClass(), context.getRequiredTestInstance()))
+ .getOrComputeIfAbsent(ThreadContextMapStore.class);
+ }
+}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextStackExtension.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextStackExtension.java
new file mode 100644
index 0000000000..c3ebe7b484
--- /dev/null
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextStackExtension.java
@@ -0,0 +1,47 @@
+/*
+ * 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 org.apache.logging.log4j.ThreadContext;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
+import org.junit.jupiter.api.extension.ExtensionContext.Store;
+
+import java.util.Collection;
+
+class ThreadContextStackExtension implements BeforeEachCallback {
+ private static class ThreadContextStackStore implements Store.CloseableResource {
+ private final Collection<String> previousStack = ThreadContext.getImmutableStack();
+
+ private ThreadContextStackStore() {
+ ThreadContext.clearStack();
+ }
+
+ @Override
+ public void close() throws Throwable {
+ ThreadContext.setStack(previousStack);
+ }
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext context) throws Exception {
+ context.getStore(Namespace.create(ThreadContext.class, context.getRequiredTestClass(), context.getRequiredTestInstance()))
+ .getOrComputeIfAbsent(ThreadContextStackStore.class);
+ }
+}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java
index 30a894761c..e88914707c 100644
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java
@@ -17,8 +17,6 @@
package org.apache.logging.log4j.test.junit;
-import org.junit.jupiter.api.extension.ExtendWith;
-
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
@@ -38,6 +36,5 @@ import java.lang.annotation.Target;
@Inherited
@UsingThreadContextMap
@UsingThreadContextStack
-@ExtendWith(ThreadContextExtension.class)
public @interface UsingAnyThreadContext {
}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextMap.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextMap.java
index 977d210623..ab1bddd7cf 100644
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextMap.java
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextMap.java
@@ -37,7 +37,7 @@ import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Documented
@Inherited
-@ExtendWith(ThreadContextExtension.class)
+@ExtendWith(ThreadContextMapExtension.class)
@ReadsSystemProperty
public @interface UsingThreadContextMap {
}
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextStack.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextStack.java
index bae2335482..6d8251a671 100644
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextStack.java
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingThreadContextStack.java
@@ -37,7 +37,7 @@ import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Documented
@Inherited
-@ExtendWith(ThreadContextExtension.class)
+@ExtendWith(ThreadContextStackExtension.class)
@ReadsSystemProperty
public @interface UsingThreadContextStack {
}
diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/CloseableThreadContextTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/CloseableThreadContextTest.java
index b9fa6879ac..bc92763e1f 100644
--- a/log4j-api-test/src/test/java/org/apache/logging/log4j/CloseableThreadContextTest.java
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/CloseableThreadContextTest.java
@@ -17,8 +17,6 @@
package org.apache.logging.log4j;
import org.apache.logging.log4j.test.junit.UsingAnyThreadContext;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ReadsSystemProperty;
@@ -40,12 +38,6 @@ public class CloseableThreadContextTest {
private final String key = "key";
private final String value = "value";
- @BeforeEach
- @AfterEach
- void clearThreadContext() {
- ThreadContext.clearAll();
- }
-
@Test
public void shouldAddAnEntryToTheMap() {
try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) {