From 7ecae66911e9f851647788da11d3d9b77f48e2f0 Mon Sep 17 00:00:00 2001 From: Alba Herrerias Date: Tue, 10 Dec 2024 16:06:11 +0000 Subject: [PATCH] Migrate `log4j-jul` to JUnit 5 --- log4j-jul/pom.xml | 72 +++++++++++++++---- .../log4j/jul/test/CallerInformationTest.java | 40 +++++------ 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/log4j-jul/pom.xml b/log4j-jul/pom.xml index 60672017a82..f10b6bbed27 100644 --- a/log4j-jul/pom.xml +++ b/log4j-jul/pom.xml @@ -73,8 +73,8 @@ test - junit - junit + org.junit.jupiter + junit-jupiter-engine test @@ -92,15 +92,6 @@ 1 false - - - - - org.apache.maven.surefire - surefire-junit47 - ${surefire.version} - - default-test @@ -109,9 +100,12 @@ test - - Log4jBridgeHandlerTest.java - + + DefaultLevelConverterCustomJulLevelsTest.java + DefaultLevelConverterTest.java + JavaLevelTranslatorTest.java + Log4jLevelTranslatorTest.java + @@ -131,6 +125,56 @@ + + async-logger-test + + test + + test + + + **/AsyncLoggerThreadsTest.java + + + + org.apache.logging.log4j.jul.LogManager + org.apache.logging.log4j.core.async.AsyncLoggerContextSelector + + + + + log-manager + + test + + test + + + **/*ApiLoggerTest.java + **/*BracketInNotInterpolatedMessageTest.java + **/*CallerInformationTest.java + + + org.apache.logging.log4j.jul.LogManager + + + + + core-logger-test + + test + + test + + + **/CoreLoggerTest.java + + + org.apache.logging.log4j.jul.LogManager + org.apache.logging.log4j.jul.CoreLoggerAdapter + + + diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java index 1b57a3918d0..082f7af73fd 100644 --- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java @@ -16,53 +16,45 @@ */ package org.apache.logging.log4j.jul.test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import java.util.logging.Logger; import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.apache.logging.log4j.jul.LogManager; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; +import org.apache.logging.log4j.core.test.junit.LoggerContextSource; +import org.apache.logging.log4j.core.test.junit.Named; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; public class CallerInformationTest { // config from log4j-core test-jar private static final String CONFIG = "log4j2-calling-class.xml"; - @Rule - public final LoggerContextRule ctx = new LoggerContextRule(CONFIG); - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass + @AfterAll public static void tearDownClass() { System.clearProperty("java.util.logging.manager"); } @Test - public void testClassLogger() { - final ListAppender app = ctx.getListAppender("Class").clear(); + @LoggerContextSource(CONFIG) + public void testClassLogger(@Named("Class") final ListAppender app) { + app.clear(); final Logger logger = Logger.getLogger("ClassLogger"); logger.info("Ignored message contents."); logger.warning("Verifying the caller class is still correct."); logger.severe("Hopefully nobody breaks me!"); final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 3, messages.size()); + assertEquals(3, messages.size(), "Incorrect number of messages."); for (final String message : messages) { - assertEquals("Incorrect caller class name.", this.getClass().getName(), message); + assertEquals(this.getClass().getName(), message, "Incorrect caller class name."); } } @Test - public void testMethodLogger() { - final ListAppender app = ctx.getListAppender("Method").clear(); + @LoggerContextSource(CONFIG) + public void testMethodLogger(@Named("Method") final ListAppender app) { + app.clear(); final Logger logger = Logger.getLogger("MethodLogger"); logger.info("More messages."); logger.warning("CATASTROPHE INCOMING!"); @@ -70,9 +62,9 @@ public void testMethodLogger() { logger.warning("brains~~~"); logger.info("Itchy. Tasty."); final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 5, messages.size()); + assertEquals(5, messages.size(), "Incorrect number of messages."); for (final String message : messages) { - assertEquals("Incorrect caller method name.", "testMethodLogger", message); + assertEquals("testMethodLogger", message, "Incorrect caller method name."); } } }