View Javadoc

1   /*
2    * $Id: AbstractEmailFunctionalTestCase.java 20018 2010-10-27 14:55:56Z 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.functional;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.config.i18n.LocaleMessageHandler;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.DynamicPortTestCase;
18  import org.mule.transport.email.GreenMailUtilities;
19  import org.mule.transport.email.ImapConnector;
20  import org.mule.transport.email.MailProperties;
21  import org.mule.transport.email.Pop3Connector;
22  import org.mule.util.SystemUtils;
23  
24  import com.icegreen.greenmail.util.GreenMail;
25  import com.icegreen.greenmail.util.ServerSetup;
26  
27  import java.util.HashMap;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import javax.activation.CommandMap;
32  import javax.activation.MailcapCommandMap;
33  import javax.mail.Address;
34  import javax.mail.Message;
35  import javax.mail.internet.MimeMessage;
36  import javax.mail.internet.MimeMultipart;
37  
38  public abstract class AbstractEmailFunctionalTestCase extends DynamicPortTestCase
39  {
40      public static final long DELIVERY_DELAY_MS = 10000;
41  
42      protected static final String CONFIG_BASE = "-functional-test.xml";
43      protected static final boolean MIME_MESSAGE = true;
44      protected static final boolean STRING_MESSAGE = false;
45  
46      protected static final String DEFAULT_EMAIL = "bob@example.com";
47      protected static final String DEFAULT_USER = "bob";
48      protected static final String DEFAULT_MESSAGE = "Test email message";
49      protected static final String DEFAULT_PASSWORD = "password";
50  
51      private String protocol;
52      private boolean isMimeMessage;
53      private int port;
54      private String configFile;
55      private GreenMail server;
56      private String email;
57      private String user;
58      private String message;
59      private String password;
60      private String charset;
61      private boolean addAttachments;
62  
63      protected AbstractEmailFunctionalTestCase(boolean isMimeMessage, String protocol)
64      {
65          this(isMimeMessage, protocol, protocol + CONFIG_BASE, null, null);
66      }
67  
68      protected AbstractEmailFunctionalTestCase(boolean isMimeMessage, String protocol, Locale locale, String charset)
69      {
70          this(isMimeMessage, protocol, protocol + CONFIG_BASE, locale, charset);
71      }
72  
73      protected AbstractEmailFunctionalTestCase(boolean isMimeMessage, String protocol, String configFile)
74      {
75          this(isMimeMessage, protocol, configFile, null, null);
76      }
77  
78      protected AbstractEmailFunctionalTestCase(boolean isMimeMessage, String protocol, String configFile, Locale locale, String charset)
79      {
80          this(isMimeMessage, protocol, configFile,
81                  DEFAULT_EMAIL, DEFAULT_USER, (locale == null ? DEFAULT_MESSAGE : getMessage(locale)), DEFAULT_PASSWORD, charset);
82      }
83  
84      protected AbstractEmailFunctionalTestCase(boolean isMimeMessage, String protocol,
85          String configFile, String email, String user, String message, String password, String charset)
86      {
87          this.isMimeMessage = isMimeMessage;
88          this.protocol = protocol;
89          //this.port = port;
90          this.configFile = configFile;
91          this.email = email;
92          this.user = user;
93          this.message = message;
94          this.password = password;
95          this.charset = charset;
96      }
97  
98      @Override
99      protected String getConfigResources()
100     {
101         return configFile;
102     }
103 
104     @Override
105     protected void suitePreSetUp() throws Exception
106     {
107         this.port = getPorts().get(0);
108         startServer();
109         initDefaultCommandMap();
110     }
111 
112     /**
113      * This is required to make all tests work on JDK5.
114      */
115     private void initDefaultCommandMap()
116     {
117         if (SystemUtils.JAVA_VERSION_FLOAT < 1.6f)
118         {
119             MailcapCommandMap commandMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
120             commandMap.addMailcap("application/xml;;  x-java-content-handler=com.sun.mail.handlers.text_plain");
121             commandMap.addMailcap("application/text;; x-java-content-handler=com.sun.mail.handlers.text_plain");
122             CommandMap.setDefaultCommandMap(commandMap);
123         }
124     }
125 
126     @Override
127     protected void suitePostTearDown() throws Exception
128     {
129         stopServer();
130     }
131 
132     protected void doSend() throws Exception
133     {
134         Object msg;
135         if (isMimeMessage)
136         {
137             msg = GreenMailUtilities.toMessage(message, email, charset);
138         }
139         else
140         {
141             msg = message;
142         }
143 
144         MuleClient client = new MuleClient(muleContext);
145         Map<String, Object> props = null;
146         if (charset != null)
147         {
148             props = new HashMap<String, Object>();
149             props.put(MailProperties.CONTENT_TYPE_PROPERTY, "text/plain; charset=" + charset);
150         }
151         if (addAttachments)
152         {
153             MuleMessage muleMessage = new DefaultMuleMessage(msg, props, muleContext);
154             createOutboundAttachments(muleMessage);
155             client.dispatch("vm://send", muleMessage);
156         }
157         else
158         {
159             client.dispatch("vm://send", msg, props);
160         }
161 
162         server.waitForIncomingEmail(DELIVERY_DELAY_MS, 1);
163 
164         MimeMessage[] messages = server.getReceivedMessages();
165         assertNotNull("did not receive any messages", messages);
166         assertEquals("did not receive 1 mail", 1, messages.length);
167         verifyMessage(messages[0]);
168     }
169 
170     protected void verifyMessage(MimeMessage received) throws Exception
171     {
172         if (addAttachments)
173         {
174             assertTrue("Did not receive a multipart message",
175                 received.getContent() instanceof MimeMultipart);
176             verifyMessage((MimeMultipart) received.getContent());
177         }
178         else
179         {
180             assertTrue("Did not receive a message with String contents",
181                 received.getContent() instanceof String);
182             verifyMessage((String) received.getContent());
183         }
184 
185         Address[] recipients = received.getRecipients(Message.RecipientType.TO);
186         assertNotNull(recipients);
187         assertEquals("number of recipients", 1, recipients.length);
188         assertEquals("recipient", email, recipients[0].toString());
189     }
190 
191     protected void verifyMessage(MimeMultipart mimeMultipart) throws Exception
192     {
193         fail("multipart message was not expected");
194     }
195 
196     protected void verifyMessage(String receivedText)
197     {
198         // for some reason, something is adding a newline at the end of messages
199         // so we need to strip that out for comparison
200         assertEquals(message, receivedText.trim());
201     }
202 
203     protected void doRequest() throws Exception
204     {
205         assertEquals(1, server.getReceivedMessages().length);
206 
207         MuleClient client = new MuleClient(muleContext);
208         MuleMessage reply = client.request("vm://receive", RECEIVE_TIMEOUT);
209         
210         assertNotNull(reply);
211         Object payload = reply.getPayload();
212         if (isMimeMessage)
213         {
214             assertTrue("payload is " + payload.getClass().getName(), payload instanceof MimeMessage);
215             verifyMessage((MimeMessage) payload);
216         }
217         else
218         {
219             assertTrue(payload instanceof String);
220             verifyMessage((String) payload);
221         }
222     }
223 
224     private void startServer() throws Exception
225     {
226         logger.debug("starting server on port " + port);
227         ServerSetup setup = new ServerSetup(port, null, protocol);
228         server = new GreenMail(setup);
229         server.start();
230         if (protocol.startsWith(Pop3Connector.POP3) || protocol.startsWith(ImapConnector.IMAP))
231         {
232             GreenMailUtilities.storeEmail(server.getManagers().getUserManager(),
233                     email, user, password,
234                     GreenMailUtilities.toMessage(message, email, charset));
235         }
236         logger.debug("server started for protocol " + protocol);
237     }
238 
239     private void stopServer()
240     {
241         server.stop();
242     }
243 
244     private static String getMessage(Locale locale) {
245         return LocaleMessageHandler.getString("test-data", locale, "AbstractEmailFunctionalTestCase.getMessage", new Object[] {});
246     }
247 
248     @Override
249     protected int getNumPortsToFind()
250     {
251         return 1;
252     }
253 
254     public void setAddAttachments(boolean addAttachments)
255     {
256         this.addAttachments = addAttachments;
257     }
258 
259     private void createOutboundAttachments(MuleMessage msg) throws Exception
260     {
261         msg.addOutboundAttachment("hello", "hello", "text/plain");
262         msg.addOutboundAttachment("goodbye", "<a/>", "text/xml");
263     }
264 }