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.pattern;
015
016import java.io.BufferedReader;
017import java.io.PrintWriter;
018import java.io.StringReader;
019import java.io.StringWriter;
020import java.lang.reflect.InvocationTargetException;
021import java.util.Arrays;
022import java.util.List;
023
024import ch.qos.logback.core.CoreConstants;
025import org.junit.After;
026import org.junit.Before;
027import org.junit.Test;
028
029import ch.qos.logback.classic.Level;
030import ch.qos.logback.classic.Logger;
031import ch.qos.logback.classic.LoggerContext;
032import ch.qos.logback.classic.spi.ILoggingEvent;
033import ch.qos.logback.classic.spi.LoggingEvent;
034import ch.qos.logback.classic.util.TestHelper;
035
036import static ch.qos.logback.classic.util.TestHelper.addSuppressed;
037import static org.assertj.core.api.Assertions.assertThat;
038import static org.junit.Assert.*;
039import static org.junit.Assume.assumeTrue;
040
041public class ThrowableProxyConverterTest {
042
043    LoggerContext lc = new LoggerContext();
044    ThrowableProxyConverter tpc = new ThrowableProxyConverter();
045    StringWriter sw = new StringWriter();
046    PrintWriter pw = new PrintWriter(sw);
047
048    @Before
049    public void setUp() throws Exception {
050        tpc.setContext(lc);
051        tpc.start();
052    }
053
054    @After
055    public void tearDown() throws Exception {
056    }
057
058    private ILoggingEvent createLoggingEvent(Throwable t) {
059        return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t, null);
060    }
061
062    @Test
063    public void suppressed() throws InvocationTargetException, IllegalAccessException {
064        assumeTrue(TestHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make
065                                                      // sense.
066        Exception ex = null;
067        try {
068            someMethod();
069        } catch (Exception e) {
070            Exception fooException = new Exception("Foo");
071            Exception barException = new Exception("Bar");
072            addSuppressed(e, fooException);
073            addSuppressed(e, barException);
074            ex = e;
075        }
076        verify(ex);
077    }
078
079    @Test
080    public void suppressedWithCause() 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            ex = new Exception("Wrapper", e);
088            Exception fooException = new Exception("Foo");
089            Exception barException = new Exception("Bar");
090            addSuppressed(ex, fooException);
091            addSuppressed(e, barException);
092        }
093        verify(ex);
094    }
095
096    @Test
097    public void suppressedWithSuppressed() throws Exception {
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(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}