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);
  }
  /**
   * 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.
   */
  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);
  }
 @Override
 protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception {
   Account account = calItem.getAccount();
   Locale locale = account.getLocale();
   TimeZone tz = Util.getAccountTimeZone(account);
   MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
   String to = account.getAttr(Provisioning.A_zimbraCalendarReminderDeviceEmail);
   if (to == null) {
     ZimbraLog.scheduler.info(
         "Unable to send calendar reminder sms since %s is not set",
         Provisioning.A_zimbraCalendarReminderDeviceEmail);
     return;
   }
   mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
   mm.setText(getText(calItem, invite, locale, tz), MimeConstants.P_CHARSET_UTF8);
   mm.saveChanges();
   MailSender mailSender = calItem.getMailbox().getMailSender();
   mailSender.setSaveToSent(false);
   mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
 }
  /** Tests adding a <tt>ParsedMessage</tt> to a mailbox. */
  public void testAddMessage() throws Exception {
    String msg =
        TestUtil.getTestMessage(NAME_PREFIX + " testAddMessage", SENDER_NAME, SENDER_NAME, null);

    // Test ParsedMessage from byte[]
    ParsedMessage pm = new ParsedMessage(msg.getBytes(), true);
    runAddMessageTest(msg, pm);

    // Test ParsedMessage from File
    mFile = File.createTempFile("TestParsedMessage", null);
    FileOutputStream out = new FileOutputStream(mFile);
    out.write(msg.getBytes());
    pm = new ParsedMessage(mFile, null, true);
    runAddMessageTest(msg, pm);

    // Test ParsedMessage from MimeMessage
    MimeMessage mimeMsg =
        new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(msg.getBytes()));
    pm = new ParsedMessage(mimeMsg, true);
    runAddMessageTest(msg, pm);
  }
  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);
    assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    assertTrue(pm.getSubject().contains("newsubject"));
    pm = new ParsedMessage(mimeMsg, true);
    assertTrue((new String(pm.getRawData()).contains("oldsubject")));
    assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
    assertTrue(pm.getSubject().contains("newsubject"));
  }
Beispiel #6
0
  /** Modify Contact having an attachment (bug 70488). */
  @Test
  public void modifyContactHavingAttachment() throws Exception {
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", "Contact Initial Content");
    byte[] attachData = "attachment 1".getBytes();
    Attachment textAttachment = new Attachment(attachData, "text/plain", "customField", "file.txt");
    Mailbox mbox =
        MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact =
        mbox.createContact(
            null,
            new ParsedContact(attrs, Lists.newArrayList(textAttachment)),
            Mailbox.ID_FOLDER_CONTACTS,
            null);

    ParsedContact pc =
        new ParsedContact(contact)
            .modify(new ParsedContact.FieldDeltaList(), new ArrayList<Attachment>());
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), pc.getContentStream());
    MimePart mp = Mime.getMimePart(mm, "1");
    Assert.assertEquals("text/plain", mp.getContentType());
    Assert.assertEquals("attachment 1", mp.getContent());
  }