Skip to content

Commit 1fe1c2d

Browse files
committed
Migrate log4j-jul to JUnit 5
1 parent caffa21 commit 1fe1c2d

12 files changed

+202
-206
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/AbstractLoggerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.logging.log4j.core.test.appender.ListAppender;
2727
import org.apache.logging.log4j.jul.ApiLogger;
2828
import org.apache.logging.log4j.jul.LevelTranslator;
29-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3030

3131
/**
3232
*

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

+13-19
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,25 @@
1818

1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.equalTo;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
2324

2425
import java.util.logging.Logger;
2526
import org.apache.logging.log4j.core.test.appender.ListAppender;
26-
import org.apache.logging.log4j.jul.LogManager;
27-
import org.junit.After;
28-
import org.junit.AfterClass;
29-
import org.junit.Before;
30-
import org.junit.BeforeClass;
31-
import org.junit.Test;
27+
import org.junit.jupiter.api.AfterAll;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
3231

3332
public class ApiLoggerTest extends AbstractLoggerTest {
3433

35-
@BeforeClass
36-
public static void setUpClass() {
37-
System.setProperty("java.util.logging.manager", LogManager.class.getName());
38-
}
39-
40-
@AfterClass
34+
@AfterAll
4135
public static void tearDownClass() {
4236
System.clearProperty("java.util.logging.manager");
4337
}
4438

45-
@Before
39+
@BeforeEach
4640
public void setUp() {
4741
logger = Logger.getLogger(LOGGER_NAME);
4842
logger.setFilter(null);
@@ -55,7 +49,7 @@ public void setUp() {
5549
assertNotNull(stringAppender);
5650
}
5751

58-
@After
52+
@AfterEach
5953
public void tearDown() {
6054
if (eventAppender != null) {
6155
eventAppender.clear();
@@ -71,12 +65,12 @@ public void tearDown() {
7165
@Test
7266
public void testGetParent() {
7367
final Logger parent = logger.getParent();
74-
assertNull("No parent logger should be automatically set up using log4j-api", parent);
68+
assertNull(parent, "No parent logger should be automatically set up using log4j-api");
7569
}
7670

77-
@Test(expected = UnsupportedOperationException.class)
71+
@Test()
7872
public void testSetParentFails() {
79-
logger.setParent(null);
73+
assertThrows(UnsupportedOperationException.class, () -> logger.setParent(null));
8074
}
8175

8276
@Test

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,28 @@
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.stream.Collectors;
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
25-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
2625
import org.apache.logging.log4j.core.util.Constants;
27-
import org.junit.AfterClass;
28-
import org.junit.BeforeClass;
29-
import org.junit.Test;
30-
import org.junit.experimental.categories.Category;
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Tag;
29+
import org.junit.jupiter.api.Test;
3130

32-
@Category(AsyncLoggers.class)
31+
@Tag("AsyncLoggers")
3332
public class AsyncLoggerThreadsTest {
3433

35-
@BeforeClass
34+
@BeforeAll
3635
public static void beforeClass() {
3736
System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, AsyncLoggerContextSelector.class.getName());
3837
System.setProperty("java.util.logging.manager", org.apache.logging.log4j.jul.LogManager.class.getName());
3938
}
4039

41-
@AfterClass
40+
@AfterAll
4241
public static void afterClass() {
4342
System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
4443
System.clearProperty("java.util.logging.manager");
@@ -50,6 +49,6 @@ public void testAsyncLoggerThreads() {
5049
final List<Thread> asyncLoggerThreads = Thread.getAllStackTraces().keySet().stream()
5150
.filter(thread -> thread.getName().matches("Log4j2-TF.*AsyncLogger.*"))
5251
.collect(Collectors.toList());
53-
assertEquals(asyncLoggerThreads.toString(), 1, asyncLoggerThreads.size());
52+
assertEquals(1, asyncLoggerThreads.size(), asyncLoggerThreads.toString());
5453
}
5554
}

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

+5-12
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,21 @@
1717
package org.apache.logging.log4j.jul.test;
1818

1919
import static java.util.logging.Level.INFO;
20+
import static org.hamcrest.MatcherAssert.assertThat;
2021
import static org.hamcrest.Matchers.hasSize;
21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertThat;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
2323

2424
import java.util.List;
2525
import java.util.logging.LogRecord;
2626
import java.util.logging.Logger;
2727
import org.apache.logging.log4j.core.LogEvent;
2828
import org.apache.logging.log4j.core.test.appender.ListAppender;
29-
import org.apache.logging.log4j.jul.LogManager;
30-
import org.junit.AfterClass;
31-
import org.junit.BeforeClass;
32-
import org.junit.Test;
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.Test;
3331

3432
public class BracketInNotInterpolatedMessageTest {
3533

36-
@BeforeClass
37-
public static void setUpClass() {
38-
System.setProperty("java.util.logging.manager", LogManager.class.getName());
39-
}
40-
41-
@AfterClass
34+
@AfterAll
4235
public static void tearDownClass() {
4336
System.clearProperty("java.util.logging.manager");
4437
}

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() throws Exception {
51-
final ListAppender app = ctx.getListAppender("Class").clear();
40+
@LoggerContextSource(CONFIG)
41+
public void testClassLogger(@Named("Class") final ListAppender app) throws Exception {
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() throws Exception {
65-
final ListAppender app = ctx.getListAppender("Method").clear();
55+
@LoggerContextSource(CONFIG)
56+
public void testMethodLogger(@Named("Method") final ListAppender app) throws Exception {
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
}

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

+7-16
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
import org.apache.logging.log4j.core.LoggerContext;
2424
import org.apache.logging.log4j.core.test.appender.ListAppender;
2525
import org.apache.logging.log4j.jul.Constants;
26-
import org.apache.logging.log4j.jul.CoreLoggerAdapter;
27-
import org.apache.logging.log4j.jul.LogManager;
2826
import org.apache.logging.log4j.util.Strings;
29-
import org.junit.After;
30-
import org.junit.AfterClass;
31-
import org.junit.Before;
32-
import org.junit.BeforeClass;
33-
import org.junit.Test;
27+
import org.junit.jupiter.api.AfterAll;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
3431

3532
public class CoreLoggerTest extends AbstractLoggerTest {
3633

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

58-
@BeforeClass
59-
public static void setUpClass() {
60-
System.setProperty("java.util.logging.manager", LogManager.class.getName());
61-
System.setProperty(Constants.LOGGER_ADAPTOR_PROPERTY, CoreLoggerAdapter.class.getName());
62-
}
63-
64-
@AfterClass
55+
@AfterAll
6556
public static void tearDownClass() {
6657
System.clearProperty("java.util.logging.manager");
6758
System.clearProperty(Constants.LOGGER_ADAPTOR_PROPERTY);
6859
}
6960

70-
@Before
61+
@BeforeEach
7162
public void setUp() {
7263
// Reset the logger context
7364
LoggerContext.getContext(false).reconfigure();
@@ -83,7 +74,7 @@ public void setUp() {
8374
assertThat(stringAppender).isNotNull();
8475
}
8576

86-
@After
77+
@AfterEach
8778
public void tearDown() {
8879
if (eventAppender != null) {
8980
eventAppender.clear();

0 commit comments

Comments
 (0)