From 0e81d6549a08f6732f3f4830808e2f4b25c25537 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Thu, 22 Aug 2024 17:17:09 +0200 Subject: [PATCH] remove `TestHandler` after usage in `SLF4JLogTest` --- .../src/test/java/quickfix/SLF4JLogTest.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java index b95b3e4750..999418c015 100644 --- a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java +++ b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java @@ -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"); @@ -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 @@ -112,10 +120,12 @@ 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); @@ -123,10 +133,12 @@ public void testLogHeartbeatFiltering() throws Exception { 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 @@ -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); } /** @@ -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 records = new ArrayList<>();