View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.PatternLayout;
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.classic.spi.LoggingEvent;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import java.io.PrintWriter;
27  import java.io.StringWriter;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  import static ch.qos.logback.classic.util.TestHelper.makeNestedException;
32  import static ch.qos.logback.classic.util.TestHelper.positionOf;
33  import static org.assertj.core.api.Assertions.assertThat;
34  
35  /**
36   * @author Tomasz Nurkiewicz
37   * @since 2010-08-15, 18:34:21
38   */
39  public class RootCauseFirstThrowableProxyConverterTest {
40  
41      private LoggerContext context = new LoggerContext();
42      private ThrowableProxyConverter converter = new RootCauseFirstThrowableProxyConverter();
43      private StringWriter stringWriter = new StringWriter();
44      private PrintWriter printWriter = new PrintWriter(stringWriter);
45  
46      @Before
47      public void setUp() throws Exception {
48          converter.setContext(context);
49          converter.start();
50      }
51  
52      private ILoggingEvent createLoggingEvent(Throwable t) {
53          return new LoggingEvent(this.getClass().getName(), context.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t, null);
54      }
55  
56      @Test
57      public void integration() {
58          // given
59          context.setPackagingDataEnabled(true);
60          PatternLayout pl = new PatternLayout();
61          pl.setContext(context);
62          pl.setPattern("%m%rEx%n");
63          pl.start();
64  
65          // when
66          ILoggingEvent e = createLoggingEvent(new Exception("x"));
67          String result = pl.doLayout(e);
68  
69          // then
70          // make sure that at least some package data was output
71          Pattern p = Pattern.compile("\\s*at .*?\\[.*?\\]");
72          Matcher m = p.matcher(result);
73          int i = 0;
74          while (m.find()) {
75              i++;
76          }
77          assertThat(i).isGreaterThan(5);
78      }
79  
80      @Test
81      public void smoke() {
82          // given
83          Exception exception = new Exception("smoke");
84          exception.printStackTrace(printWriter);
85  
86          // when
87          ILoggingEvent le = createLoggingEvent(exception);
88          String result = converter.convert(le);
89  
90          // then
91          result = result.replace("common frames omitted", "more");
92          result = result.replaceAll(" ~?\\[.*\\]", "");
93          assertThat(result).isEqualTo(stringWriter.toString());
94      }
95  
96      @Test
97      public void nested() {
98          // given
99          Throwable nestedException = makeNestedException(2);
100         nestedException.printStackTrace(printWriter);
101 
102         // when
103         ILoggingEvent le = createLoggingEvent(nestedException);
104         String result = converter.convert(le);
105 
106         // then
107         assertThat(result).startsWith("java.lang.Exception: nesting level=0");
108         assertThat(positionOf("nesting level=0").in(result)).isLessThan(positionOf("nesting level =1").in(result));
109         assertThat(positionOf("nesting level =1").in(result)).isLessThan(positionOf("nesting level =2").in(result));
110     }
111 
112 }