[logback-dev] svn commit: r1886 - in logback/trunk/logback-classic/src: main/java/org/slf4j/impl test/java/org/slf4j/impl

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Oct 28 11:18:59 CET 2008


Author: ceki
Date: Tue Oct 28 11:18:59 2008
New Revision: 1886

Modified:
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/CopyOnInheritThreadLocal.java
   logback/trunk/logback-classic/src/test/java/org/slf4j/impl/LogbackMDCAdapterTest.java

Log:
Fix LBCLASSIC-77

On application which do not put data in the MDC, the CopyOnInheriteThreadLocal would
throw a NullPointerException.

Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/CopyOnInheritThreadLocal.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/org/slf4j/impl/CopyOnInheritThreadLocal.java	(original)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/CopyOnInheritThreadLocal.java	Tue Oct 28 11:18:59 2008
@@ -17,8 +17,12 @@
   @Override
   protected HashMap<String, String> childValue(
       HashMap<String, String> parentValue) {
-    HashMap<String, String> hm = new HashMap<String, String>(parentValue);
-    return hm;
+    if (parentValue == null) {
+      return null;
+    } else {
+      HashMap<String, String> hm = new HashMap<String, String>(parentValue);
+      return hm;
+    }
   }
 
 }

Modified: logback/trunk/logback-classic/src/test/java/org/slf4j/impl/LogbackMDCAdapterTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/org/slf4j/impl/LogbackMDCAdapterTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/org/slf4j/impl/LogbackMDCAdapterTest.java	Tue Oct 28 11:18:59 2008
@@ -27,6 +27,45 @@
   int diff = new Random().nextInt();
 
   /**
+   * Test that CopyOnInheritThreadLocal does not barf when the 
+   * MDC hashmap is null
+   * 
+   * @throws InterruptedException
+   */
+  @Test
+  public void lbclassic77() throws InterruptedException {
+    LogbackMDCAdapter lma = new LogbackMDCAdapter();
+
+    HashMap<String, String> parentHM = getHashMapFromMDCAdapter(lma);
+    assertNull(parentHM);
+    
+    ChildThreadForMDCAdapter childThread = new ChildThreadForMDCAdapter(lma);
+    childThread.start();
+    childThread.join();
+    assertTrue(childThread.successul);
+    assertNull(childThread.childHM);
+  }
+  
+  class ChildThreadForMDCAdapter extends Thread {
+
+    LogbackMDCAdapter logbackMDCAdapter;
+    boolean successul;
+    HashMap<String, String> childHM;
+
+    ChildThreadForMDCAdapter(LogbackMDCAdapter logbackMDCAdapter) {
+      this.logbackMDCAdapter = logbackMDCAdapter;
+    }
+    
+    @Override
+    public void run() {
+      childHM = getHashMapFromMDCAdapter(logbackMDCAdapter);
+      logbackMDCAdapter.get("");
+      successul = true;
+    }
+  }
+
+  // ================================================= 
+  /**
    * Test that LogbackMDCAdapter copies its hashmap when a child
    * thread inherits it.
    * 
@@ -38,32 +77,33 @@
     String otherMDCKey = "o" + diff;
     MDC.put(mdcKey, mdcKey + A_SUFFIX);
 
-    HashMap<String, String> parentHM = getHM();
+    HashMap<String, String> parentHM = getHashMapFromMDC();
 
-    MyThread myThread = new MyThread(mdcKey, otherMDCKey);
-    myThread.start();
-    myThread.join();
+    ChildThreadForMDC childThread = new ChildThreadForMDC(mdcKey, otherMDCKey);
+    childThread.start();
+    childThread.join();
 
     assertNull(MDC.get(otherMDCKey));
-    assertTrue(myThread.successul);
-    assertTrue(parentHM != myThread.childHM);
+    assertTrue(childThread.successul);
+    assertTrue(parentHM != childThread.childHM);
   }
 
-  class MyThread extends Thread {
+   
+  class ChildThreadForMDC extends Thread {
 
     String mdcKey;
     String otherMDCKey;
     boolean successul;
     HashMap<String, String> childHM;
 
-    MyThread(String mdcKey, String otherMDCKey) {
+    ChildThreadForMDC(String mdcKey, String otherMDCKey) {
       this.mdcKey = mdcKey;
       this.otherMDCKey = otherMDCKey;
     }
 
     @Override
     public void run() {
-      childHM = getHM();
+      childHM = getHashMapFromMDC();
 
       MDC.put(otherMDCKey, otherMDCKey + A_SUFFIX);
       assertNotNull(MDC.get(mdcKey));
@@ -73,10 +113,14 @@
     }
   }
 
-  HashMap<String, String> getHM() {
-    LogbackMDCAdapter lma = (LogbackMDCAdapter) MDC.getMDCAdapter();
+  HashMap<String, String> getHashMapFromMDCAdapter(LogbackMDCAdapter lma) {
     CopyOnInheritThreadLocal copyOnInheritThreadLocal = lma.copyOnInheritThreadLocal;
     return copyOnInheritThreadLocal.get();
+  }
 
+  HashMap<String, String> getHashMapFromMDC() {
+    LogbackMDCAdapter lma = (LogbackMDCAdapter) MDC.getMDCAdapter();
+    CopyOnInheritThreadLocal copyOnInheritThreadLocal = lma.copyOnInheritThreadLocal;
+    return copyOnInheritThreadLocal.get();
   }
 }


More information about the logback-dev mailing list