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