From 262283d628565cd7b2576495ac8a241367f42572 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 25 Jan 2016 11:53:37 +0000 Subject: [PATCH] Refactor common elements of testing to AbstractWiserTest Signed-off-by: Paul Campbell --- .../wiser/assertions/AbstractWiserTest.java | 63 +++++++++++++++++++ .../wiser/assertions/WiserAssertionsTest.java | 49 +-------------- 2 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java diff --git a/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java new file mode 100644 index 0000000..56282f8 --- /dev/null +++ b/src/test/java/net/kemitix/wiser/assertions/AbstractWiserTest.java @@ -0,0 +1,63 @@ +package net.kemitix.wiser.assertions; + +import org.junit.After; +import org.junit.Before; +import org.subethamail.wiser.Wiser; + +import java.util.Properties; + +import javax.mail.Session; + +/** + * Abstract base class for wiser tests. + * + * @author pcampbell + */ +public abstract class AbstractWiserTest { + + /** + * Test mail server port. + */ + protected static final int PORT = 12345; + + /** + * Test mail server. + */ + private Wiser wiser; + + /** + * Prepare each test. + */ + @Before + @SuppressWarnings("magicnumber") + public void setUp() { + wiser = new Wiser(PORT); + wiser.start(); + } + + /** + * Clean up after each test. + */ + @After + public void tearDown() { + wiser.stop(); + } + + /** + * Instantiates the WiserAssertions. + * + * @return the wiser assertions + */ + protected WiserAssertions getAssertions() { + return WiserAssertions.assertReceivedMessage(wiser); + } + + protected Session getSession() { + Properties properties = new Properties(); + properties.setProperty("mail.smtp.host", "localhost"); + properties.setProperty("mail.smtp.port", "" + WiserAssertionsTest.PORT); + Session session = Session.getDefaultInstance(properties); + return session; + } + +} diff --git a/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java index 891f608..34890a8 100644 --- a/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java +++ b/src/test/java/net/kemitix/wiser/assertions/WiserAssertionsTest.java @@ -1,21 +1,17 @@ package net.kemitix.wiser.assertions; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.subethamail.wiser.Wiser; import static org.junit.Assert.assertNotNull; import java.io.IOException; -import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; -import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; @@ -27,7 +23,7 @@ import javax.mail.internet.MimeMultipart; * * @author pcampbell */ -public class WiserAssertionsTest { +public class WiserAssertionsTest extends AbstractWiserTest { /** * Logger. @@ -35,34 +31,6 @@ public class WiserAssertionsTest { private static final Logger LOG = Logger.getLogger(WiserAssertionsTest.class.getName()); - /** - * Test mail server. - */ - private Wiser wiser; - - /** - * Prepare each test. - */ - @Before - @SuppressWarnings("magicnumber") - public void setUp() { - wiser = new Wiser(PORT); - wiser.start(); - } - - /** - * Test mail server port. - */ - private static final int PORT = 12345; - - /** - * Clean up after each test. - */ - @After - public void tearDown() { - wiser.stop(); - } - /** * Sends a mime multipart message to the Wiser server. * @@ -76,12 +44,8 @@ public class WiserAssertionsTest { final String to, final String subject, final String body) { - Properties properties = new Properties(); - properties.setProperty("mail.smtp.host", "localhost"); - properties.setProperty("mail.smtp.port", "" + PORT); - Session session = Session.getDefaultInstance(properties); try { - MimeMessage message = new MimeMessage(session); + MimeMessage message = new MimeMessage(getSession()); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, to); message.setSubject(subject, "UTF-8"); @@ -96,15 +60,6 @@ public class WiserAssertionsTest { } } - /** - * Instantiates the WiserAssertions. - * - * @return the wiser assertions - */ - private WiserAssertions getAssertions() { - return WiserAssertions.assertReceivedMessage(wiser); - } - /** * Test {@link WiserAssertions#withContent(java.lang.String)} where the * content of the email matches.