Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for SystemStatusListener and LogbackLoggingSystem #44012

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
final class SystemStatusListener extends OnConsoleStatusListener {

static final long RETROSPECTIVE_THRESHOLD = 300;
private static final long RETROSPECTIVE_THRESHOLD = 300;

private final boolean debug;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.InfoStatus;
import ch.qos.logback.core.status.OnConsoleStatusListener;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.status.WarnStatus;
import ch.qos.logback.core.util.DynamicClassLoadingException;
Expand Down Expand Up @@ -673,6 +674,18 @@ void logbackDebugPropertyIsHonored(CapturedOutput output) {
}
}

@Test
void logbackSystemStatusListenerShouldBeRegisteredWhenCustomLogbackXmlHasStatusListener(CapturedOutput output) {
this.loggingSystem.beforeInitialize();
initialize(this.initializationContext, "classpath:logback-include-status-listener.xml", null);
LoggerContext loggerContext = this.logger.getLoggerContext();
assertThat(loggerContext.getStatusManager().getCopyOfStatusListenerList()).hasSize(2)
.allSatisfy((listener) -> assertThat(listener).isInstanceOf(OnConsoleStatusListener.class))
.anySatisfy((listener) -> assertThat(listener).isInstanceOf(SystemStatusListener.class));
this.logger.info("Hello world");
assertThat(output).contains("Hello world");
}

@Test
void logbackSystemStatusListenerShouldBeRegistered(CapturedOutput output) {
this.loggingSystem.beforeInitialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.logging.logback;

import java.util.List;
import java.util.function.Supplier;

import ch.qos.logback.classic.LoggerContext;
Expand All @@ -31,6 +32,7 @@

import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -49,6 +51,15 @@ class SystemStatusListenerTests {

private static final String TEST_MESSAGE = "testtesttest";

private final StatusManager statusManager = mock(StatusManager.class);

private final LoggerContext loggerContext = mock(LoggerContext.class);

SystemStatusListenerTests() {
given(this.loggerContext.getStatusManager()).willReturn(this.statusManager);
given(this.statusManager.add(any(StatusListener.class))).willReturn(true);
}

@Test
void addStatusWithInfoLevelWhenNoDebugDoesNotPrint(CapturedOutput output) {
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
Expand Down Expand Up @@ -91,15 +102,43 @@ void addStatusWithErrorLevelWhenDebugPrintsToSystemOut(CapturedOutput output) {
assertThat(output.getErr()).doesNotContain(TEST_MESSAGE);
}

@Test
void shouldRetrospectivePrintStatusOnStartAndDebugIsDisabled(CapturedOutput output) {
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null),
new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null)));
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
assertThat(output.getErr()).contains("WARN " + TEST_MESSAGE);
assertThat(output.getErr()).contains("ERROR " + TEST_MESSAGE);
assertThat(output.getErr()).doesNotContain("INFO");
assertThat(output.getOut()).isEmpty();
}

@Test
void shouldRetrospectivePrintStatusOnStartAndDebugIsEnabled(CapturedOutput output) {
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null),
new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null)));
addStatus(true, () -> new InfoStatus(TEST_MESSAGE, null));
assertThat(output.getErr()).isEmpty();
assertThat(output.getOut()).contains("WARN " + TEST_MESSAGE);
assertThat(output.getOut()).contains("ERROR " + TEST_MESSAGE);
assertThat(output.getOut()).contains("INFO " + TEST_MESSAGE);
}

@Test
void shouldNotRetrospectivePrintWhenStatusIsOutdated(CapturedOutput output) {
ErrorStatus outdatedStatus = new ErrorStatus(TEST_MESSAGE, null);
ReflectionTestUtils.setField(outdatedStatus, "timestamp", System.currentTimeMillis() - 300);
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(outdatedStatus));
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
assertThat(output.getOut()).isEmpty();
assertThat(output.getErr()).isEmpty();
}

private void addStatus(boolean debug, Supplier<Status> statusFactory) {
StatusManager statusManager = mock(StatusManager.class);
given(statusManager.add(any(StatusListener.class))).willReturn(true);
LoggerContext loggerContext = mock(LoggerContext.class);
given(loggerContext.getStatusManager()).willReturn(statusManager);
SystemStatusListener.addTo(loggerContext, debug);
SystemStatusListener.addTo(this.loggerContext, debug);
ArgumentCaptor<StatusListener> listener = ArgumentCaptor.forClass(StatusListener.class);
then(statusManager).should().add(listener.capture());
assertThat(listener.getValue()).extracting("context").isSameAs(loggerContext);
then(this.statusManager).should().add(listener.capture());
assertThat(listener.getValue()).extracting("context").isSameAs(this.loggerContext);
listener.getValue().addStatusEvent(statusFactory.get());
}

Expand Down