You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2019/12/07 04:23:14 UTC

[logging-log4j2] 01/02: LOG4J2-2732 - Add ThreadContext.putIfNotNull method

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

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

commit 2ae61fa13a17cca1011c5d8b3065916e2afd399d
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Fri Dec 6 21:22:16 2019 -0700

    LOG4J2-2732 - Add ThreadContext.putIfNotNull method
---
 .../main/java/org/apache/logging/log4j/ThreadContext.java    |  1 +
 .../java/org/apache/logging/log4j/ThreadContextTest.java     | 12 ++++++++++++
 src/changes/changes.xml                                      |  3 +++
 3 files changed, 16 insertions(+)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
index cad4770..5318410 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
@@ -256,6 +256,7 @@ public final class ThreadContext {
      *
      * @param key The key name.
      * @param value The key value.
+     * @since 2.13.0
      */
     public static void putIfNull(final String key, final String value) {
         if(!contextMap.containsKey(key)) {
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
index baec493..63f21df 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
@@ -108,6 +108,18 @@ public class ThreadContextTest {
     }
 
     @Test
+    public void testPutIfNotNull() {
+        ThreadContext.clearMap();
+        assertNull(ThreadContext.get("testKey"));
+        ThreadContext.put("testKey", "testValue");
+        assertEquals("testValue", ThreadContext.get("testKey"));
+        assertEquals("Incorrect value in test key", "testValue", ThreadContext.get("testKey"));
+        ThreadContext.putIfNull("testKey", "new Value");
+        assertEquals("Incorrect value in test key", "testValue", ThreadContext.get("testKey"));
+        ThreadContext.clearMap();
+    }
+
+    @Test
     public void testPutAll() {
         assertTrue(ThreadContext.isEmpty());
         assertFalse(ThreadContext.containsKey("key"));
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 24a45ba..29b5b75 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -160,6 +160,9 @@
       </action>
     </release>
     <release version="2.13.0" date="2019-MM-DD" description="GA Release 2.13.0">
+      <action issue="LOG4J2-2732" dev="rgoers" type="add" due-to="Matt Pavlovich">
+        Add ThreadContext.putIfNotNull method.
+      </action>
       <action issue="LOG4J2-2731" dev="rgoers" type="add">
         Add a Level Patttern Selector.
       </action>