View Javadoc

1   /**
2    * Copyright (c) 2004-2013 QOS.ch, Copyright (C) 2015 Google Inc.
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   */
24  
25  package org.slf4j.helpers;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertNull;
30  import static org.junit.Assert.fail;
31  
32  import java.lang.Thread.UncaughtExceptionHandler;
33  import java.util.Map;
34  
35  import org.junit.After;
36  import org.junit.Test;
37  import org.slf4j.spi.MDCAdapter;
38  
39  /**
40   * Tests for {@link BasicMDCAdapter}
41   * 
42   * @author Lukasz Cwik
43   */
44  public class BasicMDCAdapterTest {
45      MDCAdapter mdc = new BasicMDCAdapter();
46  
47      @After
48      public void tearDown() throws Exception {
49          mdc.clear();
50      }
51  
52      @Test
53      public void testSettingAndGettingWithMDC() {
54          assertNull(mdc.get("testKey"));
55          mdc.put("testKey", "testValue");
56          assertEquals(mdc.get("testKey"), "testValue");
57      }
58  
59      @Test
60      public void testOverwritingAKeyInMDC() {
61          assertNull(mdc.get("testKey"));
62          mdc.put("testKey", "testValue");
63          mdc.put("testKey", "differentTestValue");
64          assertEquals(mdc.get("testKey"), "differentTestValue");
65      }
66  
67      @Test
68      public void testClearingMDC() {
69          mdc.put("testKey", "testValue");
70          assertFalse(mdc.getCopyOfContextMap().isEmpty());
71          mdc.clear();
72          assertNull(mdc.getCopyOfContextMap());
73      }
74  
75      @Test
76      public void testGetCopyOfContextMapFromMDC() {
77          mdc.put("testKey", "testValue");
78          Map<String, String> copy = mdc.getCopyOfContextMap();
79          mdc.put("anotherTestKey", "anotherTestValue");
80          assertFalse(copy.size() == mdc.getCopyOfContextMap().size());
81      }
82  
83      @Test
84      public void testMDCInheritsValuesFromParentThread() throws Exception {
85          mdc.put("parentKey", "parentValue");
86          runAndWait(new Runnable() {
87              public void run() {
88                  mdc.put("childKey", "childValue");
89                  assertEquals("parentValue", mdc.get("parentKey"));
90              }
91          });
92      }
93  
94      @Test
95      public void testMDCDoesntGetValuesFromChildThread() throws Exception {
96          mdc.put("parentKey", "parentValue");
97          runAndWait(new Runnable() {
98              public void run() {
99                  mdc.put("childKey", "childValue");
100             }
101         });
102         assertEquals("parentValue", mdc.get("parentKey"));
103         assertNull(mdc.get("childKey"));
104     }
105 
106     @Test
107     public void testMDCChildThreadCanOverwriteParentThread() throws Exception {
108         mdc.put("sharedKey", "parentValue");
109         runAndWait(new Runnable() {
110             public void run() {
111                 assertEquals("parentValue", mdc.get("sharedKey"));
112                 mdc.put("sharedKey", "childValue");
113                 assertEquals("childValue", mdc.get("sharedKey"));
114             }
115         });
116         assertEquals("parentValue", mdc.get("sharedKey"));
117     }
118 
119     private void runAndWait(Runnable runnable) throws Exception {
120         RecordingExceptionHandler handler = new RecordingExceptionHandler();
121         Thread thread = new Thread(runnable);
122         thread.setUncaughtExceptionHandler(handler);
123         thread.start();
124         try {
125             thread.join();
126         } catch (Throwable t) {
127             fail("Unexpected failure in child thread:" + t.getMessage());
128         }
129         assertFalse(handler.getMessage(), handler.hadException());
130     }
131 
132     /** A {@link UncaughtExceptionHandler} that records whether the thread threw an exception. */
133     private static class RecordingExceptionHandler implements UncaughtExceptionHandler {
134         private Throwable exception;
135 
136         public void uncaughtException(Thread t, Throwable e) {
137             exception = e;
138         }
139 
140         boolean hadException() {
141             return exception != null;
142         }
143 
144         String getMessage() {
145             return exception != null ? exception.getMessage() : "";
146         }
147     }
148 }