1   /*
2    * $Id: Rfc822ByteArrayTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.providers.email.transformers;
12  
13  import org.mule.impl.endpoint.MuleEndpoint;
14  import org.mule.umo.UMOException;
15  import org.mule.umo.transformer.TransformerException;
16  
17  import java.io.IOException;
18  import java.util.Properties;
19  
20  import javax.mail.MessagingException;
21  import javax.mail.Session;
22  import javax.mail.internet.InternetAddress;
23  import javax.mail.internet.MimeMessage;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Unit tests for the two RFC 822 related transformers
29   */
30  public class Rfc822ByteArrayTestCase extends TestCase
31  {
32  
33      public void testToByteArray() throws MessagingException, TransformerException
34      {
35          mimeMessageToByteArray(newMimeMessage());
36      }
37  
38      public void testToByteArrayAndBack() throws MessagingException, UMOException, IOException
39      {
40          MimeMessage first = newMimeMessage();
41          byte[] bytes = mimeMessageToByteArray(first);
42          MimeMessage second = byteArrayToMimeMessage(bytes);
43          assertEquals(first.getContent(), second.getContent());
44          assertEquals(1, second.getFrom().length);
45          assertEquals(first.getFrom().length, second.getFrom().length);
46          assertEquals(first.getFrom()[0], second.getFrom()[0]);
47      }
48  
49      protected MimeMessage byteArrayToMimeMessage(byte[] bytes) throws UMOException
50      {
51          Rfc822ByteArraytoMimeMessage transformer = new Rfc822ByteArraytoMimeMessage();
52          MuleEndpoint endpoint = new MuleEndpoint("smtp://localhost", false);
53          transformer.setEndpoint(endpoint);
54          Object result = transformer.transform(bytes);
55          assertTrue(result instanceof MimeMessage);
56          return (MimeMessage) result;
57      }
58  
59      protected byte[] mimeMessageToByteArray(MimeMessage mimeMessage) throws TransformerException
60      {
61          Object result = new MimeMessageToRfc822ByteArray().transform(mimeMessage);
62          assertTrue(result instanceof byte[]);
63          return (byte[]) result;
64      }
65  
66      protected MimeMessage newMimeMessage() throws MessagingException
67      {
68          MimeMessage message = new MimeMessage(newSession());
69          message.setText("text");
70          message.setFrom(new InternetAddress("bob@example.com"));
71          return message;
72      }
73  
74      protected Session newSession()
75      {
76          return Session.getDefaultInstance(new Properties(), null);
77      }
78  
79  }