Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate log4j-taglib to JUnit 5 #3227

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions log4j-taglib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,35 @@
package org.apache.logging.log4j.taglib;

import static org.apache.logging.log4j.util.Strings.LINE_SEPARATOR;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import javax.servlet.jsp.tagext.Tag;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockPageContext;

/**
*
*/
@LoggerContextSource("log4j-test1.xml")
public class CatchingTagTest {

private static final String CONFIG = "log4j-test1.xml";

@ClassRule
public static LoggerContextRule context = new LoggerContextRule(CONFIG);

private final Logger logger = context.getLogger("LoggingMessageTagSupportTestLogger");
private final LoggerContext context;
private final Logger logger;
private CatchingTag tag;

@Before
public CatchingTagTest(final LoggerContext context) {
this.context = context;
this.logger = context.getLogger("LoggingMessageTagSupportTestLogger");
}

@BeforeEach
public void setUp() {
this.tag = new CatchingTag();
this.tag.setPageContext(new MockPageContext());
Expand All @@ -54,7 +56,7 @@ public void setUp() {
public void testDoEndTag() throws Exception {
this.tag.setException(new Exception("This is a test."));

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

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

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

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

private void verify(final String expected) {
final ListAppender listApp = context.getListAppender("List");
final ListAppender listApp = context.getConfiguration().getAppender("List");
final List<String> events = listApp.getMessages();
try {
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.CatchingTagTest " + expected + LINE_SEPARATOR, events.get(0));
assertEquals(1, events.size(), "Incorrect number of messages.");
assertEquals("o.a.l.l.t.CatchingTagTest " + expected + LINE_SEPARATOR, events.get(0), "Incorrect message.");
} finally {
listApp.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.logging.log4j.taglib;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
Expand All @@ -26,8 +26,8 @@
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockJspWriter;
import org.springframework.mock.web.MockPageContext;

Expand All @@ -41,7 +41,7 @@ public class DumpTagTest {
private MockPageContext context;
private DumpTag tag;

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

this.writer.flush();
final String output = new String(this.output.toByteArray(), UTF8);
assertEquals("The output is not correct.", "<dl></dl>", output);
assertEquals("<dl></dl>", output, "The output is not correct.");
}

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

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

this.writer.flush();
final String output = new String(this.output.toByteArray(), UTF8);
assertEquals(
"The output is not correct.",
"<dl>" + "<dt><code>testAttribute01</code></dt><dd><code>testValue01</code></dd>"
+ "<dt><code>anotherAttribute02</code></dt><dd><code>finalValue02</code></dd>"
+ "</dl>",
output);
output,
"The output is not correct.");
}

@Test
Expand All @@ -94,11 +94,11 @@ public void testDoEndTagSessionScopeNoAttributes() throws Exception {

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

this.writer.flush();
final String output = new String(this.output.toByteArray(), UTF8);
assertEquals("The output is not correct.", "<dl></dl>", output);
assertEquals("<dl></dl>", output, "The output is not correct.");
}

@Test
Expand All @@ -109,15 +109,15 @@ public void testDoEndTagSessionScope() throws Exception {

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

this.writer.flush();
final String output = new String(this.output.toByteArray(), UTF8);
assertEquals(
"The output is not correct.",
"<dl>" + "<dt><code>coolAttribute01</code></dt><dd><code>weirdValue01</code></dd>"
+ "<dt><code>testAttribute02</code></dt><dd><code>testValue02</code></dd>"
+ "</dl>",
output);
output,
"The output is not correct.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@
*/
package org.apache.logging.log4j.taglib;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import javax.servlet.jsp.tagext.Tag;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockPageContext;

/**
*
*/
@LoggerContextSource("log4j-test1.xml")
public class EnterTagTest {
private static final String CONFIG = "log4j-test1.xml";

@ClassRule
public static LoggerContextRule context = new LoggerContextRule(CONFIG);

private final Logger logger = context.getLogger("LoggingMessageTagSupportTestLogger");
private final LoggerContext context;
private final Logger logger;
private EntryTag tag;
private static final String CONFIG = "log4j-test1.xml";

public EnterTagTest(final LoggerContext context) {
this.context = context;
this.logger = context.getLogger("LoggingMessageTagSupportTestLogger");
}

@Before
@BeforeEach
public void setUp() {
this.tag = new EntryTag();
this.tag.setPageContext(new MockPageContext());
Expand All @@ -49,7 +53,7 @@ public void setUp() {

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

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

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

private void verify(final String expected) {
final ListAppender listApp = context.getListAppender("List");
final ListAppender listApp = context.getConfiguration().getAppender("List");
final List<String> events = listApp.getMessages();
try {
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.EnterTagTest " + expected, events.get(0));
assertEquals(1, events.size(), "Incorrect number of messages.");
assertEquals("o.a.l.l.t.EnterTagTest " + expected, events.get(0), "Incorrect message.");
} finally {
listApp.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
*/
package org.apache.logging.log4j.taglib;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
*
*/
public class ExceptionAwareTagSupportTest {
private ExceptionAwareTagSupport tag;

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

@Test
public void testException() {
assertNull("The exception should be null (1).", this.tag.getException());
assertNull(this.tag.getException(), "The exception should be null (1).");

Exception e = new Exception();
this.tag.setException(e);
assertSame("The exception is not correct (1).", e, this.tag.getException());
assertSame(e, this.tag.getException(), "The exception is not correct (1).");

this.tag.init();
assertNull("The exception should be null (2).", this.tag.getException());
assertNull(this.tag.getException(), "The exception should be null (2).");

e = new RuntimeException();
this.tag.setException(e);
assertSame("The exception is not correct (2).", e, this.tag.getException());
assertSame(e, this.tag.getException(), "The exception is not correct (2).");
}
}
Loading