import net.pterodactylus.rhynodge.output.DefaultOutput;
import net.pterodactylus.rhynodge.output.Output;
+import net.pterodactylus.util.test.DisableLog4jLogging;
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);
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;
/**
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
+@DisableLog4jLogging
public class EpisodeFilterTest {
private final EpisodeFilter episodeFilter = new EpisodeFilter();
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
/**
* Unit test for [ReactionRunner].
*/
+@DisableLog4jLogging
class ReactionRunnerTest {
@Test
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
import org.junit.jupiter.api.assertThrows
import java.time.LocalDateTime
+@DisableLog4jLogging
class SavoyMergerTest {
@Test
--- /dev/null
+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
+
+}
--- /dev/null
+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()
+
+}