Skip to content

Commit

Permalink
Merge pull request #867 from quickfix-j/chrjohn-slf4j-test-handler
Browse files Browse the repository at this point in the history
remove `TestHandler` after usage in `SLF4JLogTest`
  • Loading branch information
chrjohn authored Aug 23, 2024
2 parents 5b3ad51 + 756cd86 commit 9fe49bb
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
SystemTime.setTimeSource(null);
removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
removeLogHandlers("event");
removeLogHandlers("errorEvent");
removeLogHandlers("in");
removeLogHandlers("out");
}

@Test
Expand All @@ -65,7 +73,7 @@ public void testLog() throws Exception {
setUpLoggerForTest(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY);
log.onErrorEvent(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY, sessionID, loggedText);

setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
log.onIncoming(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY, sessionID, loggedText);
Expand Down Expand Up @@ -215,17 +223,25 @@ private TestHandler getTestHandler(String categoryName) {

private TestHandler setUpLoggerForTest(String category) {
final Logger logger = Logger.getLogger(category);
removeLogHandlers(logger);
final TestHandler testHandler = new TestHandler();
logger.addHandler(testHandler);
return testHandler;
}

private void removeLogHandlers(String category) {
final Logger logger = Logger.getLogger(category);
removeLogHandlers(logger);
}

private void removeLogHandlers(Logger logger) {
logger.setUseParentHandlers(false);
final Handler[] handlers = logger.getHandlers();
for (final Handler handler : handlers) {
//System.err.println("Removing unexpected handler: " + handlers[i]);
logger.removeHandler(handler);
}
final TestHandler testHandler = new TestHandler();
logger.addHandler(testHandler);
return testHandler;
}

private class TestHandler extends java.util.logging.Handler {
public final ArrayList<LogRecord> records = new ArrayList<>();

Expand Down

0 comments on commit 9fe49bb

Please sign in to comment.