KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > email > test > EmailServiceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.email.test;
19
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.sape.carbon.core.component.Lookup;
26 import org.sape.carbon.core.exception.InvalidParameterException;
27 import org.sape.carbon.services.email.MailDataObject;
28 import org.sape.carbon.services.email.MailFailureException;
29 import org.sape.carbon.services.email.MailService;
30 import org.sape.carbon.services.email.util.MailAttachment;
31 import org.sape.carbon.services.email.util.MailContentTypeEnum;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * <p>Test Harness for the Email Service. The following functionality is
42  * tested in this test harness.
43  * <ul>
44  * <li>Sending text/plain and text/html mails to single and multiple
45  * recipients. </li>
46  * <li>Sending non-ascii characters using UTF-8 charset. </li>
47  * <li>Sending mails with attachments. </li>
48  * <li>Testing MailFailureException under appropriate conditions like
49  * invalid host name of SMTP server, sending mail to 0 recipients, sending
50  * mails with non-existent attachments, sending mail with unsupported
51  * charset and sending mails with null in fromPersonal. </li>
52  * </p>
53  *
54  * <p>Copyright 2002 Sapient</p>
55  * @stereotype test harness
56  * @since carbon 1.0
57  * @author $Author: dvoet $ $Date: 2003/05/05 21:21:29 $
58  * @version $Revision: 1.19 $
59  *
60  */

