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.jupiter.api.Assertions.assertEquals;
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.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.status.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      @BeforeEach
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
58                  .getLogger(Logger.ROOT_LOGGER_NAME);
59          FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
60          assertTrue(fileAppender.isStarted());
61          assertTrue(fileAppender.getEncoder() instanceof LayoutWrappingEncoder);
62      }
63  
64      @Test
65      public void immediateFlushInEncoder_TRUE() throws JoranException {
66          immediateFlushInEncoder(true);
67      }
68  
69      @Test
70      public void immediateFlushInEncoder_FALSE() throws JoranException {
71          immediateFlushInEncoder(false);
72      }
73  
74      public void immediateFlushInEncoder(Boolean immediateFlush) throws JoranException {
75          loggerContext.putProperty("immediateFlush", immediateFlush.toString());
76          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/immediateFlushInEncoder.xml");
77          StatusPrinter.print(loggerContext);
78          StatusChecker checker = new StatusChecker(loggerContext);
79  
80          checker.assertContainsMatch(Status.WARN,
81                  "As of version 1.2.0 \"immediateFlush\" property should be set within the enclosing Appender.");
82          checker.assertContainsMatch(Status.WARN,
83                  "Please move \"immediateFlush\" property into the enclosing appender.");
84          checker.assertContainsMatch(Status.WARN,
85                  "Setting the \"immediateFlush\" property of the enclosing appender to " + immediateFlush);
86  
87          ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext
88                  .getLogger(Logger.ROOT_LOGGER_NAME);
89          FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
90          assertTrue(fileAppender.isStarted());
91          assertEquals(immediateFlush, Boolean.valueOf(fileAppender.isImmediateFlush()));
92      }
93  
94  }