Skip to content

Commit

Permalink
test inclusion with conditionals
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Apr 12, 2024
1 parent f32ed30 commit f9c04d2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ Logback: the reliable, generic, fast and flexible logging framework.
~ Copyright (C) 1999-2024, QOS.ch. All rights reserved.
~
~ This program and the accompanying materials are dual-licensed under
~ either the terms of the Eclipse Public License v1.0 as published by
~ the Eclipse Foundation
~
~ or (per the licensee's choosing)
~
~ under the terms of the GNU Lesser General Public License version 2.1
~ as published by the Free Software Foundation.
-->

<configuration>


<variable scope="system" name="JO_PREFIX" value="src/test/blackboxInput/joran"/>

<if condition='isDefined("JO_PREFIX")'>
<then>
<include file="${JO_PREFIX}/conditional/includedFile.xml"/>
</then>
</if>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ch.qos.logback.core.testUtil.CoreTestConstants;
import ch.qos.logback.core.testUtil.RandomUtil;
import ch.qos.logback.core.util.StatusPrinter;
import ch.qos.logback.core.util.StatusPrinter2;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -51,6 +52,7 @@ public class ConditionalTest {
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

Logger logger = loggerContext.getLogger(this.getClass().getName());
StatusPrinter2 statusPrinter2 = new StatusPrinter2();

StatusUtil checker = new StatusUtil(loggerContext);
int diff = RandomUtil.getPositiveInt();
Expand Down Expand Up @@ -132,7 +134,7 @@ public void conditionalInclusionWithExistingFile() throws JoranException, IOExce
String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
+ "conditional/conditionalIncludeExistingFile.xml";
configure(configFileAsStr);
StatusPrinter.print(loggerContext);
//statusPrinter2.print(loggerContext);

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

// https://jira.qos.ch/browse/LOGBACK-1732
@Test
public void conditionalInclusionWithVariableDefinition() throws JoranException, IOException, InterruptedException {

String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX
+ "conditional/includeWithVariableAndConditional.xml";
configure(configFileAsStr);

statusPrinter2.print(loggerContext);

ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON");
assertNotNull(consoleAppender);
assertTrue(checker.isErrorFree(0));
}


private AppenderTracker<ILoggingEvent> getAppenderTracker() {
SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
return ha.getAppenderTracker();
Expand All @@ -161,7 +179,7 @@ private AppenderTracker<ILoggingEvent> getAppenderTracker() {
@Test
public void nestedWithinIfThen() throws JoranException {
configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/siftNestedWithinIfThen.xml");
StatusPrinter.print(loggerContext);
//statusPrinter2.print(loggerContext);
String msg = "nestedWithinIfThen";
logger.debug(msg);
Appender<ILoggingEvent> appender = getAppenderTracker().find("ifThenDefault");
Expand All @@ -172,4 +190,6 @@ public void nestedWithinIfThen() throws JoranException {
assertEquals(msg, eventList.get(0).getMessage());
checker.isWarningOrErrorFree(0);
}


}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ch.qos.logback.classic.turbo.DebugUsersTurboFilter;
import ch.qos.logback.classic.turbo.NOPTurboFilter;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
Expand All @@ -51,6 +52,7 @@
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import org.slf4j.event.KeyValuePair;
import org.slf4j.spi.MDCAdapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -72,13 +74,15 @@
public class JoranConfiguratorTest {

LoggerContext loggerContext = new LoggerContext();
MDCAdapter mdcAdapter = new LogbackMDCAdapter();
Logger logger = loggerContext.getLogger(this.getClass().getName());
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
StatusChecker checker = new StatusChecker(loggerContext);
int diff = RandomUtil.getPositiveInt();

void configure(String file) throws JoranException {
loggerContext.setMDCAdapter(mdcAdapter);
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(loggerContext);
loggerContext.putProperty("diff", "" + diff);
Expand Down Expand Up @@ -673,15 +677,15 @@ public void kvp() throws JoranException {
}


// See LOGBACK-1746
@Test
public void inclusionWithVariables() throws JoranException {
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "include/topLevel0.xml");

Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
statusPrinter2.print(loggerContext);
//statusPrinter2.print(loggerContext);
assertEquals(Level.ERROR, root.getLevel());
}

// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=46697
@Test
public void ossFuzz_46697() throws JoranException {
Expand Down

0 comments on commit f9c04d2

Please sign in to comment.