View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic;
15  
16  import ch.qos.logback.classic.net.testObjectBuilders.LoggingEventBuilderInContext;
17  import ch.qos.logback.classic.spi.ILoggingEvent;
18  import ch.qos.logback.core.UnsynchronizedAppenderBase;
19  import ch.qos.logback.core.read.ListAppender;
20  import ch.qos.logback.core.status.OnConsoleStatusListener;
21  import ch.qos.logback.core.testUtil.RandomUtil;
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.slf4j.MDC;
25  
26  import static org.junit.Assert.*;
27  
28  /**
29   * @author Ceki Gülcü
30   * @author Torsten Juergeleit
31   */
32  public class AsyncAppenderTest {
33  
34      String thisClassName = this.getClass().getName();
35      LoggerContext context = new LoggerContext();
36      AsyncAppender asyncAppender = new AsyncAppender();
37      ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
38      OnConsoleStatusListener onConsoleStatusListener = new OnConsoleStatusListener();
39      LoggingEventBuilderInContext builder = new LoggingEventBuilderInContext(context, thisClassName, UnsynchronizedAppenderBase.class.getName());
40      int diff = RandomUtil.getPositiveInt();
41  
42      @Before
43      public void setUp() {
44          onConsoleStatusListener.setContext(context);
45          context.getStatusManager().add(onConsoleStatusListener);
46          onConsoleStatusListener.start();
47  
48          asyncAppender.setContext(context);
49          listAppender.setContext(context);
50          listAppender.setName("list");
51          listAppender.start();
52      }
53  
54      @Test
55      public void eventWasPreparedForDeferredProcessing() {
56          asyncAppender.addAppender(listAppender);
57          asyncAppender.start();
58  
59          String k = "k" + diff;
60          MDC.put(k, "v");
61          asyncAppender.doAppend(builder.build(diff));
62          MDC.clear();
63  
64          asyncAppender.stop();
65          assertFalse(asyncAppender.isStarted());
66  
67          // check the event
68          assertEquals(1, listAppender.list.size());
69          ILoggingEvent e = listAppender.list.get(0);
70  
71          // check that MDC values were correctly retained
72          assertEquals("v", e.getMDCPropertyMap().get(k));
73          assertFalse(e.hasCallerData());
74      }
75  
76      @Test
77      public void settingIncludeCallerDataPropertyCausedCallerDataToBeIncluded() {
78          asyncAppender.addAppender(listAppender);
79          asyncAppender.setIncludeCallerData(true);
80          asyncAppender.start();
81  
82          asyncAppender.doAppend(builder.build(diff));
83          asyncAppender.stop();
84  
85          // check the event
86          assertEquals(1, listAppender.list.size());
87          ILoggingEvent e = listAppender.list.get(0);
88          assertTrue(e.hasCallerData());
89          StackTraceElement ste = e.getCallerData()[0];
90          assertEquals(thisClassName, ste.getClassName());
91      }
92  }