1
2
3
4
5
6
7
8
9
10
11 package test.atlassian.mail.server;
12
13 import alt.javax.mail.Session;
14 import alt.javax.mail.internet.MimeMessage;
15 import alt.javax.mail.internet.MimeMessageImpl;
16 import com.atlassian.mail.Email;
17 import com.atlassian.mail.MailException;
18 import com.atlassian.mail.MailFactory;
19 import com.atlassian.mail.MailUtils;
20 import com.atlassian.mail.server.SMTPMailServer;
21 import com.atlassian.mail.server.impl.SMTPMailServerImpl;
22 import junit.framework.TestCase;
23 import org.apache.velocity.exception.VelocityException;
24 import test.mock.mail.MockTransport;
25 import test.mock.mail.MockSession;
26 import test.mock.mail.MockAuthenticator;
27 import test.mock.mail.server.MockMailServerManager;
28 import test.mock.mail.server.MockSMTPMailServer;
29
30 import javax.mail.*;
31 import javax.mail.internet.AddressException;
32 import javax.mail.internet.InternetAddress;
33 import javax.mail.internet.MimeBodyPart;
34 import javax.mail.internet.MimeMultipart;
35 import javax.naming.NamingException;
36 import java.io.IOException;
37 import java.util.Properties;
38
39
40
41
42
43
44
45
46
47
48
49 public class TestSMTPMailServer extends TestCase
50 {
51 public TestSMTPMailServer(String s)
52 {
53 super(s);
54 }
55
56 public void testConstructor()
57 {
58 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
59 assertEquals("owen@atlassian.com", msms.getDefaultFrom());
60 assertEquals("[OWEN]", msms.getPrefix());
61 assertTrue(false == msms.isSessionServer());
62 }
63
64 public void testGetsSets()
65 {
66 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "", "", false, "mail.atlassian.com", "ofellows", "owen01");
67 msms.setDefaultFrom("owen@atlassian.com");
68 assertEquals("owen@atlassian.com", msms.getDefaultFrom());
69 msms.setPrefix("[OWEN]");
70 assertEquals("[OWEN]", msms.getPrefix());
71 msms.setSessionServer(true);
72 assertTrue(true == msms.isSessionServer());
73 }
74
75 public void testGetSessionNoUser() throws NamingException, MailException
76 {
77 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", null, null);
78 assertNotNull(msms.getSession());
79 }
80
81 public void testGetSessionWithUser() throws NamingException, MailException
82 {
83 MockMailServerManager mmsm = new MockMailServerManager();
84 MailFactory.setServerManager(mmsm);
85
86
87
88 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01")
89 {
90 protected Authenticator getAuthenticator()
91 {
92 return new MockAuthenticator(getUsername(), getPassword());
93 }
94 };
95
96 Session session = msms.getSession();
97 assertNotNull(session);
98 assertEquals("ofellows", session.getPasswordAuthentication(new URLName("mail.atlassian.com")).getUserName());
99 assertEquals("owen01", session.getPasswordAuthentication(new URLName("mail.atlassian.com")).getPassword());
100 }
101
102 public void testSend() throws NamingException, MessagingException, AddressException, MailException
103 {
104 MockMailServerManager mmsm = new MockMailServerManager();
105 MailFactory.setServerManager(mmsm);
106 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
107
108 MockSession mockSession = (MockSession) msms.getSession();
109 MockTransport mt = (MockTransport) mockSession.getTransport();
110 MimeMessage mm = new MimeMessageImpl(mockSession);
111
112 mm.setFrom(new InternetAddress("owen@atlassian.com"));
113 mm.setRecipients(Message.RecipientType.TO, "owen@altassian.com");
114 mm.setRecipients(Message.RecipientType.CC, "mike@atlassian.con");
115 mm.setSubject("[OWEN] Test Subject");
116 mm.setContent("Test Body", "text/plain");
117
118 mt.setExpectedMessage(mm);
119
120 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body").setFrom("owen@atlassian.com");
121 msms.send(email);
122
123 mt.verify();
124 }
125
126 public void testSendWithAttachment() throws NamingException, MessagingException, AddressException, MailException, IOException
127 {
128 MockMailServerManager mmsm = new MockMailServerManager();
129 MailFactory.setServerManager(mmsm);
130 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", "ofellows", "owen01");
131
132 MockSession mockSession = (MockSession) msms.getSession();
133 MockTransport mt = (MockTransport) mockSession.getTransport();
134 MimeMessage mm = new MimeMessageImpl(mockSession);
135
136 mm.setFrom(new InternetAddress("owen@atlassian.com"));
137 mm.setRecipients(Message.RecipientType.TO, "owen@altassian.com");
138 mm.setRecipients(Message.RecipientType.CC, "mike@atlassian.com");
139 mm.setSubject("[OWEN] Test Subject");
140
141 Multipart multipart = new MimeMultipart();
142
143 MimeBodyPart exportZip = MailUtils.createAttachmentMimeBodyPart("C:/export.zip");
144 MimeBodyPart logTxt = MailUtils.createAttachmentMimeBodyPart("C:/log.txt");
145 multipart.addBodyPart(exportZip);
146 multipart.addBodyPart(logTxt);
147
148
149 MimeBodyPart messageBodyPart = new MimeBodyPart();
150 messageBodyPart.setText("Test Body", "text/plain");
151
152 multipart.addBodyPart(messageBodyPart);
153
154
155 mm.setContent(multipart);
156
157
158
159
160 mt.setExpectedMessage(mm);
161
162
163 multipart.removeBodyPart(messageBodyPart);
164
165 try
166 {
167 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body").setFrom("owen@atlassian.com").setMultipart(multipart);
168 msms.send(email);
169 }
170 catch (Exception e)
171 {
172 fail();
173 }
174
175 mt.verify();
176 }
177
178 public void testParseAddressPass(String pAddress) throws AddressException
179 {
180 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", false, "mail.atlassian.com", null, null);
181 InternetAddress[] address = msms.parseAddresses("owen.fellows@owen.com,owen@atlassian.com");
182 assertEquals(2, address.length);
183 assertEquals("owen.fellows@owen.com", address[0]);
184 assertEquals("owen@atlassian.com", address[1]);
185 }
186
187
188
189
190 public void testGetJNDIServer() throws NamingException, MailException
191 {
192 final javax.mail.Session sessionInstance = javax.mail.Session.getInstance(new Properties(), null);
193
194 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", true, "mail.atlassian.com", null, null)
195 {
196 protected Object getJndiSession() throws NamingException
197 {
198 return sessionInstance;
199 }
200 };
201
202 alt.javax.mail.Session session = msms.getSession();
203 assertEquals(sessionInstance, session.getWrappedSession());
204
205 }
206
207
208
209
210
211 public void testIncorrectJndiClass() throws NamingException, MailException
212 {
213 SMTPMailServer msms = new SMTPMailServerImpl(new Long(1), "name", "desc", "owen@atlassian.com", "[OWEN]", true, "mail.atlassian.com", null, null)
214 {
215 protected Object getJndiSession() throws NamingException
216 {
217 return new Object();
218 }
219 };
220
221 try
222 {
223 msms.getSession();
224 fail("Expected IllegalArgumentException when wrong class type in jdni location");
225 }
226 catch (IllegalArgumentException e)
227 {
228
229 }
230 }
231
232
233
234
235
236 public void testSendFail() throws Exception
237 {
238 MockMailServerManager mmsm = new MockMailServerManager();
239
240 MailFactory.setServerManager(mmsm);
241 MockSMTPMailServer mailServer = new MockSMTPMailServer(null, "default", "", "", "", false, "mail.atlassian.com", "", "");
242 MailFactory.getServerManager().create(mailServer);
243 try
244 {
245 Email email = new Email("mike@atlassian.com").setSubject("Test Subject").setBody("Test Body");
246 mailServer.send(email);
247 fail();
248 }
249 catch (MailException e)
250 {
251 assertEquals("Tried to send mail (Test Subject) from no one (no 'from' and 'default from' specified).", e.getMessage());
252 }
253 }
254
255 public void testSendMail1() throws Exception, MessagingException, VelocityException
256 {
257 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
258
259 MockSession mockSession = (MockSession) msms.getSession();
260 MockTransport mt = (MockTransport) mockSession.getTransport();
261 MimeMessage mm = new MimeMessageImpl(mockSession);
262
263 mm.setFrom(new InternetAddress("owen@atlassian.com"));
264 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
265 mm.setSubject("Test Subject 1");
266 mm.setContent("Test 1", "text/plain");
267 mt.setExpectedMessage(mm);
268
269 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 1").setBody("Test 1").setFrom("owen@atlassian.com");
270 msms.send(email);
271
272 mt.verify();
273 }
274
275 public void testSendMail2() throws Exception, MessagingException, VelocityException
276 {
277 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
278
279 MockSession mockSession = (MockSession) msms.getSession();
280 MockTransport mt = (MockTransport) mockSession.getTransport();
281 MimeMessage mm = new MimeMessageImpl(mockSession);
282
283 mm.setFrom(new InternetAddress("owen@atlassian.com"));
284 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
285 mm.setRecipients(Message.RecipientType.CC, "scott@atlassian.con");
286 mm.setSubject("Test Subject 2");
287 mm.setContent("/testing/Test 2", "text/plain");
288
289 mt.setExpectedMessage(mm);
290
291 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 2").setCc("scott@atlassian.com").setBody("/testing/Test 2").setFrom("owen@atlassian.com");
292 msms.send(email);
293
294 mt.verify();
295 }
296
297 public void testSendMail3() throws Exception, MessagingException, AddressException, VelocityException
298 {
299 MockSMTPMailServer msms = new MockSMTPMailServer(new Long(-1), "default", "", "owen@atlassian.com", "", false, "mail.atlassian.com", "", "");
300
301 MockSession mockSession = (MockSession) msms.getSession();
302 MockTransport mt = (MockTransport) mockSession.getTransport();
303 MimeMessage mm = new MimeMessageImpl(mockSession);
304
305 mm.setFrom(new InternetAddress("owen@atlassian.com"));
306 mm.setRecipients(Message.RecipientType.TO, "mike@altassian.com");
307 mm.setSubject("Test Subject 3");
308 mm.setContent("/testing/Test 3", "UTF-8");
309
310 mt.setExpectedMessage(mm);
311
312 Email email = new Email("mike@atlassian.com").setSubject("Test Subject 3").setBody("/testing/Test 3").setFrom("owen@atlassian.com");
313 msms.send(email);
314
315 mt.verify();
316 }
317 }