1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
41
42
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
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 }