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.encoder;
15  
16  import static ch.qos.logback.core.CoreConstants.CODES_URL;
17  import static org.junit.Assert.assertTrue;
18  import static org.junit.Assert.assertEquals;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.slf4j.Logger;
23  
24  import ch.qos.logback.classic.ClassicTestConstants;
25  import ch.qos.logback.classic.LoggerContext;
26  import ch.qos.logback.classic.joran.JoranConfigurator;
27  import ch.qos.logback.classic.spi.ILoggingEvent;
28  import ch.qos.logback.core.FileAppender;
29  import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
30  import ch.qos.logback.core.joran.spi.JoranException;
31  import ch.qos.logback.core.status.Status;
32  import ch.qos.logback.core.testUtil.StatusChecker;
33  import ch.qos.logback.core.util.StatusPrinter;
34  
35  public class LayoutInsteadOfEncoderTest {
36  
37      // TeztConstants.TEST_SRC_PREFIX + "input/joran/ignore.xml"
38      JoranConfigurator jc = new JoranConfigurator();
39      LoggerContext loggerContext = new LoggerContext();
40  
41      @Before
42      public void setUp() {
43          jc.setContext(loggerContext);
44  
45      }
46  
47      // jc.doConfigure(TeztConstants.TEST_SRC_PREFIX + "input/joran/ignore.xml");
48  
49      @Test
50      public void layoutInsteadOfEncoer() throws JoranException {
51          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/layoutInsteadOfEncoder.xml");
52          StatusPrinter.print(loggerContext);
53          StatusChecker checker = new StatusChecker(loggerContext);
54          checker.assertContainsMatch(Status.WARN, "This appender no longer admits a layout as a sub-component");
55          checker.assertContainsMatch(Status.WARN, "See also " + CODES_URL + "#layoutInsteadOfEncoder for details");
56  
57          ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
58          FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
59          assertTrue(fileAppender.isStarted());
60          assertTrue(fileAppender.getEncoder() instanceof LayoutWrappingEncoder);
61      }
62      
63      @Test
64      public void immediateFlushInEncoder_TRUE() throws JoranException {
65          immediateFlushInEncoder(true);
66      }
67      
68      @Test
69      public void immediateFlushInEncoder_FALSE() throws JoranException {
70          immediateFlushInEncoder(false);
71      }
72      
73      public void immediateFlushInEncoder(Boolean immediateFlush) throws JoranException {
74          loggerContext.putProperty("immediateFlush", immediateFlush.toString());
75          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/immediateFlushInEncoder.xml");
76          StatusPrinter.print(loggerContext);
77          StatusChecker checker = new StatusChecker(loggerContext);
78  
79          checker.assertContainsMatch(Status.WARN, "As of version 1.2.0 \"immediateFlush\" property should be set within the enclosing Appender.");
80          checker.assertContainsMatch(Status.WARN, "Please move \"immediateFlush\" property into the enclosing appender.");
81          checker.assertContainsMatch(Status.WARN, "Setting the \"immediateFlush\" property of the enclosing appender to "+immediateFlush);
82  
83          ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
84          FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
85          assertTrue(fileAppender.isStarted());
86          assertEquals(immediateFlush, Boolean.valueOf(fileAppender.isImmediateFlush())); 
87      }
88  
89    
90  }