From 1e97f1ce161773b0136d713ea363f25d7d1cd261 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 10 Aug 2016 14:23:20 +0200 Subject: [PATCH] inject UuidGenerator into MimeMessage via constructor instead of parameter --- .../java/com/fsck/k9/mail/MessageTest.java | 2 +- .../com/fsck/k9/mail/PgpMimeMessageTest.java | 2 +- .../fsck/k9/mail/ReconstructMessageTest.java | 3 +- .../internet/BinaryTempFileMessageBody.java | 2 +- .../fsck/k9/mail/internet/MimeMessage.java | 33 ++++++++++--------- .../fsck/k9/mail/store/imap/ImapFolder.java | 4 +-- .../k9/mail/store/imap/ImapFolderPusher.java | 6 ++-- .../fsck/k9/mail/store/imap/ImapMessage.java | 10 +++++- .../fsck/k9/mail/store/pop3/Pop3Store.java | 16 ++++++--- .../k9/mail/store/webdav/WebDavFolder.java | 6 ++-- .../k9/mail/store/webdav/WebDavMessage.java | 9 ++++- .../k9/mail/internet/CharsetSupportTest.java | 26 +++++++-------- .../k9/mail/internet/ListHeadersTest.java | 2 +- .../mail/internet/MimeMessageParseTest.java | 4 +-- .../mail/store/webdav/WebDavFolderTest.java | 2 +- .../mail/store/webdav/WebDavMessageTest.java | 2 +- .../ReconstructMessageFromDatabaseTest.java | 4 +-- .../fsck/k9/provider/EmailProviderTest.java | 10 +++--- .../k9/controller/MessagingController.java | 2 +- .../mailstore/AttachmentMessageBodyUtil.java | 2 +- .../com/fsck/k9/mailstore/LocalFolder.java | 5 +-- .../com/fsck/k9/mailstore/LocalMessage.java | 16 +++++++-- .../com/fsck/k9/mailstore/LocalStore.java | 4 +-- .../k9/mailstore/MimePartStreamParser.java | 2 +- .../com/fsck/k9/message/MessageBuilder.java | 4 +-- .../k9/crypto/MessageDecryptVerifierTest.java | 10 +++--- .../MessageViewInfoExtractorTest.java | 12 +++---- .../k9/message/MessageCreationHelper.java | 2 +- .../extractors/MessagePreviewCreatorTest.java | 2 +- 29 files changed, 119 insertions(+), 85 deletions(-) diff --git a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java index 5a3607282..8f071ed8b 100644 --- a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java +++ b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/MessageTest.java @@ -461,7 +461,7 @@ public class MessageTest { } private MimeMessage sampleMessage() throws MessagingException, IOException { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.setFrom(new Address("from@example.com")); message.setRecipient(RecipientType.TO, new Address("to@example.com")); message.setSubject("Test Message"); diff --git a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/PgpMimeMessageTest.java b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/PgpMimeMessageTest.java index 5fa6f5f1c..dc973718b 100644 --- a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/PgpMimeMessageTest.java +++ b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/PgpMimeMessageTest.java @@ -221,7 +221,7 @@ public class PgpMimeMessageTest { InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes()); MimeMessage message; try { - message = new MimeMessage(messageInputStream, true); + message = MimeMessage.parseMimeMessage(messageInputStream, true); } finally { messageInputStream.close(); } diff --git a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/ReconstructMessageTest.java b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/ReconstructMessageTest.java index fbf32f5e7..634eaf284 100644 --- a/k9mail-library/src/androidTest/java/com/fsck/k9/mail/ReconstructMessageTest.java +++ b/k9mail-library/src/androidTest/java/com/fsck/k9/mail/ReconstructMessageTest.java @@ -8,7 +8,6 @@ import java.io.InputStream; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import android.test.AndroidTestCase; import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.MimeMessage; @@ -58,7 +57,7 @@ public class ReconstructMessageTest { InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes()); MimeMessage message; try { - message = new MimeMessage(messageInputStream, true); + message = MimeMessage.parseMimeMessage(messageInputStream, true); } finally { messageInputStream.close(); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java index d4eb0435d..5ac2fd68a 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/BinaryTempFileMessageBody.java @@ -39,7 +39,7 @@ public class BinaryTempFileMessageBody extends BinaryTempFileBody implements Com * could be sent along without processing. But since we * don't know, we recursively parse it. */ - MimeMessage message = new MimeMessage(in, true); + MimeMessage message = MimeMessage.parseMimeMessage(in, true); message.setUsing7bitTransport(); message.writeTo(out); } else { diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java index 7eeb82f08..1a3703c25 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java @@ -43,6 +43,8 @@ import org.apache.james.mime4j.util.MimeUtil; * RFC 2045 style headers. */ public class MimeMessage extends Message { + protected final UuidGenerator uuidGenerator; + private MimeHeader mHeader = new MimeHeader(); protected Address[] mFrom; protected Address[] mTo; @@ -61,22 +63,23 @@ public class MimeMessage extends Message { protected int mSize; private String serverExtra; - public MimeMessage() { + + public static MimeMessage createMimeMessage() { + UuidGenerator uuidGenerator = UuidGenerator.getInstance(); + return new MimeMessage(uuidGenerator); } - - /** - * Parse the given InputStream using Apache Mime4J to build a MimeMessage. - * - * @param in - * @param recurse A boolean indicating to recurse through all nested MimeMessage subparts. - * @throws IOException - * @throws MessagingException - */ - public MimeMessage(InputStream in, boolean recurse) throws IOException, MessagingException { - parse(in, recurse); + public static MimeMessage parseMimeMessage(InputStream in, boolean recurse) throws IOException, MessagingException { + MimeMessage mimeMessage = createMimeMessage(); + mimeMessage.parse(in, recurse); + return mimeMessage; } + protected MimeMessage(UuidGenerator uuidGenerator) { + this.uuidGenerator = uuidGenerator; + } + + /** * Parse the given InputStream using Apache Mime4J to build a MimeMessage. * Does not recurse through nested bodyparts. @@ -319,7 +322,7 @@ public class MimeMessage extends Message { return mMessageId; } - public void generateMessageId(UuidGenerator uuidGenerator) throws MessagingException { + public void generateMessageId() { String hostname = null; if (mFrom != null && mFrom.length >= 1) { @@ -502,7 +505,7 @@ public class MimeMessage extends Message { expect(Part.class); Part part = (Part) stack.peek(); - MimeMessage m = new MimeMessage(); + MimeMessage m = createMimeMessage(); part.setBody(m); stack.addFirst(m); } @@ -643,7 +646,7 @@ public class MimeMessage extends Message { @Override public MimeMessage clone() { - MimeMessage message = new MimeMessage(); + MimeMessage message = createMimeMessage(); copy(message); return message; } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java index a7edfcc5f..f5558165d 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java @@ -501,7 +501,7 @@ class ImapFolder extends Folder { @Override public ImapMessage getMessage(String uid) throws MessagingException { - return new ImapMessage(uid, this); + return ImapMessage.createImapMessage(uid, this); } @Override @@ -635,7 +635,7 @@ class ImapFolder extends Folder { listener.messageStarted(uid, i, count); } - ImapMessage message = new ImapMessage(uid, this); + ImapMessage message = ImapMessage.createImapMessage(uid, this); messages.add(message); if (listener != null) { diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java index 33f32cb22..2abb003de 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java @@ -587,7 +587,7 @@ class ImapFolderPusher extends ImapFolder { List messages = new ArrayList(); for (long uid = startUid; uid <= newUid; uid++) { - ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); + ImapMessage message = ImapMessage.createImapMessage(Long.toString(uid), ImapFolderPusher.this); messages.add(message); } @@ -626,7 +626,7 @@ class ImapFolderPusher extends ImapFolder { } for (String uid : removeUids) { - ImapMessage message = new ImapMessage(uid, ImapFolderPusher.this); + ImapMessage message = ImapMessage.createImapMessage(uid, ImapFolderPusher.this); try { message.setFlagInternal(Flag.DELETED, true); @@ -662,7 +662,7 @@ class ImapFolderPusher extends ImapFolder { List messages = new ArrayList(count); for (long uid = startUid; uid < uidNext; uid++) { - ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); + ImapMessage message = ImapMessage.createImapMessage(Long.toString(uid), ImapFolderPusher.this); messages.add(message); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java index 54b607549..25d9cd3e6 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java @@ -6,15 +6,23 @@ import java.util.Collections; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.UuidGenerator; import com.fsck.k9.mail.internet.MimeMessage; class ImapMessage extends MimeMessage { - ImapMessage(String uid, Folder folder) { + static ImapMessage createImapMessage(String uid, Folder folder) { + UuidGenerator uuidGenerator = UuidGenerator.getInstance(); + return new ImapMessage(uuidGenerator, uid, folder); + } + + private ImapMessage(UuidGenerator uuidGenerator, String uid, Folder folder) { + super(uuidGenerator); this.mUid = uid; this.mFolder = folder; } + public void setSize(int size) { this.mSize = size; } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java index f6334392c..4daa3f8aa 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java @@ -586,7 +586,7 @@ public class Pop3Store extends RemoteStore { public Pop3Message getMessage(String uid) throws MessagingException { Pop3Message message = mUidToMsgMap.get(uid); if (message == null) { - message = new Pop3Message(uid, this); + message = Pop3Message.createPop3Message(uid, this); } return message; } @@ -668,7 +668,7 @@ public class Pop3Store extends RemoteStore { return; } String msgUid = uidParts[2]; - message = new Pop3Message(msgUid, this); + message = Pop3Message.createPop3Message(msgUid, this); indexMessage(msgNum, message); } } @@ -708,7 +708,7 @@ public class Pop3Store extends RemoteStore { if (msgNum >= start && msgNum <= end) { Pop3Message message = mMsgNumToMsgMap.get(msgNum); if (message == null) { - message = new Pop3Message(msgUid, this); + message = Pop3Message.createPop3Message(msgUid, this); indexMessage(msgNum, message); } } @@ -754,7 +754,7 @@ public class Pop3Store extends RemoteStore { Pop3Message message = mUidToMsgMap.get(msgUid); if (message == null) { - message = new Pop3Message(msgUid, this); + message = Pop3Message.createPop3Message(msgUid, this); } indexMessage(msgNum, message); } @@ -1184,7 +1184,13 @@ public class Pop3Store extends RemoteStore { }//Pop3Folder static class Pop3Message extends MimeMessage { - public Pop3Message(String uid, Pop3Folder folder) { + public static Pop3Message createPop3Message(String uid, Pop3Folder folder) { + UuidGenerator uuidGenerator = UuidGenerator.getInstance(); + return new Pop3Message(uuidGenerator, uid, folder); + } + + private Pop3Message(UuidGenerator uuidGenerator, String uid, Pop3Folder folder) { + super(uuidGenerator); mUid = uid; mFolder = folder; mSize = -1; diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java index 164a9a5e6..8beea760d 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java @@ -225,7 +225,7 @@ class WebDavFolder extends Folder { @Override public WebDavMessage getMessage(String uid) throws MessagingException { - return new WebDavMessage(uid, this); + return WebDavMessage.createWebDavMessage(uid, this); } @Override @@ -265,7 +265,7 @@ class WebDavFolder extends Folder { if (listener != null) { listener.messageStarted(uids[i], i, uidsLength); } - WebDavMessage message = new WebDavMessage(uids[i], this); + WebDavMessage message = WebDavMessage.createWebDavMessage(uids[i], this); message.setUrl(uidToUrl.get(uids[i])); messages.add(message); @@ -696,7 +696,7 @@ class WebDavFolder extends Folder { + " while sending/appending message. Response = " + response.getStatusLine().toString() + " for message " + messageURL); } - WebDavMessage retMessage = new WebDavMessage(message.getUid(), this); + WebDavMessage retMessage = WebDavMessage.createWebDavMessage(message.getUid(), this); retMessage.setUrl(messageURL); retMessages.add(retMessage); diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java index 3d59e3d7c..a89545534 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java @@ -5,6 +5,7 @@ import android.util.Log; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.UuidGenerator; import com.fsck.k9.mail.internet.MimeMessage; import java.util.Collections; @@ -21,7 +22,13 @@ import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8; class WebDavMessage extends MimeMessage { private String mUrl = ""; - WebDavMessage(String uid, Folder folder) { + static WebDavMessage createWebDavMessage(String uid, Folder folder) { + UuidGenerator uuidGenerator = UuidGenerator.getInstance(); + return new WebDavMessage(uuidGenerator, uid, folder); + } + + private WebDavMessage(UuidGenerator uuidGenerator, String uid, Folder folder) { + super(uuidGenerator); this.mUid = uid; this.mFolder = folder; } diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/CharsetSupportTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/CharsetSupportTest.java index 5d64202da..d7cf2bb76 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/CharsetSupportTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/CharsetSupportTest.java @@ -20,7 +20,7 @@ public class CharsetSupportTest { charsetOnMail = "CP932"; expect = "shift_jis"; - assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, new MimeMessage())); + assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, MimeMessage.createMimeMessage())); // charsetOnMail = "koi8-u"; // expect = "koi8-r"; @@ -28,73 +28,73 @@ public class CharsetSupportTest { MimeMessage message; - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@docomo.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@dwmail.jp"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@pdx.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@willcom.com"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@emnet.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@emobile.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-docomo-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@softbank.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-softbank-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@vodafone.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-softbank-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@disney.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-softbank-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@vertuclub.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-softbank-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@ezweb.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-kddi-shift_jis-2007"; assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message)); - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setHeader("From", "aaa@ido.ne.jp"); charsetOnMail = "shift_jis"; expect = "x-kddi-shift_jis-2007"; diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/ListHeadersTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/ListHeadersTest.java index ce59c2547..a9775958a 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/ListHeadersTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/ListHeadersTest.java @@ -117,7 +117,7 @@ public class ListHeadersTest { } private MimeMessage buildMimeMessageWithListPostValue(String... values) throws MessagingException { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); for (String value : values) { message.addHeader("List-Post", value); } diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java index 0c2838880..cce6dd438 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java @@ -210,11 +210,11 @@ public class MimeMessageParseTest { } private static MimeMessage parseWithoutRecurse(InputStream data) throws Exception { - return new MimeMessage(data, false); + return MimeMessage.parseMimeMessage(data, false); } private static MimeMessage parseWithRecurse(InputStream data) throws Exception { - return new MimeMessage(data, true); + return MimeMessage.parseMimeMessage(data, true); } private static void checkAddresses(Address[] actual, String... expected) { diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java index c9e9a3b2b..5a4ebc97b 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java @@ -279,7 +279,7 @@ public class WebDavFolderTest { List messages = new ArrayList<>(); for (int i = 0; i < 25; i++) { - WebDavMessage message = new WebDavMessage("message" + i, folder); + WebDavMessage message = WebDavMessage.createWebDavMessage("message" + i, folder); message.setUrl("http://example.org/Exchange/user/Inbox/message" + i + ".EML"); messages.add(message); } diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java index 9be44d893..59d06e169 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java @@ -35,7 +35,7 @@ public class WebDavMessageTest { MockitoAnnotations.initMocks(this); when(mockFolder.getName()).thenReturn("Inbox"); when(mockFolder.getUrl()).thenReturn("http://example.org/Inbox"); - message = new WebDavMessage("message1", mockFolder); + message = WebDavMessage.createWebDavMessage("message1", mockFolder); } @Test diff --git a/k9mail/src/androidTest/java/com/fsck/k9/mailstore/ReconstructMessageFromDatabaseTest.java b/k9mail/src/androidTest/java/com/fsck/k9/mailstore/ReconstructMessageFromDatabaseTest.java index 9a7249b97..491ffa2c2 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/mailstore/ReconstructMessageFromDatabaseTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/mailstore/ReconstructMessageFromDatabaseTest.java @@ -95,7 +95,7 @@ public class ReconstructMessageFromDatabaseTest extends ApplicationTestCase public void testAddMissingPart() throws MessagingException, IOException { LocalFolder folder = createFolderInDatabase(); - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.addHeader("To", "to@example.com"); message.addHeader("MIME-Version", "1.0"); message.addHeader("Content-Type", "text/plain"); @@ -128,7 +128,7 @@ public class ReconstructMessageFromDatabaseTest extends ApplicationTestCase protected MimeMessage parseMessage() throws IOException, MessagingException { InputStream messageInputStream = new ByteArrayInputStream(MESSAGE_SOURCE.getBytes()); try { - return new MimeMessage(messageInputStream, true); + return MimeMessage.parseMimeMessage(messageInputStream, true); } finally { messageInputStream.close(); } diff --git a/k9mail/src/androidTest/java/com/fsck/k9/provider/EmailProviderTest.java b/k9mail/src/androidTest/java/com/fsck/k9/provider/EmailProviderTest.java index c84cfd8f3..28d29f139 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/provider/EmailProviderTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/provider/EmailProviderTest.java @@ -33,22 +33,22 @@ public class EmailProviderTest extends ProviderTestCase2 { } private void buildMessages() throws MessagingException { - message = new MimeMessage(); + message = MimeMessage.createMimeMessage(); message.setSubject("Test Subject"); message.setSentDate(new GregorianCalendar(2016, 1, 2).getTime(), false); message.setMessageId(""); - laterMessage = new MimeMessage(); + laterMessage = MimeMessage.createMimeMessage(); laterMessage.setSubject("Test Subject2"); laterMessage.setSentDate(new GregorianCalendar(2016, 1, 3).getTime(), false); - reply = new MimeMessage(); + reply = MimeMessage.createMimeMessage(); reply.setSubject("Re: Test Subject"); reply.setSentDate(new GregorianCalendar(2016, 1, 3).getTime(), false); reply.setMessageId(""); reply.setInReplyTo(""); - replyAtSameTime = new MimeMessage(); + replyAtSameTime = MimeMessage.createMimeMessage(); replyAtSameTime.setSubject("Re: Test Subject"); replyAtSameTime.setSentDate(new GregorianCalendar(2016, 1, 2).getTime(), false); replyAtSameTime.setMessageId(""); @@ -301,7 +301,7 @@ public class EmailProviderTest extends ProviderTestCase2 { Account account = Preferences.getPreferences(getContext()).newAccount(); account.getUuid(); - Message message = new MimeMessage(); + Message message = MimeMessage.createMimeMessage(); message.setSubject("Test Subject"); message.setSentDate(new GregorianCalendar(2016, 1, 2).getTime(), false); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index a1ec80e87..310b04462 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -2413,7 +2413,7 @@ public class MessagingController { Store localStore = account.getLocalStore(); LocalFolder localFolder = (LocalFolder)localStore.getFolder(account.getErrorFolderName()); - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMessageHelper.setBody(message, new TextBody(body)); message.setFlag(Flag.X_DOWNLOADED_FULL, true); diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentMessageBodyUtil.java b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentMessageBodyUtil.java index 71fc418d6..a1175eb03 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentMessageBodyUtil.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentMessageBodyUtil.java @@ -21,7 +21,7 @@ class AttachmentMessageBodyUtil { * could be sent along without processing. But since we * don't know, we recursively parse it. */ - MimeMessage message = new MimeMessage(in, true); + MimeMessage message = MimeMessage.parseMimeMessage(in, true); message.setUsing7bitTransport(); message.writeTo(out); } else { diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index a629c09dd..229624c54 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -739,7 +739,7 @@ public class LocalFolder extends Folder implements Serializable { ((Multipart) parentPart.getBody()).addBodyPart(bodyPart); part = bodyPart; } else if (MimeUtility.isMessage(parentMimeType)) { - Message innerMessage = new MimeMessage(); + Message innerMessage = MimeMessage.createMimeMessage(); parentPart.setBody(innerMessage); part = innerMessage; } else { @@ -833,7 +833,8 @@ public class LocalFolder extends Folder implements Serializable { public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { open(OPEN_MODE_RW); - LocalMessage message = new LocalMessage(LocalFolder.this.localStore, uid, LocalFolder.this); + LocalMessage message = + LocalMessage.createLocalMessage(LocalFolder.this.localStore, uid, LocalFolder.this); Cursor cursor = null; try { diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java index 57923ec86..2773bf991 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java @@ -17,6 +17,7 @@ import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.UuidGenerator; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.message.MessageHeaderParser; import com.fsck.k9.mailstore.LockableDatabase.DbCallback; @@ -40,16 +41,25 @@ public class LocalMessage extends MimeMessage { private String mimeType; private PreviewType previewType; - private LocalMessage(LocalStore localStore) { + + static LocalMessage createLocalMessage(LocalStore localStore, String uid, Folder folder) { + UuidGenerator uuidGenerator = UuidGenerator.getInstance(); + return new LocalMessage(uuidGenerator, localStore, uid, folder); + } + + private LocalMessage(UuidGenerator uuidGenerator, LocalStore localStore) { + super(uuidGenerator); this.localStore = localStore; } - LocalMessage(LocalStore localStore, String uid, Folder folder) { + private LocalMessage(UuidGenerator uuidGenerator, LocalStore localStore, String uid, Folder folder) { + super(uuidGenerator); this.localStore = localStore; this.mUid = uid; this.mFolder = folder; } + void populateFromGetMessageCursor(Cursor cursor) throws MessagingException { final String subject = cursor.getString(0); this.setSubject(subject == null ? "" : subject); @@ -496,7 +506,7 @@ public class LocalMessage extends MimeMessage { @Override public LocalMessage clone() { - LocalMessage message = new LocalMessage(this.localStore); + LocalMessage message = new LocalMessage(uuidGenerator, localStore); super.copy(message); message.mId = mId; diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java index 267b4e6da..6a3690f20 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalStore.java @@ -602,7 +602,7 @@ public class LocalStore extends Store implements Serializable { cursor = db.rawQuery(queryString + " LIMIT 10", placeHolders); while (cursor.moveToNext()) { - LocalMessage message = new LocalMessage(LocalStore.this, null, folder); + LocalMessage message = LocalMessage.createLocalMessage(LocalStore.this, null, folder); message.populateFromGetMessageCursor(cursor); messages.add(message); @@ -615,7 +615,7 @@ public class LocalStore extends Store implements Serializable { cursor = db.rawQuery(queryString + " LIMIT -1 OFFSET 10", placeHolders); while (cursor.moveToNext()) { - LocalMessage message = new LocalMessage(LocalStore.this, null, folder); + LocalMessage message = LocalMessage.createLocalMessage(LocalStore.this, null, folder); message.populateFromGetMessageCursor(cursor); messages.add(message); diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/MimePartStreamParser.java b/k9mail/src/main/java/com/fsck/k9/mailstore/MimePartStreamParser.java index 4727ed72b..d5c7695f7 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/MimePartStreamParser.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/MimePartStreamParser.java @@ -81,7 +81,7 @@ public class MimePartStreamParser { } else { Part part = (Part) stack.peek(); - Message innerMessage = new MimeMessage(); + Message innerMessage = MimeMessage.createMimeMessage(); part.setBody(innerMessage); stack.push(innerMessage); diff --git a/k9mail/src/main/java/com/fsck/k9/message/MessageBuilder.java b/k9mail/src/main/java/com/fsck/k9/message/MessageBuilder.java index 23447c406..16bb75887 100644 --- a/k9mail/src/main/java/com/fsck/k9/message/MessageBuilder.java +++ b/k9mail/src/main/java/com/fsck/k9/message/MessageBuilder.java @@ -84,7 +84,7 @@ public abstract class MessageBuilder { protected MimeMessage build() throws MessagingException { //FIXME: check arguments - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); buildHeader(message); buildBody(message); @@ -124,7 +124,7 @@ public abstract class MessageBuilder { message.setReferences(references); } - message.generateMessageId(uuidGenerator); + message.generateMessageId(); if (isDraft && isPgpInlineEnabled) { message.setFlag(Flag.X_DRAFT_OPENPGP_INLINE, true); diff --git a/k9mail/src/test/java/com/fsck/k9/crypto/MessageDecryptVerifierTest.java b/k9mail/src/test/java/com/fsck/k9/crypto/MessageDecryptVerifierTest.java index 52bed1116..be23af415 100644 --- a/k9mail/src/test/java/com/fsck/k9/crypto/MessageDecryptVerifierTest.java +++ b/k9mail/src/test/java/com/fsck/k9/crypto/MessageDecryptVerifierTest.java @@ -35,7 +35,7 @@ public class MessageDecryptVerifierTest { @Test public void findEncryptedPartsShouldReturnEmptyListForEmptyMessage() throws Exception { - MimeMessage emptyMessage = new MimeMessage(); + MimeMessage emptyMessage = MimeMessage.createMimeMessage(); List encryptedParts = MessageDecryptVerifier.findEncryptedParts(emptyMessage); @@ -44,7 +44,7 @@ public class MessageDecryptVerifierTest { @Test public void findEncryptedPartsShouldReturnEmptyListForSimpleMessage() throws Exception { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.setBody(new TextBody("message text")); List encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); @@ -54,7 +54,7 @@ public class MessageDecryptVerifierTest { @Test public void findEncryptedPartsShouldReturnEmptyEncryptedPart() throws Exception { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMultipart multipartEncrypted = MimeMultipart.createMimeMultipart(); multipartEncrypted.setSubType("encrypted"); MimeMessageHelper.setBody(message, multipartEncrypted); @@ -68,7 +68,7 @@ public class MessageDecryptVerifierTest { @Test public void findEncryptedPartsShouldReturnMultipleEncryptedParts() throws Exception { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMultipart multipartMixed = MimeMultipart.createMimeMultipart(); multipartMixed.setSubType("mixed"); MimeMessageHelper.setBody(message, multipartMixed); @@ -273,7 +273,7 @@ public class MessageDecryptVerifierTest { } MimeMessage messageFromBody(BodyPart bodyPart) throws MessagingException { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMessageHelper.setBody(message, bodyPart.getBody()); return message; } diff --git a/k9mail/src/test/java/com/fsck/k9/mailstore/MessageViewInfoExtractorTest.java b/k9mail/src/test/java/com/fsck/k9/mailstore/MessageViewInfoExtractorTest.java index b6f556daa..eeb1df1ed 100644 --- a/k9mail/src/test/java/com/fsck/k9/mailstore/MessageViewInfoExtractorTest.java +++ b/k9mail/src/test/java/com/fsck/k9/mailstore/MessageViewInfoExtractorTest.java @@ -69,7 +69,7 @@ public class MessageViewInfoExtractorTest { TextBody body = new TextBody(BODY_TEXT); // Create message - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMessageHelper.setBody(message, body); // Prepare fixture @@ -95,7 +95,7 @@ public class MessageViewInfoExtractorTest { TextBody body = new TextBody(BODY_TEXT); // Create message - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); MimeMessageHelper.setBody(message, body); @@ -123,7 +123,7 @@ public class MessageViewInfoExtractorTest { TextBody body = new TextBody(bodyText); // Create message - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); MimeMessageHelper.setBody(message, body); @@ -158,7 +158,7 @@ public class MessageViewInfoExtractorTest { multipart.addBodyPart(bodyPart2); // Create message - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMessageHelper.setBody(message, multipart); // Extract text @@ -201,7 +201,7 @@ public class MessageViewInfoExtractorTest { TextBody innerBody = new TextBody(innerBodyText); // Create message/rfc822 body - MimeMessage innerMessage = new MimeMessage(); + MimeMessage innerMessage = MimeMessage.createMimeMessage(); innerMessage.addSentDate(new Date(112, 02, 17), false); innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") }); innerMessage.setSubject("Subject"); @@ -217,7 +217,7 @@ public class MessageViewInfoExtractorTest { multipart.addBodyPart(bodyPart2); // Create message - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); MimeMessageHelper.setBody(message, multipart); // Extract text diff --git a/k9mail/src/test/java/com/fsck/k9/message/MessageCreationHelper.java b/k9mail/src/test/java/com/fsck/k9/message/MessageCreationHelper.java index 26de0d465..f58106ca2 100644 --- a/k9mail/src/test/java/com/fsck/k9/message/MessageCreationHelper.java +++ b/k9mail/src/test/java/com/fsck/k9/message/MessageCreationHelper.java @@ -52,7 +52,7 @@ public class MessageCreationHelper { } private static Message createMessage(String mimeType, Body body) throws MessagingException { - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.createMimeMessage(); message.setBody(body); message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType); diff --git a/k9mail/src/test/java/com/fsck/k9/message/extractors/MessagePreviewCreatorTest.java b/k9mail/src/test/java/com/fsck/k9/message/extractors/MessagePreviewCreatorTest.java index 7bab3f572..dbe013379 100644 --- a/k9mail/src/test/java/com/fsck/k9/message/extractors/MessagePreviewCreatorTest.java +++ b/k9mail/src/test/java/com/fsck/k9/message/extractors/MessagePreviewCreatorTest.java @@ -103,6 +103,6 @@ public class MessagePreviewCreatorTest { } private Message createDummyMessage() { - return new MimeMessage(); + return MimeMessage.createMimeMessage(); } }