Skip to content

Commit

Permalink
move levelChangePropagator related tests to logback-classic-blackbox
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 13, 2024
1 parent f7d5cf8 commit c8c46e3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}


}
Original file line number Diff line number Diff line change
@@ -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) {
}
}
2 changes: 2 additions & 0 deletions logback-classic-blackbox/src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void assertIsErrorFree() {
}

public void assertIsErrorFree(long treshhold) {
Assertions.assertTrue(isErrorFree(0));
Assertions.assertTrue(isErrorFree(treshhold));
}

public void assertIsWarningOrErrorFree() {
Expand Down

0 comments on commit c8c46e3

Please sign in to comment.