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 static org.junit.Assert.assertEquals;
17  import static org.assertj.core.api.Assertions.assertThat;
18  
19  import java.io.PrintWriter;
20  import java.io.StringWriter;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.classic.Level;
29  import ch.qos.logback.classic.Logger;
30  import ch.qos.logback.classic.LoggerContext;
31  import ch.qos.logback.classic.PatternLayout;
32  import ch.qos.logback.classic.spi.ILoggingEvent;
33  import ch.qos.logback.classic.spi.LoggingEvent;
34  
35  public class ExtendedThrowableProxyConverterTest {
36  
37      LoggerContext lc = new LoggerContext();
38      ExtendedThrowableProxyConverter etpc = new ExtendedThrowableProxyConverter();
39      StringWriter sw = new StringWriter();
40      PrintWriter pw = new PrintWriter(sw);
41  
42      @Before
43      public void setUp() throws Exception {
44          lc.setPackagingDataEnabled(true);
45          etpc.setContext(lc);
46          etpc.start();
47      }
48  
49      @After
50      public void tearDown() throws Exception {
51      }
52  
53      private ILoggingEvent createLoggingEvent(Throwable t) {
54          return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t, null);
55      }
56  
57      @Test
58      public void integration() {
59          PatternLayout pl = new PatternLayout();
60          pl.setContext(lc);
61          pl.setPattern("%m%n%xEx");
62          pl.start();
63          ILoggingEvent e = createLoggingEvent(new Exception("x"));
64          String res = pl.doLayout(e);
65  
66          // make sure that at least some package data was output
67          Pattern p = Pattern.compile("\\s*at .*?\\[.*?\\]");
68          Matcher m = p.matcher(res);
69          int i = 0;
70          while (m.find()) {
71              i++;
72          }
73          assertThat(i).isGreaterThan(5);
74      }
75  
76      @Test
77      public void smoke() {
78          Exception t = new Exception("smoke");
79          verify(t);
80      }
81  
82      @Test
83      public void nested() {
84          Throwable t = makeNestedException(1);
85          verify(t);
86      }
87  
88      void verify(Throwable t) {
89          t.printStackTrace(pw);
90  
91          ILoggingEvent le = createLoggingEvent(t);
92          String result = etpc.convert(le);
93          result = result.replace("common frames omitted", "more");
94          result = result.replaceAll(" ~?\\[.*\\]", "");
95          assertEquals(sw.toString(), result);
96      }
97  
98      Throwable makeNestedException(int level) {
99          if (level == 0) {
100             return new Exception("nesting level=" + level);
101         }
102         Throwable cause = makeNestedException(level - 1);
103         return new Exception("nesting level =" + level, cause);
104     }
105 }