Skip to content

Commit

Permalink
Migrate log4j-jakarta-smtp to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbaHerrerias committed Oct 1, 2024
1 parent 832cade commit 241fb9b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
6 changes: 0 additions & 6 deletions log4j-jakarta-smtp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,50 @@
import java.util.Iterator;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.AvailablePortFinder;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.smtp.SimpleSmtpServer;
import org.apache.logging.log4j.core.test.smtp.SmtpMessage;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SmtpAppenderAsyncTest {

private static int PORT;

private SimpleSmtpServer smtpServer;

@BeforeClass
public static void setupClass() {
@BeforeAll
public static void setupAll() {
PORT = AvailablePortFinder.getNextAvailable();
System.setProperty("smtp.port", String.valueOf(PORT));
}

@Before
@BeforeEach
public void setup() {
smtpServer = SimpleSmtpServer.start(PORT);
}

@Rule
public LoggerContextRule ctx = new LoggerContextRule("SmtpAppenderAsyncTest.xml");

@Test
public void testSync() {
testSmtpAppender(ctx.getLogger("sync"));
@LoggerContextSource("SmtpAppenderAsyncTest.xml")
public void testSync(final LoggerContext ctx) {
testSmtpAppender(ctx.getLogger("sync"), ctx);
}

@Test
public void testAsync() {
testSmtpAppender(ctx.getLogger("async"));
@LoggerContextSource("SmtpAppenderAsyncTest.xml")
public void testAsync(final LoggerContext ctx) {
testSmtpAppender(ctx.getLogger("async"), ctx);
}

private void testSmtpAppender(final Logger logger) {
private void testSmtpAppender(final Logger logger, final LoggerContext ctx) {
ThreadContext.put("MDC1", "mdc1");
logger.error("the message");
ctx.getLoggerContext().stop();
ctx.stop();
smtpServer.stop();

assertEquals(1, smtpServer.getReceivedEmailSize());
Expand All @@ -83,15 +82,15 @@ private void testSmtpAppender(final Logger logger) {
}
}

@After
@AfterEach
public void teardown() {
if (smtpServer != null) {
smtpServer.stop();
}
}

@AfterClass
public static void teardownClass() {
@AfterAll
public static void teardownAll() {
System.clearProperty("smtp.port");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.apache.logging.log4j.smtp;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import jakarta.mail.Address;
import jakarta.mail.Message;
Expand All @@ -34,13 +34,12 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.SmtpAppender;
import org.apache.logging.log4j.core.test.AvailablePortFinder;
import org.apache.logging.log4j.core.test.categories.Appenders;
import org.apache.logging.log4j.core.test.smtp.SimpleSmtpServer;
import org.apache.logging.log4j.core.test.smtp.SmtpMessage;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Category(Appenders.Smtp.class)
@Tag("Appenders.Smtp")
public class SmtpAppenderTest {

private static final String HOST = "localhost";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.logging.log4j.core.util.DummyNanoClock;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.logging.log4j.message.ReusableSimpleMessage;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class SmtpManagerTest {

Expand Down

0 comments on commit 241fb9b

Please sign in to comment.