Search in sources :

Example 51 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestParsedMessage method testParsedMessage.

@Test
public void testParsedMessage() throws Exception {
    ExpectedResults expected = new ExpectedResults();
    String subject = NAME_PREFIX + " testParsedMessage";
    expected.convertedSubject = subject;
    expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
    expected.wasMutated = false;
    // Test ParsedMessage created from byte[]
    ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(expected.rawContent.getBytes(), true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from File
    mFile = File.createTempFile("TestParsedMessage", ".msg");
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(expected.rawContent.getBytes());
    pm = new ParsedMessage(mFile, null, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mFile, null, true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from MimeMessage.
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
    pm = new ParsedMessage(mimeMsg, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mimeMsg, true);
    verifyParsedMessage(pm, expected);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 52 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestParsedMessage method testMimeConverter.

@Test
public void testMimeConverter() throws Exception {
    MimeVisitor.registerConverter(TestMimeVisitor.class);
    ExpectedResults expected = new ExpectedResults();
    String subject = NAME_PREFIX + " testMimeConverter oldsubject";
    expected.convertedSubject = NAME_PREFIX + " testMimeConverter newsubject";
    expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
    expected.wasMutated = false;
    // Test ParsedMessage created from byte[]
    ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(expected.rawContent.getBytes(), true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from File
    mFile = File.createTempFile("TestParsedMessage", ".msg");
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(expected.rawContent.getBytes());
    out.close();
    pm = new ParsedMessage(mFile, null, false);
    verifyParsedMessage(pm, expected);
    pm = new ParsedMessage(mFile, null, true);
    verifyParsedMessage(pm, expected);
    // Test ParsedMessage created from MimeMessage.  Can't verify entire content
    // because JavaMail mangles the headers.
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
    pm = new ParsedMessage(mimeMsg, false);
    Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    Assert.assertTrue(pm.getSubject().contains("newsubject"));
    pm = new ParsedMessage(mimeMsg, true);
    Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    Assert.assertTrue(pm.getSubject().contains("newsubject"));
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) FileOutputStream(java.io.FileOutputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 53 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestParsedMessage method testMimeMutator.

/**
     * Tests message mutation.  We can't verify the entire content, since mutation
     * calls JavaMail, which mangles the headers.  We'll have to settle for confirming
     * that the subject was updated correctly.
     */
@Test
public void testMimeMutator() throws Exception {
    MimeVisitor.registerMutator(TestMimeVisitor.class);
    String subject = NAME_PREFIX + " testMimeConverter oldsubject";
    String content = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
    // Test ParsedMessage created from byte[]
    ParsedMessage pm = new ParsedMessage(content.getBytes(), false);
    pm = new ParsedMessage(content.getBytes(), true);
    String substring = "newsubject";
    verifyMutatedMessage(pm, substring, true);
    // Test ParsedMessage created from File
    mFile = createTempFile(content);
    pm = new ParsedMessage(mFile, null, false);
    verifyMutatedMessage(pm, substring, true);
    mFile.delete();
    mFile = createTempFile(content);
    pm = new ParsedMessage(mFile, null, true);
    verifyMutatedMessage(pm, substring, true);
    // Test ParsedMessage created from MimeMessage, attachment indexing off.
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    pm = new ParsedMessage(mimeMsg, false);
    verifyMutatedMessage(pm, substring, true);
    // Test ParsedMessage created from MimeMessage, attachment indexing on.
    mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    pm = new ParsedMessage(mimeMsg, true);
    verifyMutatedMessage(pm, substring, true);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)53 MimeMessage (javax.mail.internet.MimeMessage)50 Test (org.junit.Test)34 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)30 InputStream (java.io.InputStream)17 Session (javax.mail.Session)14 Transport (javax.mail.Transport)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)13 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)13 JMSession (com.zimbra.cs.util.JMSession)13 Message (com.zimbra.cs.mailbox.Message)12 Account (com.zimbra.cs.account.Account)10 MessagingException (javax.mail.MessagingException)10 OperationContext (com.zimbra.cs.mailbox.OperationContext)9 ItemId (com.zimbra.cs.service.util.ItemId)9 InternetAddress (javax.mail.internet.InternetAddress)9 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)8 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)8 Date (java.util.Date)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6