logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1426064 - /logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
Date Wed, 26 Dec 2012 22:27:46 GMT
Author: rgoers
Date: Wed Dec 26 22:27:45 2012
New Revision: 1426064

URL: http://svn.apache.org/viewvc?rev=1426064&view=rev
Log:
Add unit test for ThreadContext inheritance

Modified:
    logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java

Modified: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java?rev=1426064&r1=1426063&r2=1426064&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
Wed Dec 26 22:27:45 2012
@@ -19,6 +19,7 @@ package org.apache.logging.log4j;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  *
@@ -32,4 +33,43 @@ public class ThreadContextTest {
         assertEquals("Incorrect parameterized stack value", ThreadContext.pop(), "ThreadContextTest
is running");
         assertEquals("Incorrect simple stack value", ThreadContext.pop(), "Hello");
     }
+
+    @Test
+    public void testInheritance() throws Exception {
+        ThreadContext.clear();
+        ThreadContext.put("Greeting", "Hello");
+        StringBuilder sb = new StringBuilder();
+        TestThread thread = new TestThread(sb);
+        thread.start();
+        thread.join();
+        String str = sb.toString();
+        assertTrue("Unexpected ThreadContext value. Expected Hello. Actual " + str, "Hello".equals(str));
+        sb = new StringBuilder();
+        thread = new TestThread(sb);
+        thread.start();
+        thread.join();
+        str = sb.toString();
+        assertTrue("Unexpected ThreadContext value. Expected Hello. Actual " + str, "Hello".equals(str));
+    }
+
+
+    private class TestThread extends Thread {
+
+        private final StringBuilder sb;
+
+        public TestThread(StringBuilder sb) {
+            this.sb = sb;
+        }
+
+        @Override
+        public void run() {
+            String greeting = ThreadContext.get("Greeting");
+            if (greeting == null) {
+                sb.append("null");
+            } else {
+                sb.append(greeting);
+            }
+            ThreadContext.clear();
+        }
+    }
 }



Mime
View raw message