Skip to content

Commit

Permalink
Migrate log4j-jul to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbaHerrerias committed Dec 10, 2024
1 parent caffa21 commit 1fe1c2d
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 206 deletions.
72 changes: 58 additions & 14 deletions log4j-jul/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -92,15 +92,6 @@
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
<dependencies>
<!-- Manual override of the Surefire provider -->
<!-- The `surefire-platform` provider initializes JUL before calling our tests -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>default-test</id>
Expand All @@ -109,9 +100,12 @@
</goals>
<phase>test</phase>
<configuration>
<excludes>
<exclude>Log4jBridgeHandlerTest.java</exclude>
</excludes>
<includes>
<include>DefaultLevelConverterCustomJulLevelsTest.java</include>
<include>DefaultLevelConverterTest.java</include>
<include>JavaLevelTranslatorTest.java</include>
<include>Log4jLevelTranslatorTest.java</include>
</includes>
</configuration>
</execution>
<execution>
Expand All @@ -131,6 +125,56 @@
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>async-logger-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<includes>
<include>**/AsyncLoggerThreadsTest.java</include>
</includes>
<!-- Use custom `j.u.l.LogManager` and an asynchronous selector -->
<systemPropertyVariables>
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
<log4j2.contextSelector>org.apache.logging.log4j.core.async.AsyncLoggerContextSelector</log4j2.contextSelector>
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>log-manager</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<includes>
<include>**/*ApiLoggerTest.java</include>
<include>**/*BracketInNotInterpolatedMessageTest.java</include>
<include>**/*CallerInformationTest.java</include>
</includes>
<systemPropertyVariables>
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>core-logger-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<includes>
<include>**/CoreLoggerTest.java</include>
</includes>
<systemPropertyVariables>
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
<log4j2.julLoggerAdapter>org.apache.logging.log4j.jul.CoreLoggerAdapter</log4j2.julLoggerAdapter>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.ApiLogger;
import org.apache.logging.log4j.jul.LevelTranslator;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,25 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.logging.Logger;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.LogManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ApiLoggerTest extends AbstractLoggerTest {

@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");
}

@Before
@BeforeEach
public void setUp() {
logger = Logger.getLogger(LOGGER_NAME);
logger.setFilter(null);
Expand All @@ -55,7 +49,7 @@ public void setUp() {
assertNotNull(stringAppender);
}

@After
@AfterEach
public void tearDown() {
if (eventAppender != null) {
eventAppender.clear();
Expand All @@ -71,12 +65,12 @@ public void tearDown() {
@Test
public void testGetParent() {
final Logger parent = logger.getParent();
assertNull("No parent logger should be automatically set up using log4j-api", parent);
assertNull(parent, "No parent logger should be automatically set up using log4j-api");
}

@Test(expected = UnsupportedOperationException.class)
@Test()
public void testSetParentFails() {
logger.setParent(null);
assertThrows(UnsupportedOperationException.class, () -> logger.setParent(null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,28 @@
*/
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.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
import org.apache.logging.log4j.core.util.Constants;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Category(AsyncLoggers.class)
@Tag("AsyncLoggers")
public class AsyncLoggerThreadsTest {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, AsyncLoggerContextSelector.class.getName());
System.setProperty("java.util.logging.manager", org.apache.logging.log4j.jul.LogManager.class.getName());
}

@AfterClass
@AfterAll
public static void afterClass() {
System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
System.clearProperty("java.util.logging.manager");
Expand All @@ -50,6 +49,6 @@ public void testAsyncLoggerThreads() {
final List<Thread> asyncLoggerThreads = Thread.getAllStackTraces().keySet().stream()
.filter(thread -> thread.getName().matches("Log4j2-TF.*AsyncLogger.*"))
.collect(Collectors.toList());
assertEquals(asyncLoggerThreads.toString(), 1, asyncLoggerThreads.size());
assertEquals(1, asyncLoggerThreads.size(), asyncLoggerThreads.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,21 @@
package org.apache.logging.log4j.jul.test;

import static java.util.logging.Level.INFO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.LogManager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

public class BracketInNotInterpolatedMessageTest {

@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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,55 @@
*/
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() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
@LoggerContextSource(CONFIG)
public void testClassLogger(@Named("Class") final ListAppender app) throws Exception {
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<String> 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() throws Exception {
final ListAppender app = ctx.getListAppender("Method").clear();
@LoggerContextSource(CONFIG)
public void testMethodLogger(@Named("Method") final ListAppender app) throws Exception {
app.clear();
final Logger logger = Logger.getLogger("MethodLogger");
logger.info("More messages.");
logger.warning("CATASTROPHE INCOMING!");
logger.severe("ZOMBIES!!!");
logger.warning("brains~~~");
logger.info("Itchy. Tasty.");
final List<String> 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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.Constants;
import org.apache.logging.log4j.jul.CoreLoggerAdapter;
import org.apache.logging.log4j.jul.LogManager;
import org.apache.logging.log4j.util.Strings;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CoreLoggerTest extends AbstractLoggerTest {

Expand All @@ -55,19 +52,13 @@ private static Level getEffectiveLevel(final Logger logger) {
throw new RuntimeException("No level is enabled.");
}

@BeforeClass
public static void setUpClass() {
System.setProperty("java.util.logging.manager", LogManager.class.getName());
System.setProperty(Constants.LOGGER_ADAPTOR_PROPERTY, CoreLoggerAdapter.class.getName());
}

@AfterClass
@AfterAll
public static void tearDownClass() {
System.clearProperty("java.util.logging.manager");
System.clearProperty(Constants.LOGGER_ADAPTOR_PROPERTY);
}

@Before
@BeforeEach
public void setUp() {
// Reset the logger context
LoggerContext.getContext(false).reconfigure();
Expand All @@ -83,7 +74,7 @@ public void setUp() {
assertThat(stringAppender).isNotNull();
}

@After
@AfterEach
public void tearDown() {
if (eventAppender != null) {
eventAppender.clear();
Expand Down
Loading

0 comments on commit 1fe1c2d

Please sign in to comment.