From a863eba63179b78fa2019c6de04d8f3abe913d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Engelmann?= Date: Thu, 28 Nov 2024 16:26:45 +0100 Subject: [PATCH 1/4] Added a UnitTest for the TriggerOnErrorOrWorseLogAppender. --- .../TriggerOnErrorOrWorseLogAppenderTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java diff --git a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java new file mode 100644 index 00000000..bc2177c0 --- /dev/null +++ b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java @@ -0,0 +1,78 @@ +/* + * This Source Code Form is subject to the terms of the MIT License. + * Copyright (c) 2023 Draegerwerk AG & Co. KGaA. + * + * SPDX-License-Identifier: MIT + */ + +package com.draeger.medical.sdccc.util; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LogEvent; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for the {@linkplain TriggerOnErrorOrWorseLogAppender} + */ +public class TriggerOnErrorOrWorseLogAppenderTest { + + @Test + public void testAppendWithDifferentLogLevels() { + testAppend(Level.ALL, false); + testAppend(Level.TRACE, false); + testAppend(Level.DEBUG, false); + testAppend(Level.INFO, false); + testAppend(Level.WARN, false); + testAppend(Level.ERROR, true); + testAppend(Level.FATAL, true); + testAppend(Level.OFF, true); + } + + public void testAppend(Level level, boolean expectHandlerCall) { + + AtomicBoolean flag = new AtomicBoolean(false); + + // given + TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", null); + classUnderTest.setOnErrorOrWorseHandler(new TriggerOnErrorOrWorseLogAppender.OnErrorOrWorseHandler() { + @Override + public void onErrorOrWorse(LogEvent event) { + flag.set(true); + } + }); + LogEvent event = mock(LogEvent.class); + when(event.getLevel()).thenReturn(level); + + // when + classUnderTest.append(event); + + // then + if (expectHandlerCall) { + assertTrue(flag.get()); + } else { + assertFalse(flag.get()); + } + } + +} From 5f44c72323ea31602915975fbc2e7be049c3be91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Engelmann?= Date: Thu, 28 Nov 2024 16:30:51 +0100 Subject: [PATCH 2/4] Added a UnitTest for the TriggerOnErrorOrWorseLogAppender. --- .../TriggerOnErrorOrWorseLogAppenderTest.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java index bc2177c0..7f48c6a2 100644 --- a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java +++ b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java @@ -8,28 +8,15 @@ package com.draeger.medical.sdccc.util; import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LogEvent; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -54,7 +41,9 @@ public void testAppend(Level level, boolean expectHandlerCall) { AtomicBoolean flag = new AtomicBoolean(false); // given - TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", null); + Filter filter = mock(Filter.class); + TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", + filter); classUnderTest.setOnErrorOrWorseHandler(new TriggerOnErrorOrWorseLogAppender.OnErrorOrWorseHandler() { @Override public void onErrorOrWorse(LogEvent event) { From e0a7724705f28ed03b3bb2a4d14469165d538f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Engelmann?= Date: Thu, 28 Nov 2024 16:32:19 +0100 Subject: [PATCH 3/4] Added a UnitTest for the TriggerOnErrorOrWorseLogAppender. --- .../sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java index 7f48c6a2..ad5d4b59 100644 --- a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java +++ b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java @@ -44,12 +44,7 @@ public void testAppend(Level level, boolean expectHandlerCall) { Filter filter = mock(Filter.class); TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", filter); - classUnderTest.setOnErrorOrWorseHandler(new TriggerOnErrorOrWorseLogAppender.OnErrorOrWorseHandler() { - @Override - public void onErrorOrWorse(LogEvent event) { - flag.set(true); - } - }); + classUnderTest.setOnErrorOrWorseHandler((LogEvent event) -> flag.set(true)); LogEvent event = mock(LogEvent.class); when(event.getLevel()).thenReturn(level); From 72eb26b0209206a4cc5863799246eb246f3f5dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Engelmann?= Date: Fri, 29 Nov 2024 12:22:01 +0100 Subject: [PATCH 4/4] Fixed Checkstyle warnings and spotless:apply. --- .../TriggerOnErrorOrWorseLogAppenderTest.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java index ad5d4b59..b23ee3ec 100644 --- a/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java +++ b/sdccc/src/test/java/com/draeger/medical/sdccc/util/TriggerOnErrorOrWorseLogAppenderTest.java @@ -7,23 +7,25 @@ package com.draeger.medical.sdccc.util; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.LogEvent; -import org.junit.jupiter.api.Test; - -import java.util.concurrent.atomic.AtomicBoolean; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LogEvent; +import org.junit.jupiter.api.Test; + /** - * Unit tests for the {@linkplain TriggerOnErrorOrWorseLogAppender} + * Unit tests for the {@linkplain TriggerOnErrorOrWorseLogAppender}. */ public class TriggerOnErrorOrWorseLogAppenderTest { + /** + * Test if the append method handles all Log Levels appropriately. + */ @Test public void testAppendWithDifferentLogLevels() { testAppend(Level.ALL, false); @@ -36,16 +38,20 @@ public void testAppendWithDifferentLogLevels() { testAppend(Level.OFF, true); } - public void testAppend(Level level, boolean expectHandlerCall) { + /** + * Used by the unit test testAppendWithDifferentLogLevels(). + * @param level the Log Level to test + * @param expectHandlerCall true if it is expected that the Handler is called, false otherwise. + */ + private void testAppend(final Level level, final boolean expectHandlerCall) { - AtomicBoolean flag = new AtomicBoolean(false); + final AtomicBoolean flag = new AtomicBoolean(false); // given - Filter filter = mock(Filter.class); - TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", - filter); + final Filter filter = mock(Filter.class); + final TriggerOnErrorOrWorseLogAppender classUnderTest = new TriggerOnErrorOrWorseLogAppender("log", filter); classUnderTest.setOnErrorOrWorseHandler((LogEvent event) -> flag.set(true)); - LogEvent event = mock(LogEvent.class); + final LogEvent event = mock(LogEvent.class); when(event.getLevel()).thenReturn(level); // when @@ -58,5 +64,4 @@ public void testAppend(Level level, boolean expectHandlerCall) { assertFalse(flag.get()); } } - }