View Javadoc

1   /*
2    * $Id: MailMuleMessageFactoryTestCase.java 19585 2010-09-10 20:39:04Z aperepel $
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.Message;
20  import javax.mail.Multipart;
21  import javax.mail.Session;
22  import javax.mail.internet.MimeBodyPart;
23  import javax.mail.internet.MimeMessage;
24  import javax.mail.internet.MimeMultipart;
25  import javax.mail.util.ByteArrayDataSource;
26  
27  public class MailMuleMessageFactoryTestCase extends AbstractMuleMessageFactoryTestCase
28  {
29      @Override
30      protected MuleMessageFactory doCreateMuleMessageFactory()
31      {
32          return new MailMuleMessageFactory(muleContext);
33      }
34  
35      @Override
36      protected MimeMessage getValidTransportMessage() throws Exception
37      {
38          MimeMessage message = new MimeMessage((Session) null);
39          message.setContent(TEST_MESSAGE, "text/plain; charset=ISO-8859-1");
40          return message;
41      }
42  
43      @Override
44      protected Object getUnsupportedTransportMessage()
45      {
46          return "this is not a valid transport message for MailMuleMessageFactory";
47      }
48      
49      public void testAttachments() throws Exception
50      {
51          Message payload = createMimeMessageWithAttachment();
52  
53          MuleMessageFactory factory = createMuleMessageFactory();
54          MuleMessage muleMessage = factory.create(payload, encoding);
55          assertEquals(2, muleMessage.getInboundAttachmentNames().size());
56      }
57      
58      private Message createMimeMessageWithAttachment() throws Exception
59      {
60          MimeBodyPart mainBody = new MimeBodyPart();
61          mainBody.setText("This is the main message text");
62          
63          MimeBodyPart attachment = new MimeBodyPart();
64          DataSource source = new ByteArrayDataSource(TEST_MESSAGE.getBytes(), "text/plain");
65          attachment.setDataHandler(new DataHandler(source));
66          attachment.setFileName("message.txt");
67  
68          Multipart multipart = new MimeMultipart();
69          multipart.addBodyPart(mainBody);
70          multipart.addBodyPart(attachment);
71            
72          MimeMessage message = getValidTransportMessage();
73          message.setContent(multipart);
74          return message;
75      }
76  }