From 1a696326ffeee940e8ab088b799a49839602d89c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Nov 2024 22:37:35 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=94=87=20Disable=20log4j=20during=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../rhynodge/actions/EmailActionTest.java | 2 ++ .../rhynodge/filters/EpisodeFilterTest.java | 2 ++ .../rhynodge/engine/ReactionRunnerTest.kt | 2 ++ .../filters/webpages/savoy/SavoyMergerTest.kt | 2 ++ .../pterodactylus/util/test/DisableLog4jLogging.kt | 35 ++++++++++++++++++++++ .../util/test/DisableLog4jLoggingTest.kt | 29 ++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLogging.kt create mode 100644 src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLoggingTest.kt diff --git a/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java b/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java index 9fe5688..c866923 100644 --- a/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java +++ b/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java @@ -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 David ‘Bombe’ Roden */ +@DisableLog4jLogging public class EmailActionTest { private final Transport transport = mock(Transport.class); diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/EpisodeFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/EpisodeFilterTest.java index f2813e2..41ff5a8 100644 --- a/src/test/java/net/pterodactylus/rhynodge/filters/EpisodeFilterTest.java +++ b/src/test/java/net/pterodactylus/rhynodge/filters/EpisodeFilterTest.java @@ -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 David ‘Bombe’ Roden */ +@DisableLog4jLogging public class EpisodeFilterTest { private final EpisodeFilter episodeFilter = new EpisodeFilter(); diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/engine/ReactionRunnerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/engine/ReactionRunnerTest.kt index a7b6ddc..a9d004f 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/engine/ReactionRunnerTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/engine/ReactionRunnerTest.kt @@ -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 diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt index b7ee7c8..0910779 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/webpages/savoy/SavoyMergerTest.kt @@ -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 index 0000000..1858e3a --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLogging.kt @@ -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 index 0000000..b8999a0 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/util/test/DisableLog4jLoggingTest.kt @@ -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() + +} -- 2.7.4