1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic;
15
16 import java.util.HashMap;
17
18 import ch.qos.logback.core.testUtil.RandomUtil;
19 import org.junit.jupiter.api.Disabled;
20 import org.junit.jupiter.api.Test;
21 import org.slf4j.MDC;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26
27 public class MDCTest {
28
29 int diff = RandomUtil.getPositiveInt();
30
31
32 @Test
33 public void test() throws InterruptedException {
34 MDCTestThread threadA = new MDCTestThread("a");
35 threadA.start();
36
37 MDCTestThread threadB = new MDCTestThread("b");
38 threadB.start();
39
40 threadA.join();
41 threadB.join();
42
43 assertNull(threadA.x0);
44 assertEquals("a", threadA.x1);
45 assertNull(threadA.x2);
46
47 assertNull(threadB.x0);
48 assertEquals("b", threadB.x1);
49 assertNull(threadB.x2);
50
51 }
52
53 @Test
54 public void testLBCLASSIC_98() {
55 MDC.setContextMap(new HashMap<String, String>());
56 }
57
58
59
60 @Disabled
61 @Test
62 public void closableTestA() {
63 String key = "key-" + diff;
64 String val = "val-" + diff;
65
66 try (MDC.MDCCloseable closeable = MDC.putCloseable(key, val)) {
67 if (1 == 1)
68 throw new IllegalStateException("x");
69 } catch (IllegalStateException e) {
70 assertNotNull(MDC.get(key));
71 assertEquals(val, MDC.get(key));
72 } finally {
73 }
74 assertNull(MDC.get(key));
75 }
76
77 @Test
78 public void closableTest() {
79 String key = "key-" + diff;
80 String val = "val-" + diff;
81 MDC.MDCCloseable closeable = MDC.putCloseable(key, val);
82
83 try {
84 if (1 == 1)
85 throw new IllegalStateException("x");
86 } catch (IllegalStateException e) {
87 assertNotNull(MDC.get(key));
88 assertEquals(val, MDC.get(key));
89 } finally {
90 closeable.close();
91 }
92 assertNull(MDC.get(key));
93 }
94
95 }