1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2005, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.pattern;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertTrue;
14  import static org.junit.Assert.fail;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.slf4j.MDC;
22  import org.slf4j.MarkerFactory;
23  
24  import ch.qos.logback.classic.Level;
25  import ch.qos.logback.classic.Logger;
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.TestConstants;
28  import ch.qos.logback.classic.spi.LoggingEvent;
29  import ch.qos.logback.core.CoreConstants;
30  import ch.qos.logback.core.net.SyslogConstants;
31  import ch.qos.logback.core.pattern.DynamicConverter;
32  import ch.qos.logback.core.pattern.FormatInfo;
33  
34  public class ConverterTest {
35  
36    LoggerContext lc = new LoggerContext();
37    Logger logger = lc.getLogger(ConverterTest.class);
38    LoggingEvent le;
39    List<String> optionList = new ArrayList<String>();
40  
41    // The LoggingEvent is massaged with an FCQN of FormattingConverter. This
42    // forces the returned caller information to match the caller stack for this
43    // this particular test.
44    LoggingEvent makeLoggingEvent(Exception ex) {
45      return new LoggingEvent(
46          ch.qos.logback.core.pattern.FormattingConverter.class.getName(),
47          logger, Level.INFO, "Some message", ex, null);
48    }
49  
50    Exception getException(String msg, Exception cause) {
51      return new Exception(msg, cause);
52    }
53  
54    @Before
55    public void setUp() throws Exception {
56      Exception rootEx = getException("Innermost", null);
57      Exception nestedEx = getException("Nested", rootEx);
58  
59      Exception ex = new Exception("Bogus exception", nestedEx);
60  
61      le = makeLoggingEvent(ex);
62    }
63  
64    @Test
65    public void testLineOfCaller() {
66      {
67        DynamicConverter<LoggingEvent> converter = new LineOfCallerConverter();
68        StringBuffer buf = new StringBuffer();
69        converter.write(buf, le);
70        // the number below should be the line number of the previous line
71        assertEquals("69", buf.toString());
72      }
73    }
74  
75    @Test
76    public void testLevel() {
77      {
78        DynamicConverter<LoggingEvent> converter = new LevelConverter();
79        StringBuffer buf = new StringBuffer();
80        converter.write(buf, le);
81        assertEquals("INFO", buf.toString());
82      }
83      {
84        DynamicConverter<LoggingEvent> converter = new LevelConverter();
85        converter.setFormattingInfo(new FormatInfo(1, 1, true, false));
86        StringBuffer buf = new StringBuffer();
87        converter.write(buf, le);
88        assertEquals("I", buf.toString());
89      }
90    }
91  
92    @Test
93    public void testThread() {
94      DynamicConverter<LoggingEvent> converter = new ThreadConverter();
95      StringBuffer buf = new StringBuffer();
96      converter.write(buf, le);
97      String regex = TestConstants.NAKED_MAIN_REGEX;
98      assertTrue(buf.toString().matches(regex));
99    }
100 
101   @Test
102   public void testMessage() {
103     DynamicConverter<LoggingEvent> converter = new MessageConverter();
104     StringBuffer buf = new StringBuffer();
105     converter.write(buf, le);
106     assertEquals("Some message", buf.toString());
107   }
108 
109   @Test
110   public void testLineSeparator() {
111     DynamicConverter<LoggingEvent> converter = new LineSeparatorConverter();
112     StringBuffer buf = new StringBuffer();
113     converter.write(buf, le);
114     assertEquals(CoreConstants.LINE_SEPARATOR, buf.toString());
115   }
116 
117   @Test
118   public void testException() {
119     {
120       DynamicConverter<LoggingEvent> converter = new ThrowableProxyConverter();
121       StringBuffer buf = new StringBuffer();
122       converter.write(buf, le);
123     }
124 
125     {
126       DynamicConverter<LoggingEvent> converter = new ThrowableProxyConverter();
127       this.optionList.add("3");
128       converter.setOptionList(this.optionList);
129       StringBuffer buf = new StringBuffer();
130       converter.write(buf, le);
131     }
132   }
133 
134   @Test
135   public void testLogger() {
136     {
137       ClassicConverter converter = new LoggerConverter();
138       StringBuffer buf = new StringBuffer();
139       converter.write(buf, le);
140       assertEquals(this.getClass().getName(), buf.toString());
141     }
142 
143     {
144       ClassicConverter converter = new LoggerConverter();
145       this.optionList.add("20");
146       converter.setOptionList(this.optionList);
147       converter.start();
148       StringBuffer buf = new StringBuffer();
149       converter.write(buf, le);
150       assertEquals("c.q.l.c.p.ConverterTest", buf.toString());
151     }
152 
153     {
154       DynamicConverter<LoggingEvent> converter = new LoggerConverter();
155       this.optionList.clear();
156       this.optionList.add("0");
157       converter.setOptionList(this.optionList);
158       converter.start();
159       StringBuffer buf = new StringBuffer();
160       converter.write(buf, le);
161       assertEquals("ConverterTest", buf.toString());
162     }
163   }
164 
165   @Test
166   public void testClass() {
167     DynamicConverter<LoggingEvent> converter = new ClassOfCallerConverter();
168     StringBuffer buf = new StringBuffer();
169     converter.write(buf, le);
170     assertEquals(this.getClass().getName(), buf.toString());
171   }
172 
173   @Test
174   public void testMethodOfCaller() {
175     DynamicConverter<LoggingEvent> converter = new MethodOfCallerConverter();
176     StringBuffer buf = new StringBuffer();
177     converter.write(buf, le);
178     assertEquals("testMethodOfCaller", buf.toString());
179   }
180 
181   @Test
182   public void testFileOfCaller() {
183     DynamicConverter<LoggingEvent> converter = new FileOfCallerConverter();
184     StringBuffer buf = new StringBuffer();
185     converter.write(buf, le);
186     assertEquals("ConverterTest.java", buf.toString());
187   }
188 
189   @Test
190   public void testCallerData() {
191     {
192       DynamicConverter<LoggingEvent> converter = new CallerDataConverter();
193       converter.start();
194 
195       StringBuffer buf = new StringBuffer();
196       converter.write(buf, le);
197       if (buf.length() < 10) {
198         fail("buf is too short");
199       }
200     }
201 
202     {
203       DynamicConverter<LoggingEvent> converter = new CallerDataConverter();
204       this.optionList.add("2");
205       this.optionList.add("XXX");
206       converter.setOptionList(this.optionList);
207       converter.start();
208 
209       StringBuffer buf = new StringBuffer();
210       LoggingEvent event = makeLoggingEvent(null);
211       event.setMarker(MarkerFactory.getMarker("XXX"));
212       converter.write(buf, event);
213       if (buf.length() < 10) {
214         fail("buf is too short");
215       }
216     }
217 
218     {
219       DynamicConverter<LoggingEvent> converter = new CallerDataConverter();
220       this.optionList.clear();
221       this.optionList.add("2");
222       this.optionList.add("XXX");
223       this.optionList.add("*");
224       converter.setOptionList(this.optionList);
225       converter.start();
226 
227       StringBuffer buf = new StringBuffer();
228       LoggingEvent event = makeLoggingEvent(null);
229       event.setMarker(MarkerFactory.getMarker("YYY"));
230       converter.write(buf, event);
231       if (buf.length() < 10) {
232         fail("buf is too short");
233       }
234     }
235     {
236       DynamicConverter<LoggingEvent> converter = new CallerDataConverter();
237       this.optionList.clear();
238       this.optionList.add("2");
239       this.optionList.add("XXX");
240       this.optionList.add("+");
241       converter.setOptionList(this.optionList);
242       converter.start();
243 
244       StringBuffer buf = new StringBuffer();
245       LoggingEvent event = makeLoggingEvent(null);
246       event.setMarker(MarkerFactory.getMarker("YYY"));
247       converter.write(buf, event);
248       if (buf.length() < 10) {
249         fail("buf is too short");
250       }
251     }
252 
253     {
254       DynamicConverter<LoggingEvent> converter = new CallerDataConverter();
255       this.optionList.clear();
256       this.optionList.add("2");
257       this.optionList.add("XXX");
258       this.optionList.add("*");
259       converter.setOptionList(this.optionList);
260       converter.start();
261 
262       StringBuffer buf = new StringBuffer();
263       converter.write(buf, le);
264       if (buf.length() < 10) {
265         fail("buf is too short");
266       }
267       // System.out.println(buf);
268     }
269 
270   }
271 
272   @Test
273   public void testRelativeTime() throws Exception {
274     DynamicConverter<LoggingEvent> converter = new RelativeTimeConverter();
275     StringBuffer buf0 = new StringBuffer();
276     StringBuffer buf1 = new StringBuffer();
277     LoggingEvent e0 = makeLoggingEvent(null);
278     LoggingEvent e1 = makeLoggingEvent(null);
279     converter.write(buf0, e0);
280     converter.write(buf1, e1);
281     assertEquals(buf0.toString(), buf1.toString());
282     int rt0 = Integer.parseInt(buf0.toString());
283     if (rt0 < 50) {
284       fail("relative time should be > 50, but it is " + rt0);
285     }
286   }
287 
288   @Test
289   public void testSyslogStart() throws Exception {
290     DynamicConverter<LoggingEvent> converter = new SyslogStartConverter();
291     this.optionList.clear();
292     this.optionList.add("MAIL");
293     converter.setOptionList(this.optionList);
294     converter.start();
295 
296     LoggingEvent event = makeLoggingEvent(null);
297 
298     StringBuffer buf = new StringBuffer();
299     converter.write(buf, event);
300 
301     String expected = "<"
302         + (SyslogConstants.LOG_MAIL + SyslogConstants.INFO_SEVERITY) + ">";
303     assertTrue(buf.toString().startsWith(expected));
304   }
305 
306   @Test
307   public void testMDCConverter() throws Exception {
308     MDC.clear();
309     MDC.put("someKey", "someValue");
310     MDCConverter converter = new MDCConverter();
311     this.optionList.clear();
312     this.optionList.add("someKey");
313     converter.setOptionList(optionList);
314     converter.start();
315 
316     LoggingEvent event = makeLoggingEvent(null);
317 
318     String result = converter.convert(event);
319     assertEquals("someValue", result);
320   }
321 
322   @Test
323   public void contextNameConverter() {
324     ClassicConverter converter = new ContextNameConverter();
325     converter.setContext(lc);
326     lc.setName("aValue");
327     LoggingEvent event = makeLoggingEvent(null);
328 
329     String result = converter.convert(event);
330     assertEquals("aValue", result);
331   }
332 }