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.core.helpers;
015
016import static org.junit.Assert.assertEquals;
017
018import java.io.PrintWriter;
019import java.io.StringWriter;
020
021import org.junit.After;
022import org.junit.Before;
023import org.junit.Test;
024
025import ch.qos.logback.core.CoreConstants;
026
027public class ThrowableToStringArrayTest {
028
029    StringWriter sw = new StringWriter();
030    PrintWriter pw = new PrintWriter(sw);
031
032    @Before
033    public void setUp() throws Exception {
034    }
035
036    @After
037    public void tearDown() throws Exception {
038    }
039
040    public void verify(Throwable t) {
041        t.printStackTrace(pw);
042
043        String[] sa = ThrowableToStringArray.convert(t);
044        StringBuilder sb = new StringBuilder();
045        for (String tdp : sa) {
046            sb.append(tdp);
047            sb.append(CoreConstants.LINE_SEPARATOR);
048        }
049        String expected = sw.toString();
050        String result = sb.toString().replace("common frames omitted", "more");
051        assertEquals(expected, result);
052    }
053
054    @Test
055    public void smoke() {
056        Exception e = new Exception("smoke");
057        verify(e);
058    }
059
060    @Test
061    public void nested() {
062        Exception w = null;
063        try {
064            someMethod();
065        } catch (Exception e) {
066            w = new Exception("wrapping", e);
067        }
068        verify(w);
069    }
070
071    @Test
072    public void multiNested() {
073        Exception w = null;
074        try {
075            someOtherMethod();
076        } catch (Exception e) {
077            w = new Exception("wrapping", e);
078        }
079        verify(w);
080    }
081
082    void someMethod() throws Exception {
083        throw new Exception("someMethod");
084    }
085
086    void someOtherMethod() throws Exception {
087        try {
088            someMethod();
089        } catch (Exception e) {
090            throw new Exception("someOtherMethod", e);
091        }
092    }
093}