View Javadoc

1   /*
2    * $Id: Rfc822ByteArrayTestCase.java 20385 2010-11-29 20:25:26Z dfeist $
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.transformers;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.endpoint.ImmutableEndpoint;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.transport.email.SmtpConnector;
18  
19  import java.io.IOException;
20  import java.util.Properties;
21  
22  import javax.mail.MessagingException;
23  import javax.mail.Session;
24  import javax.mail.internet.InternetAddress;
25  import javax.mail.internet.MimeMessage;
26  
27  public class Rfc822ByteArrayTestCase extends FunctionalTestCase
28  {
29      @Override
30      protected String getConfigResources()
31      {
32          return "rfc822-byte-array-test.xml";
33      }
34  
35      public void testToByteArray() throws MessagingException, TransformerException
36      {
37          mimeMessageToByteArray(newMimeMessage());
38      }
39  
40      public void testToByteArrayAndBack() throws MessagingException, MuleException, IOException
41      {
42          MimeMessage first = newMimeMessage();
43          byte[] bytes = mimeMessageToByteArray(first);
44          MimeMessage second = byteArrayToMimeMessage(bytes);
45          assertEquals(first.getSubject(), second.getSubject());
46          assertEquals(first.getContent(), second.getContent());
47          assertEquals(1, second.getFrom().length);
48          assertEquals(first.getFrom().length, second.getFrom().length);
49          assertEquals(first.getFrom()[0], second.getFrom()[0]);
50      }
51  
52      protected MimeMessage byteArrayToMimeMessage(byte[] bytes) throws MuleException
53      {
54          Rfc822ByteArraytoMimeMessage transformer = new Rfc822ByteArraytoMimeMessage();
55          ImmutableEndpoint endpoint = 
56              muleContext.getEndpointFactory().getOutboundEndpoint(SmtpConnector.SMTP);
57          transformer.setEndpoint(endpoint);
58          Object result = transformer.transform(bytes);
59          assertTrue(result instanceof MimeMessage);
60          return (MimeMessage) result;
61      }
62  
63      protected byte[] mimeMessageToByteArray(MimeMessage mimeMessage) throws TransformerException
64      {
65          Object result = new MimeMessageToRfc822ByteArray().transform(mimeMessage);
66          assertTrue(result instanceof byte[]);
67          return (byte[]) result;
68      }
69  
70      protected MimeMessage newMimeMessage() throws MessagingException
71      {
72          MimeMessage message = new MimeMessage(newSession());
73          message.setText("text");
74          message.setSubject("text");
75          message.setFrom(new InternetAddress("bob@example.com"));
76          return message;
77      }
78  
79      protected Session newSession()
80      {
81          return Session.getDefaultInstance(new Properties(), null);
82      }
83  }