1
2
3
4
5
6
7
8
9
10
11
12
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
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
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 }