1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.blackbox.joran;
16
17 import ch.qos.logback.classic.Level;
18 import ch.qos.logback.classic.Logger;
19 import ch.qos.logback.classic.LoggerContext;
20 import ch.qos.logback.classic.blackbox.BlackboxClassicTestConstants;
21 import ch.qos.logback.classic.joran.JoranConfigurator;
22 import ch.qos.logback.classic.spi.ILoggingEvent;
23 import ch.qos.logback.classic.util.LogbackMDCAdapter;
24 import ch.qos.logback.core.joran.spi.JoranException;
25 import ch.qos.logback.core.read.ListAppender;
26 import ch.qos.logback.core.testUtil.RandomUtil;
27 import ch.qos.logback.core.testUtil.StringListAppender;
28 import ch.qos.logback.core.util.StatusPrinter;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertNotNull;
33 import static org.junit.jupiter.api.Assertions.assertNull;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35
36 public class BlackboxJoranConfiguratorTest {
37 LoggerContext loggerContext = new LoggerContext();
38 LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
39 Logger logger = loggerContext.getLogger(this.getClass().getName());
40 Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
41
42 int diff = RandomUtil.getPositiveInt();
43
44 void configure(String file) throws JoranException {
45 loggerContext.setMDCAdapter(logbackMDCAdapter);
46 JoranConfigurator jc = new JoranConfigurator();
47 jc.setContext(loggerContext);
48 loggerContext.putProperty("diff", "" + diff);
49 jc.doConfigure(file);
50
51 }
52
53 @Test
54 public void eval() throws JoranException {
55 configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "callerData.xml");
56 String msg = "hello world";
57 logger.debug("toto");
58 logger.debug(msg);
59
60 StringListAppender<ILoggingEvent> slAppender = (StringListAppender<ILoggingEvent>) loggerContext
61 .getLogger("root").getAppender("STR_LIST");
62 assertNotNull(slAppender);
63 assertEquals(2, slAppender.strList.size());
64 assertTrue(slAppender.strList.get(0).contains(" DEBUG - toto"));
65
66 String str1 = slAppender.strList.get(1);
67 assertTrue(str1.contains("Caller+0"));
68 assertTrue(str1.contains(" DEBUG - hello world"));
69 }
70
71 @Test
72 public void testEvaluatorFilter() throws JoranException {
73 configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "evaluatorFilter.xml");
74
75
76
77 logger.warn("hello");
78 logger.error("to be ignored");
79
80 ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) root.getAppender("LIST");
81
82 assertNotNull(listAppender);
83 assertEquals(1, listAppender.list.size());
84 ILoggingEvent back = listAppender.list.get(0);
85 assertEquals(Level.WARN, back.getLevel());
86 assertEquals("hello", back.getMessage());
87 }
88
89 @Test
90 public void testEvaluatorFilterWithImports() throws JoranException {
91 configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "evaluatorFilterWithImports.xml");
92
93
94
95 logger.warn("hello");
96 logger.error("to be ignored");
97
98 ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) root.getAppender("LIST");
99
100 assertNotNull(listAppender);
101 assertEquals(1, listAppender.list.size());
102 ILoggingEvent back = listAppender.list.get(0);
103 assertEquals(Level.WARN, back.getLevel());
104 assertEquals("hello", back.getMessage());
105 }
106
107 @Test
108 public void conditional1673() throws JoranException {
109 loggerContext.putProperty("EXTRA", "true");
110 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "issues/logback_1673.xml";
111 configure(configFileAsStr);
112 }
113
114 @Test
115 public void conditional1673bisWithActiveThen() throws JoranException {
116 loggerContext.putProperty("EXTRA", "true");
117 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "issues/logback_1673bis.xml";
118 configure(configFileAsStr);
119 Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
120 ListAppender<ILoggingEvent> listThen = (ListAppender<ILoggingEvent>) root.getAppender("LIST_THEN");
121 assertNotNull(listThen);
122
123 ListAppender<ILoggingEvent> listElse = (ListAppender<ILoggingEvent>) root.getAppender("LIST_ELSE");
124 assertNull(listElse);
125 }
126
127 @Test
128 public void conditional1673bisWithActiveElse() throws JoranException {
129 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "issues/logback_1673bis.xml";
130 configure(configFileAsStr);
131 Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
132 ListAppender<ILoggingEvent> listThen = (ListAppender<ILoggingEvent>) root.getAppender("LIST_THEN");
133 assertNull(listThen);
134
135 ListAppender<ILoggingEvent> listElse = (ListAppender<ILoggingEvent>) root.getAppender("LIST_ELSE");
136 assertNotNull(listElse);
137 }
138
139 @Test
140 public void nestedIf() throws JoranException {
141 loggerContext.putProperty("EXTRA", "true");
142 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "issues/logback_1678.xml";
143 configure(configFileAsStr);
144 StatusPrinter.print(loggerContext);
145
146 }
147
148 }