View Javadoc

1   /*
2    * $Id: MailMuleMessageFactoryTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.MuleMessageFactory;
15  import org.mule.transport.AbstractMuleMessageFactoryTestCase;
16  
17  import javax.activation.DataHandler;
18  import javax.activation.DataSource;
19  import javax.mail.BodyPart;
20  import javax.mail.Message;
21  import javax.mail.Message.RecipientType;
22  import javax.mail.MessagingException;
23  import javax.mail.Multipart;
24  import javax.mail.Session;
25  import javax.mail.internet.MimeBodyPart;
26  import javax.mail.internet.MimeMessage;
27  import javax.mail.internet.MimeMultipart;
28  import javax.mail.util.ByteArrayDataSource;
29  
30  import org.junit.Test;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  public class MailMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
35  {
36      @Override
37      protected MuleMessageFactory doCreateMuleMessageFactory()
38      {
39          return new MailMuleMessageFactory(muleContext);
40      }
41  
42      @Override
43      protected MimeMessage getValidTransportMessage() throws Exception
44      {
45          MimeMessage message = new MimeMessage((Session) null);
46          message.setContent(TEST_MESSAGE, "text/plain; charset=ISO-8859-1");
47          return message;
48      }
49  
50      @Override
51      protected Object getUnsupportedTransportMessage()
52      {
53          return "this is not a valid transport message for MailMuleMessageFactory";
54      }
55  
56      @Test
57      public void testAttachments() throws Exception
58      {
59          Message payload = createMimeMessageWithAttachment();
60  
61          MuleMessageFactory factory = createMuleMessageFactory();
62          MuleMessage muleMessage = factory.create(payload, encoding);
63          assertEquals(2, muleMessage.getInboundAttachmentNames().size());
64      }
65  
66      @Test
67      public void testAddRecipientProperties() throws Exception
68      {
69          String to = "Information <info@domain.com>";
70          String cc = "\"info@\" <domain.com info@domain.com>";
71          String bcc = "'invalid@domain.com', info <info@domain.com>";
72  
73          MimeMessage payload = getValidTransportMessage();
74          payload.setHeader(RecipientType.TO.toString(), to);
75          payload.setHeader(RecipientType.CC.toString(), cc);
76          payload.setHeader(RecipientType.BCC.toString(), bcc);
77  
78          MuleMessageFactory factory = createMuleMessageFactory();
79          MuleMessage muleMessage = factory.create(payload, encoding);
80  
81          assertEquals(to, muleMessage.getOutboundProperty(MailProperties.INBOUND_TO_ADDRESSES_PROPERTY));
82          assertEquals(cc, muleMessage.getOutboundProperty(MailProperties.INBOUND_CC_ADDRESSES_PROPERTY));
83          assertEquals(bcc, muleMessage.getOutboundProperty(MailProperties.INBOUND_BCC_ADDRESSES_PROPERTY));
84      }
85  
86      @Test
87      public void testAttachmentsWithSameName() throws Exception
88      {
89          Message payload = createMimeMessageWithSameAttachmentNames();
90  
91          MuleMessageFactory factory = createMuleMessageFactory();
92          MuleMessage muleMessage = factory.create(payload, encoding);
93          assertEquals(3, muleMessage.getInboundAttachmentNames().size());
94      }
95  
96      private Message createMimeMessageWithAttachment() throws Exception
97      {
98          MimeBodyPart mainBody = new MimeBodyPart();
99          mainBody.setText("This is the main message text");
100 
101         MimeBodyPart attachment = createBodyPart(TEST_MESSAGE, "message.txt");
102 
103         Multipart multipart = createMultipart(mainBody, attachment);
104 
105         MimeMessage message = getValidTransportMessage();
106         message.setContent(multipart);
107         return message;
108     }
109 
110     private Message createMimeMessageWithSameAttachmentNames() throws Exception
111     {
112         MimeBodyPart mainBody = new MimeBodyPart();
113         mainBody.setText("This is the main message text");
114 
115         MimeBodyPart firstAttachment = createBodyPart("The first attachment content", "message.txt");
116         MimeBodyPart secondAttachment = createBodyPart("The second attachment content", "message.txt");
117 
118         Multipart multipart = createMultipart(mainBody, firstAttachment, secondAttachment);
119 
120         MimeMessage message = getValidTransportMessage();
121         message.setContent(multipart);
122         return message;
123     }
124 
125     private MimeBodyPart createBodyPart(String content, String fileName) throws MessagingException
126     {
127         MimeBodyPart attachment = new MimeBodyPart();
128         DataSource source = new ByteArrayDataSource(content.getBytes(), "text/plain");
129         attachment.setDataHandler(new DataHandler(source));
130         attachment.setFileName(fileName);
131         return attachment;
132     }
133 
134     private Multipart createMultipart(BodyPart... parts) throws MessagingException
135     {
136         Multipart multipart = new MimeMultipart();
137 
138         for (BodyPart part : parts)
139         {
140             multipart.addBodyPart(part);
141         }
142 
143         return multipart;
144     }
145 }