1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.blackbox.joran.conditional;
15
16 import ch.qos.logback.classic.Logger;
17 import ch.qos.logback.classic.LoggerContext;
18 import ch.qos.logback.classic.blackbox.BlackboxClassicTestConstants;
19 import ch.qos.logback.classic.joran.JoranConfigurator;
20 import ch.qos.logback.classic.sift.SiftingAppender;
21 import ch.qos.logback.classic.spi.ILoggingEvent;
22 import ch.qos.logback.classic.util.LogbackMDCAdapter;
23 import ch.qos.logback.core.Appender;
24 import ch.qos.logback.core.ConsoleAppender;
25 import ch.qos.logback.core.FileAppender;
26 import ch.qos.logback.core.joran.spi.JoranException;
27 import ch.qos.logback.core.read.ListAppender;
28 import ch.qos.logback.core.sift.AppenderTracker;
29 import ch.qos.logback.core.status.StatusUtil;
30 import ch.qos.logback.core.testUtil.CoreTestConstants;
31 import ch.qos.logback.core.testUtil.RandomUtil;
32 import ch.qos.logback.core.util.StatusPrinter;
33 import ch.qos.logback.core.util.StatusPrinter2;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37
38 import java.io.IOException;
39 import java.net.InetAddress;
40 import java.net.UnknownHostException;
41 import java.util.List;
42
43 import static org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.assertNotNull;
45 import static org.junit.jupiter.api.Assertions.assertNull;
46 import static org.junit.jupiter.api.Assertions.assertTrue;
47
48 public class ConditionalTest {
49
50 LoggerContext loggerContext = new LoggerContext();
51 LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
52 Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
53
54 Logger logger = loggerContext.getLogger(this.getClass().getName());
55 StatusPrinter2 statusPrinter2 = new StatusPrinter2();
56
57 StatusUtil checker = new StatusUtil(loggerContext);
58 int diff = RandomUtil.getPositiveInt();
59 String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
60
61 @BeforeEach
62 public void setUp() throws UnknownHostException {
63 loggerContext.setMDCAdapter(logbackMDCAdapter);
64 loggerContext.setName("c" + diff);
65 loggerContext.putProperty("randomOutputDir", randomOutputDir);
66 }
67
68 @AfterEach
69 public void tearDown() {
70 StatusPrinter.printIfErrorsOccured(loggerContext);
71 }
72
73 void configure(String file) throws JoranException {
74 JoranConfigurator jc = new JoranConfigurator();
75 jc.setContext(loggerContext);
76 jc.doConfigure(file);
77 }
78
79 @SuppressWarnings("rawtypes")
80 @Test
81 public void conditionalConsoleApp_IF_THEN_True() throws JoranException, IOException, InterruptedException {
82 InetAddress localhost = InetAddress.getLocalHost();
83 System.out.println("In conditionalConsoleApp_IF_THEN_True, canonicalHostName=\""
84 + localhost.getCanonicalHostName() + "] and hostNmae=\"" + localhost.getHostName() + "\"");
85 loggerContext.putProperty("aHost", localhost.getHostName());
86
87 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml";
88 configure(configFileAsStr);
89 FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
90 assertNotNull(fileAppender);
91
92 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
93 assertNotNull(consoleAppender);
94 assertTrue(checker.isErrorFree(0));
95 }
96
97 @SuppressWarnings("rawtypes")
98 @Test
99 public void conditionalConsoleApp_IF_THEN_False() throws JoranException, IOException, InterruptedException {
100
101 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml";
102 configure(configFileAsStr);
103 FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
104 assertNotNull(fileAppender);
105
106 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
107 assertNull(consoleAppender);
108 assertTrue(checker.isErrorFree(0));
109 }
110
111 @SuppressWarnings("rawtypes")
112 @Test
113 public void conditionalConsoleApp_IF_THEN_ELSE() throws JoranException, IOException, InterruptedException {
114
115 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp_ELSE.xml";
116 configure(configFileAsStr);
117
118 FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
119 assertNotNull(fileAppender);
120
121 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
122 assertNull(consoleAppender);
123
124 ListAppender listAppender = (ListAppender) root.getAppender("LIST");
125 assertNotNull(listAppender);
126
127
128 assertTrue(checker.isErrorFree(0));
129 }
130
131 @Test
132 public void conditionalInclusionWithExistingFile() throws JoranException, IOException, InterruptedException {
133
134 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
135 + "conditional/conditionalIncludeExistingFile.xml";
136 configure(configFileAsStr);
137
138
139 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON");
140 assertNotNull(consoleAppender);
141
142 assertTrue(checker.isErrorFree(0));
143 }
144
145 @Test
146 public void conditionalInclusionWithInexistentFile() throws JoranException, IOException, InterruptedException {
147
148 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
149 + "conditional/conditionalIncludeInexistentFile.xml";
150 configure(configFileAsStr);
151
152 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON");
153 assertNull(consoleAppender);
154 assertTrue(checker.isErrorFree(0));
155 }
156
157
158 @Test
159 public void conditionalInclusionWithVariableDefinition() throws JoranException, IOException, InterruptedException {
160
161 String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
162 + "conditional/includeWithVariableAndConditional.xml";
163 configure(configFileAsStr);
164
165 statusPrinter2.print(loggerContext);
166
167 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON");
168 assertNotNull(consoleAppender);
169 assertTrue(checker.isErrorFree(0));
170 }
171
172
173 private AppenderTracker<ILoggingEvent> getAppenderTracker() {
174 SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
175 return ha.getAppenderTracker();
176 }
177
178
179 @Test
180 public void nestedWithinIfThen() throws JoranException {
181 configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/siftNestedWithinIfThen.xml");
182
183 String msg = "nestedWithinIfThen";
184 logger.debug(msg);
185 Appender<ILoggingEvent> appender = getAppenderTracker().find("ifThenDefault");
186 assertNotNull(appender);
187 ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) appender;
188 List<ILoggingEvent> eventList = listAppender.list;
189 assertEquals(1, listAppender.list.size());
190 assertEquals(msg, eventList.get(0).getMessage());
191 checker.isWarningOrErrorFree(0);
192 }
193
194
195 }