Skip to content

Commit e749fa1

Browse files
Migrate log4j-slf4j2-impl to JUnit 5 (#3080)
Migrate `log4j-slf4j2-impl` to JUnit 5.
1 parent 17024c7 commit e749fa1

10 files changed

+115
-121
lines changed

log4j-slf4j2-impl/pom.xml

+12-19
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@
7878
<artifactId>log4j-core-test</artifactId>
7979
<scope>test</scope>
8080
</dependency>
81-
<dependency>
82-
<groupId>org.apache.logging.log4j</groupId>
83-
<artifactId>log4j-to-slf4j</artifactId>
84-
<scope>test</scope>
85-
</dependency>
8681
<dependency>
8782
<groupId>org.assertj</groupId>
8883
<artifactId>assertj-core</artifactId>
@@ -108,11 +103,6 @@
108103
<artifactId>junit-jupiter-params</artifactId>
109104
<scope>test</scope>
110105
</dependency>
111-
<dependency>
112-
<groupId>org.junit.vintage</groupId>
113-
<artifactId>junit-vintage-engine</artifactId>
114-
<scope>test</scope>
115-
</dependency>
116106
</dependencies>
117107
<build>
118108
<plugins>
@@ -121,35 +111,38 @@
121111
<groupId>org.apache.maven.plugins</groupId>
122112
<artifactId>maven-surefire-plugin</artifactId>
123113
<executions>
114+
<!-- Separate test execution to verify that the presence of both:
115+
~ * `log4j-slf4j2-impl` (bridge from SLF4J to Log4j API)
116+
~ * `log4j-to-slf4j` (bridge from Log4j API to SLF4J)
117+
~ does not cause a stack overflow.
118+
-->
124119
<execution>
125120
<id>loop-test</id>
126121
<goals>
127122
<goal>test</goal>
128123
</goals>
129-
<phase>test</phase>
130124
<configuration>
125+
<additionalClasspathDependencies>
126+
<dependency>
127+
<groupId>org.apache.logging.log4j</groupId>
128+
<artifactId>log4j-to-slf4j</artifactId>
129+
<version>${project.version}</version>
130+
</dependency>
131+
</additionalClasspathDependencies>
131132
<includes>
132133
<include>**/OverflowTest.java</include>
133134
</includes>
134-
<includeJUnit5Engines>junit-vintage</includeJUnit5Engines>
135135
</configuration>
136136
</execution>
137137
<execution>
138138
<id>default-test</id>
139-
<goals>
140-
<goal>test</goal>
141-
</goals>
142-
<phase>test</phase>
143139
<configuration>
144140
<includes>
145141
<include>**/*Test.java</include>
146142
</includes>
147143
<excludes>
148144
<exclude>**/OverflowTest.java</exclude>
149145
</excludes>
150-
<classpathDependencyExcludes>
151-
<classpathDependencyExcludes>org.apache.logging.log4j:log4j-to-slf4j</classpathDependencyExcludes>
152-
</classpathDependencyExcludes>
153146
</configuration>
154147
</execution>
155148
</executions>

log4j-slf4j2-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
*/
1717
package org.apache.logging.other.pkg;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

2121
import java.util.List;
2222
import java.util.concurrent.CopyOnWriteArrayList;
2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.status.StatusData;
2525
import org.apache.logging.log4j.status.StatusListener;
2626
import org.apache.logging.log4j.status.StatusLogger;
27-
import org.junit.Test;
27+
import org.junit.jupiter.api.Test;
2828
import org.slf4j.LoggerFactory;
2929

3030
/**

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java

+17-22
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,24 @@
1616
*/
1717
package org.apache.logging.slf4j;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

2121
import java.util.List;
2222
import org.apache.logging.log4j.core.test.appender.ListAppender;
23-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
24-
import org.junit.ClassRule;
25-
import org.junit.Test;
23+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
24+
import org.apache.logging.log4j.core.test.junit.Named;
25+
import org.junit.jupiter.api.Test;
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
2828
import org.slf4j.spi.CallerBoundaryAware;
2929
import org.slf4j.spi.LoggingEventBuilder;
3030

31+
@LoggerContextSource("log4j2-calling-class.xml")
3132
public class CallerInformationTest {
3233

33-
// config from log4j-core test-jar
34-
private static final String CONFIG = "log4j2-calling-class.xml";
35-
36-
@ClassRule
37-
public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
38-
3934
@Test
40-
public void testClassLogger() throws Exception {
41-
final ListAppender app = ctx.getListAppender("Class").clear();
35+
public void testClassLogger(@Named("Class") final ListAppender app) throws Exception {
36+
app.clear();
4237
final Logger logger = LoggerFactory.getLogger("ClassLogger");
4338
logger.info("Ignored message contents.");
4439
logger.warn("Verifying the caller class is still correct.");
@@ -47,15 +42,15 @@ public void testClassLogger() throws Exception {
4742
logger.atWarn().log("Verifying the caller class is still correct.");
4843
logger.atError().log("Hopefully nobody breaks me!");
4944
final List<String> messages = app.getMessages();
50-
assertEquals("Incorrect number of messages.", 6, messages.size());
45+
assertEquals(6, messages.size(), "Incorrect number of messages.");
5146
for (final String message : messages) {
52-
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
47+
assertEquals(this.getClass().getName(), message, "Incorrect caller class name.");
5348
}
5449
}
5550

5651
@Test
57-
public void testMethodLogger() throws Exception {
58-
final ListAppender app = ctx.getListAppender("Method").clear();
52+
public void testMethodLogger(@Named("Method") final ListAppender app) throws Exception {
53+
app.clear();
5954
final Logger logger = LoggerFactory.getLogger("MethodLogger");
6055
logger.info("More messages.");
6156
logger.warn("CATASTROPHE INCOMING!");
@@ -68,23 +63,23 @@ public void testMethodLogger() throws Exception {
6863
logger.atWarn().log("brains~~~");
6964
logger.atInfo().log("Itchy. Tasty.");
7065
final List<String> messages = app.getMessages();
71-
assertEquals("Incorrect number of messages.", 10, messages.size());
66+
assertEquals(10, messages.size(), "Incorrect number of messages.");
7267
for (final String message : messages) {
73-
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
68+
assertEquals("testMethodLogger", message, "Incorrect caller method name.");
7469
}
7570
}
7671

7772
@Test
78-
public void testFqcnLogger() throws Exception {
79-
final ListAppender app = ctx.getListAppender("Fqcn").clear();
73+
public void testFqcnLogger(@Named("Fqcn") final ListAppender app) throws Exception {
74+
app.clear();
8075
final Logger logger = LoggerFactory.getLogger("FqcnLogger");
8176
LoggingEventBuilder loggingEventBuilder = logger.atInfo();
8277
((CallerBoundaryAware) loggingEventBuilder).setCallerBoundary("MyFqcn");
8378
loggingEventBuilder.log("A message");
8479
final List<String> messages = app.getMessages();
85-
assertEquals("Incorrect number of messages.", 1, messages.size());
80+
assertEquals(1, messages.size(), "Incorrect number of messages.");
8681
for (final String message : messages) {
87-
assertEquals("Incorrect fqcn.", "MyFqcn", message);
82+
assertEquals("MyFqcn", message, "Incorrect fqcn.");
8883
}
8984
}
9085
}

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package org.apache.logging.slf4j;
1818

19-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2020

21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

@@ -47,7 +47,10 @@ public void run() {
4747

4848
private void trigger() {
4949
Holder.LOGGER.info("Attempt to trigger");
50-
assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof Log4jLogger);
50+
assertInstanceOf(
51+
Log4jLogger.class,
52+
Holder.LOGGER,
53+
"Logger is of type " + Holder.LOGGER.getClass().getName());
5154
}
5255
}
5356
}

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616
*/
1717
package org.apache.logging.slf4j;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
21+
1922
import org.apache.logging.log4j.Marker;
2023
import org.apache.logging.log4j.MarkerManager;
21-
import org.junit.Assert;
22-
import org.junit.BeforeClass;
23-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
2426

2527
public class Log4jMarkerTest {
2628

2729
private static Log4jMarkerFactory markerFactory;
2830

29-
@BeforeClass
31+
@BeforeAll
3032
public static void startup() {
3133
markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory();
3234
}
@@ -38,9 +40,9 @@ public void testEquals() {
3840
final Log4jMarker marker1 = new Log4jMarker(markerFactory, markerA);
3941
final Log4jMarker marker2 = new Log4jMarker(markerFactory, markerA);
4042
final Log4jMarker marker3 = new Log4jMarker(markerFactory, markerB);
41-
Assert.assertEquals(marker1, marker2);
42-
Assert.assertNotEquals(marker1, null);
43-
Assert.assertNotEquals(null, marker1);
44-
Assert.assertNotEquals(marker1, marker3);
43+
assertEquals(marker1, marker2);
44+
assertNotEquals(marker1, null);
45+
assertNotEquals(null, marker1);
46+
assertNotEquals(marker1, marker3);
4547
}
4648
}

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
*/
1717
package org.apache.logging.slf4j;
1818

19-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import java.util.Set;
2223
import org.apache.logging.log4j.core.LifeCycle;
2324
import org.apache.logging.log4j.spi.LoggerContext;
24-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2526
import org.slf4j.LoggerFactory;
2627

2728
/**
@@ -35,9 +36,9 @@ public void testCleanup() throws Exception {
3536
factory.getLogger("test");
3637
Set<LoggerContext> set = factory.getLoggerContexts();
3738
final LoggerContext ctx1 = set.toArray(LoggerContext.EMPTY_ARRAY)[0];
38-
assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle);
39+
assertInstanceOf(LifeCycle.class, ctx1, "LoggerContext is not enabled for shutdown");
3940
((LifeCycle) ctx1).stop();
4041
set = factory.getLoggerContexts();
41-
assertTrue("Expected no LoggerContexts", set.isEmpty());
42+
assertTrue(set.isEmpty(), "Expected no LoggerContexts");
4243
}
4344
}

log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java

+30-26
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
package org.apache.logging.slf4j;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2222

2323
import java.util.List;
2424
import org.apache.logging.log4j.Level;
2525
import org.apache.logging.log4j.core.LogEvent;
26+
import org.apache.logging.log4j.core.LoggerContext;
2627
import org.apache.logging.log4j.core.config.Configurator;
2728
import org.apache.logging.log4j.core.test.appender.ListAppender;
28-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
29+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
30+
import org.apache.logging.log4j.core.test.junit.Named;
2931
import org.apache.logging.log4j.util.Strings;
30-
import org.junit.After;
31-
import org.junit.Before;
32-
import org.junit.ClassRule;
33-
import org.junit.Test;
32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
3435
import org.slf4j.Logger;
3536
import org.slf4j.LoggerFactory;
3637
import org.slf4j.MDC;
@@ -41,21 +42,23 @@
4142
/**
4243
*
4344
*/
45+
@LoggerContextSource("log4j-test1.xml")
4446
public class LoggerTest {
4547

46-
private static final String CONFIG = "log4j-test1.xml";
47-
48-
@ClassRule
49-
public static LoggerContextRule ctx = new LoggerContextRule(CONFIG);
50-
51-
Logger logger = LoggerFactory.getLogger("LoggerTest");
48+
private final Logger logger;
49+
private final LoggerContext ctx;
5250

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

57+
public LoggerTest(final LoggerContext context) {
58+
this.ctx = context;
59+
this.logger = LoggerFactory.getLogger("LoggerTest");
60+
}
61+
5962
@Test
6063
public void debugNoParms() {
6164
logger.debug("Debug message {}");
@@ -113,7 +116,7 @@ public void supportsCustomSLF4JMarkers() {
113116
@Test
114117
public void testRootLogger() {
115118
final Logger l = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
116-
assertNotNull("No Root Logger", l);
119+
assertNotNull(l, "No Root Logger");
117120
assertEquals(Logger.ROOT_LOGGER_NAME, l.getName());
118121
}
119122

@@ -159,7 +162,7 @@ public void testThrowable() {
159162

160163
@Test
161164
public void testLazyLoggingEventBuilder() {
162-
final ListAppender appender = ctx.getListAppender("UnformattedList");
165+
final ListAppender appender = ctx.getConfiguration().getAppender("UnformattedList");
163166
final Level oldLevel = ctx.getRootLogger().getLevel();
164167
try {
165168
Configurator.setRootLevel(Level.ERROR);
@@ -173,34 +176,35 @@ public void testLazyLoggingEventBuilder() {
173176
}
174177

175178
private ListAppender getAppenderByName(final String name) {
176-
final ListAppender listApp = ctx.getListAppender(name);
177-
assertNotNull("Missing Appender", listApp);
179+
final ListAppender listApp = ctx.getConfiguration().getAppender(name);
180+
assertNotNull(listApp, "Missing Appender");
178181
return listApp;
179182
}
180183

181184
private void verify(final String expected) {
182185
final ListAppender listApp = getAppenderByName("List");
183186
final List<String> events = listApp.getMessages();
184-
assertEquals("Incorrect number of messages. Expected 1 Actual " + events.size(), 1, events.size());
187+
assertEquals(1, events.size(), "Incorrect number of messages. Expected 1 Actual " + events.size());
185188
final String actual = events.get(0);
186-
assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
189+
assertEquals(expected, actual, "Incorrect message. Expected \" + expected + \". Actual \" + actual");
187190
listApp.clear();
188191
}
189192

190193
private void verifyThrowable(final Throwable expected) {
191194
final ListAppender listApp = getAppenderByName("UnformattedList");
192195
final List<LogEvent> events = listApp.getEvents();
193-
assertEquals("Incorrect number of messages", 1, events.size());
196+
assertEquals(1, events.size(), "Incorrect number of messages");
194197
final LogEvent actual = events.get(0);
195-
assertEquals("Incorrect throwable.", expected, actual.getThrown());
198+
assertEquals(expected, actual.getThrown(), "Incorrect throwable.");
196199
listApp.clear();
197200
}
198201

199-
@Before
200-
@After
201-
public void cleanup() {
202+
@BeforeEach
203+
@AfterEach
204+
public void cleanup(
205+
@Named("List") final ListAppender list, @Named("UnformattedList") final ListAppender unformattedList) {
202206
MDC.clear();
203-
ctx.getListAppender("List").clear();
204-
ctx.getListAppender("UnformattedList").clear();
207+
list.clear();
208+
unformattedList.clear();
205209
}
206210
}

0 commit comments

Comments
 (0)