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 java.io.BufferedReader;
17  import java.io.PrintWriter;
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import ch.qos.logback.core.CoreConstants;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import ch.qos.logback.classic.Level;
30  import ch.qos.logback.classic.Logger;
31  import ch.qos.logback.classic.LoggerContext;
32  import ch.qos.logback.classic.spi.ILoggingEvent;
33  import ch.qos.logback.classic.spi.LoggingEvent;
34  import ch.qos.logback.classic.util.TestHelper;
35  
36  import static ch.qos.logback.classic.util.TestHelper.addSuppressed;
37  import static org.assertj.core.api.Assertions.assertThat;
38  import static org.junit.Assert.*;
39  import static org.junit.Assume.assumeTrue;
40  
41  public class ThrowableProxyConverterTest {
42  
43      LoggerContext lc = new LoggerContext();
44      ThrowableProxyConverter tpc = new ThrowableProxyConverter();
45      StringWriter sw = new StringWriter();
46      PrintWriter pw = new PrintWriter(sw);
47  
48      @Before
49      public void setUp() throws Exception {
50          tpc.setContext(lc);
51          tpc.start();
52      }
53  
54      @After
55      public void tearDown() throws Exception {
56      }
57  
58      private ILoggingEvent createLoggingEvent(Throwable t) {
59          return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t, null);
60      }
61  
62      @Test
63      public void suppressed() throws InvocationTargetException, IllegalAccessException {
64          assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
65                                                        // sense.
66          Exception ex = null;
67          try {
68              someMethod();
69          } catch (Exception e) {
70              Exception fooException = new Exception("Foo");
71              Exception barException = new Exception("Bar");
72              addSuppressed(e, fooException);
73              addSuppressed(e, barException);
74              ex = e;
75          }
76          verify(ex);
77      }
78  
79      @Test
80      public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException {
81          assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
82                                                        // sense.
83          Exception ex = null;
84          try {
85              someMethod();
86          } catch (Exception e) {
87              ex = new Exception("Wrapper", e);
88              Exception fooException = new Exception("Foo");
89              Exception barException = new Exception("Bar");
90              addSuppressed(ex, fooException);
91              addSuppressed(e, barException);
92          }
93          verify(ex);
94      }
95  
96      @Test
97      public void suppressedWithSuppressed() throws Exception {
98          assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
99                                                        // sense.
100         Exception ex = null;
101         try {
102             someMethod();
103         } catch (Exception e) {
104             ex = new Exception("Wrapper", e);
105             Exception fooException = new Exception("Foo");
106             Exception barException = new Exception("Bar");
107             addSuppressed(barException, fooException);
108             addSuppressed(e, barException);
109         }
110         verify(ex);
111     }
112 
113     @Test
114     public void smoke() {
115         Exception t = new Exception("smoke");
116         verify(t);
117     }
118 
119     @Test
120     public void nested() {
121         Throwable t = TestHelper.makeNestedException(1);
122         verify(t);
123     }
124 
125     @Test
126     public void withArgumentOfOne() throws Exception {
127         final Throwable t = TestHelper.makeNestedException(0);
128         t.printStackTrace(pw);
129         final ILoggingEvent le = createLoggingEvent(t);
130 
131         final List<String> optionList = Arrays.asList("1");
132         tpc.setOptionList(optionList);
133         tpc.start();
134 
135         final String result = tpc.convert(le);
136 
137         final BufferedReader reader = new BufferedReader(new StringReader(result));
138         assertTrue(reader.readLine().contains(t.getMessage()));
139         assertNotNull(reader.readLine());
140         assertNull("Unexpected line in stack trace", reader.readLine());
141     }
142 
143     @Test
144     public void withShortArgument() throws Exception {
145         final Throwable t = TestHelper.makeNestedException(0);
146         t.printStackTrace(pw);
147         final ILoggingEvent le = createLoggingEvent(t);
148 
149         final List<String> options = Arrays.asList("short");
150         tpc.setOptionList(options);
151         tpc.start();
152 
153         final String result = tpc.convert(le);
154 
155         final BufferedReader reader = new BufferedReader(new StringReader(result));
156         assertTrue(reader.readLine().contains(t.getMessage()));
157         assertNotNull(reader.readLine());
158         assertNull("Unexpected line in stack trace", reader.readLine());
159     }
160 
161     @Test
162     public void skipSelectedLine() throws Exception {
163         String nameOfContainingMethod = "skipSelectedLine";
164         // given
165         final Throwable t = TestHelper.makeNestedException(0);
166         t.printStackTrace(pw);
167         final ILoggingEvent le = createLoggingEvent(t);
168         tpc.setOptionList(Arrays.asList("full", nameOfContainingMethod));
169         tpc.start();
170 
171         // when
172         final String result = tpc.convert(le);
173 
174         // then
175         assertThat(result).doesNotContain(nameOfContainingMethod);
176         
177     }
178 
179     @Test
180     public void skipMultipleLines() throws Exception {
181         String nameOfContainingMethod = "skipMultipleLines";
182         // given
183         final Throwable t = TestHelper.makeNestedException(0);
184         t.printStackTrace(pw);
185         final ILoggingEvent le = createLoggingEvent(t);
186         tpc.setOptionList(Arrays.asList("full", nameOfContainingMethod, "junit"));
187         tpc.start();
188 
189         // when
190         final String result = tpc.convert(le);
191 
192         // then
193         assertThat(result).doesNotContain(nameOfContainingMethod).doesNotContain("junit");
194     }
195 
196     @Test
197     public void shouldLimitTotalLinesExcludingSkipped() throws Exception {
198         // given
199         final Throwable t = TestHelper.makeNestedException(0);
200         t.printStackTrace(pw);
201         final ILoggingEvent le = createLoggingEvent(t);
202         tpc.setOptionList(Arrays.asList("3", "shouldLimitTotalLinesExcludingSkipped"));
203         tpc.start();
204 
205         // when
206         final String result = tpc.convert(le);
207 
208         // then
209         String[] lines = result.split(CoreConstants.LINE_SEPARATOR);
210         assertThat(lines).hasSize(3 + 1);
211     }
212 
213     void someMethod() throws Exception {
214         throw new Exception("someMethod");
215     }
216 
217     void verify(Throwable t) {
218         t.printStackTrace(pw);
219 
220         ILoggingEvent le = createLoggingEvent(t);
221         String result = tpc.convert(le);
222         System.out.println(result);
223         result = result.replace("common frames omitted", "more");
224         assertEquals(sw.toString(), result);
225     }
226 }