View Javadoc
1   package uk.org.lidalia.slf4jtest;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.concurrent.CountDownLatch;
7   
8   import org.junit.Test;
9   
10  import static org.junit.Assert.assertEquals;
11  import static org.junit.Assert.assertNull;
12  
13  public class TestMDCAdapterTests {
14  
15      TestMDCAdapter testMDCAdapter = new TestMDCAdapter();
16      String key = "key";
17      String value = "value";
18  
19      @Test
20      public void putGetRemoveLoop() {
21          assertNull(testMDCAdapter.get(key));
22          testMDCAdapter.put(key, value);
23          assertEquals(value, testMDCAdapter.get(key));
24          testMDCAdapter.remove(key);
25          assertNull(testMDCAdapter.get(key));
26      }
27  
28      @Test
29      public void getCopyOfContextMap() {
30          testMDCAdapter.put(key, value);
31          Map<String, String> expected = new HashMap<String, String>();
32          expected.put(key, value);
33          assertEquals(expected, testMDCAdapter.getCopyOfContextMap());
34      }
35  
36      @Test
37      @SuppressWarnings("raw type")
38      public void getCopyOfContextMapIsCopy() {
39          testMDCAdapter.put(key, value);
40          Map<String, String> expected = new HashMap<>();
41          expected.put(key, value);
42          Map actual = testMDCAdapter.getCopyOfContextMap();
43          testMDCAdapter.clear();
44          assertEquals(expected, actual);
45      }
46  
47      @Test
48      public void clear() {
49          testMDCAdapter.put(key, value);
50          testMDCAdapter.clear();
51          assertEquals(Collections.emptyMap(), testMDCAdapter.getCopyOfContextMap());
52      }
53  
54      @Test
55      public void setContextMapSetsCopy() {
56          Map<String, String> newValues = new HashMap<String, String>();
57          newValues.put(key, value);
58          testMDCAdapter.setContextMap(newValues);
59          Map<String, String> expected = new HashMap<String, String>(newValues);
60          newValues.clear();
61          assertEquals(expected, testMDCAdapter.getCopyOfContextMap());
62      }
63  
64      @Test
65      public void testMdcAdapterIsThreadLocal() throws InterruptedException {
66          final CountDownLatch latch = new CountDownLatch(2);
67          final Map<String, String> results = new HashMap<String, String>();
68          Thread thread1 = new Thread() {
69              @Override
70              public void run() {
71                  testMDCAdapter.put(key, "value1");
72                  latch.countDown();
73                  try {
74                      latch.await();
75                  } catch (InterruptedException e) {
76                      Thread.currentThread().interrupt();
77                  }
78                  results.put("thread1", testMDCAdapter.get(key));
79              }
80          };
81          Thread thread2 = new Thread() {
82              @Override
83              public void run() {
84                  testMDCAdapter.put(key, "value2");
85                  latch.countDown();
86                  try {
87                      latch.await();
88                  } catch (InterruptedException e) {
89                      Thread.currentThread().interrupt();
90                  }
91                  results.put("thread2", testMDCAdapter.get(key));
92              }
93          };
94          thread1.start();
95          thread2.start();
96          thread1.join();
97          thread2.join();
98          assertEquals("value1", results.get("thread1"));
99          assertEquals("value2", results.get("thread2"));
100     }
101 }