1   /*
2    * $Id: Rfc822ByteArrayTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transformers.MimeMessageToRfc822ByteArray;
18  import org.mule.transport.email.transformers.Rfc822ByteArraytoMimeMessage;
19  
20  import java.io.IOException;
21  import java.util.Properties;
22  
23  import javax.mail.MessagingException;
24  import javax.mail.Session;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeMessage;
27  
28  public class Rfc822ByteArrayTestCase extends FunctionalTestCase
29  {
30  
31      protected String getConfigResources()
32      {
33          return "rfc822-byte-array-test.xml";
34      }
35  
36      public void testToByteArray() throws MessagingException, TransformerException
37      {
38          mimeMessageToByteArray(newMimeMessage());
39      }
40  
41      public void testToByteArrayAndBack() throws MessagingException, MuleException, IOException
42      {
43          MimeMessage first = newMimeMessage();
44          byte[] bytes = mimeMessageToByteArray(first);
45          MimeMessage second = byteArrayToMimeMessage(bytes);
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 = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(
56              "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.setFrom(new InternetAddress("bob@example.com"));
75          return message;
76      }
77  
78      protected Session newSession()
79      {
80          return Session.getDefaultInstance(new Properties(), null);
81      }
82  
83  }