Skip to content

Commit

Permalink
Fix location-based tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Jan 15, 2024
1 parent cc0de07 commit 2d9774c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void builder() {
final List<LogEvent> events = app.getEvents();
assertEventCount(events, 3);
assertEquals(
"org.apache.logging.log4j.core.LoggerTest.builder(LoggerTest.java:115)",
"org.apache.logging.log4j.core.LoggerTest.builder(LoggerTest.java:121)",
events.get(0).getSource().toString(),
"Incorrect location");
assertEquals(Level.DEBUG, events.get(0).getLevel(), "Incorrect Level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ void testMemMapLocation(final LoggerContext context) throws Exception {
"Expected file size to shrink");

final List<String> lines = Files.readAllLines(logFile);
assertEquals(2, lines.size());
assertThat(lines.get(0))
.endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation"
+ "(MemoryMappedFileAppenderTest.java:103): Test log1");
assertThat(lines.get(1))
.endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation"
+ "(MemoryMappedFileAppenderTest.java:106): Test log2");
assertThat(lines)
.hasSize(2)
.contains(
"org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:103): Test log1",
"org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:106): Test log2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testCustomPattern() {
formatter.format(event, buf);
}
final String str = buf.toString();
final String expected = "INFO [PatternParserTest :98 ] - Hello, world" + Strings.LINE_SEPARATOR;
final String expected = "INFO [PatternParserTest :100 ] - Hello, world" + Strings.LINE_SEPARATOR;
assertTrue(str.endsWith(expected), "Expected to end with: " + expected + ". Actual: " + str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
@LoggerContextSource(value = "log4j-patternSelector.xml", reconfigure = ReconfigurationPolicy.BEFORE_EACH)
public class PatternSelectorTest {

private static final int CURRENT_LINE = 37;

@Test
public void testMarkerPatternSelector(@Named("List") final ListAppender app) {
final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestMarkerPatternSelector");
Expand All @@ -46,8 +48,10 @@ public void testMarkerPatternSelector(@Named("List") final ListAppender app) {
3,
messages.size(),
"Incorrect number of messages. Expected 3, Actual " + messages.size() + ": " + messages);
final String expect = String.format("[TRACE] TestMarkerPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testMarkerPatternSelector:40 Enter ======%n");
final String expect = String.format(
"[TRACE] TestMarkerPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testMarkerPatternSelector:%d Enter ======%n",
CURRENT_LINE + 5);
assertEquals(expect, messages.get(0));
assertEquals("[INFO ] TestMarkerPatternSelector Hello World" + Strings.LINE_SEPARATOR, messages.get(1));
app.clear();
Expand All @@ -67,11 +71,15 @@ public void testScriptPatternSelector(@Named("List2") final ListAppender app) {
4,
messages.size(),
"Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages);
String expect = "[TRACE] TestScriptPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testScriptPatternSelector:60 Enter ======" + Strings.LINE_SEPARATOR;
String expect = String.format(
"[TRACE] TestScriptPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testScriptPatternSelector:%d Enter ======%n",
CURRENT_LINE + 27);
assertEquals(expect, messages.get(0));
expect = "[INFO ] TestScriptPatternSelector o.a.l.l.s.l.PatternSelectorTest.testScriptPatternSelector.61 "
+ "Hello World" + Strings.LINE_SEPARATOR;
expect = String.format(
"[INFO ] TestScriptPatternSelector o.a.l.l.s.l.PatternSelectorTest.testScriptPatternSelector.%d "
+ "Hello World%n",
CURRENT_LINE + 28);
assertEquals(expect, messages.get(1));
assertEquals("[INFO ] NoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
app.clear();
Expand All @@ -91,13 +99,15 @@ public void testJavaScriptPatternSelector(@Named("List3") final ListAppender app
4,
messages.size(),
"Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages);
String expect = "[TRACE] TestJavaScriptPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testJavaScriptPatternSelector:84 Enter ======"
+ Strings.LINE_SEPARATOR;
String expect = String.format(
"[TRACE] TestJavaScriptPatternSelector ====== "
+ "o.a.l.l.s.l.PatternSelectorTest.testJavaScriptPatternSelector:%d Enter ======%n",
CURRENT_LINE + 55);
assertEquals(expect, messages.get(0));
expect = "[INFO ] TestJavaScriptPatternSelector "
+ "o.a.l.l.s.l.PatternSelectorTest.testJavaScriptPatternSelector.85 Hello World"
+ Strings.LINE_SEPARATOR;
expect = String.format(
"[INFO ] TestJavaScriptPatternSelector "
+ "o.a.l.l.s.l.PatternSelectorTest.testJavaScriptPatternSelector.%d Hello World%n",
CURRENT_LINE + 56);
assertEquals(expect, messages.get(1));
assertEquals("[INFO ] JavascriptNoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
app.clear();
Expand Down

0 comments on commit 2d9774c

Please sign in to comment.