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