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.spi;
15  
16  import static ch.qos.logback.classic.util.TestHelper.addSuppressed;
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assume.assumeTrue;
19  
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.lang.reflect.InvocationTargetException;
23  
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.classic.util.TestHelper;
29  
30  public class ThrowableProxyTest {
31  
32      StringWriter sw = new StringWriter();
33      PrintWriter pw = new PrintWriter(sw);
34  
35      @Before
36      public void setUp() throws Exception {
37      }
38  
39      @After
40      public void tearDown() throws Exception {
41      }
42  
43      public void verify(Throwable t) {
44          t.printStackTrace(pw);
45  
46          IThrowableProxy tp = new ThrowableProxy(t);
47  
48          String result = ThrowableProxyUtil.asString(tp);
49          result = result.replace("common frames omitted", "more");
50  
51          String expected = sw.toString();
52  
53          System.out.println("========expected");
54          System.out.println(expected);
55  
56          System.out.println("========result");
57          System.out.println(result);
58  
59          assertEquals(expected, result);
60      }
61  
62      @Test
63      public void smoke() {
64          Exception e = new Exception("smoke");
65          verify(e);
66      }
67  
68      @Test
69      public void nested() {
70          Exception w = null;
71          try {
72              someMethod();
73          } catch (Exception e) {
74              w = new Exception("wrapping", e);
75          }
76          verify(w);
77      }
78  
79      @Test
80      public void suppressed() 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              Exception fooException = new Exception("Foo");
88              Exception barException = new Exception("Bar");
89              addSuppressed(e, fooException);
90              addSuppressed(e, barException);
91              ex = e;
92          }
93          verify(ex);
94      }
95  
96      @Test
97      public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException {
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(ex, fooException);
108             addSuppressed(e, barException);
109         }
110         verify(ex);
111     }
112 
113     @Test
114     public void suppressedWithSuppressed() throws Exception {
115         assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
116                                                       // sense.
117         Exception ex = null;
118         try {
119             someMethod();
120         } catch (Exception e) {
121             ex = new Exception("Wrapper", e);
122             Exception fooException = new Exception("Foo");
123             Exception barException = new Exception("Bar");
124             addSuppressed(barException, fooException);
125             addSuppressed(e, barException);
126         }
127         verify(ex);
128     }
129 
130     // see also http://jira.qos.ch/browse/LBCLASSIC-216
131     @Test
132     public void nullSTE() {
133         Throwable t = new Exception("someMethodWithNullException") {
134             private static final long serialVersionUID = 1L;
135 
136             @Override
137             public StackTraceElement[] getStackTrace() {
138                 return null;
139             }
140         };
141         // we can't test output as Throwable.printStackTrace method uses
142         // the private getOurStackTrace method instead of getStackTrace
143 
144         // tests ThrowableProxyUtil.steArrayToStepArray
145         new ThrowableProxy(t);
146 
147         // tests ThrowableProxyUtil.findNumberOfCommonFrames
148         Exception top = new Exception("top", t);
149         new ThrowableProxy(top);
150     }
151 
152     @Test
153     public void multiNested() {
154         Exception w = null;
155         try {
156             someOtherMethod();
157         } catch (Exception e) {
158             w = new Exception("wrapping", e);
159         }
160         verify(w);
161     }
162 
163     void someMethod() throws Exception {
164         throw new Exception("someMethod");
165     }
166 
167     void someMethodWithNullException() throws Exception {
168         throw new Exception("someMethodWithNullException") {
169             private static final long serialVersionUID = -2419053636101615373L;
170 
171             @Override
172             public StackTraceElement[] getStackTrace() {
173                 return null;
174             }
175         };
176     }
177 
178     void someOtherMethod() throws Exception {
179         try {
180             someMethod();
181         } catch (Exception e) {
182             throw new Exception("someOtherMethod", e);
183         }
184     }
185 }