1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.email.connectors;
12
13 import org.mule.transport.AbstractConnectorTestCase;
14 import org.mule.transport.email.GreenMailUtilities;
15
16 import com.icegreen.greenmail.util.GreenMail;
17 import com.icegreen.greenmail.util.ServerSetup;
18
19 import javax.mail.Message;
20 import javax.mail.internet.MimeMessage;
21
22 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
23
24
25
26
27 public abstract class AbstractMailConnectorFunctionalTestCase extends AbstractConnectorTestCase
28 {
29
30 public static final String LOCALHOST = "127.0.0.1";
31 public static final String USER = "bob";
32 public static final String PROVIDER = "example.com";
33 public static final String EMAIL = USER + "@" + PROVIDER;
34 public static final String PASSWORD = "secret";
35 public static final String MESSAGE = "Test Email Message";
36
37 public static final int START_ATTEMPTS = 3;
38 public static final int TEST_ATTEMPTS = 5;
39 public static final long STARTUP_PERIOD_MS = 1000;
40
41
42 public static final boolean SEND_INITIAL_EMAIL = true;
43 public static final boolean NO_INITIAL_EMAIL = false;
44
45 private static final AtomicInteger nameCount = new AtomicInteger(0);
46
47 private MimeMessage message;
48 private GreenMail servers;
49 private boolean initialEmail = false;
50 private String protocol;
51 private int port;
52
53 protected AbstractMailConnectorFunctionalTestCase(boolean initialEmail, String protocol, int port)
54 {
55 super();
56 this.initialEmail = initialEmail;
57 this.protocol = protocol;
58 this.port = port;
59 }
60
61
62 protected void doSetUp() throws Exception
63 {
64 super.doSetUp();
65 startServers();
66 }
67
68
69 protected void doTearDown() throws Exception
70 {
71 stopServers();
72 super.doTearDown();
73 }
74
75 private void storeEmail() throws Exception
76 {
77 GreenMailUtilities.storeEmail(servers.getManagers().getUserManager(),
78 EMAIL, USER, PASSWORD, (MimeMessage) getValidMessage());
79 assertEquals(1, servers.getReceivedMessages().length);
80 }
81
82 private void startServers() throws Exception
83 {
84 servers = new GreenMail(getSetups());
85 GreenMailUtilities.robustStartup(servers, LOCALHOST, port, START_ATTEMPTS, TEST_ATTEMPTS, STARTUP_PERIOD_MS);
86 if (initialEmail)
87 {
88 storeEmail();
89 }
90 }
91
92 private ServerSetup[] getSetups()
93 {
94 return new ServerSetup[]{new ServerSetup(port, null, protocol)};
95 }
96
97 private void stopServers() throws Exception
98 {
99 if (null != servers)
100 {
101 servers.stop();
102 }
103 }
104
105 protected GreenMail getServers()
106 {
107 return servers;
108 }
109
110
111 public Object getValidMessage() throws Exception
112 {
113 if (null == message)
114 {
115 message = GreenMailUtilities.toMessage(MESSAGE, EMAIL);
116 }
117 return message;
118 }
119
120 public String getTestEndpointURI()
121 {
122 return protocol + "://" + USER + ":" + PASSWORD + "@" + LOCALHOST + ":" + port +
123 "?connector=" + connectorName;
124 }
125
126 protected void assertMessageOk(Object message) throws Exception
127 {
128 assertTrue("Did not receive a MimeMessage", message instanceof MimeMessage);
129 MimeMessage received = (MimeMessage) message;
130
131
132 assertTrue("Did not receive a message with String contents",
133 received.getContent() instanceof String);
134 String receivedText = ((String) received.getContent()).trim();
135 assertEquals(MESSAGE, receivedText);
136 assertNotNull(received.getRecipients(Message.RecipientType.TO));
137 assertEquals(1, received.getRecipients(Message.RecipientType.TO).length);
138 assertEquals(received.getRecipients(Message.RecipientType.TO)[0].toString(), EMAIL);
139 }
140
141 protected String uniqueName(String root)
142 {
143 return root + nameCount.incrementAndGet();
144 }
145
146 }