From 6e9883b0f1c945c55d370b90506325be992a0cca Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Fri, 9 Feb 2024 17:02:05 -0500 Subject: [PATCH] Change extensions to use default Logback configs The reason to do this is to provide more flexibility. Unless a custom Logback config file is provided, use the LogbackTestHelpers.resetLogback() method which attempts both logback-test.xml with logback.xml as a fallback. --- .../junit/jupiter/ResetLogbackLoggingExtension.java | 12 ++++++++---- .../test/logback/InMemoryAppenderExtension.java | 11 +++++++---- .../jupiter/ResetLogbackLoggingExtensionTest.java | 11 ++++------- .../test/logback/InMemoryAppenderExtensionTest.java | 4 +--- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtension.java b/src/main/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtension.java index 6339328..0f84257 100644 --- a/src/main/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtension.java +++ b/src/main/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtension.java @@ -1,6 +1,7 @@ package org.kiwiproject.test.junit.jupiter; -import ch.qos.logback.classic.ClassicConstants; +import static org.apache.commons.lang3.StringUtils.isBlank; + import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -61,12 +62,15 @@ public class ResetLogbackLoggingExtension implements AfterAllCallback { @Getter - @Builder.Default - private final String logbackConfigFilePath = ClassicConstants.TEST_AUTOCONFIG_FILE; + private String logbackConfigFilePath; @Override public void afterAll(ExtensionContext context) { - LogbackTestHelpers.resetLogback(logbackConfigFilePath); + if (isBlank(logbackConfigFilePath)) { + LogbackTestHelpers.resetLogback(); + } else { + LogbackTestHelpers.resetLogback(logbackConfigFilePath); + } LOG.info("Logback was reset using configuration: {}", logbackConfigFilePath); } } diff --git a/src/main/java/org/kiwiproject/test/logback/InMemoryAppenderExtension.java b/src/main/java/org/kiwiproject/test/logback/InMemoryAppenderExtension.java index 72fa3e1..0f714cc 100644 --- a/src/main/java/org/kiwiproject/test/logback/InMemoryAppenderExtension.java +++ b/src/main/java/org/kiwiproject/test/logback/InMemoryAppenderExtension.java @@ -1,9 +1,9 @@ package org.kiwiproject.test.logback; import static java.util.Objects.nonNull; +import static org.apache.commons.lang3.StringUtils.isBlank; import static org.assertj.core.api.Assertions.assertThat; -import ch.qos.logback.classic.ClassicConstants; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.Appender; @@ -33,9 +33,8 @@ public class InMemoryAppenderExtension implements BeforeEachCallback, AfterEachC private final Class loggerClass; private final String appenderName; - // Use the default Logback test configuration file as our default value. @Getter - private String logbackConfigFilePath = ClassicConstants.TEST_AUTOCONFIG_FILE; + private String logbackConfigFilePath; @Getter @Accessors(fluent = true) @@ -177,7 +176,11 @@ private Appender getAppender(Logger logbackLogger) { System.out.println("You can customize the logging configuration using #withLogbackConfigFilePath"); // Reset the Logback logging system - LogbackTestHelpers.resetLogback(logbackConfigFilePath); + if (isBlank(logbackConfigFilePath)) { + LogbackTestHelpers.resetLogback(); + } else { + LogbackTestHelpers.resetLogback(logbackConfigFilePath); + } // Try again and return whatever we get. It should not be null after resetting, unless // the reset failed, or the appender was not configured correctly. diff --git a/src/test/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtensionTest.java b/src/test/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtensionTest.java index 9c145b0..cfa4b9a 100644 --- a/src/test/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtensionTest.java +++ b/src/test/java/org/kiwiproject/test/junit/jupiter/ResetLogbackLoggingExtensionTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import ch.qos.logback.classic.ClassicConstants; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,17 +9,15 @@ class ResetLogbackLoggingExtensionTest { @Test - void shouldConstructWithLogbackTestFileAsDefaultConfigLocation() { + void shouldConstructWithNullAsDefaultConfigLocation() { var extension = new ResetLogbackLoggingExtension(); - assertThat(extension.getLogbackConfigFilePath()) - .isEqualTo(ClassicConstants.TEST_AUTOCONFIG_FILE); + assertThat(extension.getLogbackConfigFilePath()).isNull(); } @Test - void shouldBuildWithLogbackTestFileAsDefaultConfigLocation() { + void shouldBuildWithNullAsDefaultConfigLocation() { var extension = ResetLogbackLoggingExtension.builder().build(); - assertThat(extension.getLogbackConfigFilePath()) - .isEqualTo(ClassicConstants.TEST_AUTOCONFIG_FILE); + assertThat(extension.getLogbackConfigFilePath()).isNull(); } @Test diff --git a/src/test/java/org/kiwiproject/test/logback/InMemoryAppenderExtensionTest.java b/src/test/java/org/kiwiproject/test/logback/InMemoryAppenderExtensionTest.java index 076cd15..d817921 100644 --- a/src/test/java/org/kiwiproject/test/logback/InMemoryAppenderExtensionTest.java +++ b/src/test/java/org/kiwiproject/test/logback/InMemoryAppenderExtensionTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import ch.qos.logback.classic.ClassicConstants; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -14,8 +13,7 @@ class InMemoryAppenderExtensionTest { @Test void shouldUseLogbackTestFileAsDefaultConfigLocation() { var extension = new InMemoryAppenderExtension(InMemoryAppenderExtensionTest.class); - assertThat(extension.getLogbackConfigFilePath()) - .isEqualTo(ClassicConstants.TEST_AUTOCONFIG_FILE); + assertThat(extension.getLogbackConfigFilePath()).isNull(); } @Test