Skip to content

Commit

Permalink
Migrate log4j-slf4j2-impl to JUnit 5 (#3080)
Browse files Browse the repository at this point in the history
Migrate `log4j-slf4j2-impl` to JUnit 5.
  • Loading branch information
AlbaHerrerias authored Oct 23, 2024
1 parent 17024c7 commit e749fa1
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 121 deletions.
31 changes: 12 additions & 19 deletions log4j-slf4j2-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -108,11 +103,6 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -121,35 +111,38 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<!-- Separate test execution to verify that the presence of both:
~ * `log4j-slf4j2-impl` (bridge from SLF4J to Log4j API)
~ * `log4j-to-slf4j` (bridge from Log4j API to SLF4J)
~ does not cause a stack overflow.
-->
<execution>
<id>loop-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<additionalClasspathDependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${project.version}</version>
</dependency>
</additionalClasspathDependencies>
<includes>
<include>**/OverflowTest.java</include>
</includes>
<includeJUnit5Engines>junit-vintage</includeJUnit5Engines>
</configuration>
</execution>
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/OverflowTest.java</exclude>
</excludes>
<classpathDependencyExcludes>
<classpathDependencyExcludes>org.apache.logging.log4j:log4j-to-slf4j</classpathDependencyExcludes>
</classpathDependencyExcludes>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package org.apache.logging.other.pkg;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.status.StatusData;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,24 @@
*/
package org.apache.logging.slf4j;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.ClassRule;
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.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.CallerBoundaryAware;
import org.slf4j.spi.LoggingEventBuilder;

@LoggerContextSource("log4j2-calling-class.xml")
public class CallerInformationTest {

// config from log4j-core test-jar
private static final String CONFIG = "log4j2-calling-class.xml";

@ClassRule
public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);

@Test
public void testClassLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
public void testClassLogger(@Named("Class") final ListAppender app) throws Exception {
app.clear();
final Logger logger = LoggerFactory.getLogger("ClassLogger");
logger.info("Ignored message contents.");
logger.warn("Verifying the caller class is still correct.");
Expand All @@ -47,15 +42,15 @@ public void testClassLogger() throws Exception {
logger.atWarn().log("Verifying the caller class is still correct.");
logger.atError().log("Hopefully nobody breaks me!");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 6, messages.size());
assertEquals(6, 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();
public void testMethodLogger(@Named("Method") final ListAppender app) throws Exception {
app.clear();
final Logger logger = LoggerFactory.getLogger("MethodLogger");
logger.info("More messages.");
logger.warn("CATASTROPHE INCOMING!");
Expand All @@ -68,23 +63,23 @@ public void testMethodLogger() throws Exception {
logger.atWarn().log("brains~~~");
logger.atInfo().log("Itchy. Tasty.");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 10, messages.size());
assertEquals(10, 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.");
}
}

@Test
public void testFqcnLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Fqcn").clear();
public void testFqcnLogger(@Named("Fqcn") final ListAppender app) throws Exception {
app.clear();
final Logger logger = LoggerFactory.getLogger("FqcnLogger");
LoggingEventBuilder loggingEventBuilder = logger.atInfo();
((CallerBoundaryAware) loggingEventBuilder).setCallerBoundary("MyFqcn");
loggingEventBuilder.log("A message");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 1, messages.size());
assertEquals(1, messages.size(), "Incorrect number of messages.");
for (final String message : messages) {
assertEquals("Incorrect fqcn.", "MyFqcn", message);
assertEquals("MyFqcn", message, "Incorrect fqcn.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.logging.slf4j;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -47,7 +47,10 @@ public void run() {

private void trigger() {
Holder.LOGGER.info("Attempt to trigger");
assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof Log4jLogger);
assertInstanceOf(
Log4jLogger.class,
Holder.LOGGER,
"Logger is of type " + Holder.LOGGER.getClass().getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
*/
package org.apache.logging.slf4j;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class Log4jMarkerTest {

private static Log4jMarkerFactory markerFactory;

@BeforeClass
@BeforeAll
public static void startup() {
markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory();
}
Expand All @@ -38,9 +40,9 @@ public void testEquals() {
final Log4jMarker marker1 = new Log4jMarker(markerFactory, markerA);
final Log4jMarker marker2 = new Log4jMarker(markerFactory, markerA);
final Log4jMarker marker3 = new Log4jMarker(markerFactory, markerB);
Assert.assertEquals(marker1, marker2);
Assert.assertNotEquals(marker1, null);
Assert.assertNotEquals(null, marker1);
Assert.assertNotEquals(marker1, marker3);
assertEquals(marker1, marker2);
assertNotEquals(marker1, null);
assertNotEquals(null, marker1);
assertNotEquals(marker1, marker3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
*/
package org.apache.logging.slf4j;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Set;
import org.apache.logging.log4j.core.LifeCycle;
import org.apache.logging.log4j.spi.LoggerContext;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

/**
Expand All @@ -35,9 +36,9 @@ public void testCleanup() throws Exception {
factory.getLogger("test");
Set<LoggerContext> set = factory.getLoggerContexts();
final LoggerContext ctx1 = set.toArray(LoggerContext.EMPTY_ARRAY)[0];
assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle);
assertInstanceOf(LifeCycle.class, ctx1, "LoggerContext is not enabled for shutdown");
((LifeCycle) ctx1).stop();
set = factory.getLoggerContexts();
assertTrue("Expected no LoggerContexts", set.isEmpty());
assertTrue(set.isEmpty(), "Expected no LoggerContexts");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
package org.apache.logging.slf4j;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.Named;
import org.apache.logging.log4j.util.Strings;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
Expand All @@ -41,21 +42,23 @@
/**
*
*/
@LoggerContextSource("log4j-test1.xml")
public class LoggerTest {

private static final String CONFIG = "log4j-test1.xml";

@ClassRule
public static LoggerContextRule ctx = new LoggerContextRule(CONFIG);

Logger logger = LoggerFactory.getLogger("LoggerTest");
private final Logger logger;
private final LoggerContext ctx;

@Test
public void debug() {
logger.debug("Debug message");
verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR);
}

public LoggerTest(final LoggerContext context) {
this.ctx = context;
this.logger = LoggerFactory.getLogger("LoggerTest");
}

@Test
public void debugNoParms() {
logger.debug("Debug message {}");
Expand Down Expand Up @@ -113,7 +116,7 @@ public void supportsCustomSLF4JMarkers() {
@Test
public void testRootLogger() {
final Logger l = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
assertNotNull("No Root Logger", l);
assertNotNull(l, "No Root Logger");
assertEquals(Logger.ROOT_LOGGER_NAME, l.getName());
}

Expand Down Expand Up @@ -159,7 +162,7 @@ public void testThrowable() {

@Test
public void testLazyLoggingEventBuilder() {
final ListAppender appender = ctx.getListAppender("UnformattedList");
final ListAppender appender = ctx.getConfiguration().getAppender("UnformattedList");
final Level oldLevel = ctx.getRootLogger().getLevel();
try {
Configurator.setRootLevel(Level.ERROR);
Expand All @@ -173,34 +176,35 @@ public void testLazyLoggingEventBuilder() {
}

private ListAppender getAppenderByName(final String name) {
final ListAppender listApp = ctx.getListAppender(name);
assertNotNull("Missing Appender", listApp);
final ListAppender listApp = ctx.getConfiguration().getAppender(name);
assertNotNull(listApp, "Missing Appender");
return listApp;
}

private void verify(final String expected) {
final ListAppender listApp = getAppenderByName("List");
final List<String> events = listApp.getMessages();
assertEquals("Incorrect number of messages. Expected 1 Actual " + events.size(), 1, events.size());
assertEquals(1, events.size(), "Incorrect number of messages. Expected 1 Actual " + events.size());
final String actual = events.get(0);
assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
assertEquals(expected, actual, "Incorrect message. Expected \" + expected + \". Actual \" + actual");
listApp.clear();
}

private void verifyThrowable(final Throwable expected) {
final ListAppender listApp = getAppenderByName("UnformattedList");
final List<LogEvent> events = listApp.getEvents();
assertEquals("Incorrect number of messages", 1, events.size());
assertEquals(1, events.size(), "Incorrect number of messages");
final LogEvent actual = events.get(0);
assertEquals("Incorrect throwable.", expected, actual.getThrown());
assertEquals(expected, actual.getThrown(), "Incorrect throwable.");
listApp.clear();
}

@Before
@After
public void cleanup() {
@BeforeEach
@AfterEach
public void cleanup(
@Named("List") final ListAppender list, @Named("UnformattedList") final ListAppender unformattedList) {
MDC.clear();
ctx.getListAppender("List").clear();
ctx.getListAppender("UnformattedList").clear();
list.clear();
unformattedList.clear();
}
}
Loading

0 comments on commit e749fa1

Please sign in to comment.