View Javadoc

1   /*
2    * $Id: Rfc822ByteArrayTestCase.java 22431 2011-07-18 07:40:35Z 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.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.junit4.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  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  public class Rfc822ByteArrayTestCase extends FunctionalTestCase
33  {
34  
35      @Override
36      protected String getConfigResources()
37      {
38          return "rfc822-byte-array-test.xml";
39      }
40  
41      @Test
42      public void testToByteArray() throws MessagingException, TransformerException
43      {
44          mimeMessageToByteArray(newMimeMessage());
45      }
46  
47      @Test
48      public void testToByteArrayAndBack() throws MessagingException, MuleException, IOException
49      {
50          MimeMessage first = newMimeMessage();
51          byte[] bytes = mimeMessageToByteArray(first);
52          MimeMessage second = byteArrayToMimeMessage(bytes);
53          assertEquals(first.getSubject(), second.getSubject());
54          assertEquals(first.getContent(), second.getContent());
55          assertEquals(1, second.getFrom().length);
56          assertEquals(first.getFrom().length, second.getFrom().length);
57          assertEquals(first.getFrom()[0], second.getFrom()[0]);
58      }
59  
60      protected MimeMessage byteArrayToMimeMessage(byte[] bytes) throws MuleException
61      {
62          Rfc822ByteArraytoMimeMessage transformer = new Rfc822ByteArraytoMimeMessage();
63          ImmutableEndpoint endpoint = 
64              muleContext.getEndpointFactory().getOutboundEndpoint(SmtpConnector.SMTP);
65          transformer.setEndpoint(endpoint);
66          Object result = transformer.transform(bytes);
67          assertTrue(result instanceof MimeMessage);
68          return (MimeMessage) result;
69      }
70  
71      protected byte[] mimeMessageToByteArray(MimeMessage mimeMessage) throws TransformerException
72      {
73          Object result = new MimeMessageToRfc822ByteArray().transform(mimeMessage);
74          assertTrue(result instanceof byte[]);
75          return (byte[]) result;
76      }
77  
78      protected MimeMessage newMimeMessage() throws MessagingException
79      {
80          MimeMessage message = new MimeMessage(newSession());
81          message.setText("text");
82          message.setSubject("text");
83          message.setFrom(new InternetAddress("bob@example.com"));
84          return message;
85      }
86  
87      protected Session newSession()
88      {
89          return Session.getDefaultInstance(new Properties(), null);
90      }
91  }