1   package ch.qos.logback.classic.pattern;
2   
3   import static org.junit.Assert.*;
4   
5   import org.junit.After;
6   import org.junit.Before;
7   import org.junit.Test;
8   import org.slf4j.MDC;
9   
10  import ch.qos.logback.classic.Level;
11  import ch.qos.logback.classic.LoggerContext;
12  import ch.qos.logback.classic.spi.LoggingEvent;
13  import ch.qos.logback.core.util.SystemInfo;
14  
15  public class MDCConverterTest {
16  
17    LoggerContext lc;
18    MDCConverter converter;
19  
20    @Before
21    public void setUp() throws Exception {
22      lc = new LoggerContext();
23      converter = new MDCConverter();
24      converter.start();
25    }
26  
27    @After
28    public void tearDown() throws Exception {
29      lc = null;
30      converter.stop();
31      converter = null;
32    }
33  
34    @Test
35    public void testConverWithOneEntry() {
36      MDC.clear();
37      MDC.put("testKey", "testValue");
38      LoggingEvent le = createLoggingEvent();
39      String result = converter.convert(le);
40      assertEquals("testKey=testValue", result);
41    }
42  
43    @Test
44    public void testConverWithMultipleEntries() {
45      MDC.clear();
46      MDC.put("testKey", "testValue");
47      MDC.put("testKey2", "testValue2");
48      LoggingEvent le = createLoggingEvent();
49      String result = converter.convert(le);
50      if (SystemInfo.getJavaVendor().contains("IBM")) {
51        assertEquals("testKey2=testValue2, testKey=testValue", result);
52      } else {
53        assertEquals("testKey=testValue, testKey2=testValue2", result);
54      }
55    }
56  
57    private LoggingEvent createLoggingEvent() {
58      LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc
59          .getLogger(LoggerContext.ROOT_NAME), Level.DEBUG, "test message", null,
60          null);
61      return le;
62    }
63  }