Skip to content

Commit 6373ae4

Browse files
Neetika23Neetika Khandelwal
andauthored
Bael 9172 (#18546)
* Validate List * Validate List * Validate List * Validate List * Validate List * Validate List * BAEL-9172 * BAEL-9172 * BAEL-9172 --------- Co-authored-by: Neetika Khandelwal <[email protected]>
1 parent d27aba2 commit 6373ae4

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.logging;
2+
3+
import ch.qos.logback.classic.Level;
4+
import ch.qos.logback.classic.Logger;
5+
import ch.qos.logback.classic.spi.ILoggingEvent;
6+
import ch.qos.logback.core.read.ListAppender;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.util.List;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
class LoggerBreakUnitTest {
18+
19+
private Logger logger;
20+
private ListAppender<ILoggingEvent> listAppender;
21+
22+
@BeforeEach
23+
void setUp() {
24+
logger = (Logger) LoggerFactory.getLogger(LoggerBreakUnitTest.class);
25+
logger.setLevel(Level.INFO);
26+
27+
listAppender = new ListAppender<>();
28+
listAppender.start();
29+
logger.addAppender(listAppender);
30+
}
31+
32+
@Test
33+
void givenLogMessage_whenEmptyLineLogged_thenLogContainsBlankEntry() {
34+
logger.info("Start process");
35+
logger.info("");
36+
37+
List<ILoggingEvent> logs = listAppender.list;
38+
39+
assertEquals(2, logs.size());
40+
assertEquals("Start process", logs.get(0)
41+
.getFormattedMessage());
42+
assertEquals("", logs.get(1)
43+
.getFormattedMessage());
44+
}
45+
46+
@Test
47+
void givenLogMessage_whenUsingSystemLineSeparator_thenLogContainsPlatformSpecificLineBreak() {
48+
logger.info("Processing done{}", System.lineSeparator());
49+
50+
List<ILoggingEvent> logs = listAppender.list;
51+
52+
assertEquals(1, logs.size());
53+
assertTrue(logs.get(0)
54+
.getFormattedMessage()
55+
.endsWith(System.lineSeparator()));
56+
}
57+
58+
@Test
59+
void givenLogMessage_whenLineBreakIsConcatenated_thenLogSplitsCorrectly() {
60+
logger.info("Processing done" + System.lineSeparator());
61+
62+
List<ILoggingEvent> logs = listAppender.list;
63+
64+
assertEquals(1, logs.size());
65+
assertTrue(logs.get(0)
66+
.getFormattedMessage()
67+
.endsWith(System.lineSeparator()));
68+
}
69+
70+
@Test
71+
void givenLineSeparator_whenAppendedToLogMessages_thenLogEndsWithCorrectLineBreak() {
72+
String newline = System.getProperty("line.separator");
73+
74+
logger.info("Processing started" + newline);
75+
logger.info("Processing ended");
76+
77+
List<ILoggingEvent> logs = listAppender.list;
78+
79+
assertEquals(2, logs.size());
80+
81+
String firstMessage = logs.get(0)
82+
.getFormattedMessage();
83+
String secondMessage = logs.get(1)
84+
.getFormattedMessage();
85+
86+
assertTrue(firstMessage.endsWith(newline), "First message should end with the system line separator");
87+
assertEquals("Processing ended", secondMessage);
88+
}
89+
}
90+

0 commit comments

Comments
 (0)