Skip to content

Commit f9c04d2

Browse files
committed
test inclusion with conditionals
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent f32ed30 commit f9c04d2

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--
2+
~ Logback: the reliable, generic, fast and flexible logging framework.
3+
~ Copyright (C) 1999-2024, 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+
15+
<configuration>
16+
17+
18+
<variable scope="system" name="JO_PREFIX" value="src/test/blackboxInput/joran"/>
19+
20+
<if condition='isDefined("JO_PREFIX")'>
21+
<then>
22+
<include file="${JO_PREFIX}/conditional/includedFile.xml"/>
23+
</then>
24+
</if>
25+
26+
</configuration>

logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/conditional/ConditionalTest.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import ch.qos.logback.core.testUtil.CoreTestConstants;
3131
import ch.qos.logback.core.testUtil.RandomUtil;
3232
import ch.qos.logback.core.util.StatusPrinter;
33+
import ch.qos.logback.core.util.StatusPrinter2;
3334
import org.junit.jupiter.api.AfterEach;
3435
import org.junit.jupiter.api.BeforeEach;
3536
import org.junit.jupiter.api.Test;
@@ -51,6 +52,7 @@ public class ConditionalTest {
5152
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
5253

5354
Logger logger = loggerContext.getLogger(this.getClass().getName());
55+
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
5456

5557
StatusUtil checker = new StatusUtil(loggerContext);
5658
int diff = RandomUtil.getPositiveInt();
@@ -132,7 +134,7 @@ public void conditionalInclusionWithExistingFile() throws JoranException, IOExce
132134
String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
133135
+ "conditional/conditionalIncludeExistingFile.xml";
134136
configure(configFileAsStr);
135-
StatusPrinter.print(loggerContext);
137+
//statusPrinter2.print(loggerContext);
136138

137139
ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON");
138140
assertNotNull(consoleAppender);
@@ -152,6 +154,22 @@ public void conditionalInclusionWithInexistentFile() throws JoranException, IOEx
152154
assertTrue(checker.isErrorFree(0));
153155
}
154156

157+
// https://jira.qos.ch/browse/LOGBACK-1732
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+
155173
private AppenderTracker<ILoggingEvent> getAppenderTracker() {
156174
SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
157175
return ha.getAppenderTracker();
@@ -161,7 +179,7 @@ private AppenderTracker<ILoggingEvent> getAppenderTracker() {
161179
@Test
162180
public void nestedWithinIfThen() throws JoranException {
163181
configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/siftNestedWithinIfThen.xml");
164-
StatusPrinter.print(loggerContext);
182+
//statusPrinter2.print(loggerContext);
165183
String msg = "nestedWithinIfThen";
166184
logger.debug(msg);
167185
Appender<ILoggingEvent> appender = getAppenderTracker().find("ifThenDefault");
@@ -172,4 +190,6 @@ public void nestedWithinIfThen() throws JoranException {
172190
assertEquals(msg, eventList.get(0).getMessage());
173191
checker.isWarningOrErrorFree(0);
174192
}
193+
194+
175195
}

logback-classic/src/test/input/joran/include/includeWithVariableAndConditional.xml

Whitespace-only changes.

logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import ch.qos.logback.classic.turbo.DebugUsersTurboFilter;
2828
import ch.qos.logback.classic.turbo.NOPTurboFilter;
2929
import ch.qos.logback.classic.turbo.TurboFilter;
30+
import ch.qos.logback.classic.util.LogbackMDCAdapter;
3031
import ch.qos.logback.core.ConsoleAppender;
3132
import ch.qos.logback.core.CoreConstants;
3233
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
@@ -51,6 +52,7 @@
5152
import org.junit.jupiter.api.Test;
5253
import org.slf4j.MDC;
5354
import org.slf4j.event.KeyValuePair;
55+
import org.slf4j.spi.MDCAdapter;
5456

5557
import java.io.FileInputStream;
5658
import java.io.FileNotFoundException;
@@ -72,13 +74,15 @@
7274
public class JoranConfiguratorTest {
7375

7476
LoggerContext loggerContext = new LoggerContext();
77+
MDCAdapter mdcAdapter = new LogbackMDCAdapter();
7578
Logger logger = loggerContext.getLogger(this.getClass().getName());
7679
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
7780
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
7881
StatusChecker checker = new StatusChecker(loggerContext);
7982
int diff = RandomUtil.getPositiveInt();
8083

8184
void configure(String file) throws JoranException {
85+
loggerContext.setMDCAdapter(mdcAdapter);
8286
JoranConfigurator jc = new JoranConfigurator();
8387
jc.setContext(loggerContext);
8488
loggerContext.putProperty("diff", "" + diff);
@@ -673,15 +677,15 @@ public void kvp() throws JoranException {
673677
}
674678

675679

680+
// See LOGBACK-1746
676681
@Test
677682
public void inclusionWithVariables() throws JoranException {
678683
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "include/topLevel0.xml");
679-
680684
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
681-
statusPrinter2.print(loggerContext);
685+
//statusPrinter2.print(loggerContext);
682686
assertEquals(Level.ERROR, root.getLevel());
683687
}
684-
688+
685689
// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=46697
686690
@Test
687691
public void ossFuzz_46697() throws JoranException {

0 commit comments

Comments
 (0)