From 0e81d6549a08f6732f3f4830808e2f4b25c25537 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Thu, 22 Aug 2024 17:17:09 +0200 Subject: [PATCH 1/4] 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<>(); From ee9e6133cc2a0a4cc20191b6168178786bc70942 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Thu, 22 Aug 2024 17:19:27 +0200 Subject: [PATCH 2/4] typo --- quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java index 999418c015..ec084a4435 100644 --- a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java +++ b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java @@ -163,7 +163,7 @@ public void testLogFilteredByLevel() throws Exception { getTestHandler(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY).setLevel(Level.SEVERE); log.onErrorEvent(loggedText); assertMessageNotLogged(SLF4JLog.DEFAULT_EVENT_CATEGORY); - removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY); + removeLogHandlers(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY); setUpLoggerForTest(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY); getTestHandler(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY).setLevel(Level.WARNING); From 3e5fc70d5d059db696764afdd36285bb0bce17ec Mon Sep 17 00:00:00 2001 From: Christoph John Date: Thu, 22 Aug 2024 17:53:32 +0200 Subject: [PATCH 3/4] moved cleanup to end of method --- .../src/test/java/quickfix/SLF4JLogTest.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java index ec084a4435..1c31223385 100644 --- a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java +++ b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java @@ -61,22 +61,18 @@ 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"); @@ -87,21 +83,27 @@ 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); + + // cleanup + 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"); } @@ -120,12 +122,10 @@ 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); @@ -133,11 +133,13 @@ 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); + + // cleanup + removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY); removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); } @@ -157,24 +159,26 @@ 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_ERROR_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); + + // cleanup + removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY); + removeLogHandlers(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY); + removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY); removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); } From 163f343618d8d160a83efe26ae4e0fa602d57712 Mon Sep 17 00:00:00 2001 From: Christoph John Date: Fri, 23 Aug 2024 10:05:20 +0200 Subject: [PATCH 4/4] move cleanup to `tearDown()` method --- .../src/test/java/quickfix/SLF4JLogTest.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java index 1c31223385..6f77f048f5 100644 --- a/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java +++ b/quickfixj-core/src/test/java/quickfix/SLF4JLogTest.java @@ -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 @@ -95,16 +103,6 @@ public void testLog() throws Exception { setUpLoggerForTest("out"); log.onOutgoing(loggedText); assertMessageLogged("out", sessionID, loggedText); - - // cleanup - 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 @@ -137,10 +135,6 @@ public void testLogHeartbeatFiltering() throws Exception { setUpLoggerForTest(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); log.onOutgoing(loggedText); assertMessageNotLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); - - // cleanup - removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY); - removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); } @Test @@ -174,12 +168,6 @@ public void testLogFilteredByLevel() throws Exception { getTestHandler(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY).setLevel(Level.WARNING); log.onOutgoing(loggedText); assertMessageNotLogged(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); - - // cleanup - removeLogHandlers(SLF4JLog.DEFAULT_EVENT_CATEGORY); - removeLogHandlers(SLF4JLog.DEFAULT_ERROR_EVENT_CATEGORY); - removeLogHandlers(SLF4JLog.DEFAULT_INCOMING_MSG_CATEGORY); - removeLogHandlers(SLF4JLog.DEFAULT_OUTGOING_MSG_CATEGORY); } /**