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.junit.Before;
import org.subethamail.wiser.Wiser; import org.subethamail.wiser.Wiser;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Properties; import java.util.Properties;
import javax.mail.Session; import javax.mail.Session;
/** abstract class AbstractWiserTest {
* Abstract base class for wiser tests.
*
* @author pcampbell
*/
public abstract class AbstractWiserTest {
/** private int port;
* Test mail server port.
*/
protected static final int PORT = 12345;
/**
* Test mail server.
*/
private Wiser wiser; private Wiser wiser;
/**
* Prepare each test.
*/
@Before @Before
@SuppressWarnings("magicnumber") public void setUp() throws IOException {
public void setUp() { port = findFreePort();
wiser = new Wiser(PORT); wiser = new Wiser(port);
wiser.start(); wiser.start();
} }
/** private int findFreePort() throws IOException {
* Clean up after each test. try(final ServerSocket serverSocket = new ServerSocket()) {
*/ serverSocket.bind(new InetSocketAddress(0));
return serverSocket.getLocalPort();
}
}
@After @After
public void tearDown() { public void tearDown() {
wiser.stop(); wiser.stop();
@ -56,9 +49,16 @@ public abstract class AbstractWiserTest {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "localhost"); properties.setProperty("mail.smtp.host", "localhost");
properties.setProperty("mail.smtp.port", "" + PORT); properties.setProperty("mail.smtp.port", "" + port);
Session session = Session.getInstance(properties); return Session.getInstance(properties);
return session;
} }
/**
* 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.setSubject("Subject");
message.setText("Hi Carl,\n\nA new message was just posted."); message.setText("Hi Carl,\n\nA new message was just posted.");
final JavaMailSenderImpl sender = new JavaMailSenderImpl(); final JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setPort(PORT); sender.setPort(getPort());
//when //when
sender.send(message); sender.send(message);
//then //then

View file

@ -25,9 +25,6 @@ import javax.mail.internet.MimeMultipart;
*/ */
public class WiserAssertionsTest extends AbstractWiserTest { public class WiserAssertionsTest extends AbstractWiserTest {
/**
* Logger.
*/
private static final Logger LOG private static final Logger LOG
= Logger.getLogger(WiserAssertionsTest.class.getName()); = Logger.getLogger(WiserAssertionsTest.class.getName());