🔇 Disable log4j during tests
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 Nov 2024 21:37:35 +0000 (22:37 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 Nov 2024 21:37:35 +0000 (22:37 +0100)
src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java
src/test/java/net/pterodactylus/rhynodge/filters/EpisodeFilterTest.java
src/test/kotlin/net/pterodactylus/rhynodge/engine/ReactionRunnerTest.kt
src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt
src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLogging.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLoggingTest.kt [new file with mode: 0644]

index 9fe5688..c866923 100644 (file)
@@ -13,6 +13,7 @@ import javax.mail.Transport;
 
 import net.pterodactylus.rhynodge.output.DefaultOutput;
 import net.pterodactylus.rhynodge.output.Output;
+import net.pterodactylus.util.test.DisableLog4jLogging;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -20,6 +21,7 @@ import org.junit.jupiter.api.Test;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
  */
+@DisableLog4jLogging
 public class EmailActionTest {
 
        private final Transport transport = mock(Transport.class);
index f2813e2..41ff5a8 100644 (file)
@@ -10,6 +10,7 @@ import net.pterodactylus.rhynodge.states.FailedState;
 import net.pterodactylus.rhynodge.states.HtmlState;
 import net.pterodactylus.rhynodge.states.TorrentState;
 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
+import net.pterodactylus.util.test.DisableLog4jLogging;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -17,6 +18,7 @@ import org.junit.jupiter.api.Test;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
  */
+@DisableLog4jLogging
 public class EpisodeFilterTest {
 
        private final EpisodeFilter episodeFilter = new EpisodeFilter();
index a7b6ddc..a9d004f 100644 (file)
@@ -8,6 +8,7 @@ import net.pterodactylus.rhynodge.output.Output
 import net.pterodactylus.rhynodge.states.AbstractState
 import net.pterodactylus.rhynodge.states.FailedState
 import net.pterodactylus.rhynodge.states.StateManager
+import net.pterodactylus.util.test.DisableLog4jLogging
 import org.hamcrest.Description
 import org.hamcrest.Matcher
 import org.hamcrest.MatcherAssert.assertThat
@@ -23,6 +24,7 @@ import java.util.concurrent.atomic.AtomicReference
 /**
  * Unit test for [ReactionRunner].
  */
+@DisableLog4jLogging
 class ReactionRunnerTest {
 
        @Test
index b7ee7c8..0910779 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.rhynodge.filters.webpages.savoy
 
 import net.pterodactylus.rhynodge.states.StateManagerTest.TestState
+import net.pterodactylus.util.test.DisableLog4jLogging
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.empty
@@ -9,6 +10,7 @@ import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.assertThrows
 import java.time.LocalDateTime
 
+@DisableLog4jLogging
 class SavoyMergerTest {
 
        @Test
diff --git a/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLogging.kt b/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLogging.kt
new file mode 100644 (file)
index 0000000..1858e3a
--- /dev/null
@@ -0,0 +1,35 @@
+package net.pterodactylus.util.test
+
+import org.apache.log4j.Level
+import org.apache.log4j.Logger
+import org.junit.jupiter.api.extension.AfterEachCallback
+import org.junit.jupiter.api.extension.BeforeEachCallback
+import org.junit.jupiter.api.extension.ExtendWith
+import org.junit.jupiter.api.extension.ExtensionContext
+
+/**
+ * Disables logging with log4j for the annotated class or method.
+ *
+ * Logging is disabled by setting the [root logger][Logger.getRootLogger]’s
+ * [log level][Logger.level] to [Level.OFF].
+ */
+@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
+@Retention(AnnotationRetention.RUNTIME)
+@ExtendWith(DisableLog4jLoggingExtension::class)
+annotation class DisableLog4jLogging
+
+class DisableLog4jLoggingExtension : BeforeEachCallback, AfterEachCallback {
+
+       override fun beforeEach(context: ExtensionContext?) {
+               oldLevel = rootLogger.level
+               rootLogger.level = Level.OFF
+       }
+
+       override fun afterEach(context: ExtensionContext?) {
+               rootLogger.level = oldLevel
+       }
+
+       private val rootLogger = Logger.getRootLogger()
+       private var oldLevel: Level? = null
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLoggingTest.kt b/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLoggingTest.kt
new file mode 100644 (file)
index 0000000..b8999a0
--- /dev/null
@@ -0,0 +1,29 @@
+package net.pterodactylus.util.test
+
+import org.apache.log4j.Level
+import org.apache.log4j.Logger
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.jupiter.api.Test
+
+class DisableLog4jLoggingTest {
+
+       @Test
+       fun `extension disables logging on root logger`() {
+               rootLogger.level = Level.ALL
+               extension.beforeEach(null)
+               assertThat(rootLogger.level, equalTo(Level.OFF))
+       }
+
+       @Test
+       fun `extension restores level of root logger after test`() {
+               rootLogger.level = object : Level(123, "test", 7) {}
+               extension.beforeEach(null)
+               extension.afterEach(null)
+               assertThat(rootLogger.level.toInt(), equalTo(123))
+       }
+
+       private val rootLogger = Logger.getRootLogger()
+       private val extension = DisableLog4jLoggingExtension()
+
+}