Merge pull request #33 from kemitix/31-dynamic-port

Use a variable free port for test mail server
This commit is contained in:
Paul Campbell 2018-08-24 19:52:19 +01:00 committed by GitHub
commit c8139986f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 29 deletions

View file

@ -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;
}
}

View file

@ -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

View file

@ -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());