Skip to content

Commit 213b2ea

Browse files
nosanphilwebb
authored andcommitted
Add more tests for SystemStatusListener and LogbackLoggingSystem
See gh-44012 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent b722430 commit 213b2ea

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SystemStatusListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
final class SystemStatusListener extends OnConsoleStatusListener {
4040

41-
static final long RETROSPECTIVE_THRESHOLD = 300;
41+
private static final long RETROSPECTIVE_THRESHOLD = 300;
4242

4343
private final boolean debug;
4444

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
4343
import ch.qos.logback.core.status.ErrorStatus;
4444
import ch.qos.logback.core.status.InfoStatus;
45+
import ch.qos.logback.core.status.OnConsoleStatusListener;
4546
import ch.qos.logback.core.status.StatusManager;
4647
import ch.qos.logback.core.status.WarnStatus;
4748
import ch.qos.logback.core.util.DynamicClassLoadingException;
@@ -673,6 +674,18 @@ void logbackDebugPropertyIsHonored(CapturedOutput output) {
673674
}
674675
}
675676

677+
@Test
678+
void logbackSystemStatusListenerShouldBeRegisteredWhenCustomLogbackXmlHasStatusListener(CapturedOutput output) {
679+
this.loggingSystem.beforeInitialize();
680+
initialize(this.initializationContext, "classpath:logback-include-status-listener.xml", null);
681+
LoggerContext loggerContext = this.logger.getLoggerContext();
682+
assertThat(loggerContext.getStatusManager().getCopyOfStatusListenerList()).hasSize(2)
683+
.allSatisfy((listener) -> assertThat(listener).isInstanceOf(OnConsoleStatusListener.class))
684+
.anySatisfy((listener) -> assertThat(listener).isInstanceOf(SystemStatusListener.class));
685+
this.logger.info("Hello world");
686+
assertThat(output).contains("Hello world");
687+
}
688+
676689
@Test
677690
void logbackSystemStatusListenerShouldBeRegistered(CapturedOutput output) {
678691
this.loggingSystem.beforeInitialize();

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SystemStatusListenerTests.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging.logback;
1818

19+
import java.util.List;
1920
import java.util.function.Supplier;
2021

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

3233
import org.springframework.boot.testsupport.system.CapturedOutput;
3334
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
35+
import org.springframework.test.util.ReflectionTestUtils;
3436

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

5052
private static final String TEST_MESSAGE = "testtesttest";
5153

54+
private final StatusManager statusManager = mock(StatusManager.class);
55+
56+
private final LoggerContext loggerContext = mock(LoggerContext.class);
57+
58+
SystemStatusListenerTests() {
59+
given(this.loggerContext.getStatusManager()).willReturn(this.statusManager);
60+
given(this.statusManager.add(any(StatusListener.class))).willReturn(true);
61+
}
62+
5263
@Test
5364
void addStatusWithInfoLevelWhenNoDebugDoesNotPrint(CapturedOutput output) {
5465
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
@@ -91,15 +102,43 @@ void addStatusWithErrorLevelWhenDebugPrintsToSystemOut(CapturedOutput output) {
91102
assertThat(output.getErr()).doesNotContain(TEST_MESSAGE);
92103
}
93104

105+
@Test
106+
void shouldRetrospectivePrintStatusOnStartAndDebugIsDisabled(CapturedOutput output) {
107+
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null),
108+
new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null)));
109+
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
110+
assertThat(output.getErr()).contains("WARN " + TEST_MESSAGE);
111+
assertThat(output.getErr()).contains("ERROR " + TEST_MESSAGE);
112+
assertThat(output.getErr()).doesNotContain("INFO");
113+
assertThat(output.getOut()).isEmpty();
114+
}
115+
116+
@Test
117+
void shouldRetrospectivePrintStatusOnStartAndDebugIsEnabled(CapturedOutput output) {
118+
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null),
119+
new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null)));
120+
addStatus(true, () -> new InfoStatus(TEST_MESSAGE, null));
121+
assertThat(output.getErr()).isEmpty();
122+
assertThat(output.getOut()).contains("WARN " + TEST_MESSAGE);
123+
assertThat(output.getOut()).contains("ERROR " + TEST_MESSAGE);
124+
assertThat(output.getOut()).contains("INFO " + TEST_MESSAGE);
125+
}
126+
127+
@Test
128+
void shouldNotRetrospectivePrintWhenStatusIsOutdated(CapturedOutput output) {
129+
ErrorStatus outdatedStatus = new ErrorStatus(TEST_MESSAGE, null);
130+
ReflectionTestUtils.setField(outdatedStatus, "timestamp", System.currentTimeMillis() - 300);
131+
given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(outdatedStatus));
132+
addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null));
133+
assertThat(output.getOut()).isEmpty();
134+
assertThat(output.getErr()).isEmpty();
135+
}
136+
94137
private void addStatus(boolean debug, Supplier<Status> statusFactory) {
95-
StatusManager statusManager = mock(StatusManager.class);
96-
given(statusManager.add(any(StatusListener.class))).willReturn(true);
97-
LoggerContext loggerContext = mock(LoggerContext.class);
98-
given(loggerContext.getStatusManager()).willReturn(statusManager);
99-
SystemStatusListener.addTo(loggerContext, debug);
138+
SystemStatusListener.addTo(this.loggerContext, debug);
100139
ArgumentCaptor<StatusListener> listener = ArgumentCaptor.forClass(StatusListener.class);
101-
then(statusManager).should().add(listener.capture());
102-
assertThat(listener.getValue()).extracting("context").isSameAs(loggerContext);
140+
then(this.statusManager).should().add(listener.capture());
141+
assertThat(listener.getValue()).extracting("context").isSameAs(this.loggerContext);
103142
listener.getValue().addStatusEvent(statusFactory.get());
104143
}
105144

0 commit comments

Comments
 (0)