james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [2/3] james-project git commit: MAILET-145: Use MimeMessageBuilder in other places
Date Thu, 12 Jan 2017 16:32:59 GMT
MAILET-145: Use MimeMessageBuilder in other places


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/883edefb
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/883edefb
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/883edefb

Branch: refs/heads/master
Commit: 883edefb854bf0bdff50e3c3ba46739a95e1a901
Parents: 9172ef2
Author: Benoit Tellier <btellier@linagora.com>
Authored: Wed Dec 28 14:35:19 2016 +0700
Committer: Quynh Nguyen <qnguyen@linagora.com>
Committed: Thu Jan 12 11:09:20 2017 +0700

----------------------------------------------------------------------
 .../org/apache/mailet/base/test/MailUtil.java   |  25 +-
 .../mailet/base/test/MimeMessageBuilder.java    |   4 +-
 .../transport/matchers/GenericRegexMatcher.java |  15 +-
 .../transport/mailets/AddSubjectPrefixTest.java |  16 +-
 .../MailAttributesToMimeHeadersTest.java        |  10 +-
 .../transport/mailets/RemoveMimeHeaderTest.java |  38 +--
 .../transport/mailets/StripAttachmentTest.java  | 341 +++++++++----------
 .../james/transport/mailets/UnwrapTextTest.java |   1 -
 .../james/transport/mailets/BounceTest.java     |   3 +-
 .../mailets/delivery/LocalDeliveryTest.java     |  39 +--
 .../mailets/delivery/MailDispatcherTest.java    |  43 +--
 .../mailets/delivery/MailboxAppenderTest.java   |  27 +-
 .../mailets/delivery/SieveIntegrationTest.java  |  66 ++--
 .../mailets/delivery/SimpleMailStoreTest.java   |  27 +-
 .../mailets/delivery/ToRecipientFolderTest.java |  38 +--
 .../managesieve/ManageSieveMailetTestCase.java  |  48 +--
 16 files changed, 308 insertions(+), 433 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java b/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
index 070eef7..4758d77 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
@@ -23,8 +23,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 
 import org.apache.mailet.Mail;