61 public class EmailServiceTest extends TestCase {
62
63     /**
64      * Provides a handle to Apache-commons logger
65      */

66     private Log log = LogFactory.getLog(this.getClass());
67
68     /** Number of emails to send in the test harness **/
69     // changed by Akash from 5 to 2 to fasten the test case.
70
public static int ITERATIONS = 2;
71
72     /** Constants used for sending the emails. */
73     public static final String JavaDoc FROM_EMAIL = "ejfw-test@sapient.com";
74     public static final String JavaDoc FROM_PERSONAL = "Email Service Test Harness";
75     public static final String JavaDoc TO_EMAIL = "ejfw-test@sapient.com";
76     public static final String JavaDoc TO_PERSONAL = "EJFW Test Account";
77     public static final String JavaDoc SUBJECT = "Email Test Harness Message";
78     public static final String JavaDoc MESSAGE_CONTENTS =
79            "This is a test email from carbon email service test harness";
80
81     public static final String JavaDoc HTML_CONTENTS =
82            "<html><b>This message should be in bold</b></html>";
83     public static final String JavaDoc CC_EMAIL = "ejfw-test@sapient.com";
84     public static final String JavaDoc CC_PERSONAL = "EJFW Test CC Account";
85     public static final String JavaDoc BCC_EMAIL = "ejfw-test@sapient.com";
86     public static final String JavaDoc BCC_PERSONAL = "EJFW Test BCC Account";
87     public static final String JavaDoc NON_ASCII_CHARACTERS = "ôôôôôôôôôôôôôô";
88
89     public static final String JavaDoc UTF_8 = "UTF-8";
90
91     public EmailServiceTest(String JavaDoc name) {
92         super(name);
93     }
94
95
96     /**
97      * Test Failure mode when the configuration is invalid, E.x wrong
98      * SMTP server name.
99      *
100      */

101     public void testInvalidConfigInfo() {
102
103         MailService mail = (MailService)
104             Lookup.getInstance().fetchComponent("/email/test/invalidEmail");
105
106         try {
107
108             MailDataObject mailDataObject = new MailDataObject();
109             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
110             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
111             mailDataObject.setSubject(SUBJECT);
112             mailDataObject.setBody(MESSAGE_CONTENTS,
113                 MailContentTypeEnum.PLAIN_TEXT);
114
115             mail.sendMail(mailDataObject);
116             fail("Able to send email to wrong smtp server. ");
117         }
118         catch(MailFailureException mfe) {
119             // Expected result - eat it.
120
}
121     }
122
123
124     /**
125      * <p> Used internally to send emails to single recipient.
126      *
127      * @param bodyType
128      * @param body
129      * @param mail
130      * @param iterations
131      *
132      * @return returns the time taken to send the emails.
133      */

134     protected long testSendingMailsToSingleRecipient(MailContentTypeEnum
135         bodyType, String JavaDoc body, MailService mail, int iterations) {
136
137         long startTime, endTime;
138
139         startTime = System.currentTimeMillis();
140
141         for (int i=0; i<iterations; i++) {
142             try {
143
144                 MailDataObject mailDataObject = new MailDataObject();
145                 mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
146                 mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
147                 mailDataObject.setSubject(SUBJECT);
148                 mailDataObject.setBody(body, bodyType);
149
150                 mail.sendMail(mailDataObject);
151             } catch(MailFailureException mfe) {
152                   fail("Failed to send email to single recipient: Due to "+ mfe);
153             }
154           }
155
156         endTime = System.currentTimeMillis();
157
158         return ((endTime - startTime)/iterations);
159     }
160
161
162     /**
163      * <p> Used internally to send emails to multiple recipient.
164      *
165      * @param bodyType
166      * @param body
167      * @param mail
168      * @param iterations
169      *
170      * @return returns the time taken to send the emails.
171      */

172     protected long testSendingMailsToMultipleRecipients(
173         MailContentTypeEnum bodyType, String JavaDoc body, MailService mail,
174         int iterations) {
175
176         long startTime, endTime;
177
178         HashMap JavaDoc toMap = new HashMap JavaDoc();
179         toMap.put(TO_EMAIL, TO_PERSONAL);
180
181         HashMap JavaDoc ccMap = new HashMap JavaDoc();
182         ccMap.put(CC_EMAIL, CC_PERSONAL);
183
184         HashMap JavaDoc bccMap = new HashMap JavaDoc();
185         bccMap.put(BCC_EMAIL, BCC_PERSONAL);
186
187         HashMap JavaDoc headers = new HashMap JavaDoc();
188         headers.put("X:Mailer", "Sapient Java Framework Email Service");
189
190         startTime = System.currentTimeMillis();
191
192         for (int i=0; i<iterations; i++) {
193             try {
194
195                 MailDataObject mailDataObject = new MailDataObject();
196                 mailDataObject.addTo(toMap);
197                 mailDataObject.addCC(ccMap);
198                 mailDataObject.addBCC(bccMap);
199                 mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
200                 mailDataObject.setSubject(SUBJECT);
201                 mailDataObject.setBody(body, bodyType);
202                 mailDataObject.addHeader(headers);
203
204                 mail.sendMail(mailDataObject);
205             } catch(MailFailureException mfe) {
206                   fail("Failed to send email to multiple recipients: Due to "+ mfe);
207             }
208           }
209
210         endTime = System.currentTimeMillis();
211
212         return ((endTime - startTime)/iterations);
213     }
214
215
216     /**
217      * Test Failure mode when unable to send the plain text email.
218      */

219     public void testSendingPlainTextMails() {
220
221         if (log.isInfoEnabled()) {
222             log.info("Starting sending plain text emails to single recipient");
223         }
224
225         MailService mail = (MailService) Lookup.getInstance().
226             fetchComponent("/email/test/testEmail");
227
228         // Test sending plain text emails to single recipient.
229
long fullTime = testSendingMailsToSingleRecipient(
230             MailContentTypeEnum.PLAIN_TEXT, MESSAGE_CONTENTS, mail,
231             EmailServiceTest.ITERATIONS);
232
233         if (log.isInfoEnabled()) {
234             log.info(
235                 "Time to send each plain text email to single recipients: "
236                 + fullTime);
237         }
238
239         // Test sending plain text emails to multiple recipients.
240
fullTime = testSendingMailsToMultipleRecipients(
241             MailContentTypeEnum.PLAIN_TEXT, MESSAGE_CONTENTS, mail,
242             EmailServiceTest.ITERATIONS);
243
244         if (log.isInfoEnabled()) {
245             log.info(
246                 "Time to send each plain text email to multiple recipients: "
247                 + fullTime);
248         }
249     }
250
251     /**
252      * Test Failure mode when unable to send the html email.
253      */

254     public void testSendingHtmlMails() {
255
256         if (log.isInfoEnabled()) {
257             log.info("Starting sending html emails to single recipient.");
258         }
259
260         MailService mail = (MailService) Lookup.getInstance().
261             fetchComponent("/email/test/testEmail");
262
263         // Test sending plain text emails to single recipient.
264
long fullTime = testSendingMailsToSingleRecipient(
265             MailContentTypeEnum.HTML, HTML_CONTENTS, mail,
266             EmailServiceTest.ITERATIONS);
267
268         if (log.isInfoEnabled()) {
269             log.info(
270                 "Time to send each html email to single recipients: "
271                 + fullTime);
272         }
273
274         // Test sending plain text emails to multiple recipients.
275
fullTime = testSendingMailsToMultipleRecipients(
276             MailContentTypeEnum.HTML, HTML_CONTENTS, mail,
277             EmailServiceTest.ITERATIONS);
278
279         if (log.isInfoEnabled()) {
280             log.info("Time to send each html email to multiple recipients: "
281                 + fullTime);
282         }
283     }
284
285
286     /**
287      * Test Failure mode when unable to send the email with attachment.
288      */

289     public void testSendingMailWithAttachments()
290     {
291
292         if (log.isInfoEnabled()) {
293             log.info("Starting sending email with attachment");
294         }
295
296         String JavaDoc relativePath;
297         if(System.getProperty("user.dir").indexOf("modules") != -1) {
298             // This means the test is run from ${build.home}/modules/email
299
// directory
300
relativePath = "";
301         } else {
302             relativePath = "modules/email/";
303         }
304
305
306         HashMap JavaDoc toMap = new HashMap JavaDoc();
307         toMap.put(TO_EMAIL, TO_PERSONAL);
308
309         MailAttachment attachments[] = new MailAttachment[2];
310         attachments[0] = new MailAttachment("Attachment1.txt", "Attachment1.txt",
311             relativePath +
312             "code/org/sape/carbon/services/email/test/Attachment1.txt");
313
314         URL JavaDoc url = null;
315
316
317         try {
318             url = new URL JavaDoc("file", "localhost", relativePath +
319                 "code/org/sape/carbon/services/email/test/Attachment2.txt");
320         } catch(MalformedURLException JavaDoc mue) {
321             fail("Bad url of the attachment. "+mue);
322         }
323
324         attachments[1] = new MailAttachment("Attachment2.txt",
325                                                "Attachment2.txt", url);
326
327         MailService mail = (MailService) Lookup.getInstance().
328                                fetchComponent("/email/test/testEmail");
329
330         try {
331
332             MailDataObject mailDataObject = new MailDataObject();
333             mailDataObject.addTo(toMap);
334             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
335             mailDataObject.setSubject(SUBJECT);
336             mailDataObject.setBody(HTML_CONTENTS,
337                 MailContentTypeEnum.HTML);
338             mailDataObject.setAttachments(attachments);
339             mail.sendMail(mailDataObject);
340
341         } catch(MailFailureException mfe) {
342             fail("Failed to send email with attachment: Due to "+ mfe);
343         }
344     }
345
346     /**
347      * Test Failure mode when unable to send mail in UTF-8. This test sends both
348      * HTML and Plain text email in UTF-8.
349     */

350     public void testSendingMailInUTF8() {
351
352         if (log.isInfoEnabled()) {
353             log.info("Starting sending plain text mail in UTF-8");
354         }
355
356         HashMap JavaDoc toMap = new HashMap JavaDoc();
357         toMap.put(TO_EMAIL, TO_PERSONAL);
358
359         MailService mail = (MailService) Lookup.getInstance().
360             fetchComponent("/email/test/testEmail");
361
362         try {
363
364             MailDataObject mailDataObject = new MailDataObject();
365             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
366             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
367             mailDataObject.setSubject(SUBJECT);
368             mailDataObject.setBody(NON_ASCII_CHARACTERS,
369                 MailContentTypeEnum.PLAIN_TEXT);
370             mailDataObject.setCharset(UTF_8);
371
372             mail.sendMail(mailDataObject);
373
374         } catch(MailFailureException mfe) {
375             fail("Failed to send plain text mail in UTF-8: Due to "+ mfe);
376         }
377
378         if (log.isInfoEnabled()) {
379             log.info("Starting sending html mail in UTF-8");
380         }
381
382         try {
383
384             MailDataObject mailDataObject = new MailDataObject();
385             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
386             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
387             mailDataObject.setSubject(SUBJECT);
388             mailDataObject.setBody("<html><b>" + NON_ASCII_CHARACTERS +
389                 "</b></html>", MailContentTypeEnum.HTML);
390             mailDataObject.setCharset(UTF_8);
391
392             mail.sendMail(mailDataObject);
393
394         } catch(MailFailureException mfe) {
395             fail("Failed to send html mail in UTF-8: Due to:"+ mfe);
396         }
397     }
398
399
400     /**
401      * Test Failure mode when able to send the mail to 0 recipients!!.
402      */

403     public void testSendingMailToZeroRecipients() {
404
405         // Try sending mails with no recipients.
406
if (log.isInfoEnabled()) {
407             log.info("Starting sending mail to nobody");
408         }
409
410         MailService mail = (MailService) Lookup.getInstance().
411             fetchComponent("/email/test/testEmail");
412
413         try {
414
415             MailDataObject mailDataObject = new MailDataObject();
416             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
417             mailDataObject.setSubject(SUBJECT);
418             mailDataObject.setBody(MESSAGE_CONTENTS,
419                 MailContentTypeEnum.PLAIN_TEXT);
420
421             mail.sendMail(mailDataObject);
422
423             fail("Succeeded in sending mail to 0 recipients!!");
424
425         } catch(MailFailureException mfe) {
426             // Expected result - eat it.
427
} catch(InvalidParameterException ipe) {
428             // Expected result - eat it.
429
}
430
431
432         try {
433
434             MailDataObject mailDataObject = new MailDataObject();
435             mailDataObject.addTo(null, TO_PERSONAL);
436             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
437             mailDataObject.setSubject(SUBJECT);
438             mailDataObject.setBody(MESSAGE_CONTENTS,
439                 MailContentTypeEnum.PLAIN_TEXT);
440
441             mail.sendMail(mailDataObject);
442
443             fail("Succeeded in sending mail with null in TO_EMAIL!!");
444         } catch(MailFailureException mfe) {
445             // Expected result - eat it.
446
} catch(InvalidParameterException ipe) {
447             // Expected result - eat it.
448
}
449     }
450
451
452     /**
453      * Test Failure mode when able to send the mail with null in from fields.
454     */

455     public void testSendingMailFromNobody()
456     {
457         // Try sending mail with null in the FROM_PERSONAL.
458
if (log.isInfoEnabled()) {
459             log.info("Starting sending mail with null in "
460                 + "FROM_PERSONAL and FROM_EMAIL");
461         }
462
463         MailService mail = (MailService) Lookup.getInstance().
464                       fetchComponent("/email/test/testEmail");
465
466         try {
467
468             MailDataObject mailDataObject = new MailDataObject();
469             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
470             mailDataObject.setFrom(null, FROM_PERSONAL);
471             mailDataObject.setSubject(SUBJECT);
472             mailDataObject.setBody(MESSAGE_CONTENTS,
473                 MailContentTypeEnum.PLAIN_TEXT);
474
475             mail.sendMail(mailDataObject);
476
477             fail("Succeeded in sending mail with null in FROM_EMAIL!! ");
478         } catch(MailFailureException mfe) {
479             // Expected result - eat it.
480
} catch(InvalidParameterException ipe) {
481             // Expected result - eat it.
482
}
483     }
484
485
486     /**
487      * Test Failure mode when unable to send the email with non-existent attachment.
488     */

489     public void testSendingMailWithNonExistentAttachment() {
490
491         // Try sending mail with non-existent attachment.
492
if (log.isInfoEnabled()) {
493             log.info("Starting sending mail with non-existent attachment ");
494         }
495
496         MailService mail = (MailService) Lookup.getInstance().
497             fetchComponent("/email/test/testEmail");
498
499         try {
500             MailAttachment[] attachments = new MailAttachment[1];
501             String JavaDoc name = "FileNotExist";
502             attachments[0] = new MailAttachment(name, name, name);
503
504             MailDataObject mailDataObject = new MailDataObject();
505             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
506             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
507             mailDataObject.setSubject(SUBJECT);
508             mailDataObject.setBody(HTML_CONTENTS,
509                 MailContentTypeEnum.HTML);
510             mailDataObject.setAttachments(attachments);
511
512             mail.sendMail(mailDataObject);
513
514             fail("Succeeded in sending mail with non-existant attachment.");
515         } catch(MailFailureException mfe) {
516             // Expected result - eat it.
517
}
518     }
519
520
521     /**
522      * Test failure mode when able to send mail using unsupported charset.
523      */

524     public void testSendingMailUsingBadCharset()
525     {
526
527         // Try sending mail with null in the FROM_PERSONAL.
528
if (log.isInfoEnabled()) {
529             log.info("Starting sending mail with bad charset");
530         }
531
532         MailService mail = (MailService) Lookup.getInstance().
533             fetchComponent("/email/test/testEmail");
534
535         try {
536
537             String JavaDoc badCharset = "%%%";
538             MailDataObject mailDataObject = new MailDataObject();
539             mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
540             mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
541             mailDataObject.setSubject(SUBJECT);
542             mailDataObject.setBody(NON_ASCII_CHARACTERS,
543                 MailContentTypeEnum.PLAIN_TEXT);
544             mailDataObject.setCharset(badCharset);
545
546             mail.sendMail(mailDataObject);
547
548             fail("Succeeded in sending mail with unsupported charset!!");
549
550         } catch(MailFailureException mfe) {
551             // Expected result - eat it.
552
}
553     }
554
555
556     /**
557      * Adds all tests into the master suite.
558      */

559     public static Test suite() {
560         TestSuite masterSuite = new TestSuite();
561         masterSuite.addTest(new EmailServiceTest("testSendingPlainTextMails"));
562         masterSuite.addTest(new EmailServiceTest("testSendingHtmlMails"));
563         masterSuite.addTest(new EmailServiceTest(
564             "testSendingMailWithAttachments"));
565         masterSuite.addTest(new EmailServiceTest("testSendingMailInUTF8"));
566
567         // testInvalidConfigInfo is enabled now. Changed the invalid SMTP host
568
// from some.smtpserver to footest.sapient. This has reduced time by 50%
569
// on my (akash) desktop
570
masterSuite.addTest(new EmailServiceTest("testInvalidConfigInfo"));
571
572         masterSuite.addTest(new EmailServiceTest(
573             "testSendingMailToZeroRecipients"));
574         masterSuite.addTest(new EmailServiceTest("testSendingMailFromNobody"));
575         masterSuite.addTest(new EmailServiceTest(
576             "testSendingMailWithNonExistentAttachment"));
577         masterSuite.addTest(new EmailServiceTest(
578             "testSendingMailUsingBadCharset"));
579         return masterSuite;
580     }
581 }
582
583
Popular Tags