From c8c46e35e3ab9917eba0a3faae419799d84b0fe3 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Tue, 13 Aug 2024 18:53:52 +0200 Subject: [PATCH] move levelChangePropagator related tests to logback-classic-blackbox Signed-off-by: Ceki Gulcu --- .../joran/jul/levelChangePropagator0.xml | 0 .../joran/jul/levelChangePropagator1.xml | 0 .../joran/BlackboxJoranConfiguratorTest.java | 45 ++++++++++++- .../classic/blackbox/joran/StatusChecker.java | 66 +++++++++++++++++++ .../src/test/java/module-info.java | 2 + .../classic/joran/JoranConfiguratorTest.java | 54 +-------------- .../core/status/testUtil/StatusChecker.java | 2 +- 7 files changed, 115 insertions(+), 54 deletions(-) rename {logback-classic/src/test/input => logback-classic-blackbox/src/test/blackboxInput}/joran/jul/levelChangePropagator0.xml (100%) rename {logback-classic/src/test/input => logback-classic-blackbox/src/test/blackboxInput}/joran/jul/levelChangePropagator1.xml (100%) create mode 100644 logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/StatusChecker.java diff --git a/logback-classic/src/test/input/joran/jul/levelChangePropagator0.xml b/logback-classic-blackbox/src/test/blackboxInput/joran/jul/levelChangePropagator0.xml similarity index 100% rename from logback-classic/src/test/input/joran/jul/levelChangePropagator0.xml rename to logback-classic-blackbox/src/test/blackboxInput/joran/jul/levelChangePropagator0.xml diff --git a/logback-classic/src/test/input/joran/jul/levelChangePropagator1.xml b/logback-classic-blackbox/src/test/blackboxInput/joran/jul/levelChangePropagator1.xml similarity index 100% rename from logback-classic/src/test/input/joran/jul/levelChangePropagator1.xml rename to logback-classic-blackbox/src/test/blackboxInput/joran/jul/levelChangePropagator1.xml diff --git a/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/BlackboxJoranConfiguratorTest.java b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/BlackboxJoranConfiguratorTest.java index 80943c4918..54e0ed1d4c 100644 --- a/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/BlackboxJoranConfiguratorTest.java +++ b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/BlackboxJoranConfiguratorTest.java @@ -19,6 +19,7 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.blackbox.BlackboxClassicTestConstants; import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.classic.jul.JULHelper; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.util.LogbackMDCAdapter; import ch.qos.logback.core.joran.spi.JoranException; @@ -28,6 +29,8 @@ import ch.qos.logback.core.util.StatusPrinter; import org.junit.jupiter.api.Test; +import java.io.IOException; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -38,7 +41,7 @@ public class BlackboxJoranConfiguratorTest { LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter(); Logger logger = loggerContext.getLogger(this.getClass().getName()); Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); - //StatusChecker checker = new StatusChecker(loggerContext); + StatusChecker checker = new StatusChecker(loggerContext); int diff = RandomUtil.getPositiveInt(); void configure(String file) throws JoranException { @@ -145,4 +148,44 @@ public void nestedIf() throws JoranException { } + @Test + public void levelChangePropagator0() throws JoranException, IOException, InterruptedException { + String loggerName = "changePropagator0" + diff; + java.util.logging.Logger.getLogger(loggerName).setLevel(java.util.logging.Level.INFO); + String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator0.xml"; + configure(configFileAsStr); + + checker.assertIsErrorFree(); + verifyJULLevel(loggerName, null); + verifyJULLevel("a.b.c." + diff, Level.WARN); + verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); + } + + @Test + public void levelChangePropagator1() throws JoranException, IOException, InterruptedException { + String loggerName = "changePropagator1" + diff; + java.util.logging.Logger logger1 = java.util.logging.Logger.getLogger(loggerName); + logger1.setLevel(java.util.logging.Level.INFO); + verifyJULLevel(loggerName, Level.INFO); + String configFileAsStr = BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator1.xml"; + configure(configFileAsStr); + + checker.assertIsErrorFree(); + verifyJULLevel(loggerName, Level.INFO); // + verifyJULLevel("a.b.c." + diff, Level.WARN); + verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); + } + + void verifyJULLevel(String loggerName, Level expectedLevel) { + java.util.logging.Logger julLogger = JULHelper.asJULLogger(loggerName); + java.util.logging.Level julLevel = julLogger.getLevel(); + + if (expectedLevel == null) { + assertNull(julLevel); + } else { + assertEquals(JULHelper.asJULLevel(expectedLevel), julLevel); + } + } + + } diff --git a/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/StatusChecker.java b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/StatusChecker.java new file mode 100644 index 0000000000..995c0317ce --- /dev/null +++ b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/joran/StatusChecker.java @@ -0,0 +1,66 @@ +/* + * 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. + */ + +package ch.qos.logback.classic.blackbox.joran; + +import ch.qos.logback.core.Context; +import ch.qos.logback.core.status.StatusManager; +import ch.qos.logback.core.status.StatusUtil; +import org.junit.jupiter.api.Assertions; + +public class StatusChecker extends StatusUtil { + + public StatusChecker(StatusManager sm) { + super(sm); + } + + public StatusChecker(Context context) { + super(context); + } + + public void assertContainsMatch(int level, String regex) { + Assertions.assertTrue(containsMatch(level, regex)); + } + + public void assertNoMatch(String regex) { + Assertions.assertFalse(containsMatch(regex)); + } + + public void assertContainsMatch(String regex) { + Assertions.assertTrue(containsMatch(regex)); + } + + public void assertContainsException(Class scanExceptionClass) { + Assertions.assertTrue(containsException(scanExceptionClass)); + } + + public void assertContainsException(Class scanExceptionClass, String msg) { + Assertions.assertTrue(containsException(scanExceptionClass, msg)); + } + + public void assertIsErrorFree() { + Assertions.assertTrue(isErrorFree(0)); + } + + public void assertIsErrorFree(long treshhold) { + Assertions.assertTrue(isErrorFree(treshhold)); + } + + public void assertIsWarningOrErrorFree() { + Assertions.assertTrue(isWarningOrErrorFree(0)); + } + + public void assertErrorCount(int i) { + } +} diff --git a/logback-classic-blackbox/src/test/java/module-info.java b/logback-classic-blackbox/src/test/java/module-info.java index e243ed3384..afde459b40 100644 --- a/logback-classic-blackbox/src/test/java/module-info.java +++ b/logback-classic-blackbox/src/test/java/module-info.java @@ -12,6 +12,8 @@ requires org.junit.jupiter.engine; requires org.slf4j; + requires java.logging; + exports ch.qos.logback.classic.blackbox.boolex; exports ch.qos.logback.classic.blackbox.joran; diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java index 2e497d0e99..ed9b3e299b 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java @@ -13,14 +13,8 @@ */ package ch.qos.logback.classic.joran; -import ch.qos.logback.classic.AsyncAppender; -import ch.qos.logback.classic.ClassicConstants; -import ch.qos.logback.classic.ClassicTestConstants; -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.*; import ch.qos.logback.classic.joran.serializedModel.HardenedModelInputStream; -import ch.qos.logback.classic.jul.JULHelper; import ch.qos.logback.classic.model.ConfigurationModel; import ch.qos.logback.classic.model.LoggerModel; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -42,7 +36,6 @@ import ch.qos.logback.core.spi.ScanException; import ch.qos.logback.core.status.Status; import ch.qos.logback.core.status.testUtil.StatusChecker; -import ch.qos.logback.core.testUtil.CoreTestConstants; import ch.qos.logback.core.testUtil.RandomUtil; import ch.qos.logback.core.testUtil.StringListAppender; import ch.qos.logback.core.util.CachingDateFormatter; @@ -56,7 +49,6 @@ import org.slf4j.spi.MDCAdapter; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @@ -66,11 +58,7 @@ import static ch.qos.logback.core.model.processor.ImplicitModelHandler.IGNORING_UNKNOWN_PROP; import static ch.qos.logback.core.model.processor.ShutdownHookModelHandler.RENAME_WARNING; import static ch.qos.logback.core.testUtil.CoreTestConstants.OUTPUT_DIR_PREFIX; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class JoranConfiguratorTest { @@ -394,45 +382,7 @@ public void encoderCharset() throws JoranException, IOException, InterruptedExce checker.assertIsErrorFree(); } - void verifyJULLevel(String loggerName, Level expectedLevel) { - java.util.logging.Logger julLogger = JULHelper.asJULLogger(loggerName); - java.util.logging.Level julLevel = julLogger.getLevel(); - if (expectedLevel == null) { - assertNull(julLevel); - } else { - assertEquals(JULHelper.asJULLevel(expectedLevel), julLevel); - } - - } - - @Test - public void levelChangePropagator0() throws JoranException, IOException, InterruptedException { - String loggerName = "changePropagator0" + diff; - java.util.logging.Logger.getLogger(loggerName).setLevel(java.util.logging.Level.INFO); - String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator0.xml"; - configure(configFileAsStr); - - checker.assertIsErrorFree(); - verifyJULLevel(loggerName, null); - verifyJULLevel("a.b.c." + diff, Level.WARN); - verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); - } - - @Test - public void levelChangePropagator1() throws JoranException, IOException, InterruptedException { - String loggerName = "changePropagator1" + diff; - java.util.logging.Logger logger1 = java.util.logging.Logger.getLogger(loggerName); - logger1.setLevel(java.util.logging.Level.INFO); - verifyJULLevel(loggerName, Level.INFO); - String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator1.xml"; - configure(configFileAsStr); - - checker.assertIsErrorFree(); - verifyJULLevel(loggerName, Level.INFO); // - verifyJULLevel("a.b.c." + diff, Level.WARN); - verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); - } @Disabled // because slow @Test diff --git a/logback-core/src/test/java/ch/qos/logback/core/status/testUtil/StatusChecker.java b/logback-core/src/test/java/ch/qos/logback/core/status/testUtil/StatusChecker.java index 7535ea7e1d..4e2d68d05e 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/status/testUtil/StatusChecker.java +++ b/logback-core/src/test/java/ch/qos/logback/core/status/testUtil/StatusChecker.java @@ -56,7 +56,7 @@ public void assertIsErrorFree() { } public void assertIsErrorFree(long treshhold) { - Assertions.assertTrue(isErrorFree(0)); + Assertions.assertTrue(isErrorFree(treshhold)); } public void assertIsWarningOrErrorFree() {