Skip to content

Commit

Permalink
Change extensions to use default Logback configs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sleberknight committed Feb 9, 2024
1 parent 347166b commit 6e9883b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -177,7 +176,11 @@ private Appender<ILoggingEvent> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

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;

@DisplayName("ResetLogbackLoggingExtension")
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 6e9883b

Please sign in to comment.