1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail;
18
19 import java.io.BufferedOutputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.Enumeration;
26 import java.util.List;
27
28 import javax.activation.DataHandler;
29 import javax.mail.Header;
30 import javax.mail.MessagingException;
31 import javax.mail.Multipart;
32 import javax.mail.internet.InternetAddress;
33 import javax.mail.internet.MimeMessage;
34
35 import junit.framework.TestCase;
36
37 import org.apache.commons.mail.settings.EmailConfiguration;
38 import org.subethamail.wiser.Wiser;
39 import org.subethamail.wiser.WiserMessage;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class BaseEmailTestCase extends TestCase
53 {
54
55 public static final int BODY_END_PAD = 3;
56
57 public static final int BODY_START_PAD = 2;
58
59
60 private static final String LINE_SEPARATOR = "\r\n";
61
62
63 private static int mailServerPort = EmailConfiguration.MAIL_SERVER_PORT;
64
65
66 protected Wiser fakeMailServer;
67
68
69 protected String strTestMailServer = EmailConfiguration.MAIL_SERVER;
70
71 protected String strTestMailFrom = EmailConfiguration.TEST_FROM;
72
73 protected String strTestMailTo = EmailConfiguration.TEST_TO;
74
75 protected String strTestUser = EmailConfiguration.TEST_USER;
76
77 protected String strTestPasswd = EmailConfiguration.TEST_PASSWD;
78
79 protected String strTestURL = EmailConfiguration.TEST_URL;
80
81
82 protected String[] testCharsValid =
83 {
84 " ",
85 "a",
86 "A",
87 "\uc5ec",
88 "0123456789",
89 "012345678901234567890",
90 "\n"
91 };
92
93
94 protected String[] testCharsNotValid = {"", null};
95
96
97 private File emailOutputDir;
98
99
100
101
102 public BaseEmailTestCase(String name)
103 {
104 super(name);
105 emailOutputDir = new File("target/test-emails");
106 if (!emailOutputDir.exists())
107 {
108 emailOutputDir.mkdirs();
109 }
110 }
111
112
113 protected void tearDown()
114 {
115
116 if (this.fakeMailServer != null && !isMailServerStopped(fakeMailServer))
117 {
118 this.fakeMailServer.stop();
119 assertTrue("Mail server didn't stop", isMailServerStopped(fakeMailServer));
120 }
121
122 this.fakeMailServer = null;
123 }
124
125
126
127
128
129 protected int getMailServerPort()
130 {
131 return mailServerPort;
132 }
133
134
135
136
137
138
139 protected void saveEmailToFile(WiserMessage email) throws IOException
140 {
141 File emailFile =
142 new File(emailOutputDir, "email" + new Date().getTime() + ".txt");
143 FileWriter fw = new FileWriter(emailFile);
144 fw.write(email.toString());
145 fw.close();
146 }
147
148
149
150
151
152 public String getMessageAsString(int intMsgNo)
153 {
154 List receivedMessages = fakeMailServer.getMessages();
155 assertTrue("mail server didn't get enough messages", receivedMessages.size() >= intMsgNo);
156
157 WiserMessage emailMessage = (WiserMessage) receivedMessages.get(intMsgNo);
158
159 if (emailMessage != null)
160 {
161 try
162 {
163 return serializeEmailMessage(emailMessage);
164 }
165 catch (Exception e)
166 {
167
168 }
169 }
170 fail("Message not found");
171 return "";
172 }
173
174
175
176
177
178
179 public void getMailServer()
180 {
181 if (this.fakeMailServer == null || isMailServerStopped(fakeMailServer))
182 {
183 mailServerPort++;
184
185 this.fakeMailServer = new Wiser();
186 this.fakeMailServer.setPort(getMailServerPort());
187 this.fakeMailServer.start();
188
189 assertFalse("fake mail server didn't start", isMailServerStopped(fakeMailServer));
190
191 Date dtStartWait = new Date();
192 while (isMailServerStopped(fakeMailServer))
193 {
194
195 if (this.fakeMailServer != null
196 && !isMailServerStopped(fakeMailServer))
197 {
198 break;
199 }
200
201
202 if ((dtStartWait.getTime() + EmailConfiguration.TIME_OUT)
203 <= new Date().getTime())
204 {
205 fail("Mail server failed to start");
206 }
207 }
208 }
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223 protected WiserMessage validateSend(
224 Wiser mailServer,
225 String strSubject,
226 InternetAddress fromAdd,
227 List toAdd,
228 List ccAdd,
229 List bccAdd,
230 boolean boolSaveToFile)
231 throws IOException
232 {
233 assertTrue("mail server doesn't contain expected message",
234 mailServer.getMessages().size() == 1);
235 WiserMessage emailMessage = (WiserMessage) mailServer.getMessages().get(0);
236
237 if (boolSaveToFile)
238 {
239 this.saveEmailToFile(emailMessage);
240 }
241
242 try
243 {
244
245 MimeMessage mimeMessage = emailMessage.getMimeMessage();
246
247
248 assertEquals("got wrong subject from mail",
249 strSubject, mimeMessage.getHeader("Subject", null));
250
251
252 assertEquals("got wrong From: address from mail",
253 fromAdd.toString(), mimeMessage.getHeader("From", null));
254
255
256 assertTrue("got wrong To: address from mail",
257 toAdd.toString().indexOf(mimeMessage.getHeader("To", null)) != -1);
258
259
260 if (ccAdd.size() > 0)
261 {
262 assertTrue("got wrong Cc: address from mail",
263 ccAdd.toString().indexOf(mimeMessage.getHeader("Cc", null))
264 != -1);
265 }
266
267
268 if (bccAdd.size() > 0)
269 {
270 assertTrue("got wrong Bcc: address from mail",
271 bccAdd.toString().indexOf(mimeMessage.getHeader("Bcc", null))
272 != -1);
273 }
274 }
275 catch (MessagingException me)
276 {
277 IllegalStateException ise =
278 new IllegalStateException("caught MessagingException in validateSend()");
279 ise.initCause(me);
280 throw ise;
281 }
282
283 return emailMessage;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298 protected void validateSend(
299 Wiser mailServer,
300 String strSubject,
301 Multipart content,
302 InternetAddress fromAdd,
303 List toAdd,
304 List ccAdd,
305 List bccAdd,
306 boolean boolSaveToFile)
307 throws IOException
308 {
309
310 WiserMessage emailMessage = this.validateSend(
311 mailServer,
312 strSubject,
313 fromAdd,
314 toAdd,
315 ccAdd,
316 bccAdd,
317 boolSaveToFile);
318
319
320
321
322 String strSentContent =
323 content.getContentType();
324
325
326 String emailMessageBody = getMessageBody(emailMessage);
327 String strMessageBody =
328 emailMessageBody.substring(BaseEmailTestCase.BODY_START_PAD,
329 emailMessageBody.length()
330 - BaseEmailTestCase.BODY_END_PAD);
331 assertTrue("didn't find expected content type in message body",
332 strMessageBody.indexOf(strSentContent) != -1);
333 }
334
335
336
337
338
339
340
341
342
343
344
345
346
347 protected void validateSend(
348 Wiser mailServer,
349 String strSubject,
350 String strMessage,
351 InternetAddress fromAdd,
352 List toAdd,
353 List ccAdd,
354 List bccAdd,
355 boolean boolSaveToFile)
356 throws IOException
357 {
358
359 WiserMessage emailMessage = this.validateSend(
360 mailServer,
361 strSubject,
362 fromAdd,
363 toAdd,
364 ccAdd,
365 bccAdd,
366 true);
367
368
369 assertTrue("didn't find expected message content in message body",
370 getMessageBody(emailMessage).indexOf(strMessage) != -1);
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389 private String serializeEmailMessage(WiserMessage wiserMessage)
390 throws MessagingException, IOException
391 {
392 if (wiserMessage == null)
393 {
394 return "";
395 }
396
397 StringBuffer serializedEmail = new StringBuffer();
398 MimeMessage message = wiserMessage.getMimeMessage();
399
400
401 for (Enumeration headers = message.getAllHeaders(); headers
402 .hasMoreElements();)
403 {
404 Header header = (Header) headers.nextElement();
405 serializedEmail.append(header.getName());
406 serializedEmail.append(": ");
407 serializedEmail.append(header.getValue());
408 serializedEmail.append(LINE_SEPARATOR);
409 }
410
411
412 byte[] messageBody = getMessageBodyBytes(message);
413
414 serializedEmail.append(LINE_SEPARATOR);
415 serializedEmail.append(messageBody);
416 serializedEmail.append(LINE_SEPARATOR);
417
418 return serializedEmail.toString();
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432 private String getMessageBody(WiserMessage wiserMessage)
433 throws IOException
434 {
435 if (wiserMessage == null)
436 {
437 return "";
438 }
439
440 byte[] messageBody = null;
441
442 try
443 {
444 MimeMessage message = wiserMessage.getMimeMessage();
445 messageBody = getMessageBodyBytes(message);
446 }
447 catch (MessagingException me)
448 {
449
450
451 IllegalStateException ise =
452 new IllegalStateException("couldn't process MimeMessage from WiserMessage in getMessageBody()");
453 ise.initCause(me);
454 throw ise;
455 }
456
457 return (messageBody != null) ? (new String(messageBody).intern()) : "";
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474 private byte[] getMessageBodyBytes(MimeMessage mimeMessage)
475 throws IOException, MessagingException
476 {
477 DataHandler dataHandler = mimeMessage.getDataHandler();
478 ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
479 BufferedOutputStream buffOs = new BufferedOutputStream(
480 byteArrayOutStream);
481 dataHandler.writeTo(buffOs);
482 buffOs.flush();
483
484 return byteArrayOutStream.toByteArray();
485 }
486
487
488
489
490
491
492
493
494
495
496 protected boolean isMailServerStopped(Wiser fakeMailServer) {
497 return !fakeMailServer.getServer().isRunning();
498 }
499 }