Skip to content

Commit

Permalink
Migrate log4j-taglib to JUnit 5 (#3227)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbaHerrerias authored Dec 10, 2024
1 parent 43a0e29 commit a8e2779
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 121 deletions.
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,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,31 +16,34 @@
*/
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 ExitTagTest {
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 ExitTag tag;

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

@BeforeEach
public void setUp() {
this.tag = new ExitTag();
this.tag.setPageContext(new MockPageContext());
Expand All @@ -49,32 +52,32 @@ 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("Exit TRACE M-EXIT[ FLOW ] E");
}

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

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("Exit with(log4j-test1.xml) TRACE M-EXIT[ FLOW ] E");
}

@Test
public void testDoEndTagResult02() throws Exception {
this.tag.setResult(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("Exit with(5792) TRACE M-EXIT[ 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.ExitTagTest " + expected, events.get(0));
assertEquals(1, events.size(), "Incorrect number of messages.");
assertEquals("o.a.l.l.t.ExitTagTest " + expected, events.get(0), "Incorrect message.");
} finally {
listApp.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@
*/
package org.apache.logging.log4j.taglib;

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

import javax.servlet.jsp.tagext.Tag;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.MarkerManager;
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.LoggerContext;
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 IfEnabledTagTest {
private static final String CONFIG = "log4j-test1.xml";

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

private final Logger logger = context.getLogger("IfEnabledTagTest");
private final Logger logger;
private IfEnabledTag tag;

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

@BeforeEach
public void setUp() {
this.tag = new IfEnabledTag();
this.tag.setPageContext(new MockPageContext());
Expand All @@ -51,59 +52,59 @@ public void setUp() {
public void testDoStartTagEnabledString() throws Exception {
this.tag.setLevel("warn");

assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag());
assertEquals(Tag.EVAL_BODY_INCLUDE, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
}

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

assertEquals("The return value is not correct.", Tag.SKIP_BODY, this.tag.doStartTag());
assertEquals(Tag.SKIP_BODY, this.tag.doStartTag(), "The return value is not correct.");
}
}
Loading

0 comments on commit a8e2779

Please sign in to comment.