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; 015 016import ch.qos.logback.classic.net.testObjectBuilders.LoggingEventBuilderInContext; 017import ch.qos.logback.classic.spi.ILoggingEvent; 018import ch.qos.logback.core.UnsynchronizedAppenderBase; 019import ch.qos.logback.core.read.ListAppender; 020import ch.qos.logback.core.status.OnConsoleStatusListener; 021import ch.qos.logback.core.testUtil.RandomUtil; 022import org.junit.Before; 023import org.junit.Test; 024import org.slf4j.MDC; 025 026import static org.junit.Assert.*; 027 028/** 029 * @author Ceki Gülcü 030 * @author Torsten Juergeleit 031 */ 032public class AsyncAppenderTest { 033 034 String thisClassName = this.getClass().getName(); 035 LoggerContext context = new LoggerContext(); 036 AsyncAppender asyncAppender = new AsyncAppender(); 037 ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>(); 038 OnConsoleStatusListener onConsoleStatusListener = new OnConsoleStatusListener(); 039 LoggingEventBuilderInContext builder = new LoggingEventBuilderInContext(context, thisClassName, UnsynchronizedAppenderBase.class.getName()); 040 int diff = RandomUtil.getPositiveInt(); 041 042 @Before 043 public void setUp() { 044 onConsoleStatusListener.setContext(context); 045 context.getStatusManager().add(onConsoleStatusListener); 046 onConsoleStatusListener.start(); 047 048 asyncAppender.setContext(context); 049 listAppender.setContext(context); 050 listAppender.setName("list"); 051 listAppender.start(); 052 } 053 054 @Test 055 public void eventWasPreparedForDeferredProcessing() { 056 asyncAppender.addAppender(listAppender); 057 asyncAppender.start(); 058 059 String k = "k" + diff; 060 MDC.put(k, "v"); 061 asyncAppender.doAppend(builder.build(diff)); 062 MDC.clear(); 063 064 asyncAppender.stop(); 065 assertFalse(asyncAppender.isStarted()); 066 067 // check the event 068 assertEquals(1, listAppender.list.size()); 069 ILoggingEvent e = listAppender.list.get(0); 070 071 // check that MDC values were correctly retained 072 assertEquals("v", e.getMDCPropertyMap().get(k)); 073 assertFalse(e.hasCallerData()); 074 } 075 076 @Test 077 public void settingIncludeCallerDataPropertyCausedCallerDataToBeIncluded() { 078 asyncAppender.addAppender(listAppender); 079 asyncAppender.setIncludeCallerData(true); 080 asyncAppender.start(); 081 082 asyncAppender.doAppend(builder.build(diff)); 083 asyncAppender.stop(); 084 085 // check the event 086 assertEquals(1, listAppender.list.size()); 087 ILoggingEvent e = listAppender.list.get(0); 088 assertTrue(e.hasCallerData()); 089 StackTraceElement ste = e.getCallerData()[0]; 090 assertEquals(thisClassName, ste.getClassName()); 091 } 092}