Skip to content

Commit 40f1bde

Browse files
committed
Simplify log4j-jcl tests (#2163)
1 parent 377baf5 commit 40f1bde

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@
1616
*/
1717
package org.apache.logging.log4j.jcl;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
21-
import java.util.List;
2219
import org.apache.commons.logging.Log;
2320
import org.apache.commons.logging.LogFactory;
2421
import org.apache.logging.log4j.core.LoggerContext;
2522
import org.apache.logging.log4j.core.test.appender.ListAppender;
26-
import org.apache.logging.log4j.test.junit.SetTestProperty;
27-
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2824
import org.junit.jupiter.api.Test;
2925

30-
@UsingStatusListener
31-
@SetTestProperty(key = "log4j2.configurationFile", value = "org/apache/logging/log4j/jcl/CallerInformationTest.xml")
26+
import java.util.List;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
3230
public class CallerInformationTest {
3331

3432
@Test
35-
public void testClassLogger() throws Exception {
36-
final LoggerContext ctx = LoggerContext.getContext(false);
33+
@LoggerContextSource("CallerInformationTest.xml")
34+
public void testClassLogger(final LoggerContext ctx) {
3735
final ListAppender app = ctx.getConfiguration().getAppender("Class");
3836
app.clear();
3937
final Log logger = LogFactory.getLog("ClassLogger");
@@ -45,8 +43,8 @@ public void testClassLogger() throws Exception {
4543
}
4644

4745
@Test
48-
public void testMethodLogger() throws Exception {
49-
final LoggerContext ctx = LoggerContext.getContext(false);
46+
@LoggerContextSource("CallerInformationTest.xml")
47+
public void testMethodLogger(final LoggerContext ctx) {
5048
final ListAppender app = ctx.getConfiguration().getAppender("Method");
5149
app.clear();
5250
final Log logger = LogFactory.getLog("MethodLogger");

log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
*/
1717
package org.apache.logging.log4j.jcl;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
21-
import java.util.List;
2219
import org.apache.commons.logging.Log;
2320
import org.apache.commons.logging.LogFactory;
2421
import org.apache.logging.log4j.core.LoggerContext;
2522
import org.apache.logging.log4j.core.test.appender.ListAppender;
26-
import org.apache.logging.log4j.test.junit.SetTestProperty;
27-
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2824
import org.apache.logging.log4j.util.Strings;
2925
import org.junit.jupiter.api.Test;
3026

31-
@UsingStatusListener
32-
@SetTestProperty(key = "log4j2.configurationFile", value = "org/apache/logging/log4j/jcl/LoggerTest.xml")
27+
import java.util.List;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
3331
class LoggerTest {
3432

3533
@Test
@@ -39,21 +37,21 @@ void testFactory() {
3937
}
4038

4139
@Test
42-
void testLog() {
40+
@LoggerContextSource("LoggerTest.xml")
41+
void testLog(final LoggerContext context) {
4342
final Log logger = LogFactory.getLog("LoggerTest");
4443
logger.debug("Test message");
45-
verify("List", "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
44+
verify(context, "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
4645
logger.debug("Exception: ", new NullPointerException("Test"));
47-
verify("List", "o.a.l.l.j.LoggerTest Exception: MDC{}" + Strings.LINE_SEPARATOR);
46+
verify(context, "o.a.l.l.j.LoggerTest Exception: MDC{}" + Strings.LINE_SEPARATOR);
4847
logger.info("Info Message");
49-
verify("List", "o.a.l.l.j.LoggerTest Info Message MDC{}" + Strings.LINE_SEPARATOR);
48+
verify(context, "o.a.l.l.j.LoggerTest Info Message MDC{}" + Strings.LINE_SEPARATOR);
5049
logger.info("Info Message {}");
51-
verify("List", "o.a.l.l.j.LoggerTest Info Message {} MDC{}" + Strings.LINE_SEPARATOR);
50+
verify(context, "o.a.l.l.j.LoggerTest Info Message {} MDC{}" + Strings.LINE_SEPARATOR);
5251
}
5352

54-
private void verify(final String name, final String expected) {
55-
final LoggerContext context = LoggerContext.getContext(false);
56-
final ListAppender listApp = context.getConfiguration().getAppender(name);
53+
private static void verify(final LoggerContext context, final String expected) {
54+
final ListAppender listApp = context.getConfiguration().getAppender("List");
5755
final List<String> events = listApp.getMessages();
5856
assertThat(events).hasSize(1).containsExactly(expected);
5957
listApp.clear();
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)