001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.spi;
015
016import static ch.qos.logback.classic.util.TestHelper.addSuppressed;
017import static org.junit.Assert.assertEquals;
018import static org.junit.Assume.assumeTrue;
019
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.lang.reflect.InvocationTargetException;
023
024import org.junit.After;
025import org.junit.Before;
026import org.junit.Test;
027
028import ch.qos.logback.classic.util.TestHelper;
029
030public class ThrowableProxyTest {
031
032    StringWriter sw = new StringWriter();
033    PrintWriter pw = new PrintWriter(sw);
034
035    @Before
036    public void setUp() throws Exception {
037    }
038
039    @After
040    public void tearDown() throws Exception {
041    }
042
043    public void verify(Throwable t) {
044        t.printStackTrace(pw);
045
046        IThrowableProxy tp = new ThrowableProxy(t);
047
048        String result = ThrowableProxyUtil.asString(tp);
049        result = result.replace("common frames omitted", "more");
050
051        String expected = sw.toString();
052
053        System.out.println("========expected");
054        System.out.println(expected);
055
056        System.out.println("========result");
057        System.out.println(result);
058
059        assertEquals(expected, result);
060    }
061
062    @Test
063    public void smoke() {
064        Exception e = new Exception("smoke");
065        verify(e);
066    }
067
068    @Test
069    public void nested() {
070        Exception w = null;
071        try {
072            someMethod();
073        } catch (Exception e) {
074            w = new Exception("wrapping", e);
075        }
076        verify(w);
077    }
078
079    @Test
080    public void suppressed() throws InvocationTargetException, IllegalAccessException {
081        assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
082                                                      // sense.
083        Exception ex = null;
084        try {
085            someMethod();
086        } catch (Exception e) {
087            Exception fooException = new Exception("Foo");
088            Exception barException = new Exception("Bar");
089            addSuppressed(e, fooException);
090            addSuppressed(e, barException);
091            ex = e;
092        }
093        verify(ex);
094    }
095
096    @Test
097    public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException {
098        assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
099                                                      // 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}