Search in sources :

Example 1 with MemoryMailListener

use of org.xwiki.mail.internal.MemoryMailListener in project xwiki-platform by xwiki.

the class JavaIntegrationTest method sendTextMail.

@Test
public void sendTextMail() throws Exception {
    // Step 1: Create a JavaMail Session
    Session session = Session.getInstance(this.configuration.getAllProperties());
    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);
    // We also test using some default BCC addresses from configuration in this test
    this.configuration.setBCCAddresses(Arrays.asList("bcc1@doe.com", "bcc2@doe.com"));
    // Ensure we do not reuse the same message identifier for multiple similar messages in this test
    MimeMessage message2 = new MimeMessage(message);
    message2.saveChanges();
    MimeMessage message3 = new MimeMessage(message);
    message3.saveChanges();
    // Step 4: Send the mail and wait for it to be sent
    // Send 3 mails (3 times the same mail) to verify we can send several emails at once.
    MailListener memoryMailListener = this.componentManager.getInstance(MailListener.class, "memory");
    this.sender.sendAsynchronously(Arrays.asList(message, message2, message3), session, memoryMailListener);
    // Note: we don't test status reporting from the listener since this is already tested in the
    // ScriptingIntegrationTest test class.
    // Verify that the mails have been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 3);
    MimeMessage[] messages = this.mail.getReceivedMessages();
    // Note: we're receiving 9 messages since we sent 3 with 3 recipients (2 BCC and 1 to)!
    assertEquals(9, messages.length);
    // Assert the email parts that are the same for all mails
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
    assertEquals("john@doe.com", messages[0].getHeader("To", null));
// Note: We cannot assert that the BCC worked since by definition BCC information are not visible in received
// messages ;) But we checked that we received 9 emails above so that's good enough.
}
Also used : MailListener(org.xwiki.mail.MailListener) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 2 with MemoryMailListener

use of org.xwiki.mail.internal.MemoryMailListener in project xwiki-platform by xwiki.

the class PrepareMailRunnableTest method prepareMailWhenContentStoreFails.

@Test
public void prepareMailWhenContentStoreFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message1 = new MimeMessage(session);
    message1.setText("Content1");
    MimeMessage message2 = new MimeMessage(session);
    message2.setText("Content2");
    String batchId1 = UUID.randomUUID().toString();
    String batchId2 = UUID.randomUUID().toString();
    ExecutionContext context1 = new ExecutionContext();
    XWikiContext xContext1 = new XWikiContext();
    xContext1.setWikiId("wiki1");
    context1.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext1);
    ExecutionContext context2 = new ExecutionContext();
    XWikiContext xContext2 = new XWikiContext();
    xContext2.setWikiId("wiki2");
    context2.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext2);
    MemoryMailListener listener1 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item1 = new PrepareMailQueueItem(Arrays.asList(message1), session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    // Make the content store save fail
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doThrow(new MailStoreException("error")).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener1.getMailStatusResult().waitTillProcessed(10000L);
        listener2.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    MailStatusResult result1 = listener1.getMailStatusResult();
    MailStatusResult result2 = listener2.getMailStatusResult();
    // Despite the errors, both process should be ended with known total number of mails
    assertTrue(result1.isProcessed());
    assertTrue(result2.isProcessed());
    // This is the real test: we verify that there's been an error while sending each email.
    MailStatus status1 = result1.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status1.getErrorSummary());
    MailStatus status2 = result2.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status2.getErrorSummary());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 3 with MemoryMailListener

use of org.xwiki.mail.internal.MemoryMailListener in project xwiki-platform by xwiki.

the class SendMailRunnableTest method sendMailWhenSendingFails.

@Test
public void sendMailWhenSendingFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "xwiki-unknown");
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenReturn(message1);
    when(contentStore.load(session, batchId, id2)).thenReturn(message2);
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertTrue(status.getErrorSummary() != null);
        errorCount++;
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 4 with MemoryMailListener

use of org.xwiki.mail.internal.MemoryMailListener in project xwiki-platform by xwiki.

the class SendMailRunnableTest method sendMailWhenMailRetrievalFails.

@Test
public void sendMailWhenMailRetrievalFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    listener.onPrepareMessageSuccess(message1, Collections.<String, Object>emptyMap());
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    listener.onPrepareMessageSuccess(message2, Collections.<String, Object>emptyMap());
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenThrow(new MailStoreException("Store failure on message 1"));
    when(contentStore.load(session, batchId, id2)).thenThrow(new MailStoreException("Store failure on message 2"));
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_FATAL_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertEquals("MailStoreException: Store failure on message " + ++errorCount, status.getErrorSummary());
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 5 with MemoryMailListener

use of org.xwiki.mail.internal.MemoryMailListener in project xwiki-platform by xwiki.

the class JavaIntegrationTest method sendMailWithCustomMessageId.

@Test
public void sendMailWithCustomMessageId() throws Exception {
    Session session = Session.getInstance(this.configuration.getAllProperties());
    MimeMessage message = new MimeMessage(session) {

        @Override
        protected void updateMessageID() throws MessagingException {
            if (getMessageID() == null) {
                super.updateMessageID();
            }
        }
    };
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    message.setText("Test message Id support");
    message.setHeader("Message-ID", "<custom@domain>");
    message.setSubject("subject");
    MailListener memoryMailListener = this.componentManager.getInstance(MailListener.class, "memory");
    this.sender.sendAsynchronously(Arrays.asList(message), session, memoryMailListener);
    // Verify that the mails have been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();
    assertEquals("<custom@domain>", messages[0].getMessageID());
}
Also used : MailListener(org.xwiki.mail.MailListener) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Session (javax.mail.Session)6 MimeMessage (javax.mail.internet.MimeMessage)6 Test (org.junit.Test)6 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)6 Properties (java.util.Properties)4 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)4 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 MailContentStore (org.xwiki.mail.MailContentStore)4 UpdateableMailStatusResult (org.xwiki.mail.internal.UpdateableMailStatusResult)4 MailStatus (org.xwiki.mail.MailStatus)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 InternetAddress (javax.mail.internet.InternetAddress)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 MailListener (org.xwiki.mail.MailListener)2 MailStatusResult (org.xwiki.mail.MailStatusResult)2 MailStoreException (org.xwiki.mail.MailStoreException)2 Iterator (java.util.Iterator)1 BodyPart (javax.mail.BodyPart)1 Multipart (javax.mail.Multipart)1