Skip to content

Commit

Permalink
remove TestHandler after usage in SLF4JLogTest
Browse files Browse the repository at this point in the history
  • Loading branch information
chrjohn authored Aug 22, 2024
1 parent 36a07fe commit 0e81d65
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ public void testLog() throws Exception {
setUpLoggerForTest(SLF4JLog.DEFAULT_EVENT_CATEGORY);
log.onEvent(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_EVENT_CATEGORY, sessionID, loggedText);
removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY);
log.onErrorEvent(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY, sessionID, loggedText);

removeLogHandlers(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
log.onIncoming(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY, sessionID, loggedText);
removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
log.onOutgoing(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY, sessionID, loggedText);
removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);

settings.setString(sessionID, SLF4JLogFactory.SETTING_EVENT_CATEGORY, "event");
settings.setString(sessionID, SLF4JLogFactory.SETTING_ERROR_EVENT_CATEGORY, "errorEvent");
Expand All @@ -83,18 +87,22 @@ public void testLog() throws Exception {
setUpLoggerForTest("event");
log.onEvent(loggedText);
assertMessageLogged("event", sessionID, loggedText);
removeLogHandlers("event");

setUpLoggerForTest("errorEvent");
log.onErrorEvent(loggedText);
assertMessageLogged("errorEvent", sessionID, loggedText);
removeLogHandlers("errorEvent");

setUpLoggerForTest("in");
log.onIncoming(loggedText);
assertMessageLogged("in", sessionID, loggedText);
removeLogHandlers("in");

setUpLoggerForTest("out");
log.onOutgoing(loggedText);
assertMessageLogged("out", sessionID, loggedText);
removeLogHandlers("out");
}

@Test
Expand All @@ -112,21 +120,25 @@ public void testLogHeartbeatFiltering() throws Exception {
setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
log.onIncoming(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY, sessionID, loggedText);
removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
log.onOutgoing(loggedText);
assertMessageLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY, sessionID, loggedText);
removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);

settings.setBool(sessionID, SLF4JLogFactory.SETTING_LOG_HEARTBEATS, false);
log = (SLF4JLog) factory.create(sessionID);

setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
log.onIncoming(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
log.onOutgoing(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
}

@Test
Expand All @@ -145,21 +157,25 @@ public void testLogFilteredByLevel() throws Exception {
getTestHandler(SLF4JLog.DEFAULT_EVENT_CATEGORY).setLevel(Level.WARNING);
log.onEvent(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_EVENT_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY);
getTestHandler(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY).setLevel(Level.SEVERE);
log.onErrorEvent(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_EVENT_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
getTestHandler(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY).setLevel(Level.WARNING);
log.onIncoming(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY);

setUpLoggerForTest(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
getTestHandler(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY).setLevel(Level.WARNING);
log.onOutgoing(loggedText);
assertMessageNotLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY);
}

/**
Expand Down Expand Up @@ -215,17 +231,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 0e81d65

Please sign in to comment.