Skip to content

Commit f64d65b

Browse files
Migrate log4j-slf4j-impl to JUnit 5 (#3138)
Migrate `log4j-slf4j-impl` to JUnit 5
1 parent 06c483e commit f64d65b

File tree

11 files changed

+117
-119
lines changed

11 files changed

+117
-119
lines changed

log4j-slf4j-impl/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@
109109
<artifactId>junit-jupiter-engine</artifactId>
110110
<scope>test</scope>
111111
</dependency>
112-
<dependency>
113-
<groupId>org.junit.vintage</groupId>
114-
<artifactId>junit-vintage-engine</artifactId>
115-
<scope>test</scope>
116-
</dependency>
117112
</dependencies>
118113
<build>
119114
<plugins>

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

Lines changed: 2 additions & 2 deletions
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-slf4j-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,46 @@
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

29+
@LoggerContextSource("log4j2-calling-class.xml")
2930
public class CallerInformationTest {
3031

31-
// config from log4j-core test-jar
32-
private static final String CONFIG = "log4j2-calling-class.xml";
33-
34-
@ClassRule
35-
public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
36-
3732
@Test
38-
public void testClassLogger() throws Exception {
39-
final ListAppender app = ctx.getListAppender("Class").clear();
33+
public void testClassLogger(@Named("Class") final ListAppender app) throws Exception {
34+
app.clear();
4035
final Logger logger = LoggerFactory.getLogger("ClassLogger");
4136
logger.info("Ignored message contents.");
4237
logger.warn("Verifying the caller class is still correct.");
4338
logger.error("Hopefully nobody breaks me!");
4439
final List<String> messages = app.getMessages();
45-
assertEquals("Incorrect number of messages.", 3, messages.size());
40+
assertEquals(3, messages.size(), "Incorrect number of messages.");
4641
for (final String message : messages) {
47-
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
42+
assertEquals(this.getClass().getName(), message, "Incorrect caller class name.");
4843
}
4944
}
5045

5146
@Test
52-
public void testMethodLogger() throws Exception {
53-
final ListAppender app = ctx.getListAppender("Method").clear();
47+
public void testMethodLogger(@Named("Method") final ListAppender app) throws Exception {
48+
app.clear();
5449
final Logger logger = LoggerFactory.getLogger("MethodLogger");
5550
logger.info("More messages.");
5651
logger.warn("CATASTROPHE INCOMING!");
5752
logger.error("ZOMBIES!!!");
5853
logger.warn("brains~~~");
5954
logger.info("Itchy. Tasty.");
6055
final List<String> messages = app.getMessages();
61-
assertEquals("Incorrect number of messages.", 5, messages.size());
56+
assertEquals(5, messages.size(), "Incorrect number of messages.");
6257
for (final String message : messages) {
63-
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
58+
assertEquals("testMethodLogger", message, "Incorrect caller method name.");
6459
}
6560
}
6661
}

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

Lines changed: 6 additions & 3 deletions
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-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java

Lines changed: 10 additions & 8 deletions
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-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java

Lines changed: 5 additions & 4 deletions
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-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
*/
1717
package org.apache.logging.slf4j;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121

2222
import java.util.List;
2323
import org.apache.logging.log4j.core.LogEvent;
24+
import org.apache.logging.log4j.core.LoggerContext;
2425
import org.apache.logging.log4j.core.test.appender.ListAppender;
25-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.apache.logging.log4j.core.test.junit.Named;
2628
import org.apache.logging.log4j.util.Strings;
27-
import org.junit.After;
28-
import org.junit.Before;
29-
import org.junit.ClassRule;
30-
import org.junit.Test;
29+
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
3334
import org.slf4j.MDC;
@@ -37,14 +38,16 @@
3738
/**
3839
*
3940
*/
41+
@LoggerContextSource("log4j-test1.xml")
4042
public class LoggerTest {
4143

42-
private static final String CONFIG = "log4j-test1.xml";
44+
private final Logger logger;
45+
private final LoggerContext ctx;
4346

44-
@ClassRule
45-
public static LoggerContextRule ctx = new LoggerContextRule(CONFIG);
46-
47-
Logger logger = LoggerFactory.getLogger("LoggerTest");
47+
public LoggerTest(final LoggerContext context) {
48+
this.ctx = context;
49+
this.logger = LoggerFactory.getLogger("LoggerTest");
50+
}
4851

4952
@Test
5053
public void debug() {
@@ -93,7 +96,7 @@ public void supportsCustomSLF4JMarkers() {
9396
@Test
9497
public void testRootLogger() {
9598
final Logger l = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
96-
assertNotNull("No Root Logger", l);
99+
assertNotNull(l, "No Root Logger");
97100
assertEquals(Logger.ROOT_LOGGER_NAME, l.getName());
98101
}
99102

@@ -138,34 +141,35 @@ public void testThrowable() {
138141
}
139142

140143
private ListAppender getAppenderByName(final String name) {
141-
final ListAppender listApp = ctx.getListAppender(name);
142-
assertNotNull("Missing Appender", listApp);
144+
final ListAppender listApp = ctx.getConfiguration().getAppender(name);
145+
assertNotNull(listApp, "Missing Appender");
143146
return listApp;
144147
}
145148

146149
private void verify(final String expected) {
147150
final ListAppender listApp = getAppenderByName("List");
148151
final List<String> events = listApp.getMessages();
149-
assertEquals("Incorrect number of messages. Expected 1 Actual " + events.size(), 1, events.size());
152+
assertEquals(1, events.size(), "Incorrect number of messages. Expected 1 Actual " + events.size());
150153
final String actual = events.get(0);
151-
assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
154+
assertEquals(expected, actual, "Incorrect message. Expected " + expected + ". Actual " + actual);
152155
listApp.clear();
153156
}
154157

155158
private void verifyThrowable(final Throwable expected) {
156159
final ListAppender listApp = getAppenderByName("UnformattedList");
157160
final List<LogEvent> events = listApp.getEvents();
158-
assertEquals("Incorrect number of messages", 1, events.size());
161+
assertEquals(1, events.size(), "Incorrect number of messages");
159162
final LogEvent actual = events.get(0);
160-
assertEquals("Incorrect throwable.", expected, actual.getThrown());
163+
assertEquals(expected, actual.getThrown(), "Incorrect throwable.");
161164
listApp.clear();
162165
}
163166

164-
@Before
165-
@After
166-
public void cleanup() {
167+
@BeforeEach
168+
@AfterEach
169+
public void cleanup(
170+
@Named("List") final ListAppender list, @Named("UnformattedList") final ListAppender unformattedList) {
167171
MDC.clear();
168-
ctx.getListAppender("List").clear();
169-
ctx.getListAppender("UnformattedList").clear();
172+
list.clear();
173+
unformattedList.clear();
170174
}
171175
}

0 commit comments

Comments
 (0)