Skip to content

Commit a8e2779

Browse files
Migrate log4j-taglib to JUnit 5 (#3227)
1 parent 43a0e29 commit a8e2779

File tree

7 files changed

+127
-121
lines changed

7 files changed

+127
-121
lines changed

log4j-taglib/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@
8989
<artifactId>junit-jupiter-engine</artifactId>
9090
<scope>test</scope>
9191
</dependency>
92-
<dependency>
93-
<groupId>org.junit.vintage</groupId>
94-
<artifactId>junit-vintage-engine</artifactId>
95-
<scope>test</scope>
96-
</dependency>
9792
<dependency>
9893
<groupId>org.springframework</groupId>
9994
<artifactId>spring-test</artifactId>

log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,35 @@
1717
package org.apache.logging.log4j.taglib;
1818

1919
import static org.apache.logging.log4j.util.Strings.LINE_SEPARATOR;
20-
import static org.junit.Assert.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2121

2222
import java.util.List;
2323
import javax.servlet.jsp.tagext.Tag;
2424
import org.apache.logging.log4j.Level;
2525
import org.apache.logging.log4j.Logger;
26+
import org.apache.logging.log4j.core.LoggerContext;
2627
import org.apache.logging.log4j.core.test.appender.ListAppender;
27-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
28-
import org.junit.Before;
29-
import org.junit.ClassRule;
30-
import org.junit.Test;
28+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
3131
import org.springframework.mock.web.MockPageContext;
3232

3333
/**
3434
*
3535
*/
36+
@LoggerContextSource("log4j-test1.xml")
3637
public class CatchingTagTest {
3738

38-
private static final String CONFIG = "log4j-test1.xml";
39-
40-
@ClassRule
41-
public static LoggerContextRule context = new LoggerContextRule(CONFIG);
42-
43-
private final Logger logger = context.getLogger("LoggingMessageTagSupportTestLogger");
39+
private final LoggerContext context;
40+
private final Logger logger;
4441
private CatchingTag tag;
4542

46-
@Before
43+
public CatchingTagTest(final LoggerContext context) {
44+
this.context = context;
45+
this.logger = context.getLogger("LoggingMessageTagSupportTestLogger");
46+
}
47+
48+
@BeforeEach
4749
public void setUp() {
4850
this.tag = new CatchingTag();
4951
this.tag.setPageContext(new MockPageContext());
@@ -54,7 +56,7 @@ public void setUp() {
5456
public void testDoEndTag() throws Exception {
5557
this.tag.setException(new Exception("This is a test."));
5658

57-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
59+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
5860
verify("Catching ERROR M-CATCHING[ EXCEPTION ] E" + LINE_SEPARATOR + "java.lang.Exception: This is a test.");
5961
}
6062

@@ -63,7 +65,7 @@ public void testDoEndTagLevelString() throws Exception {
6365
this.tag.setLevel("info");
6466
this.tag.setException(new RuntimeException("This is another test."));
6567

66-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
68+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
6769
verify("Catching INFO M-CATCHING[ EXCEPTION ] E" + LINE_SEPARATOR
6870
+ "java.lang.RuntimeException: This is another test.");
6971
}
@@ -73,16 +75,16 @@ public void testDoEndTagLevelObject() throws Exception {
7375
this.tag.setLevel(Level.WARN);
7476
this.tag.setException(new Error("This is the last test."));
7577

76-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
78+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
7779
verify("Catching WARN M-CATCHING[ EXCEPTION ] E" + LINE_SEPARATOR + "java.lang.Error: This is the last test.");
7880
}
7981

8082
private void verify(final String expected) {
81-
final ListAppender listApp = context.getListAppender("List");
83+
final ListAppender listApp = context.getConfiguration().getAppender("List");
8284
final List<String> events = listApp.getMessages();
8385
try {
84-
assertEquals("Incorrect number of messages.", 1, events.size());
85-
assertEquals("Incorrect message.", "o.a.l.l.t.CatchingTagTest " + expected + LINE_SEPARATOR, events.get(0));
86+
assertEquals(1, events.size(), "Incorrect number of messages.");
87+
assertEquals("o.a.l.l.t.CatchingTagTest " + expected + LINE_SEPARATOR, events.get(0), "Incorrect message.");
8688
} finally {
8789
listApp.clear();
8890
}

log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EnterTagTest.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,35 @@
1616
*/
1717
package org.apache.logging.log4j.taglib;
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 javax.servlet.jsp.tagext.Tag;
2323
import org.apache.logging.log4j.Logger;
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.junit.Before;
27-
import org.junit.ClassRule;
28-
import org.junit.Test;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929
import org.springframework.mock.web.MockPageContext;
3030

3131
/**
3232
*
3333
*/
34+
@LoggerContextSource("log4j-test1.xml")
3435
public class EnterTagTest {
35-
private static final String CONFIG = "log4j-test1.xml";
36-
37-
@ClassRule
38-
public static LoggerContextRule context = new LoggerContextRule(CONFIG);
3936

40-
private final Logger logger = context.getLogger("LoggingMessageTagSupportTestLogger");
37+
private final LoggerContext context;
38+
private final Logger logger;
4139
private EntryTag tag;
40+
private static final String CONFIG = "log4j-test1.xml";
41+
42+
public EnterTagTest(final LoggerContext context) {
43+
this.context = context;
44+
this.logger = context.getLogger("LoggingMessageTagSupportTestLogger");
45+
}
4246

43-
@Before
47+
@BeforeEach
4448
public void setUp() {
4549
this.tag = new EntryTag();
4650
this.tag.setPageContext(new MockPageContext());
@@ -49,7 +53,7 @@ public void setUp() {
4953

5054
@Test
5155
public void testDoEndTag() throws Exception {
52-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
56+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
5357
verify("Enter TRACE M-ENTER[ FLOW ] E");
5458
}
5559

@@ -58,16 +62,16 @@ public void testDoEndTagAttributes() throws Exception {
5862
this.tag.setDynamicAttribute(null, null, CONFIG);
5963
this.tag.setDynamicAttribute(null, null, 5792);
6064

61-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
65+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
6266
verify("Enter params(log4j-test1.xml, 5792) TRACE M-ENTER[ FLOW ] E");
6367
}
6468

6569
private void verify(final String expected) {
66-
final ListAppender listApp = context.getListAppender("List");
70+
final ListAppender listApp = context.getConfiguration().getAppender("List");
6771
final List<String> events = listApp.getMessages();
6872
try {
69-
assertEquals("Incorrect number of messages.", 1, events.size());
70-
assertEquals("Incorrect message.", "o.a.l.l.t.EnterTagTest " + expected, events.get(0));
73+
assertEquals(1, events.size(), "Incorrect number of messages.");
74+
assertEquals("o.a.l.l.t.EnterTagTest " + expected, events.get(0), "Incorrect message.");
7175
} finally {
7276
listApp.clear();
7377
}

log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,34 @@
1616
*/
1717
package org.apache.logging.log4j.taglib;
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 javax.servlet.jsp.tagext.Tag;
2323
import org.apache.logging.log4j.Logger;
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.junit.Before;
27-
import org.junit.ClassRule;
28-
import org.junit.Test;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929
import org.springframework.mock.web.MockPageContext;
3030

3131
/**
3232
*
3333
*/
34+
@LoggerContextSource("log4j-test1.xml")
3435
public class ExitTagTest {
3536
private static final String CONFIG = "log4j-test1.xml";
36-
37-
@ClassRule
38-
public static LoggerContextRule context = new LoggerContextRule(CONFIG);
39-
40-
private final Logger logger = context.getLogger("LoggingMessageTagSupportTestLogger");
37+
private final LoggerContext context;
38+
private final Logger logger;
4139
private ExitTag tag;
4240

43-
@Before
41+
public ExitTagTest(final LoggerContext context) {
42+
this.context = context;
43+
this.logger = context.getLogger("LoggingMessageTagSupportTestLogger");
44+
}
45+
46+
@BeforeEach
4447
public void setUp() {
4548
this.tag = new ExitTag();
4649
this.tag.setPageContext(new MockPageContext());
@@ -49,32 +52,32 @@ public void setUp() {
4952

5053
@Test
5154
public void testDoEndTag() throws Exception {
52-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
55+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
5356
verify("Exit TRACE M-EXIT[ FLOW ] E");
5457
}
5558

5659
@Test
5760
public void testDoEndTagResult01() throws Exception {
5861
this.tag.setResult(CONFIG);
5962

60-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
63+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
6164
verify("Exit with(log4j-test1.xml) TRACE M-EXIT[ FLOW ] E");
6265
}
6366

6467
@Test
6568
public void testDoEndTagResult02() throws Exception {
6669
this.tag.setResult(5792);
6770

68-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
71+
assertEquals(Tag.EVAL_PAGE, this.tag.doEndTag(), "The return value is not correct.");
6972
verify("Exit with(5792) TRACE M-EXIT[ FLOW ] E");
7073
}
7174

7275
private void verify(final String expected) {
73-
final ListAppender listApp = context.getListAppender("List");
76+
final ListAppender listApp = context.getConfiguration().getAppender("List");
7477
final List<String> events = listApp.getMessages();
7578
try {
76-
assertEquals("Incorrect number of messages.", 1, events.size());
77-
assertEquals("Incorrect message.", "o.a.l.l.t.ExitTagTest " + expected, events.get(0));
79+
assertEquals(1, events.size(), "Incorrect number of messages.");
80+
assertEquals("o.a.l.l.t.ExitTagTest " + expected, events.get(0), "Incorrect message.");
7881
} finally {
7982
listApp.clear();
8083
}

log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java

+20-19
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,32 @@
1616
*/
1717
package org.apache.logging.log4j.taglib;
1818

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

2121
import javax.servlet.jsp.tagext.Tag;
2222
import org.apache.logging.log4j.Level;
2323
import org.apache.logging.log4j.Logger;
2424
import org.apache.logging.log4j.MarkerManager;
25-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
26-
import org.junit.Before;
27-
import org.junit.ClassRule;
28-
import org.junit.Test;
25+
import org.apache.logging.log4j.core.LoggerContext;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929
import org.springframework.mock.web.MockPageContext;
3030

3131
/**
3232
*
3333
*/
34+
@LoggerContextSource("log4j-test1.xml")
3435
public class IfEnabledTagTest {
35-
private static final String CONFIG = "log4j-test1.xml";
3636

37-
@ClassRule
38-
public static LoggerContextRule context = new LoggerContextRule(CONFIG);
39-
40-
private final Logger logger = context.getLogger("IfEnabledTagTest");
37+
private final Logger logger;
4138
private IfEnabledTag tag;
4239

43-
@Before
40+
public IfEnabledTagTest(final LoggerContext context) {
41+
this.logger = context.getLogger("IfEnabledTagTest");
42+
}
43+
44+
@BeforeEach
4445
public void setUp() {
4546
this.tag = new IfEnabledTag();
4647
this.tag.setPageContext(new MockPageContext());
@@ -51,59 +52,59 @@ public void setUp() {
5152
public void testDoStartTagEnabledString() throws Exception {
5253
this.tag.setLevel("warn");
5354

54-
assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
55+
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
5556
}
5657

5758
@Test
5859
public void testDoStartTagEnabledLevel() throws Exception {
5960
this.tag.setLevel(Level.WARN);
6061

61-
assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
62+
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
6263
}
6364

6465
@Test
6566
public void testDoStartTagEnabledStringMarker() throws Exception {
6667
this.tag.setMarker(MarkerManager.getMarker("E01"));
6768
this.tag.setLevel("error");
6869

69-
assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
70+
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
7071
}
7172

7273
@Test
7374
public void testDoStartTagEnabledLevelMarker() throws Exception {
7475
this.tag.setMarker(MarkerManager.getMarker("F02"));
7576
this.tag.setLevel(Level.ERROR);
7677

77-
assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
78+
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
7879
}
7980

8081
@Test
8182
public void testDoStartTagDisabledString() throws Exception {
8283
this.tag.setLevel("info");
8384

84-
assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
85+
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
8586
}
8687

8788
@Test
8889
public void testDoStartTagDisabledLevel() throws Exception {
8990
this.tag.setLevel(Level.INFO);
9091

91-
assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
92+
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
9293
}
9394

9495
@Test
9596
public void testDoStartTagDisabledStringMarker() throws Exception {
9697
this.tag.setMarker(MarkerManager.getMarker("E01"));
9798
this.tag.setLevel("trace");
9899

99-
assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
100+
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
100101
}
101102

102103
@Test
103104
public void testDoStartTagDisabledLevelMarker() throws Exception {
104105
this.tag.setMarker(MarkerManager.getMarker("F02"));
105106
this.tag.setLevel(Level.TRACE);
106107

107-
assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
108+
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
108109
}
109110
}

0 commit comments

Comments
 (0)