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 ch.qos.logback.classic.Level;
017import ch.qos.logback.classic.Logger;
018import ch.qos.logback.classic.LoggerContext;
019import ch.qos.logback.classic.PatternLayout;
020import ch.qos.logback.classic.spi.ILoggingEvent;
021import ch.qos.logback.classic.spi.LoggingEvent;
022
023import org.junit.Before;
024import org.junit.Test;
025
026import java.io.PrintWriter;
027import java.io.StringWriter;
028import java.util.regex.Matcher;
029import java.util.regex.Pattern;
030
031import static ch.qos.logback.classic.util.TestHelper.makeNestedException;
032import static ch.qos.logback.classic.util.TestHelper.positionOf;
033import static org.assertj.core.api.Assertions.assertThat;
034
035/**
036 * @author Tomasz Nurkiewicz
037 * @since 2010-08-15, 18:34:21
038 */
039public class RootCauseFirstThrowableProxyConverterTest {
040
041    private LoggerContext context = new LoggerContext();
042    private ThrowableProxyConverter converter = new RootCauseFirstThrowableProxyConverter();
043    private StringWriter stringWriter = new StringWriter();
044    private PrintWriter printWriter = new PrintWriter(stringWriter);
045
046    @Before
047    public void setUp() throws Exception {
048        converter.setContext(context);
049        converter.start();
050    }
051
052    private ILoggingEvent createLoggingEvent(Throwable t) {
053        return new LoggingEvent(this.getClass().getName(), context.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t, null);
054    }
055
056    @Test
057    public void integration() {
058        // given
059        context.setPackagingDataEnabled(true);
060        PatternLayout pl = new PatternLayout();
061        pl.setContext(context);
062        pl.setPattern("%m%rEx%n");
063        pl.start();
064
065        // when
066        ILoggingEvent e = createLoggingEvent(new Exception("x"));
067        String result = pl.doLayout(e);
068
069        // then
070        // make sure that at least some package data was output
071        Pattern p = Pattern.compile("\\s*at .*?\\[.*?\\]");
072        Matcher m = p.matcher(result);
073        int i = 0;
074        while (m.find()) {
075            i++;
076        }
077        assertThat(i).isGreaterThan(5);
078    }
079
080    @Test
081    public void smoke() {
082        // given
083        Exception exception = new Exception("smoke");
084        exception.printStackTrace(printWriter);
085
086        // when
087        ILoggingEvent le = createLoggingEvent(exception);
088        String result = converter.convert(le);
089
090        // then
091        result = result.replace("common frames omitted", "more");
092        result = result.replaceAll(" ~?\\[.*\\]", "");
093        assertThat(result).isEqualTo(stringWriter.toString());
094    }
095
096    @Test
097    public void nested() {
098        // given
099        Throwable nestedException = makeNestedException(2);
100        nestedException.printStackTrace(printWriter);
101
102        // when
103        ILoggingEvent le = createLoggingEvent(nestedException);
104        String result = converter.convert(le);
105
106        // then
107        assertThat(result).startsWith("java.lang.Exception: nesting level=0");
108        assertThat(positionOf("nesting level=0").in(result)).isLessThan(positionOf("nesting level =1").in(result));
109        assertThat(positionOf("nesting level =1").in(result)).isLessThan(positionOf("nesting level =2").in(result));
110    }
111
112}