Skip to content

Commit

Permalink
Log4j 1.2 api tests do not leak MDC values.
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak committed Jun 7, 2018
1 parent 615fc0f commit 40d9b50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
23 changes: 14 additions & 9 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LogWithMDCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ public class LogWithMDCTest {
public void testMDC() throws Exception {
MDC.put("Key1", "John");
MDC.put("Key2", "Smith");
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Key1 is missing", msgs.get(0).contains("Key1=John"));
assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith"));
try {
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Key1 is missing", msgs.get(0).contains("Key1=John"));
assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith"));
} finally {
MDC.remove("Key1");
MDC.remove("Key2");
}
}
}
23 changes: 14 additions & 9 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LogWithRouteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ public class LogWithRouteTest {
public void testMDC() throws Exception {
MDC.put("Type", "Service");
MDC.put("Name", "John Smith");
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Type is missing", msgs.get(0).contains("Type=Service"));
assertTrue("Name is missing", msgs.get(0).contains("Name=John Smith"));
try {
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Type is missing", msgs.get(0).contains("Type=Service"));
assertTrue("Name is missing", msgs.get(0).contains("Name=John Smith"));
} finally {
MDC.remove("Type");
MDC.remove("Name");
}
}
}

1 comment on commit 40d9b50

@garydgregory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I like using try-with-resources blocks to make sure we clean up tests.

Please sign in to comment.