@@ -35,8 +33,8 @@ import org.apache.mailet.MailAddress;
  */
 public class MailUtil {
 
-    private static final Session NO_SESSION = null;
-    
+    public static final String SENDER = "test@james.apache.org";
+    public static final String RECIPIENT = "test2@james.apache.org";
     private static int m_counter = 0;
 
     public static String newId() {
@@ -72,17 +70,14 @@ public class MailUtil {
     }
     
     private static MimeMessage createMimeMessage(String headerName, String headerValue, String subject) throws MessagingException {
-        String sender = "test@james.apache.org";
-        String rcpt = "test2@james.apache.org";
-
-        MimeMessage mimeMessage = new MimeMessage(NO_SESSION);
-        mimeMessage.setFrom(new InternetAddress(sender));
-        mimeMessage.setRecipients(MimeMessage.RecipientType.TO, rcpt);
-        if (headerName != null) mimeMessage.setHeader(headerName, headerValue);
-        if (subject != null) mimeMessage.setSubject(subject);
-        mimeMessage.setText("testtext");
-        mimeMessage.saveChanges();
-        return mimeMessage;
+        MimeMessageBuilder mimeMessageBuilder = MimeMessageBuilder.mimeMessageBuilder()
+            .addToRecipient(RECIPIENT)
+            .setFrom(SENDER)
+            .setSubject(subject);
+        if (headerName != null) {
+            mimeMessageBuilder.addHeader(headerName, headerValue);
+        }
+        return mimeMessageBuilder.build();
     }
     
     public static String toString(Mail mail, String charset) throws IOException, MessagingException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
index 25a4810..42a2339 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
@@ -68,7 +68,7 @@ public class MimeMessageBuilder {
             return this;
         }
 
-        public MultipartBuilder addBodys(BodyPart... bodyParts) {
+        public MultipartBuilder addBodies(BodyPart... bodyParts) {
             this.bodyParts.addAll(Arrays.asList(bodyParts));
             return this;
         }
@@ -255,7 +255,7 @@ public class MimeMessageBuilder {
 
     public MimeMessageBuilder setMultipartWithBodyParts(BodyPart... bobyParts) throws MessagingException {
         this.content = Optional.of(MimeMessageBuilder.multipartBuilder()
-            .addBodys(bobyParts)
+            .addBodies(bobyParts)
             .build());
         return this;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/main/java/org/apache/james/transport/matchers/GenericRegexMatcher.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/GenericRegexMatcher.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/GenericRegexMatcher.java
index c079acf..e9c0612 100755
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/GenericRegexMatcher.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/GenericRegexMatcher.java
@@ -21,16 +21,17 @@
 
 package org.apache.james.transport.matchers;
 
-import org.apache.mailet.base.GenericMatcher;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
-
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
 import java.util.Collection;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMatcher;
+
 /**
  * This is a generic matcher that uses regular expressions.  If any of
  * the regular expressions match, the matcher is considered to have
@@ -77,10 +78,8 @@ abstract public class GenericRegexMatcher extends GenericMatcher {
             //Loop through the header values
             if (headers != null) for (String header : headers) {
                 if (pattern.matcher(header).matches()) {
-                    // log("Match: " + headerName + "[" + j + "]: " + headers[j]);
                     return mail.getRecipients();
                 }
-                //log("       " + headerName + "[" + j + "]: " + headers[j]);
             }
         }
         return null;

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddSubjectPrefixTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
index 26c5bb4..5799c42 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
@@ -53,8 +53,8 @@ public class AddSubjectPrefixTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessageWithSubject("test");
-        FakeMail mail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
+        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject("test");
+        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
         
         mailet.service(mail);
 
@@ -72,8 +72,8 @@ public class AddSubjectPrefixTest {
 
         String subject = 
                 "=?iso8859-15?Q?Beno=EEt_TELLIER_vous_a_d=E9pos=E9_des_fichiers?=";
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessageWithSubject(subject);
-        FakeMail mail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
+        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject(subject);
+        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
         
         mailet.service(mail);
 
@@ -90,8 +90,8 @@ public class AddSubjectPrefixTest {
         mailet.init(mailetConfig);
 
         String noSubject = null;
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessageWithSubject(noSubject);
-        FakeMail mail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
+        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject(noSubject);
+        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
 
         mailet.service(mail);
 
@@ -106,8 +106,8 @@ public class AddSubjectPrefixTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessageWithSubject("");
-        FakeMail mail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
+        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject("");
+        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
 
         mailet.service(mail);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java
index 8a5ac95..4ebe09e 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java
@@ -24,12 +24,12 @@ package org.apache.james.transport.mailets;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
 
 import org.apache.mailet.Mailet;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.apache.mailet.base.test.MailUtil;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -84,10 +84,10 @@ public class MailAttributesToMimeHeadersTest {
                 .setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1)
                 .build();
         mailet.init(mailetConfig);
-        
-        MimeMessage mimeMessage = MailUtil.createMimeMessage();
-        mimeMessage.addHeader(HEADER_NAME1, "first value");
-		FakeMail mockedMail = MailUtil.createMockMail2Recipients(mimeMessage);
+
+        FakeMail mockedMail = MailUtil.createMockMail2Recipients(MimeMessageBuilder.mimeMessageBuilder()
+            .addHeader(HEADER_NAME1, "first value")
+            .build());
         mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1);
         
         mailet.service(mockedMail);

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
index 4477d0f..3a04511 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
@@ -31,7 +31,7 @@ import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMailet;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
-import org.apache.mailet.base.test.MailUtil;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -65,11 +65,10 @@ public class RemoveMimeHeaderTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mimeMessage = MailUtil.createMimeMessage();
-        mimeMessage.setHeader(HEADER1, "true");
-        mimeMessage.setHeader(HEADER2, "true");
-        mimeMessage.saveChanges();
-        Mail mail = createMail(mimeMessage);
+        Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+            .addHeader(HEADER1, "true")
+            .addHeader(HEADER2, "true")
+            .build());
 
         mailet.service(mail);
 
@@ -85,11 +84,10 @@ public class RemoveMimeHeaderTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mimeMessage = MailUtil.createMimeMessage();
-        mimeMessage.setHeader(HEADER1, "true");
-        mimeMessage.setHeader(HEADER2, "true");
-        mimeMessage.saveChanges();
-        Mail mail = createMail(mimeMessage);
+        Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+            .addHeader(HEADER1, "true")
+            .addHeader(HEADER2, "true")
+            .build());
 
         mailet.service(mail);
 
@@ -106,11 +104,10 @@ public class RemoveMimeHeaderTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mimeMessage = MailUtil.createMimeMessage();
-        mimeMessage.setHeader(HEADER1, "true");
-        mimeMessage.setHeader(HEADER2, "true");
-        mimeMessage.saveChanges();
-        Mail mail = createMail(mimeMessage);
+        Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+            .addHeader(HEADER1, "true")
+            .addHeader(HEADER2, "true")
+            .build());
 
         mailet.service(mail);
 
@@ -126,11 +123,10 @@ public class RemoveMimeHeaderTest {
                 .build();
         mailet.init(mailetConfig);
 
-        MimeMessage mimeMessage = MailUtil.createMimeMessage();
-        mimeMessage.setHeader(HEADER1, "true");
-        mimeMessage.setHeader(HEADER2, "true");
-        mimeMessage.saveChanges();
-        Mail mail = createMail(mimeMessage);
+        Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+            .addHeader(HEADER1, "true")
+            .addHeader(HEADER2, "true")
+            .build());
 
         mailet.service(mail);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
index 63a211e..d888c44 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
@@ -27,16 +27,13 @@ import static org.mockito.Mockito.when;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 
+import javax.mail.BodyPart;
 import javax.mail.MessagingException;
 import javax.mail.Part;
-import javax.mail.Session;
-import javax.mail.internet.InternetHeaders;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
@@ -48,6 +45,7 @@ import org.apache.mailet.Mailet;
 import org.apache.mailet.MailetException;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -61,10 +59,29 @@ import com.google.common.base.Optional;
 public class StripAttachmentTest {
 
     private static final String EXPECTED_ATTACHMENT_CONTENT = "\u0023\u00A4\u00E3\u00E0\u00E9";
-    private static final Optional<String> ABSENT_MIME_TYPE = Optional.<String> absent();
-    private static final String MIME_HEADER_DEFAULT = "Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n";
-    private static final String MIME_HEADER_TEXT_CALENDAR = "Content-Transfer-Encoding: 8bit\r\nContent-Type: text/calendar; charset=utf-8\r\n\r\n";
-    private static final String MIME_HEADER_TEXT_HTML = "Content-Transfer-Encoding: 8bit\r\nContent-Type: text/html; charset=utf-8\r\n\r\n";
+    private static final Optional<String> ABSENT_MIME_TYPE = Optional.absent();
+    private static final String CONTENT_TRANSFER_ENCODING_VALUE ="8bit";
+
+    public static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
+    public static final String CONTENT_TYPE = "Content-Type";
+    public static final String CONTENT_TYPE_DEFAULT = "application/octet-stream; charset=utf-8";
+    public static final String TEXT_CALENDAR_CHARSET_UTF_8 = "text/calendar; charset=utf-8";
+    public static final String TEXT_HTML_CHARSET_UTF_8 = "text/html; charset=utf-8";
+
+    private static MimeMessageBuilder.Header[] TEXT_HEADERS = {
+        new MimeMessageBuilder.Header(CONTENT_TRANSFER_ENCODING, CONTENT_TRANSFER_ENCODING_VALUE),
+        new MimeMessageBuilder.Header(CONTENT_TYPE, CONTENT_TYPE_DEFAULT)
+    };
+
+    private static MimeMessageBuilder.Header[] HTML_HEADERS = {
+        new MimeMessageBuilder.Header(CONTENT_TRANSFER_ENCODING, CONTENT_TRANSFER_ENCODING_VALUE),
+        new MimeMessageBuilder.Header(CONTENT_TYPE, TEXT_HTML_CHARSET_UTF_8)
+    };
+
+    private static MimeMessageBuilder.Header[] CALENDAR_HEADERS = {
+        new MimeMessageBuilder.Header(CONTENT_TRANSFER_ENCODING, CONTENT_TRANSFER_ENCODING_VALUE),
+        new MimeMessageBuilder.Header(CONTENT_TYPE, TEXT_CALENDAR_CHARSET_UTF_8)
+    };
 
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
@@ -86,19 +103,15 @@ public class StripAttachmentTest {
     @Test
     public void serviceShouldNotModifyMailWhenNotMultipart() throws MessagingException, IOException {
         Mailet mailet = initMailet();
-        MimeMessage message = mimeMessage();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setSubject("test")
+            .setText("simple text")
+            .build();
 
-        MimeBodyPart part = new MimeBodyPart();
-        part.setText("simple text");
-        
-        message.setSubject("test");
-        message.setContent(part, "text/plain");
-        message.saveChanges();
-
-        MimeMessage expectedMessage = mimeMessage();
-        message.setSubject("test");
-        message.setContent(part, "text/plain");
-        message.saveChanges();
+        MimeMessage expectedMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setSubject("test")
+            .setText("simple text")
+            .build();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -110,26 +123,23 @@ public class StripAttachmentTest {
         mailet.service(mail);
 
         assertThat(mail).isEqualToComparingFieldByField(expectedMail);
-        assertThat(mail.getMessage()).isEqualToComparingFieldByField(expectedMessage);
-        assertThat(mail.getMessage().getContent()).isEqualTo(part);
+        BodyPart content = (BodyPart) mail.getMessage().getContent();
+        assertThat(IOUtils.toString(content.getInputStream(), Charsets.UTF_8)).isEqualTo("simple text");
     }
     
     @Test
     public void serviceShouldSaveAttachmentInAFolderWhenPatternMatch() throws MessagingException, IOException {
         Mailet mailet = initMailet();
-        MimeMessage message = mimeMessage();
 
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart textPart = new MimeBodyPart();
-        textPart.setText("simple text");
-        multiPart.addBodyPart(textPart);
         String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
-        multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "10.tmp", MIME_HEADER_DEFAULT));
-        multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));
-        
-        message.setSubject("test");
-        message.setContent(multiPart);
-        message.saveChanges();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("simple text")
+                    .build(),
+                createAttachmentBodyPart(expectedAttachmentContent, "10.tmp", TEXT_HEADERS),
+                createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", TEXT_HEADERS))
+            .build();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -157,19 +167,17 @@ public class StripAttachmentTest {
         Mailet mailet = new StripAttachment();
         mailet.init(mci);
 
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart textPart = new MimeBodyPart();
-        textPart.setText("simple text");
-        multiPart.addBodyPart(textPart);
         String expectedFileName = "10.ical";
-        multiPart.addBodyPart(createAttachmentBodyPart("content", expectedFileName, MIME_HEADER_TEXT_CALENDAR));
-        multiPart.addBodyPart(createAttachmentBodyPart("other content", "11.ical", MIME_HEADER_DEFAULT));
-        multiPart.addBodyPart(createAttachmentBodyPart("<p>html</p>", "index.html", MIME_HEADER_TEXT_HTML));
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("simple text")
+                    .build(),
+                createAttachmentBodyPart("content", expectedFileName, CALENDAR_HEADERS),
+                createAttachmentBodyPart("other content", "11.ical", TEXT_HEADERS),
+                createAttachmentBodyPart("<p>html</p>", "index.html", HTML_HEADERS))
+            .build();
 
-        MimeMessage message = mimeMessage();
-        message.setSubject("test");
-        message.setContent(multiPart);
-        message.saveChanges();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -182,21 +190,13 @@ public class StripAttachmentTest {
         assertThat(removedAttachments).containsOnly(expectedFileName);
     }
 
-    private MimeMessage mimeMessage() {
-        return new MimeMessage(Session
-                .getDefaultInstance(new Properties()));
-    }
-
-    private MimeBodyPart createAttachmentBodyPart(String body, String fileName, String mimeHeaders) throws MessagingException, UnsupportedEncodingException {
-        MimeBodyPart part = createBodyPart(body, mimeHeaders);
-        part.setDisposition("attachment");
-        part.setFileName(fileName);
-        return part;
-    }
-
-    private MimeBodyPart createBodyPart(String body, String mimeHeaders) throws MessagingException, UnsupportedEncodingException {
-        byte[] content = (mimeHeaders + body).getBytes("UTF-8");
-        return new MimeBodyPart(new ByteArrayInputStream(content));
+    private BodyPart createAttachmentBodyPart(String body, String fileName, MimeMessageBuilder.Header... headers) throws MessagingException, IOException {
+        return MimeMessageBuilder.bodyPartBuilder()
+            .data(body)
+            .addHeaders(headers)
+            .disposition(MimeBodyPart.ATTACHMENT)
+            .filename(fileName)
+            .build();
     }
 
     @Test
@@ -211,19 +211,15 @@ public class StripAttachmentTest {
                 .build();
         mailet.init(mci);
 
-        MimeMessage message = mimeMessage();
-
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart part = new MimeBodyPart();
-        part.setText("simple text");
-        multiPart.addBodyPart(part);
         String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
-        multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "temp.tmp", MIME_HEADER_DEFAULT));
-        multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "winmail.dat", MIME_HEADER_DEFAULT));
-        
-        message.setSubject("test");
-        message.setContent(multiPart);
-        message.saveChanges();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("simple text")
+                    .build(),
+                createAttachmentBodyPart(expectedAttachmentContent, "temp.tmp", TEXT_HEADERS),
+                createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "winmail.dat", TEXT_HEADERS))
+            .build();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -245,19 +241,16 @@ public class StripAttachmentTest {
     public void serviceShouldDecodeFilenameAndSaveAttachmentInAFolderWhenPatternMatchAndDecodeFilenameTrue() throws MessagingException, IOException {
         Mailet mailet = initMailet();
 
-        MimeMessage message = mimeMessage();
-
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart part = new MimeBodyPart();
-        part.setText("simple text");
-        multiPart.addBodyPart(part);
         String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
-        multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=", MIME_HEADER_DEFAULT));
-        multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));
-        
-        message.setSubject("test");
-        message.setContent(multiPart);
-        message.saveChanges();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("simple text")
+                    .build(),
+                createAttachmentBodyPart(expectedAttachmentContent,
+                    "=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=", TEXT_HEADERS),
+                createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", TEXT_HEADERS))
+            .build();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -290,20 +283,16 @@ public class StripAttachmentTest {
                 .setProperty("attribute", customAttribute)
                 .build();
         mailet.init(mci);
-        
-        MimeMessage message = mimeMessage();
 
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart part = new MimeBodyPart();
-        part.setText("simple text");
-        multiPart.addBodyPart(part);
         String expectedKey = "10.tmp";
-        multiPart.addBodyPart(createAttachmentBodyPart(EXPECTED_ATTACHMENT_CONTENT, expectedKey, MIME_HEADER_DEFAULT));
-        multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));
-        
-        message.setSubject("test");
-        message.setContent(multiPart);
-        message.saveChanges();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("simple text")
+                    .build(),
+                createAttachmentBodyPart(EXPECTED_ATTACHMENT_CONTENT, expectedKey, TEXT_HEADERS),
+                createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", TEXT_HEADERS))
+            .build();
 
         Mail mail = FakeMail.builder()
                 .mimeMessage(message)
@@ -542,17 +531,15 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*")
                 .build();
         mailet.init(mci);
-        MimeMultipart multiPart = new MimeMultipart();
-        
-        MimeBodyPart part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("removeMe.tmp");
-        multiPart.addBodyPart(part);
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPart);
-        message.saveChanges();
+
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(MimeMessageBuilder.bodyPartBuilder()
+                .filename("removeMe.tmp")
+                .build())
+            .build();
         Mail mail = mock(Mail.class);
         //When
-        boolean actual = mailet.processMultipartPartMessage(message, mail);
+        boolean actual = mailet.processMultipartPartMessage(mimeMessage, mail);
         //Then
         assertThat(actual).isTrue();
     }
@@ -568,17 +555,16 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*")
                 .build();
         mailet.init(mci);
-        MimeMultipart multiPart = new MimeMultipart();
-        
-        MimeBodyPart part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("removeMe.tmp");
-        multiPart.addBodyPart(part);
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPart);
-        message.saveChanges();
+
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(MimeMessageBuilder.bodyPartBuilder()
+                .filename("removeMe.tmp")
+                .build())
+            .build();
+
         Mail mail = mock(Mail.class);
         //When
-        boolean actual = mailet.processMultipartPartMessage(message, mail);
+        boolean actual = mailet.processMultipartPartMessage(mimeMessage, mail);
         //Then
         assertThat(actual).isTrue();
     }
@@ -595,18 +581,20 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*")
                 .build();
         mailet.init(mci);
-        MimeMultipart multiPart = new MimeMultipart();
-        
-        MimeBodyPart part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("removeMe.tmp");
-        multiPart.addBodyPart(part);
-        multiPart.addBodyPart(part);
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPart);
-        message.saveChanges();
+
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .filename("removeMe.tmp")
+                    .build(),
+                MimeMessageBuilder.bodyPartBuilder()
+                    .filename("removeMe.tmp")
+                    .build())
+            .build();
+
         Mail mail = FakeMail.builder().build();
         //When
-        boolean actual = mailet.processMultipartPartMessage(message, mail);
+        boolean actual = mailet.processMultipartPartMessage(mimeMessage, mail);
         //Then
         assertThat(actual).isTrue();
         @SuppressWarnings("unchecked")
@@ -628,24 +616,21 @@ public class StripAttachmentTest {
                 .setProperty("attribute", customAttribute)
                 .build();
         mailet.init(mci);
-        
-        // Message with two matching attachments
-        MimeMultipart multiPart = new MimeMultipart();
-        MimeBodyPart attachmentPart1 = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        attachmentPart1.setFileName("removeMe1.tmp");
-        multiPart.addBodyPart(attachmentPart1);
-        MimeBodyPart attachmentPart2 = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        attachmentPart2.setFileName("removeMe2.tmp");
-        multiPart.addBodyPart(attachmentPart2);
-        
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPart);
-        message.saveChanges();
+
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .filename("removeMe1.tmp")
+                    .build(),
+                MimeMessageBuilder.bodyPartBuilder()
+                    .filename("removeMe2.tmp")
+                    .build())
+            .build();
         
         Mail mail = FakeMail.builder().build();
         
         //When
-        boolean actual = mailet.processMultipartPartMessage(message, mail);
+        boolean actual = mailet.processMultipartPartMessage(mimeMessage, mail);
         
         //Then
         assertThat(actual).isTrue();
@@ -666,23 +651,15 @@ public class StripAttachmentTest {
                 .build();
         mailet .init(mci);
 
-        MimeBodyPart attachmentPart = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        attachmentPart.setFileName("removeMe.tmp");
-        
-        MimeMultipart subMultiPart = new MimeMultipart();
-        subMultiPart.addBodyPart(attachmentPart);
-        
-        MimeMessage subMessage = mimeMessage();
-        subMessage.setContent(subMultiPart);
-        subMessage.saveChanges();
-        
-        MimeMultipart multiPartMixed = new MimeMultipart();
-        multiPartMixed.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("Content-Type: multipart/mixed".getBytes(Charsets.US_ASCII)))
-                , IOUtils.toByteArray(subMessage.getInputStream())));
-
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPartMixed);
-        message.saveChanges();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithSubMessage(
+                MimeMessageBuilder.mimeMessageBuilder()
+                    .setMultipartWithBodyParts(
+                        MimeMessageBuilder.bodyPartBuilder()
+                            .filename("removeMe.tmp")
+                            .build())
+                    .build())
+            .build();
         Mail mail = mock(Mail.class);
         //When
         boolean actual = mailet.processMultipartPartMessage(message, mail);
@@ -701,20 +678,16 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*\\.tmp")
                 .build();
         mailet.init(mci);
-        MimeMultipart mainMultiPart = new MimeMultipart();
-        MimeMessage subMessage = mimeMessage();
-        MimeMultipart subMultiPart = new MimeMultipart();
-        MimeBodyPart part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("dontRemoveMe.other");
-        subMultiPart.addBodyPart(part);
-        subMessage.setContent(subMultiPart);
-        subMessage.saveChanges();
-        mainMultiPart.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("Content-Type: multipart/mixed".getBytes(Charsets.US_ASCII)))
-                ,
-                IOUtils.toByteArray(subMessage.getInputStream())));
-        MimeMessage message = mimeMessage();
-        message.setContent(mainMultiPart);
-        message.saveChanges();
+
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithSubMessage(
+                MimeMessageBuilder.mimeMessageBuilder()
+                    .setMultipartWithBodyParts(
+                        MimeMessageBuilder.bodyPartBuilder()
+                            .filename("dontRemoveMe.other")
+                            .build())
+                    .build())
+            .build();
         Mail mail = mock(Mail.class);
         //When
         boolean actual = mailet.processMultipartPartMessage(message, mail);
@@ -733,19 +706,21 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*")
                 .build();
         mailet.init(mci);
-        MimeMultipart multiPart = new MimeMultipart();
-        
-        MimeBodyPart part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("removeMe.tmp");
-        multiPart.addBodyPart(part);
-        MimeMessage message = mimeMessage();
-        message.setContent(multiPart);
-        message.saveChanges();
+
+        MimeMultipart mimeMultipart = MimeMessageBuilder.multipartBuilder()
+            .addBody(MimeMessageBuilder.bodyPartBuilder()
+                .filename("removeMe.tmp")
+                .build())
+            .build();
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setContent(mimeMultipart)
+            .build();
+
         Mail mail = mock(Mail.class);
         //When
-        mailet.processMultipartPartMessage(message, mail);
+        mailet.processMultipartPartMessage(mimeMessage, mail);
         //Then
-        assertThat(multiPart.getCount()).isZero();
+        assertThat(mimeMultipart.getCount()).isZero();
     }
 
     @Test
@@ -757,8 +732,10 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*\\.tmp")
                 .build();
         mailet.init(mci);
-        Part part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
-        part.setFileName("example.tmp");
+
+        Part part = MimeMessageBuilder.bodyPartBuilder()
+            .filename("example.tmp")
+            .build();
         //When
         Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, ABSENT_MIME_TYPE);
         //Then
@@ -776,7 +753,7 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*\\.tmp")
                 .build();
         mailet.init(mci);
-        Part part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
+        Part part = MimeMessageBuilder.bodyPartBuilder().build();
 
         Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, ABSENT_MIME_TYPE);
         assertThat(maybeFilename).isAbsent();
@@ -791,7 +768,7 @@ public class StripAttachmentTest {
                 .setProperty("pattern", ".*")
                 .build();
         mailet.init(mci);
-        Part part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
+        Part part = MimeMessageBuilder.bodyPartBuilder().build();
         String fileName = "exampleWithoutSuffix";
         //When
         Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, Optional.of(fileName));

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/mailet/standard/src/test/java/org/apache/james/transport/mailets/UnwrapTextTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/UnwrapTextTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/UnwrapTextTest.java
index ee62290..6942479 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/UnwrapTextTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/UnwrapTextTest.java
@@ -19,7 +19,6 @@
 
 package org.apache.james.transport.mailets;
 
-import org.apache.james.transport.mailets.UnwrapText;
 import org.junit.Assert;
 import org.junit.Test;
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
index 8f9a0b0..4ff3ca9 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
@@ -37,6 +37,7 @@ import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -113,7 +114,7 @@ public class BounceTest {
                 .name(MAILET_NAME)
                 .recipient(MailAddressFixture.ANY_AT_JAMES)
                 .build();
-        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        MimeMessage mimeMessage = MimeMessageBuilder.defaultMimeMessage();
         mimeMessage.setText("My content");
         mail.setMessage(mimeMessage);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/LocalDeliveryTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/LocalDeliveryTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/LocalDeliveryTest.java
index 84d2e50..26fee78 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/LocalDeliveryTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/LocalDeliveryTest.java
@@ -29,17 +29,10 @@ import static org.mockito.Mockito.when;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Date;
-import java.util.Properties;
 
-import javax.activation.DataHandler;
 import javax.mail.Flags;
 import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.mailbox.MailboxManager;
@@ -57,6 +50,7 @@ import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -143,26 +137,19 @@ public class LocalDeliveryTest {
     }
 
     private Mail createMail() throws MessagingException, IOException {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        message.setSubject("Subject");
-        message.setSender(new InternetAddress("sender@any.com"));
-        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECEIVER_DOMAIN_COM));
-        MimeMultipart multipart = new MimeMultipart();
-        MimeBodyPart scriptPart = new MimeBodyPart();
-        scriptPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    "toto",
-                    "application/sieve; charset=UTF-8")
-            ));
-        scriptPart.setDisposition(MimeBodyPart.ATTACHMENT);
-        scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8");
-        scriptPart.setFileName("file.txt");
-        multipart.addBodyPart(scriptPart);
-        message.setContent(multipart);
-        message.saveChanges();
         return FakeMail.builder()
-                .mimeMessage(message)
+                .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+                    .setSender("sender@any.com")
+                    .setSubject("Subject")
+                    .addToRecipient(RECEIVER_DOMAIN_COM)
+                    .setMultipartWithBodyParts(
+                        MimeMessageBuilder.bodyPartBuilder()
+                            .data("toto")
+                            .disposition(MimeBodyPart.ATTACHMENT)
+                            .filename("file.txt")
+                            .addHeader("Content-Type", "application/sieve; charset=UTF-8")
+                            .build())
+                    .build())
                 .state(Mail.DEFAULT)
                 .recipient(new MailAddress("receiver@domain.com"))
                 .build();

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
index be24ca1..9fe8f69 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailDispatcherTest.java
@@ -28,16 +28,9 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Properties;
 
-import javax.activation.DataHandler;
 import javax.mail.MessagingException;
-import javax.mail.Multipart;
-import javax.mail.Session;
-import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
@@ -47,6 +40,7 @@ import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.RFC2822Headers;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -76,7 +70,7 @@ public class MailDispatcherTest {
             .sender(MailAddressFixture.OTHER_AT_JAMES)
             .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2)
             .state("state")
-            .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
             .build();
         testee.dispatch(mail);
 
@@ -97,7 +91,7 @@ public class MailDispatcherTest {
         FakeMail mail = FakeMail.builder()
             .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2)
             .state("state")
-            .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
             .build();
         testee.dispatch(mail);
 
@@ -116,7 +110,7 @@ public class MailDispatcherTest {
         String state = "state";
         FakeMail mail = FakeMail.builder()
             .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2)
-            .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
             .state(state)
             .build();
         testee.dispatch(mail);
@@ -136,18 +130,12 @@ public class MailDispatcherTest {
             .when(mailStore)
             .storeMail(any(MailAddress.class), any(Mail.class));
 
-        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        Multipart multipart = new MimeMultipart();
-        MimeBodyPart bodyPart = new MimeBodyPart();
-        bodyPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    "toto",
-                    "text/plain; charset=UTF-8")
-            ));
-        multipart.addBodyPart(bodyPart);
-        mimeMessage.setContent(multipart);
-        mimeMessage.saveChanges();
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("toto")
+                    .build())
+            .build();
 
         FakeMail mail = FakeMail.builder()
             .sender(MailAddressFixture.OTHER_AT_JAMES)
@@ -179,7 +167,7 @@ public class MailDispatcherTest {
         FakeMail mail = FakeMail.builder()
             .sender(MailAddressFixture.OTHER_AT_JAMES)
             .recipients(MailAddressFixture.ANY_AT_JAMES)
-            .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
             .state("state")
             .build();
         testee.dispatch(mail);
@@ -200,11 +188,12 @@ public class MailDispatcherTest {
             .consume(false)
             .build();
 
-        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
         String delivered_to_1 = "delivered_to_1";
         String delivered_to_2 = "delivered_to_2";
-        mimeMessage.addHeader(MailDispatcher.DELIVERED_TO, delivered_to_1);
-        mimeMessage.addHeader(MailDispatcher.DELIVERED_TO, delivered_to_2);
+        MimeMessage mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .addHeaders(new MimeMessageBuilder.Header(MailDispatcher.DELIVERED_TO, delivered_to_1),
+                new MimeMessageBuilder.Header(MailDispatcher.DELIVERED_TO, delivered_to_2))
+            .build();
         FakeMail mail = FakeMail.builder()
             .sender(MailAddressFixture.OTHER_AT_JAMES)
             .recipients(MailAddressFixture.ANY_AT_JAMES)
@@ -229,7 +218,7 @@ public class MailDispatcherTest {
         FakeMail mail = FakeMail.builder()
             .sender(MailAddressFixture.OTHER_AT_JAMES)
             .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2)
-            .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+            .mimeMessage(MimeMessageBuilder.defaultMimeMessage())
             .state("state")
             .build();
         testee.dispatch(mail);

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailboxAppenderTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailboxAppenderTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailboxAppenderTest.java
index 0fc54c5..c83ddc5 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailboxAppenderTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/MailboxAppenderTest.java
@@ -22,16 +22,8 @@ package org.apache.james.transport.mailets.delivery;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
-import java.util.Properties;
-
-import javax.activation.DataHandler;
 import javax.mail.MessagingException;
-import javax.mail.Multipart;
-import javax.mail.Session;
-import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
@@ -42,6 +34,7 @@ import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.MessageResult;
 import org.apache.james.mailbox.model.MessageResultIterator;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -67,18 +60,12 @@ public class MailboxAppenderTest {
 
     @Before
     public void setUp() throws Exception {
-        mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        Multipart multipart = new MimeMultipart();
-        MimeBodyPart bodyPart = new MimeBodyPart();
-        bodyPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    "toto",
-                    "text/plain; charset=UTF-8")
-            ));
-        multipart.addBodyPart(bodyPart);
-        mimeMessage.setContent(multipart);
-        mimeMessage.saveChanges();
+        mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("toto")
+                    .build())
+            .build();
 
         integrationResources = new InMemoryIntegrationResources();
         integrationResources.init();

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
index 97556de..e3bdddf 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java
@@ -24,16 +24,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.io.IOException;
-import java.util.Properties;
 
-import javax.activation.DataHandler;
 import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.sieverepository.api.exception.ScriptNotFoundException;
@@ -45,6 +38,8 @@ import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.apache.mailet.base.test.MimeMessageBuilder.Header;
 import org.joda.time.DateTime;
 import org.joda.time.format.DateTimeFormat;
 import org.joda.time.format.DateTimeFormatter;
@@ -57,16 +52,6 @@ public class SieveIntegrationTest {
     public static final String LOCAL_PART = "receiver";
     public static final String RECEIVER_DOMAIN_COM = LOCAL_PART + "@domain.com";
 
-    class Header {
-        String name;
-        String value;
-
-        public Header(String name, String value) {
-            this.name = name;
-            this.value = value;
-        }
-    }
-
     public static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss");
     public static final DateTime DATE_CLOSE = formatter.parseDateTime("2016-01-16 00:00:00");
     public static final DateTime DATE_DEFAULT = formatter.parseDateTime("2016-01-14 00:00:00");
@@ -365,7 +350,6 @@ public class SieveIntegrationTest {
         assertThat(mail.getAttribute(MailStore.DELIVERY_PATH_PREFIX + LOCAL_PART)).isEqualTo(expressMailboxNameWithSlash(SELECTED_MAILBOX.getName()));
     }
 
-
     @Test
     public void addressDomainShouldOnlyMatchLocalPart() throws Exception {
         prepareTestUsingScript("org/apache/james/transport/mailets/delivery/addressDomain.script");
@@ -940,34 +924,26 @@ public class SieveIntegrationTest {
         return createMailWithSubjectAndHeaders(subject);
     }
 
-    private FakeMail createMailWithSubjectAndHeaders(String subject, Header... headers) throws MessagingException, IOException {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        message.setSubject(subject);
-        message.setSender(new InternetAddress("sender@any.com"));
-        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECEIVER_DOMAIN_COM));
-        MimeMultipart multipart = new MimeMultipart();
-        MimeBodyPart scriptPart = new MimeBodyPart();
-        scriptPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    "A text to match",
-                    "text/plain; charset=UTF-8")
-            ));
-        scriptPart.setDisposition(MimeBodyPart.ATTACHMENT);
-        scriptPart.setHeader("Content-Type", "text/plain; charset=UTF-8");
-        scriptPart.setFileName("file.txt");
-        multipart.addBodyPart(scriptPart);
-        message.setContent(multipart);
-        for (Header header : headers) {
-            message.addHeader(header.name, header.value);
-        }
-        message.saveChanges();
+    private FakeMail createMailWithSubjectAndHeaders(String subject, MimeMessageBuilder.Header... headers) throws MessagingException, IOException {
         return FakeMail.builder()
-                .mimeMessage(message)
-                .state(Mail.DEFAULT)
-                .recipient(new MailAddress(RECEIVER_DOMAIN_COM))
-                .sender(new MailAddress("sender@any.com"))
-                .build();
+            .mimeMessage(
+                MimeMessageBuilder.mimeMessageBuilder()
+                    .setSubject(subject)
+                    .addHeaders(headers)
+                    .setSender("sender@any.com")
+                    .addToRecipient(RECEIVER_DOMAIN_COM)
+                    .setMultipartWithBodyParts(
+                        MimeMessageBuilder.bodyPartBuilder()
+                            .data("A text to match")
+                            .addHeader("Content-Type", "text/plain; charset=UTF-8")
+                            .filename("file.txt")
+                            .disposition(MimeBodyPart.ATTACHMENT)
+                            .build())
+                    .build())
+            .state(Mail.DEFAULT)
+            .recipient(new MailAddress(RECEIVER_DOMAIN_COM))
+            .sender(new MailAddress("sender@any.com"))
+            .build();
     }
 
     private String expressMailboxNameWithSlash(String name) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SimpleMailStoreTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SimpleMailStoreTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SimpleMailStoreTest.java
index 15cac3f..8c6cbf5 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SimpleMailStoreTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SimpleMailStoreTest.java
@@ -25,15 +25,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.util.Properties;
-
-import javax.activation.DataHandler;
-import javax.mail.Multipart;
-import javax.mail.Session;
-import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.commons.logging.Log;
 import org.apache.james.metrics.api.Metric;
@@ -42,6 +34,7 @@ import org.apache.james.user.api.UsersRepositoryException;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.MailAddressFixture;
 import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -65,18 +58,12 @@ public class SimpleMailStoreTest {
             .log(mock(Log.class))
             .build();
 
-        mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        Multipart multipart = new MimeMultipart();
-        MimeBodyPart bodyPart = new MimeBodyPart();
-        bodyPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    "toto",
-                    "text/plain; charset=UTF-8")
-            ));
-        multipart.addBodyPart(bodyPart);
-        mimeMessage.setContent(multipart);
-        mimeMessage.saveChanges();
+        mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data("toto")
+                    .build())
+            .build();
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java
index f166c09..1c556a7 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java
@@ -56,6 +56,7 @@ import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -193,29 +194,22 @@ public class ToRecipientFolderTest {
     }
 
     private Mail createMail() throws MessagingException, IOException {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        message.setSubject("Subject");
-        message.setSender(new InternetAddress("sender@any.com"));
-        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(USER));
-        MimeMultipart multipart = new MimeMultipart();
-        MimeBodyPart scriptPart = new MimeBodyPart();
-        scriptPart.setDataHandler(
-                new DataHandler(
-                        new ByteArrayDataSource(
-                                "toto",
-                                "application/sieve; charset=UTF-8")
-                ));
-        scriptPart.setDisposition(MimeBodyPart.ATTACHMENT);
-        scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8");
-        scriptPart.setFileName("file.txt");
-        multipart.addBodyPart(scriptPart);
-        message.setContent(multipart);
-        message.saveChanges();
         return FakeMail.builder()
-                .mimeMessage(message)
-                .state(Mail.DEFAULT)
-                .recipient(new MailAddress(USER))
-                .build();
+            .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+                .setSender("sender@any.com")
+                .setSubject("Subject")
+                .addToRecipient(USER)
+                .setMultipartWithBodyParts(
+                    MimeMessageBuilder.bodyPartBuilder()
+                        .data("toto")
+                        .disposition(MimeBodyPart.ATTACHMENT)
+                        .filename("file.txt")
+                        .addHeader("Content-Type", "application/sieve; charset=UTF-8")
+                        .build())
+                .build())
+            .state(Mail.DEFAULT)
+            .recipient(new MailAddress("receiver@domain.com"))
+            .build();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/883edefb/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
index bdf6dd5..15f4bdb 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
@@ -27,17 +27,13 @@ import static org.mockito.Mockito.when;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.util.Properties;
 
-import javax.activation.DataHandler;
 import javax.mail.MessagingException;
-import javax.mail.Session;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMessage.RecipientType;
 import javax.mail.internet.MimeMultipart;
-import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.james.managesieve.api.SieveParser;
@@ -51,6 +47,7 @@ import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -490,35 +487,26 @@ public class ManageSieveMailetTestCase {
     }
 
     private MimeMessage prepareMimeMessage(String subject) throws MessagingException {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        message.setSubject(subject);
-        message.setSender(new InternetAddress(USER));
-        message.setRecipient(RecipientType.TO, new InternetAddress(SIEVE_LOCALHOST));
-        message.saveChanges();
-        return message;
+        return MimeMessageBuilder.mimeMessageBuilder()
+            .setSubject(subject)
+            .addToRecipient(SIEVE_LOCALHOST)
+            .setSender(USER)
+            .build();
     }
 
     private MimeMessage prepareMessageWithAttachment(String scriptContent, String subject) throws MessagingException, IOException {
-        MimeMessage message = prepareMimeMessage(subject);
-        MimeMultipart multipart = new MimeMultipart();
-        MimeBodyPart scriptPart = new MimeBodyPart();
-        scriptPart.setDataHandler(
-            new DataHandler(
-                new ByteArrayDataSource(
-                    scriptContent,
-                    "application/sieve; charset=UTF-8")
-            ));
-        scriptPart.setDisposition(MimeBodyPart.ATTACHMENT);
-        // setting a DataHandler with no mailcap definition is not
-        // supported by the specs. Javamail activation still work,
-        // but Geronimo activation translate it to text/plain.
-        // Let's manually force the header.
-        scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8");
-        scriptPart.setFileName(SCRIPT_NAME);
-        multipart.addBodyPart(scriptPart);
-        message.setContent(multipart);
-        message.saveChanges();
-        return message;
+        return MimeMessageBuilder.mimeMessageBuilder()
+            .setSubject(subject)
+            .addToRecipient(SIEVE_LOCALHOST)
+            .setSender(USER)
+            .setMultipartWithBodyParts(
+                MimeMessageBuilder.bodyPartBuilder()
+                    .data(scriptContent)
+                    .disposition(MimeBodyPart.ATTACHMENT)
+                    .filename(SCRIPT_NAME)
+                    .addHeader("Content-Type", "application/sieve; charset=UTF-8")
+                    .build())
+            .build();
     }
 
     private void ensureResponse(String subject, String... contents) throws MessagingException, IOException {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message