Skip to content

Commit 7ecae66

Browse files
committed
Migrate log4j-jul to JUnit 5
1 parent 43a0e29 commit 7ecae66

File tree

2 files changed

+74
-38
lines changed

2 files changed

+74
-38
lines changed

log4j-jul/pom.xml

+58-14
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
<scope>test</scope>
7474
</dependency>
7575
<dependency>
76-
<groupId>junit</groupId>
77-
<artifactId>junit</artifactId>
76+
<groupId>org.junit.jupiter</groupId>
77+
<artifactId>junit-jupiter-engine</artifactId>
7878
<scope>test</scope>
7979
</dependency>
8080
</dependencies>
@@ -92,15 +92,6 @@
9292
<forkCount>1</forkCount>
9393
<reuseForks>false</reuseForks>
9494
</configuration>
95-
<dependencies>
96-
<!-- Manual override of the Surefire provider -->
97-
<!-- The `surefire-platform` provider initializes JUL before calling our tests -->
98-
<dependency>
99-
<groupId>org.apache.maven.surefire</groupId>
100-
<artifactId>surefire-junit47</artifactId>
101-
<version>${surefire.version}</version>
102-
</dependency>
103-
</dependencies>
10495
<executions>
10596
<execution>
10697
<id>default-test</id>
@@ -109,9 +100,12 @@
109100
</goals>
110101
<phase>test</phase>
111102
<configuration>
112-
<excludes>
113-
<exclude>Log4jBridgeHandlerTest.java</exclude>
114-
</excludes>
103+
<includes>
104+
<include>DefaultLevelConverterCustomJulLevelsTest.java</include>
105+
<include>DefaultLevelConverterTest.java</include>
106+
<include>JavaLevelTranslatorTest.java</include>
107+
<include>Log4jLevelTranslatorTest.java</include>
108+
</includes>
115109
</configuration>
116110
</execution>
117111
<execution>
@@ -131,6 +125,56 @@
131125
</systemPropertyVariables>
132126
</configuration>
133127
</execution>
128+
<execution>
129+
<id>async-logger-test</id>
130+
<goals>
131+
<goal>test</goal>
132+
</goals>
133+
<phase>test</phase>
134+
<configuration>
135+
<includes>
136+
<include>**/AsyncLoggerThreadsTest.java</include>
137+
</includes>
138+
<!-- Use custom `j.u.l.LogManager` and an asynchronous selector -->
139+
<systemPropertyVariables>
140+
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
141+
<log4j2.contextSelector>org.apache.logging.log4j.core.async.AsyncLoggerContextSelector</log4j2.contextSelector>
142+
</systemPropertyVariables>
143+
</configuration>
144+
</execution>
145+
<execution>
146+
<id>log-manager</id>
147+
<goals>
148+
<goal>test</goal>
149+
</goals>
150+
<phase>test</phase>
151+
<configuration>
152+
<includes>
153+
<include>**/*ApiLoggerTest.java</include>
154+
<include>**/*BracketInNotInterpolatedMessageTest.java</include>
155+
<include>**/*CallerInformationTest.java</include>
156+
</includes>
157+
<systemPropertyVariables>
158+
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
159+
</systemPropertyVariables>
160+
</configuration>
161+
</execution>
162+
<execution>
163+
<id>core-logger-test</id>
164+
<goals>
165+
<goal>test</goal>
166+
</goals>
167+
<phase>test</phase>
168+
<configuration>
169+
<includes>
170+
<include>**/CoreLoggerTest.java</include>
171+
</includes>
172+
<systemPropertyVariables>
173+
<java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
174+
<log4j2.julLoggerAdapter>org.apache.logging.log4j.jul.CoreLoggerAdapter</log4j2.julLoggerAdapter>
175+
</systemPropertyVariables>
176+
</configuration>
177+
</execution>
134178
</executions>
135179
</plugin>
136180
</plugins>

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java

+16-24
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,55 @@
1616
*/
1717
package org.apache.logging.log4j.jul.test;
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.logging.Logger;
2323
import org.apache.logging.log4j.core.test.appender.ListAppender;
24-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
25-
import org.apache.logging.log4j.jul.LogManager;
26-
import org.junit.AfterClass;
27-
import org.junit.BeforeClass;
28-
import org.junit.Rule;
29-
import org.junit.Test;
24+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
25+
import org.apache.logging.log4j.core.test.junit.Named;
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.Test;
3028

3129
public class CallerInformationTest {
3230

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

36-
@Rule
37-
public final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
38-
39-
@BeforeClass
40-
public static void setUpClass() {
41-
System.setProperty("java.util.logging.manager", LogManager.class.getName());
42-
}
43-
44-
@AfterClass
34+
@AfterAll
4535
public static void tearDownClass() {
4636
System.clearProperty("java.util.logging.manager");
4737
}
4838

4939
@Test
50-
public void testClassLogger() {
51-
final ListAppender app = ctx.getListAppender("Class").clear();
40+
@LoggerContextSource(CONFIG)
41+
public void testClassLogger(@Named("Class") final ListAppender app) {
42+
app.clear();
5243
final Logger logger = Logger.getLogger("ClassLogger");
5344
logger.info("Ignored message contents.");
5445
logger.warning("Verifying the caller class is still correct.");
5546
logger.severe("Hopefully nobody breaks me!");
5647
final List<String> messages = app.getMessages();
57-
assertEquals("Incorrect number of messages.", 3, messages.size());
48+
assertEquals(3, messages.size(), "Incorrect number of messages.");
5849
for (final String message : messages) {
59-
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
50+
assertEquals(this.getClass().getName(), message, "Incorrect caller class name.");
6051
}
6152
}
6253

6354
@Test
64-
public void testMethodLogger() {
65-
final ListAppender app = ctx.getListAppender("Method").clear();
55+
@LoggerContextSource(CONFIG)
56+
public void testMethodLogger(@Named("Method") final ListAppender app) {
57+
app.clear();
6658
final Logger logger = Logger.getLogger("MethodLogger");
6759
logger.info("More messages.");
6860
logger.warning("CATASTROPHE INCOMING!");
6961
logger.severe("ZOMBIES!!!");
7062
logger.warning("brains~~~");
7163
logger.info("Itchy. Tasty.");
7264
final List<String> messages = app.getMessages();
73-
assertEquals("Incorrect number of messages.", 5, messages.size());
65+
assertEquals(5, messages.size(), "Incorrect number of messages.");
7466
for (final String message : messages) {
75-
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
67+
assertEquals("testMethodLogger", message, "Incorrect caller method name.");
7668
}
7769
}
7870
}

0 commit comments

Comments
 (0)