Merge branch release/0.3.0 into master

Add support for nested multi-part emails [jonjo-manywho] [#1]

Signed-off-by: Paul Campbell <pcampbell@kemitix.net>
This commit is contained in:
Paul Campbell 2016-01-25 13:28:47 +00:00
commit cbb3b12832
7 changed files with 144 additions and 66 deletions

View file

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
0.3.0
------
* Add support for nested multi-part emails [jonjo-manywho] [#1]
0.2.0 0.2.0
------ ------

View file

@ -78,15 +78,19 @@
<module name="TreeWalker"> <module name="TreeWalker">
<!-- Support @SuppressWarnings annotation --> <!-- Support @SuppressWarnings annotation -->
<!-- See http://checkstyle.sourceforge.net/config.html --> <!-- See http://checkstyle.sourceforge.net/config.html -->
<module name="SuppressWarningsHolder"/> <module name="SuppressWarningsHolder"/>
<module name="FileContentsHolder"/>
<!-- Checks for Javadoc comments. --> <!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html --> <!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocMethod"/> <module name="JavadocMethod">
<property name="scope" value="public"/>
</module>
<module name="JavadocType"/> <module name="JavadocType"/>
<module name="JavadocVariable"/> <!--<module name="JavadocVariable"/>-->
<module name="JavadocStyle"/> <module name="JavadocStyle"/>
@ -113,12 +117,7 @@
<!-- Checks for Size Violations. --> <!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html --> <!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="LineLength"> <module name="LineLength"/>
<!-- Allow for long imports for Spring Batch -->
<!-- EnableBatchProcessing, JobBuilderFactory -->
<!-- and StepBuilderFactory -->
<property name="ignorePattern" value="^import"/>
</module>
<module name="MethodLength"/> <module name="MethodLength"/>
<module name="ParameterNumber"/> <module name="ParameterNumber"/>
@ -157,7 +156,11 @@
<module name="AvoidInlineConditionals"/> <module name="AvoidInlineConditionals"/>
<module name="EmptyStatement"/> <module name="EmptyStatement"/>
<module name="EqualsHashCode"/> <module name="EqualsHashCode"/>
<module name="HiddenField"/> <module name="HiddenField">
<property name="ignoreConstructorParameter" value="true"/>
<property name="ignoreSetter" value="true"/>
<property name="setterCanReturnItsClass" value="true"/>
</module>
<module name="IllegalInstantiation"/> <module name="IllegalInstantiation"/>
<module name="InnerAssignment"/> <module name="InnerAssignment"/>
<module name="MagicNumber"/> <module name="MagicNumber"/>
@ -183,6 +186,7 @@
</module> </module>
<module name="SuppressWarningsFilter"/> <module name="SuppressWarningsFilter"/>
<module name="SuppressionCommentFilter"/>
</module> </module>

15
pom.xml
View file

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>wiser-assertions</artifactId> <artifactId>wiser-assertions</artifactId>
<version>0.2.0</version> <version>0.3.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>wiser-assertions</name> <name>wiser-assertions</name>
@ -30,8 +30,8 @@
<parent> <parent>
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-spring-parent</artifactId> <artifactId>kemitix-parent</artifactId>
<version>1.2.1</version> <version>0.6.1</version>
</parent> </parent>
<dependencies> <dependencies>
@ -48,13 +48,20 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codemonkey.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>2.5.1</version>
<scope>test</scope> <scope>test</scope>
<type>jar</type>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -269,7 +269,12 @@ public final class WiserAssertions {
throws MessagingException, IOException { throws MessagingException, IOException {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (int i = 0; i < mimeMultipart.getCount(); i++) { for (int i = 0; i < mimeMultipart.getCount(); i++) {
sb.append(mimeMultipart.getBodyPart(i).getContent()); Object content = mimeMultipart.getBodyPart(i).getContent();
if (content instanceof MimeMultipart) {
sb.append(getMimeMultipartAsString((MimeMultipart) content));
} else {
sb.append(content);
}
} }
return sb.toString(); return sb.toString();
} }

View file

@ -0,0 +1,64 @@
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.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "localhost");
properties.setProperty("mail.smtp.port", "" + WiserAssertionsTest.PORT);
Session session = Session.getDefaultInstance(properties);
return session;
}
}

View file

@ -0,0 +1,37 @@
package net.kemitix.wiser.assertions;
import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.junit.Test;
import javax.mail.Message;
/**
* Regression test for issue #1.
*
* @see https://github.com/kemitix/wiser-assertions/issues/1
* @author pcampbell
*/
public class Issue1Test extends AbstractWiserTest {
/**
* Test {@link WiserAssertions#withContentContains(String)} where the nested
* multi-part message contains the expected text.
*/
@Test
public void shouldParseNestedMultiPartEmails() {
//given
final Email email = new Email();
email.addRecipient("Carl", "carl@b.com",
Message.RecipientType.TO);
email.setFromAddress("Bob", "bob@a.com");
email.setSubject("Subject");
email.setText("Hi Carl,\n\nA new message was just posted.");
Mailer mailer = new Mailer(getSession());
//when
mailer.sendMail(email);
//then
getAssertions().withContentContains("Hi Carl");
}
}

View file

@ -1,21 +1,17 @@
package net.kemitix.wiser.assertions; package net.kemitix.wiser.assertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.subethamail.wiser.Wiser; import org.subethamail.wiser.Wiser;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.mail.Message; import javax.mail.Message;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.Multipart; import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport; import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
@ -27,7 +23,7 @@ import javax.mail.internet.MimeMultipart;
* *
* @author pcampbell * @author pcampbell
*/ */
public class WiserAssertionsTest { public class WiserAssertionsTest extends AbstractWiserTest {
/** /**
* Logger. * Logger.
@ -35,34 +31,6 @@ public class WiserAssertionsTest {
private static final Logger LOG private static final Logger LOG
= Logger.getLogger(WiserAssertionsTest.class.getName()); = 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. * Sends a mime multipart message to the Wiser server.
* *
@ -76,12 +44,8 @@ public class WiserAssertionsTest {
final String to, final String to,
final String subject, final String subject,
final String body) { 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 { try {
MimeMessage message = new MimeMessage(session); MimeMessage message = new MimeMessage(getSession());
message.setFrom(new InternetAddress(from)); message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, to); message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject, "UTF-8"); 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 * Test {@link WiserAssertions#withContent(java.lang.String)} where the
* content of the email matches. * content of the email matches.
@ -209,7 +164,8 @@ public class WiserAssertionsTest {
//given //given
final String fragment = "foo"; final String fragment = "foo";
//when //when
sendMimeMultipartMessage("from", "to", "subject " + fragment + " tail", "body"); sendMimeMultipartMessage(
"from", "to", "subject " + fragment + " tail", "body");
//then //then
getAssertions().withSubjectContains(fragment); getAssertions().withSubjectContains(fragment);
} }