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 MDCAdapterTestBase {
45      
46      protected MDCAdapter mdc = instantiateMDC();
47  
48      
49      
50      protected MDCAdapter instantiateMDC() {
51          return new BasicMDCAdapter();
52      }
53      
54      // leave MDC clean
55      @After
56      public void tearDown() throws Exception {
57          mdc.clear();
58      }
59  
60      @Test
61      public void testSettingAndGettingWithMDC() {
62          assertNull(mdc.get("testKey"));
63          mdc.put("testKey", "testValue");
64          assertEquals(mdc.get("testKey"), "testValue");
65      }
66  
67      @Test
68      public void testOverwritingAKeyInMDC() {
69          assertNull(mdc.get("testKey"));
70          mdc.put("testKey", "testValue");
71          mdc.put("testKey", "differentTestValue");
72          assertEquals(mdc.get("testKey"), "differentTestValue");
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(() -> {
87              mdc.put("childKey", "childValue");
88              assertEquals("parentValue", mdc.get("parentKey"));
89          });
90      }
91  
92      @Test
93      public void testMDCDoesntGetValuesFromChildThread() throws Exception {
94          mdc.put("parentKey", "parentValue");
95          runAndWait(() -> mdc.put("childKey", "childValue"));
96          assertEquals("parentValue", mdc.get("parentKey"));
97          assertNull(mdc.get("childKey"));
98      }
99  
100     
101     @Test
102     public void testInvokingSetContextMap_WithANullMap_SLF4J_414() {
103         mdc.setContextMap(null);
104     }
105     
106     @Test
107     public void testMDCChildThreadCanOverwriteParentThread() throws Exception {
108         mdc.put("sharedKey", "parentValue");
109         runAndWait(() -> {
110             assertEquals("parentValue", mdc.get("sharedKey"));
111             mdc.put("sharedKey", "childValue");
112             assertEquals("childValue", mdc.get("sharedKey"));
113         });
114         assertEquals("parentValue", mdc.get("sharedKey"));
115     }
116 
117     private void runAndWait(Runnable runnable) {
118         RecordingExceptionHandler handler = new RecordingExceptionHandler();
119         Thread thread = new Thread(runnable);
120         thread.setUncaughtExceptionHandler(handler);
121         thread.start();
122         try {
123             thread.join();
124         } catch (Throwable t) {
125             fail("Unexpected failure in child thread:" + t.getMessage());
126         }
127         assertFalse(handler.getMessage(), handler.hadException());
128     }
129 
130     /** A {@link UncaughtExceptionHandler} that records whether the thread threw an exception. */
131     private static class RecordingExceptionHandler implements UncaughtExceptionHandler {
132         private Throwable exception;
133 
134         @Override
135         public void uncaughtException(Thread t, Throwable e) {
136             exception = e;
137         }
138 
139         boolean hadException() {
140             return exception != null;
141         }
142 
143         String getMessage() {
144             return exception != null ? exception.getMessage() : "";
145         }
146     }
147 }