1   /*
2    * $Id: AbstractGreenMailSupport.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;
12  
13  import com.icegreen.greenmail.util.Servers;
14  import com.icegreen.greenmail.util.ServerSetup;
15  import com.icegreen.greenmail.user.UserManager;
16  import com.icegreen.greenmail.user.GreenMailUser;
17  
18  import java.util.Properties;
19  
20  import javax.mail.internet.MimeMessage;
21  import javax.mail.internet.InternetAddress;
22  import javax.mail.Session;
23  import javax.mail.Message;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  public abstract class AbstractGreenMailSupport 
29  {
30  
31      public static final String LOCALHOST = "127.0.0.1";
32      public static final String MESSAGE = "Test Email Message";
33      public static final String AT_EXAMPLE_COM = "@example.com";
34      public static final String BOB = "bob";
35      public static final String BOB_EMAIL = BOB + AT_EXAMPLE_COM;
36      public static final String ALICE = "alice";
37      public static final String ALICE_EMAIL = ALICE + AT_EXAMPLE_COM;
38      public static final String PASSWORD = "secret";
39      public static final long STARTUP_PERIOD_MS = 100;
40  
41      protected final Log logger = LogFactory.getLog(this.getClass());
42      private Servers servers;
43  
44      protected void createUserAndStoreEmail(String email, String user, String password, Object message) throws Exception
45      {
46          // note that with greenmail 1.1 the Servers object is unreliable
47          // and the approach taken in their examples will not work.
48          // the following does work, but may break in a later version
49          // (there is some confusion in the greenmail code about
50          // whether users are identified by email or name alone)
51          // in which case try retrieving by EMAIL rather than USER
52          logger.debug("Creating mail user " + user + "/" + email + "/" + password);
53          UserManager userManager = servers.getManagers().getUserManager();
54          userManager.createUser(email, user, password);
55          GreenMailUser target = userManager.getUser(user);
56          if (null == target)
57          {
58              throw new IllegalStateException("Failure in greenmail - see comments in test code.");
59          }
60          target.deliver((MimeMessage) message);
61          Thread.sleep(STARTUP_PERIOD_MS);
62      }
63  
64      protected void createBobAndStoreEmail(Object message) throws Exception
65      {
66          createUserAndStoreEmail(BOB_EMAIL, BOB, PASSWORD, message);
67      }
68  
69      protected void createAliceAndStoreEmail(Object message) throws Exception
70      {
71          createUserAndStoreEmail(ALICE_EMAIL, ALICE, PASSWORD, message);
72      }
73  
74      protected void startServers() throws Exception
75      {
76          logger.info("Starting mail servers");
77          servers = new Servers(getSetups());
78          servers.start();
79          Thread.sleep(STARTUP_PERIOD_MS);
80      }
81  
82      protected abstract int nextPort();
83  
84      private ServerSetup[] getSetups()
85      {
86          return new ServerSetup[]{
87                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_SMTP),
88                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_SMTPS),
89                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_POP3),
90                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_POP3S),
91                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_IMAP),
92                  newServerSetup(nextPort(), ServerSetup.PROTOCOL_IMAPS)
93          };
94      }
95  
96      private ServerSetup newServerSetup(int port, String protocol)
97      {
98          logger.debug("Server for " + protocol + " will be on port " + port);
99          return new ServerSetup(port, null, protocol);
100     }
101 
102     protected void stopServers() throws Exception
103     {
104         logger.info("Stopping mail servers");
105         if (null != servers)
106         {
107             servers.stop();
108         }
109     }
110 
111     protected Servers getServers()
112     {
113         return servers;
114     }
115 
116     public MimeMessage getValidMessage(String to) throws Exception
117     {
118         MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
119         message.setContent(MESSAGE, "text/plain");
120         message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
121         return message;
122     }
123 
124 }
125