Skip to content

Commit 794cd6e

Browse files
committed
Migrate log4j-taglib to JUnit 5
1 parent caffa21 commit 794cd6e

14 files changed

+288
-313
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/DumpTagTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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.io.ByteArrayOutputStream;
2222
import java.io.OutputStreamWriter;
@@ -26,8 +26,8 @@
2626
import javax.servlet.jsp.JspWriter;
2727
import javax.servlet.jsp.PageContext;
2828
import javax.servlet.jsp.tagext.Tag;
29-
import org.junit.Before;
30-
import org.junit.Test;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
3131
import org.springframework.mock.web.MockJspWriter;
3232
import org.springframework.mock.web.MockPageContext;
3333

@@ -41,7 +41,7 @@ public class DumpTagTest {
4141
private MockPageContext context;
4242
private DumpTag tag;
4343

44-
@Before
44+
@BeforeEach
4545
public void setUp() {
4646
this.output = new ByteArrayOutputStream();
4747
this.writer = new OutputStreamWriter(this.output, UTF8);
@@ -62,11 +62,11 @@ public JspWriter getOut() {
6262
@Test
6363
public void testDoEndTagDefaultPageScopeNoAttributes() throws Exception {
6464
final int returnValue = this.tag.doEndTag();
65-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue);
65+
assertEquals(Tag.EVAL_PAGE, returnValue, "The return value is not correct.");
6666

6767
this.writer.flush();
6868
final String output = new String(this.output.toByteArray(), UTF8);
69-
assertEquals("The output is not correct.", "<dl></dl>", output);
69+
assertEquals("<dl></dl>", output, "The output is not correct.");
7070
}
7171

7272
@Test
@@ -76,16 +76,16 @@ public void testDoEndTagDefaultPageScope() throws Exception {
7676
this.context.setAttribute("badAttribute03", "skippedValue03", PageContext.SESSION_SCOPE);
7777

7878
final int returnValue = this.tag.doEndTag();
79-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue);
79+
assertEquals(Tag.EVAL_PAGE, returnValue, "The return value is not correct.");
8080

8181
this.writer.flush();
8282
final String output = new String(this.output.toByteArray(), UTF8);
8383
assertEquals(
84-
"The output is not correct.",
8584
"<dl>" + "<dt><code>testAttribute01</code></dt><dd><code>testValue01</code></dd>"
8685
+ "<dt><code>anotherAttribute02</code></dt><dd><code>finalValue02</code></dd>"
8786
+ "</dl>",
88-
output);
87+
output,
88+
"The output is not correct.");
8989
}
9090

9191
@Test
@@ -94,11 +94,11 @@ public void testDoEndTagSessionScopeNoAttributes() throws Exception {
9494

9595
this.tag.setScope("session");
9696
final int returnValue = this.tag.doEndTag();
97-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue);
97+
assertEquals(Tag.EVAL_PAGE, returnValue, "The return value is not correct.");
9898

9999
this.writer.flush();
100100
final String output = new String(this.output.toByteArray(), UTF8);
101-
assertEquals("The output is not correct.", "<dl></dl>", output);
101+
assertEquals("<dl></dl>", output, "The output is not correct.");
102102
}
103103

104104
@Test
@@ -109,15 +109,15 @@ public void testDoEndTagSessionScope() throws Exception {
109109

110110
this.tag.setScope("session");
111111
final int returnValue = this.tag.doEndTag();
112-
assertEquals("The return value is not correct.", Tag.EVAL_PAGE, returnValue);
112+
assertEquals(Tag.EVAL_PAGE, returnValue, "The return value is not correct.");
113113

114114
this.writer.flush();
115115
final String output = new String(this.output.toByteArray(), UTF8);
116116
assertEquals(
117-
"The output is not correct.",
118117
"<dl>" + "<dt><code>coolAttribute01</code></dt><dd><code>weirdValue01</code></dd>"
119118
+ "<dt><code>testAttribute02</code></dt><dd><code>testValue02</code></dd>"
120119
+ "</dl>",
121-
output);
120+
output,
121+
"The output is not correct.");
122122
}
123123
}

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/ExceptionAwareTagSupportTest.java

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

19-
import static org.junit.Assert.assertNull;
20-
import static org.junit.Assert.assertSame;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertSame;
2121

22-
import org.junit.Before;
23-
import org.junit.Test;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
2424

2525
/**
2626
*
2727
*/
2828
public class ExceptionAwareTagSupportTest {
2929
private ExceptionAwareTagSupport tag;
3030

31-
@Before
31+
@BeforeEach
3232
public void setUp() {
3333
this.tag = new ExceptionAwareTagSupport() {
3434
private static final long serialVersionUID = 1L;
@@ -37,17 +37,17 @@ public void setUp() {
3737

3838
@Test
3939
public void testException() {
40-
assertNull("The exception should be null (1).", this.tag.getException());
40+
assertNull(this.tag.getException(), "The exception should be null (1).");
4141

4242
Exception e = new Exception();
4343
this.tag.setException(e);
44-
assertSame("The exception is not correct (1).", e, this.tag.getException());
44+
assertSame(e, this.tag.getException(), "The exception is not correct (1).");
4545

4646
this.tag.init();
47-
assertNull("The exception should be null (2).", this.tag.getException());
47+
assertNull(this.tag.getException(), "The exception should be null (2).");
4848

4949
e = new RuntimeException();
5050
this.tag.setException(e);
51-
assertSame("The exception is not correct (2).", e, this.tag.getException());
51+
assertSame(e, this.tag.getException(), "The exception is not correct (2).");
5252
}
5353
}

0 commit comments

Comments
 (0)