1
2
3
4
5
6
7 package org.mule.transport.email;
8
9 import com.icegreen.greenmail.user.GreenMailUser;
10 import com.icegreen.greenmail.user.UserException;
11 import com.icegreen.greenmail.user.UserManager;
12 import com.icegreen.greenmail.util.GreenMail;
13 import com.icegreen.greenmail.util.ServerSetup;
14
15 import java.util.List;
16 import java.util.Properties;
17
18 import javax.mail.Message;
19 import javax.mail.Session;
20 import javax.mail.internet.InternetAddress;
21 import javax.mail.internet.MimeMessage;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 public abstract class AbstractGreenMailSupport
27 {
28
29 public static final String LOCALHOST = "127.0.0.1";
30 public static final String MESSAGE = "Test Email Message";
31 public static final String AT_EXAMPLE_COM = "@example.com";
32 public static final String BOB = "bob";
33 public static final String BOB_EMAIL = BOB + AT_EXAMPLE_COM;
34 public static final String ALICE = "alice";
35 public static final String ALICE_EMAIL = ALICE + AT_EXAMPLE_COM;
36 public static final String PASSWORD = "secret";
37 public static final long STARTUP_PERIOD_MS = 100;
38
39 protected final Log logger = LogFactory.getLog(this.getClass());
40 private GreenMail servers;
41
42 protected void createUserAndStoreEmail(String email, String user, String password, Object message) throws Exception
43 {
44
45
46
47
48
49
50 logger.debug("Creating mail user " + user + "/" + email + "/" + password);
51 GreenMailUser target = createUser(email, user, password);
52 target.deliver((MimeMessage) message);
53 Thread.sleep(STARTUP_PERIOD_MS);
54 }
55
56 public GreenMailUser createUser(String email, String user, String password) throws UserException
57 {
58 UserManager userManager = servers.getManagers().getUserManager();
59 userManager.createUser(email, user, password);
60 GreenMailUser target = userManager.getUser(user);
61 if (null == target)
62 {
63 throw new IllegalStateException("Failure in greenmail - see comments in test code.");
64 }
65 return target;
66 }
67
68 public void createBobAndStoreEmail(Object message) throws Exception
69 {
70 createUserAndStoreEmail(BOB_EMAIL, BOB, PASSWORD, message);
71 }
72
73 public void createAliceAndStoreEmail(Object message) throws Exception
74 {
75 createUserAndStoreEmail(ALICE_EMAIL, ALICE, PASSWORD, message);
76 }
77
78 public void startServers(List<Integer> list) throws Exception
79 {
80 logger.info("Starting mail servers");
81 servers = new GreenMail(getSetups(list));
82 servers.start();
83 Thread.sleep(STARTUP_PERIOD_MS);
84 }
85
86 protected abstract int nextPort();
87
88 private ServerSetup[] getSetups(List<Integer> list)
89 {
90 if (list.size() != 6)
91 {
92 throw new IllegalArgumentException("must pass in an array of 6 ports for server setup");
93 }
94
95 return new ServerSetup[]{
96 newServerSetup(list.get(0), ServerSetup.PROTOCOL_POP3),
97 newServerSetup(list.get(1), ServerSetup.PROTOCOL_SMTP),
98 newServerSetup(list.get(2), ServerSetup.PROTOCOL_SMTPS),
99 newServerSetup(list.get(3), ServerSetup.PROTOCOL_POP3S),
100 newServerSetup(list.get(4), ServerSetup.PROTOCOL_IMAP),
101 newServerSetup(list.get(5), ServerSetup.PROTOCOL_IMAPS)
102 };
103 }
104
105 private ServerSetup newServerSetup(int port, String protocol)
106 {
107 logger.debug("Server for " + protocol + " will be on port " + port);
108 return new ServerSetup(port, null, protocol);
109 }
110
111 protected void stopServers() throws Exception
112 {
113 logger.info("Stopping mail servers");
114 if (null != servers)
115 {
116 servers.stop();
117 }
118 }
119
120 public GreenMail getServers()
121 {
122 return servers;
123 }
124
125 public MimeMessage getValidMessage(String to) throws Exception
126 {
127 MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
128 message.setContent(MESSAGE, "text/plain");
129 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
130 return message;
131 }
132
133 }
134