From 8cdfd62dfb6c725bfde22a532ee7c823d8e030bb Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 23 Aug 2018 23:20:24 +0100 Subject: [PATCH] Use a variable free port for test mail server --- .../wiser/assertions/AbstractWiserTest.java | 50 +++++++++---------- .../kemitix/wiser/assertions/Issue6Test.java | 2 +- .../wiser/assertions/WiserAssertionsTest.java | 3 -- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java index cea04a6..7f7121f 100644 --- a/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java +++ b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java @@ -4,40 +4,33 @@ import org.junit.After; import org.junit.Before; import org.subethamail.wiser.Wiser; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.util.Properties; import javax.mail.Session; -/** - * Abstract base class for wiser tests. - * - * @author pcampbell - */ -public abstract class AbstractWiserTest { +abstract class AbstractWiserTest { - /** - * Test mail server port. - */ - protected static final int PORT = 12345; + private int port; - /** - * Test mail server. - */ private Wiser wiser; - /** - * Prepare each test. - */ @Before - @SuppressWarnings("magicnumber") - public void setUp() { - wiser = new Wiser(PORT); + public void setUp() throws IOException { + port = findFreePort(); + wiser = new Wiser(port); wiser.start(); } - /** - * Clean up after each test. - */ + private int findFreePort() throws IOException { + try(final ServerSocket serverSocket = new ServerSocket()) { + serverSocket.bind(new InetSocketAddress(0)); + return serverSocket.getLocalPort(); + } + } + @After public void tearDown() { wiser.stop(); @@ -56,9 +49,16 @@ public abstract class AbstractWiserTest { Properties properties = new Properties(); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.host", "localhost"); - properties.setProperty("mail.smtp.port", "" + PORT); - Session session = Session.getInstance(properties); - return session; + properties.setProperty("mail.smtp.port", "" + port); + return Session.getInstance(properties); } + /** + * The test mail server port. + * + * @return the port he test mail server is running on + */ + protected int getPort() { + return port; + } } diff --git a/src/test/java/net/kemitix/wiser/assertions/Issue6Test.java b/src/test/java/net/kemitix/wiser/assertions/Issue6Test.java index 72296a8..61ae67c 100644 --- a/src/test/java/net/kemitix/wiser/assertions/Issue6Test.java +++ b/src/test/java/net/kemitix/wiser/assertions/Issue6Test.java @@ -26,7 +26,7 @@ public class Issue6Test extends AbstractWiserTest { message.setSubject("Subject"); message.setText("Hi Carl,\n\nA new message was just posted."); final JavaMailSenderImpl sender = new JavaMailSenderImpl(); - sender.setPort(PORT); + sender.setPort(getPort()); //when sender.send(message); //then diff --git a/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java index 83d52f1..0928425 100644 --- a/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java +++ b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java @@ -25,9 +25,6 @@ import javax.mail.internet.MimeMultipart; */ public class WiserAssertionsTest extends AbstractWiserTest { - /** - * Logger. - */ private static final Logger LOG = Logger.getLogger(WiserAssertionsTest.class.getName());