Skip to content

Migrate log4j-jakarta-smtp to JUnit 5 #3052

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

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
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