1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.mail;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.util.ArrayList;
20  import java.util.Calendar;
21  import java.util.Date;
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  import java.util.Properties;
25  
26  import javax.mail.Authenticator;
27  import javax.mail.Session;
28  import javax.mail.internet.InternetAddress;
29  import javax.mail.internet.MimeMultipart;
30  import javax.mail.internet.ParseException;
31  
32  import org.apache.commons.mail.mocks.MockEmailConcrete;
33  
34  /**
35   * JUnit test case for Email Class
36   *
37   * @since 1.0
38   * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
39   * @version $Id: EmailTest.java 279300 2005-09-07 11:43:52Z henning $
40   */
41  
42  public class EmailTest extends BaseEmailTestCase
43  {
44      /** */
45      private MockEmailConcrete email = null;
46  
47      /** */
48      public static final String[] ARR_VALID_EMAILS =
49          {
50              "me@home.com",
51              "joe.doe@apache.org",
52              "someone_here@work-address.com.au" };
53      /**
54       * @param name name
55       */
56      public EmailTest(String name)
57      {
58          super(name);
59      }
60  
61      /** */
62      protected void setUp()
63      {
64          super.setUp();
65          // reusable objects to be used across multiple tests
66          this.email = new MockEmailConcrete();
67      }
68  
69      /** */
70      public void testGetSetDebug()
71      {
72          // JUnitDoclet begin method setBoolTest isBoolTest
73          boolean[] tests = { true, false };
74  
75          for (int i = 0; i < tests.length; i++)
76          {
77              this.email.setDebug(tests[i]);
78              assertEquals(tests[i], this.email.isDebug());
79          }
80      }
81  
82      /**
83       *
84       * @throws Exception Exception
85       */
86      public void testGetSetSession() throws Exception
87      {
88  
89          Properties properties = new Properties(System.getProperties());
90          properties.setProperty(Email.MAIL_TRANSPORT_PROTOCOL, Email.SMTP);
91  
92          properties.setProperty(
93              Email.MAIL_PORT,
94              String.valueOf(this.getMailServerPort()));
95          properties.setProperty(Email.MAIL_HOST, this.strTestMailServer);
96          properties.setProperty(Email.MAIL_DEBUG, String.valueOf(false));
97  
98          Session mySession = Session.getInstance(properties, null);
99  
100         this.email.setMailSession(mySession);
101         assertEquals(mySession, this.email.getMailSession());
102 
103     }
104 
105     /** */
106     public void testGetSetAuthentication()
107     {
108         // setup
109         String strUsername = "user.name";
110         String strPassword = "user.pwd";
111         this.email.setAuthentication(strUsername, strPassword);
112 
113         // this is cast into DefaultAuthenticator for convenience
114         // and give us access to the getPasswordAuthentication fn
115         DefaultAuthenticator retrievedAuth =
116             (DefaultAuthenticator) this.email.getAuthenticator();
117 
118         // tests
119         assertTrue(
120             Authenticator.class.isInstance(this.email.getAuthenticator()));
121         assertEquals(
122             strUsername,
123             retrievedAuth.getPasswordAuthentication().getUserName());
124         assertEquals(
125             strPassword,
126             retrievedAuth.getPasswordAuthentication().getPassword());
127     }
128 
129     /** */
130     public void testGetSetAuthenticator()
131     {
132         // setup
133         String strUsername = "user.name";
134         String strPassword = "user.pwd";
135         DefaultAuthenticator authenicator =
136             new DefaultAuthenticator(strUsername, strPassword);
137         this.email.setAuthenticator(authenicator);
138 
139         // this is cast into DefaultAuthenticator for convenience
140         // and give us access to the getPasswordAuthentication fn
141         DefaultAuthenticator retrievedAuth =
142             (DefaultAuthenticator) this.email.getAuthenticator();
143 
144         // tests
145         assertTrue(
146             Authenticator.class.isInstance(this.email.getAuthenticator()));
147         assertEquals(
148             strUsername,
149             retrievedAuth.getPasswordAuthentication().getUserName());
150         assertEquals(
151             strPassword,
152             retrievedAuth.getPasswordAuthentication().getPassword());
153     }
154 
155     /** */
156     public void testGetSetCharset()
157     {
158         for (int i = 0; i < testCharsValid.length; i++)
159         {
160             this.email.setCharset(testCharsValid[i]);
161             assertEquals(testCharsValid[i], this.email.getCharset());
162         }
163     }
164 
165     /** */
166     public void testSetContentMimeMultipart()
167     {
168         MimeMultipart[] tests =
169             { new MimeMultipart(), new MimeMultipart("abc123"), null };
170 
171         for (int i = 0; i < tests.length; i++)
172         {
173             this.email.setContent(tests[i]);
174             assertEquals(tests[i], this.email.getContentMimeMultipart());
175         }
176     }
177 
178     /** */
179     public void testSetContentObject()
180     {
181         // setup
182         String testObject = "test string object";
183         String testContentType = "";
184 
185         // ====================================================================
186         // test (string object and valid content type)
187         testObject = "test string object";
188         testContentType = " ; charset=" + Email.US_ASCII;
189 
190         this.email.setContent(testObject, testContentType);
191         assertEquals(testObject, this.email.getContentObject());
192         assertEquals(testContentType, this.email.getContentType());
193 
194         // ====================================================================
195         // test (null string object and valid content type)
196         testObject = null;
197         testContentType = " ; charset=" + Email.US_ASCII + " some more here";
198 
199         this.email.setContent(testObject, testContentType);
200         assertEquals(testObject, this.email.getContentObject());
201         assertEquals(testContentType, this.email.getContentType());
202 
203         // ====================================================================
204         // test (string object and null content type)
205         testObject = "test string object";
206         testContentType = null;
207 
208         this.email.setContent(testObject, testContentType);
209         assertEquals(testObject, this.email.getContentObject());
210         assertEquals(testContentType, this.email.getContentType());
211 
212         // ====================================================================
213         // test (string object and invalid content type)
214         testObject = "test string object";
215         testContentType = " something incorrect ";
216 
217         this.email.setContent(testObject, testContentType);
218         assertEquals(testObject, this.email.getContentObject());
219         assertEquals(testContentType, this.email.getContentType());
220     }
221 
222     /** */
223     public void testGetSetHostName()
224     {
225 
226         for (int i = 0; i < testCharsValid.length; i++)
227         {
228             this.email.setHostName(testCharsValid[i]);
229             assertEquals(testCharsValid[i], this.email.getHostName());
230         }
231     }
232 
233     /** */
234     public void testGetSetSmtpPort()
235     {
236         // ====================================================================
237         // Test Success
238         // ====================================================================
239         int[] tests = { 1, Integer.MAX_VALUE };
240 
241         for (int i = 0; i < tests.length; i++)
242         {
243             try
244             {
245                 this.email.setSmtpPort(tests[i]);
246                 assertEquals(
247                     tests[i],
248                     Integer.valueOf(this.email.getSmtpPort()).intValue());
249             }
250             catch (IllegalArgumentException e)
251             {
252                 e.printStackTrace();
253                 fail("Unexpected exception thrown");
254             }
255         }
256 
257         // ====================================================================
258         // Test Exceptions
259         // ====================================================================
260         int[] testExs = { Integer.MIN_VALUE, -1, 0 };
261 
262         for (int i = 0; i < testExs.length; i++)
263         {
264             try
265             {
266                 this.email.setSmtpPort(testExs[i]);
267                 fail("Should have thrown an exception");
268             }
269             catch (IllegalArgumentException e)
270             {
271                 assertTrue(true);
272             }
273             catch (Exception e)
274             {
275                 e.printStackTrace();
276                 fail("Unexpected exception thrown");
277             }
278         }
279 
280     }
281 
282     /**
283      *
284      * @throws Exception Exception
285      */
286     public void testSetFrom() throws Exception
287     {
288         // ====================================================================
289         // Test Success
290         // ====================================================================
291 
292         ArrayList arrExpected = new ArrayList();
293         try
294         {
295             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
296             arrExpected.add(
297                 new InternetAddress(
298                     "joe.doe@apache.org",
299                     "joe.doe@apache.org"));
300             arrExpected.add(
301                 new InternetAddress(
302                     "someone_here@work-address.com.au",
303                     "someone_here@work-address.com.au"));
304         }
305         catch (UnsupportedEncodingException e)
306         {
307             e.printStackTrace();
308             fail("Unexpected exception thrown");
309         }
310 
311         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
312         {
313 
314             // set from
315             this.email.setFrom(ARR_VALID_EMAILS[i]);
316 
317             // retrieve and verify
318             assertEquals(arrExpected.get(i), this.email.getFromAddress());
319         }
320     }
321 
322     /**
323      *
324      * @throws Exception Exception
325      */
326     public void testSetFromWithEnconding() throws Exception
327     {
328         // ====================================================================
329         // Test Success (with charset set)
330         // ====================================================================
331         String testValidEmail = "me@home.com";
332 
333             InternetAddress inetExpected =
334                 new InternetAddress("me@home.com", "me@home.com");
335 
336             // set from
337             this.email.setCharset(Email.ISO_8859_1);
338             this.email.setFrom(testValidEmail);
339 
340             // retrieve and verify
341             assertEquals(inetExpected, this.email.getFromAddress());
342 
343     }
344 
345     /**
346      *
347      * @throws Exception Exception
348      */
349     public void testSetFrom2() throws Exception
350     {
351         // ====================================================================
352         // Test Success
353         // ====================================================================
354         String[] testEmails =
355             {
356                 "me@home.com",
357                 "joe.doe@apache.org",
358                 "someone_here@work-address.com.au" };
359         String[] testEmailNames = { "Name1", "", null };
360         ArrayList arrExpected = new ArrayList();
361         try
362         {
363             arrExpected.add(new InternetAddress("me@home.com", "Name1"));
364             arrExpected.add(
365                 new InternetAddress(
366                     "joe.doe@apache.org",
367                     "joe.doe@apache.org"));
368             arrExpected.add(
369                 new InternetAddress(
370                     "someone_here@work-address.com.au",
371                     "someone_here@work-address.com.au"));
372         }
373         catch (UnsupportedEncodingException e)
374         {
375             e.printStackTrace();
376             fail("Unexpected exception thrown");
377         }
378 
379         for (int i = 0; i < testEmails.length; i++)
380         {
381             // set from
382             this.email.setFrom(testEmails[i], testEmailNames[i]);
383 
384             // retrieve and verify
385             assertEquals(arrExpected.get(i), this.email.getFromAddress());
386 
387         }
388 
389         // ====================================================================
390         // Test Exceptions
391         // ====================================================================
392         // bad encoding
393         try
394         {
395             // reset the mail class
396             MockEmailConcrete anotherEmail = new MockEmailConcrete();
397             // set a dodgy encoding scheme
398             anotherEmail.setCharset("bad.encoding\uc5ec\n");
399             // set a valid address but bad personal name
400             anotherEmail.setFrom(
401                 "me@home.com",
402                 "\t.bad.personal.name.\uc5ec\n");
403             fail("Should have thrown an exception");
404         }
405         catch (EmailException e)
406         {
407             assertTrue(true);
408         }
409         catch (Exception e)
410         {
411             e.printStackTrace();
412             fail("Unexpected exception thrown");
413         }
414     }
415 
416     /** */
417     public void testAddTo()
418     {
419         // ====================================================================
420         // Test Success
421         // ====================================================================
422 
423         ArrayList arrExpected = new ArrayList();
424         try
425         {
426             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
427             arrExpected.add(
428                 new InternetAddress(
429                     "joe.doe@apache.org",
430                     "joe.doe@apache.org"));
431             arrExpected.add(
432                 new InternetAddress(
433                     "someone_here@work-address.com.au",
434                     "someone_here@work-address.com.au"));
435         }
436         catch (UnsupportedEncodingException e)
437         {
438             e.printStackTrace();
439             fail("Unexpected exception thrown");
440         }
441 
442         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
443         {
444             try
445             {
446                 // set from
447                 this.email.addTo(ARR_VALID_EMAILS[i]);
448             }
449             catch (EmailException e)
450             {
451                 e.printStackTrace();
452                 fail("Unexpected exception thrown");
453             }
454         }
455 
456         // retrieve and verify
457         assertEquals(arrExpected.size(), this.email.getToList().size());
458         assertEquals(arrExpected.toString(), this.email.getToList().toString());
459     }
460 
461     /** */
462     public void testAddToWithEncoding()
463     {
464         // ====================================================================
465         // Test Success
466         // ====================================================================
467         this.email.charset = Email.US_ASCII;
468 
469         ArrayList arrExpected = new ArrayList();
470         try
471         {
472             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
473             arrExpected.add(
474                 new InternetAddress(
475                     "joe.doe@apache.org",
476                     "joe.doe@apache.org"));
477             arrExpected.add(
478                 new InternetAddress(
479                     "someone_here@work-address.com.au",
480                     "someone_here@work-address.com.au"));
481         }
482         catch (UnsupportedEncodingException e)
483         {
484             e.printStackTrace();
485             fail("Unexpected exception thrown");
486         }
487 
488         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
489         {
490             try
491             {
492                 // set from
493                 this.email.addTo(ARR_VALID_EMAILS[i]);
494             }
495             catch (EmailException e)
496             {
497                 e.printStackTrace();
498                 fail("Unexpected exception thrown");
499             }
500         }
501 
502         // retrieve and verify
503         assertEquals(arrExpected.size(), this.email.getToList().size());
504         assertEquals(arrExpected.toString(), this.email.getToList().toString());
505     }
506 
507     /** */
508     public void testAddTo2()
509     {
510         // ====================================================================
511         // Test Success
512         // ====================================================================
513 
514         String[] testEmailNames = { "Name1", "", null };
515 
516         ArrayList arrExpected = new ArrayList();
517         try
518         {
519             arrExpected.add(new InternetAddress("me@home.com", "Name1"));
520             arrExpected.add(
521                 new InternetAddress(
522                     "joe.doe@apache.org",
523                     "joe.doe@apache.org"));
524             arrExpected.add(
525                 new InternetAddress(
526                     "someone_here@work-address.com.au",
527                     "someone_here@work-address.com.au"));
528         }
529         catch (UnsupportedEncodingException e)
530         {
531             e.printStackTrace();
532             fail("Unexpected exception thrown");
533         }
534 
535         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
536         {
537             try
538             {
539                 // set from
540                 this.email.addTo(ARR_VALID_EMAILS[i], testEmailNames[i]);
541             }
542             catch (EmailException e)
543             {
544                 e.printStackTrace();
545                 fail("Unexpected exception thrown");
546             }
547         }
548 
549         // retrieve and verify
550         assertEquals(arrExpected.size(), this.email.getToList().size());
551         assertEquals(arrExpected.toString(), this.email.getToList().toString());
552 
553         // ====================================================================
554         // Test Exceptions
555         // ====================================================================
556         // bad encoding
557         try
558         {
559             // reset the mail class
560             MockEmailConcrete anotherEmail = new MockEmailConcrete();
561             // set a dodgy encoding scheme
562             anotherEmail.setCharset("bad.encoding\uc5ec\n");
563             // set a valid address but bad personal name
564             anotherEmail.addTo("me@home.com", "\t.bad.name.\uc5ec\n");
565             fail("Should have thrown an exception");
566         }
567         catch (EmailException e)
568         {
569             assertTrue(true);
570         }
571         catch (Exception e)
572         {
573             e.printStackTrace();
574             fail("Unexpected exception thrown");
575         }
576     }
577 
578     /** */
579     public void testSetTo()
580     {
581         // ====================================================================
582         // Test Success
583         // ====================================================================
584         ArrayList testEmailValid2 = new ArrayList();
585         try
586         {
587             testEmailValid2.add(new InternetAddress("me@home.com", "Name1"));
588             testEmailValid2.add(
589                 new InternetAddress(
590                     "joe.doe@apache.org",
591                     "joe.doe@apache.org"));
592             testEmailValid2.add(
593                 new InternetAddress(
594                     "someone_here@work-address.com.au",
595                     "someone_here@work-address.com.au"));
596         }
597         catch (UnsupportedEncodingException e)
598         {
599             e.printStackTrace();
600             fail("Unexpected exception thrown");
601         }
602 
603         try
604         {
605             this.email.setTo(testEmailValid2);
606 
607             // retrieve and verify
608             assertEquals(testEmailValid2.size(), this.email.getToList().size());
609             assertEquals(
610                 testEmailValid2.toString(),
611                 this.email.getToList().toString());
612         }
613         catch (EmailException e)
614         {
615             e.printStackTrace();
616             fail("Unexpected exception thrown");
617         }
618 
619         // ====================================================================
620         // Exception (Null Input)
621         // ====================================================================
622         try
623         {
624             this.email.setTo(null);
625             fail("Should have thrown an exception");
626         }
627         catch (EmailException e)
628         {
629             assertTrue(true);
630         }
631         catch (Exception e)
632         {
633             e.printStackTrace();
634             fail("Unexpected exception thrown");
635         }
636 
637         // ====================================================================
638         // Exception (Empty Collection)
639         // ====================================================================
640         try
641         {
642             this.email.setTo(new ArrayList());
643             fail("Should have thrown an exception");
644         }
645         catch (EmailException e)
646         {
647             assertTrue(true);
648         }
649         catch (Exception e)
650         {
651             e.printStackTrace();
652             fail("Unexpected exception thrown");
653         }
654     }
655 
656     /** */
657     public void testAddCc()
658     {
659         // ====================================================================
660         // Test Success
661         // ====================================================================
662 
663         ArrayList arrExpected = new ArrayList();
664         try
665         {
666             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
667             arrExpected.add(
668                 new InternetAddress(
669                     "joe.doe@apache.org",
670                     "joe.doe@apache.org"));
671             arrExpected.add(
672                 new InternetAddress(
673                     "someone_here@work-address.com.au",
674                     "someone_here@work-address.com.au"));
675         }
676         catch (UnsupportedEncodingException e)
677         {
678             e.printStackTrace();
679             fail("Unexpected exception thrown");
680         }
681 
682         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
683         {
684             try
685             {
686                 // set from
687                 this.email.addCc(ARR_VALID_EMAILS[i]);
688             }
689             catch (EmailException e)
690             {
691                 e.printStackTrace();
692                 fail("Unexpected exception thrown");
693             }
694         }
695 
696         // retrieve and verify
697         assertEquals(arrExpected.size(), this.email.getCcList().size());
698         assertEquals(arrExpected.toString(), this.email.getCcList().toString());
699     }
700 
701     /** */
702     public void testAddCcWithEncoding()
703     {
704         // ====================================================================
705         // Test Success
706         // ====================================================================
707         this.email.charset = Email.US_ASCII;
708 
709         ArrayList arrExpected = new ArrayList();
710         try
711         {
712             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
713             arrExpected.add(
714                 new InternetAddress(
715                     "joe.doe@apache.org",
716                     "joe.doe@apache.org"));
717             arrExpected.add(
718                 new InternetAddress(
719                     "someone_here@work-address.com.au",
720                     "someone_here@work-address.com.au"));
721         }
722         catch (UnsupportedEncodingException e)
723         {
724             e.printStackTrace();
725             fail("Unexpected exception thrown");
726         }
727 
728         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
729         {
730             try
731             {
732                 // set from
733                 this.email.addCc(ARR_VALID_EMAILS[i]);
734             }
735             catch (EmailException e)
736             {
737                 e.printStackTrace();
738                 fail("Unexpected exception thrown");
739             }
740         }
741 
742         // retrieve and verify
743         assertEquals(arrExpected.size(), this.email.getCcList().size());
744         assertEquals(arrExpected.toString(), this.email.getCcList().toString());
745     }
746 
747     /** */
748     public void testAddCc2()
749     {
750         // ====================================================================
751         // Test Success
752         // ====================================================================
753 
754         String[] testEmailNames = { "Name1", "", null };
755 
756         ArrayList arrExpected = new ArrayList();
757         try
758         {
759             arrExpected.add(new InternetAddress("me@home.com", "Name1"));
760             arrExpected.add(
761                 new InternetAddress(
762                     "joe.doe@apache.org",
763                     "joe.doe@apache.org"));
764             arrExpected.add(
765                 new InternetAddress(
766                     "someone_here@work-address.com.au",
767                     "someone_here@work-address.com.au"));
768         }
769         catch (UnsupportedEncodingException e)
770         {
771             e.printStackTrace();
772             fail("Unexpected exception thrown");
773         }
774 
775         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
776         {
777             try
778             {
779                 // set from
780                 this.email.addCc(ARR_VALID_EMAILS[i], testEmailNames[i]);
781             }
782             catch (EmailException e)
783             {
784                 e.printStackTrace();
785                 fail("Unexpected exception thrown");
786             }
787         }
788 
789         // retrieve and verify
790         assertEquals(arrExpected.size(), this.email.getCcList().size());
791         assertEquals(arrExpected.toString(), this.email.getCcList().toString());
792 
793         // ====================================================================
794         // Test Exceptions
795         // ====================================================================
796         // bad encoding
797         try
798         {
799             // reset the mail class
800             MockEmailConcrete anotherEmail = new MockEmailConcrete();
801             // set a dodgy encoding scheme
802             anotherEmail.setCharset("bad.encoding\uc5ec\n");
803             // set a valid address but bad personal name
804             anotherEmail.addCc("me@home.com", "\t.bad.name.\uc5ec\n");
805             fail("Should have thrown an exception");
806         }
807         catch (EmailException e)
808         {
809             assertTrue(true);
810         }
811         catch (Exception e)
812         {
813             e.printStackTrace();
814             fail("Unexpected exception thrown");
815         }
816     }
817 
818     /** */
819     public void testSetCc()
820     {
821         // ====================================================================
822         // Test Success
823         // ====================================================================
824         ArrayList testEmailValid2 = new ArrayList();
825         testEmailValid2.add("Name1 <me@home.com>");
826         testEmailValid2.add("\"joe.doe@apache.org\" <joe.doe@apache.org>");
827         testEmailValid2.add(
828             "\"someone_here@work.com.au\" <someone_here@work.com.au>");
829 
830         try
831         {
832             this.email.setCc(testEmailValid2);
833             assertEquals(testEmailValid2, this.email.getCcList());
834         }
835         catch (EmailException e)
836         {
837             e.printStackTrace();
838             fail("Unexpected exception thrown");
839         }
840 
841         // ====================================================================
842         // Exception (Null Input)
843         // ====================================================================
844         try
845         {
846             this.email.setCc(null);
847             fail("Should have thrown an exception");
848         }
849         catch (EmailException e)
850         {
851             assertTrue(true);
852         }
853         catch (Exception e)
854         {
855             e.printStackTrace();
856             fail("Unexpected exception thrown");
857         }
858 
859         // ====================================================================
860         // Exception (Empty Collection)
861         // ====================================================================
862         try
863         {
864             this.email.setCc(new ArrayList());
865             fail("Should have thrown an exception");
866         }
867         catch (EmailException e)
868         {
869             assertTrue(true);
870         }
871         catch (Exception e)
872         {
873             e.printStackTrace();
874             fail("Unexpected exception thrown");
875         }
876     }
877 
878     /** */
879     public void testAddBcc()
880     {
881         // ====================================================================
882         // Test Success
883         // ====================================================================
884 
885         ArrayList arrExpected = new ArrayList();
886         try
887         {
888             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
889             arrExpected.add(
890                 new InternetAddress(
891                     "joe.doe@apache.org",
892                     "joe.doe@apache.org"));
893             arrExpected.add(
894                 new InternetAddress(
895                     "someone_here@work-address.com.au",
896                     "someone_here@work-address.com.au"));
897         }
898         catch (UnsupportedEncodingException e)
899         {
900             e.printStackTrace();
901             fail("Unexpected exception thrown");
902         }
903 
904         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
905         {
906             try
907             {
908                 // set from
909                 this.email.addBcc(ARR_VALID_EMAILS[i]);
910             }
911             catch (EmailException e)
912             {
913                 e.printStackTrace();
914                 fail("Unexpected exception thrown");
915             }
916         }
917 
918         // retrieve and verify
919         assertEquals(arrExpected.size(), this.email.getBccList().size());
920         assertEquals(
921             arrExpected.toString(),
922             this.email.getBccList().toString());
923     }
924 
925     /** */
926     public void testAddBccWithEncoding()
927     {
928         // ====================================================================
929         // Test Success
930         // ====================================================================
931         this.email.charset = Email.US_ASCII;
932 
933         ArrayList arrExpected = new ArrayList();
934         try
935         {
936             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
937             arrExpected.add(
938                 new InternetAddress(
939                     "joe.doe@apache.org",
940                     "joe.doe@apache.org"));
941             arrExpected.add(
942                 new InternetAddress(
943                     "someone_here@work-address.com.au",
944                     "someone_here@work-address.com.au"));
945         }
946         catch (UnsupportedEncodingException e)
947         {
948             e.printStackTrace();
949             fail("Unexpected exception thrown");
950         }
951 
952         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
953         {
954             try
955             {
956                 // set from
957                 this.email.addBcc(ARR_VALID_EMAILS[i]);
958             }
959             catch (EmailException e)
960             {
961                 e.printStackTrace();
962                 fail("Unexpected exception thrown");
963             }
964         }
965 
966         // retrieve and verify
967         assertEquals(arrExpected.size(), this.email.getBccList().size());
968         assertEquals(
969             arrExpected.toString(),
970             this.email.getBccList().toString());
971     }
972 
973     /** */
974     public void testAddBcc2()
975     {
976         // ====================================================================
977         // Test Success
978         // ====================================================================
979 
980         String[] testEmailNames = { "Name1", "", null };
981 
982         ArrayList arrExpected = new ArrayList();
983         try
984         {
985             arrExpected.add(new InternetAddress("me@home.com", "Name1"));
986             arrExpected.add(
987                 new InternetAddress(
988                     "joe.doe@apache.org",
989                     "joe.doe@apache.org"));
990             arrExpected.add(
991                 new InternetAddress(
992                     "someone_here@work-address.com.au",
993                     "someone_here@work-address.com.au"));
994         }
995         catch (UnsupportedEncodingException e)
996         {
997             e.printStackTrace();
998             fail("Unexpected exception thrown");
999         }
1000 
1001         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
1002         {
1003             try
1004             {
1005                 // set from
1006                 this.email.addBcc(ARR_VALID_EMAILS[i], testEmailNames[i]);
1007             }
1008             catch (EmailException e)
1009             {
1010                 e.printStackTrace();
1011                 fail("Unexpected exception thrown");
1012             }
1013         }
1014 
1015         // retrieve and verify
1016         assertEquals(arrExpected.size(), this.email.getBccList().size());
1017         assertEquals(
1018             arrExpected.toString(),
1019             this.email.getBccList().toString());
1020 
1021         // ====================================================================
1022         // Test Exceptions
1023         // ====================================================================
1024         // bad encoding
1025         try
1026         {
1027             // reset the mail class
1028             MockEmailConcrete anotherEmail = new MockEmailConcrete();
1029             // set a dodgy encoding scheme
1030             anotherEmail.setCharset("bad.encoding\uc5ec\n");
1031             // set a valid address but bad personal name
1032             anotherEmail.addBcc("me@home.com", "\t.bad.name.\uc5ec\n");
1033             fail("Should have thrown an exception");
1034         }
1035         catch (EmailException e)
1036         {
1037             assertTrue(true);
1038         }
1039         catch (Exception e)
1040         {
1041             e.printStackTrace();
1042             fail("Unexpected exception thrown");
1043         }
1044     }
1045 
1046     /** */
1047     public void testSetBcc()
1048     {
1049         // ====================================================================
1050         // Test Success
1051         // ====================================================================
1052         ArrayList testInetEmailValid = new ArrayList();
1053         try
1054         {
1055             testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
1056             testInetEmailValid.add(
1057                 new InternetAddress(
1058                     "joe.doe@apache.org",
1059                     "joe.doe@apache.org"));
1060             testInetEmailValid.add(
1061                 new InternetAddress(
1062                     "someone_here@work-address.com.au",
1063                     "someone_here@work-address.com.au"));
1064         }
1065         catch (UnsupportedEncodingException e)
1066         {
1067             e.printStackTrace();
1068             fail("Unexpected exception thrown");
1069         }
1070 
1071         try
1072         {
1073             this.email.setBcc(testInetEmailValid);
1074             assertEquals(testInetEmailValid, this.email.getBccList());
1075         }
1076         catch (EmailException e)
1077         {
1078             e.printStackTrace();
1079             fail("Unexpected exception thrown");
1080         }
1081 
1082         // ====================================================================
1083         // Exception (Null Input)
1084         // ====================================================================
1085         try
1086         {
1087             this.email.setBcc(null);
1088             fail("Should have thrown an exception");
1089         }
1090         catch (EmailException e)
1091         {
1092             assertTrue(true);
1093         }
1094         catch (Exception e)
1095         {
1096             e.printStackTrace();
1097             fail("Unexpected exception thrown");
1098         }
1099 
1100         // ====================================================================
1101         // Exception (Empty Collection)
1102         // ====================================================================
1103         try
1104         {
1105             this.email.setBcc(new ArrayList());
1106             fail("Should have thrown an exception");
1107         }
1108         catch (EmailException e)
1109         {
1110             assertTrue(true);
1111         }
1112         catch (Exception e)
1113         {
1114             e.printStackTrace();
1115             fail("Unexpected exception thrown");
1116         }
1117     }
1118 
1119     /** */
1120     public void testAddReplyTo()
1121     {
1122         // ====================================================================
1123         // Test Success
1124         // ====================================================================
1125 
1126         ArrayList arrExpected = new ArrayList();
1127         try
1128         {
1129             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
1130             arrExpected.add(
1131                 new InternetAddress(
1132                     "joe.doe@apache.org",
1133                     "joe.doe@apache.org"));
1134             arrExpected.add(
1135                 new InternetAddress(
1136                     "someone_here@work-address.com.au",
1137                     "someone_here@work-address.com.au"));
1138         }
1139         catch (UnsupportedEncodingException e)
1140         {
1141             e.printStackTrace();
1142             fail("Unexpected exception thrown");
1143         }
1144 
1145         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
1146         {
1147             try
1148             {
1149                 // set from
1150                 this.email.addReplyTo(ARR_VALID_EMAILS[i]);
1151             }
1152             catch (EmailException e)
1153             {
1154                 e.printStackTrace();
1155                 fail("Unexpected exception thrown");
1156             }
1157         }
1158 
1159         // retrieve and verify
1160         assertEquals(arrExpected.size(), this.email.getReplyList().size());
1161         assertEquals(
1162             arrExpected.toString(),
1163             this.email.getReplyList().toString());
1164     }
1165 
1166     /** */
1167     public void testAddReplyToWithEncoding()
1168     {
1169         // ====================================================================
1170         // Test Success
1171         // ====================================================================
1172         this.email.charset = Email.US_ASCII;
1173 
1174         ArrayList arrExpected = new ArrayList();
1175         try
1176         {
1177             arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
1178             arrExpected.add(
1179                 new InternetAddress(
1180                     "joe.doe@apache.org",
1181                     "joe.doe@apache.org"));
1182             arrExpected.add(
1183                 new InternetAddress(
1184                     "someone_here@work-address.com.au",
1185                     "someone_here@work-address.com.au"));
1186         }
1187         catch (UnsupportedEncodingException e)
1188         {
1189             e.printStackTrace();
1190             fail("Unexpected exception thrown");
1191         }
1192 
1193         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
1194         {
1195             try
1196             {
1197                 // set from
1198                 this.email.addReplyTo(ARR_VALID_EMAILS[i]);
1199             }
1200             catch (EmailException e)
1201             {
1202                 e.printStackTrace();
1203                 fail("Unexpected exception thrown");
1204             }
1205         }
1206 
1207         // retrieve and verify
1208         assertEquals(arrExpected.size(), this.email.getReplyList().size());
1209         assertEquals(
1210             arrExpected.toString(),
1211             this.email.getReplyList().toString());
1212     }
1213 
1214     /** */
1215     public void testAddReplyTo2()
1216     {
1217         // ====================================================================
1218         // Test Success
1219         // ====================================================================
1220 
1221         String[] testEmailNames = { "Name1", "", null };
1222 
1223         ArrayList arrExpected = new ArrayList();
1224         try
1225         {
1226             arrExpected.add(new InternetAddress("me@home.com", "Name1"));
1227             arrExpected.add(
1228                 new InternetAddress(
1229                     "joe.doe@apache.org",
1230                     "joe.doe@apache.org"));
1231             arrExpected.add(
1232                 new InternetAddress(
1233                     "someone_here@work-address.com.au",
1234                     "someone_here@work-address.com.au"));
1235         }
1236         catch (UnsupportedEncodingException e)
1237         {
1238             e.printStackTrace();
1239             fail("Unexpected exception thrown");
1240         }
1241 
1242         for (int i = 0; i < ARR_VALID_EMAILS.length; i++)
1243         {
1244             try
1245             {
1246                 // set from
1247                 this.email.addReplyTo(ARR_VALID_EMAILS[i], testEmailNames[i]);
1248             }
1249             catch (EmailException e)
1250             {
1251                 e.printStackTrace();
1252                 fail("Unexpected exception thrown");
1253             }
1254         }
1255 
1256         // retrieve and verify
1257         assertEquals(arrExpected.size(), this.email.getReplyList().size());
1258         assertEquals(
1259             arrExpected.toString(),
1260             this.email.getReplyList().toString());
1261 
1262         // ====================================================================
1263         // Test Exceptions
1264         // ====================================================================
1265         // bad encoding
1266         try
1267         {
1268             // reset the mail class
1269             MockEmailConcrete anotherEmail = new MockEmailConcrete();
1270             // set a dodgy encoding scheme
1271             anotherEmail.setCharset("bad.encoding\uc5ec\n");
1272             // set a valid address but bad personal name
1273             anotherEmail.addReplyTo("me@home.com", "\t.bad.name.\uc5ec\n");
1274             fail("Should have thrown an exception");
1275         }
1276         catch (EmailException e)
1277         {
1278             assertTrue(true);
1279         }
1280         catch (Exception e)
1281         {
1282             e.printStackTrace();
1283             fail("Unexpected exception thrown");
1284         }
1285     }
1286 
1287     /** */
1288     public void testAddHeader()
1289     {
1290         // ====================================================================
1291         // Test Success
1292         // ====================================================================
1293         Hashtable ht = new Hashtable();
1294         ht.put("X-Priority", "1");
1295         ht.put("Disposition-Notification-To", "me@home.com");
1296         ht.put("X-Mailer", "Sendmail");
1297 
1298         Enumeration enumKey = ht.keys();
1299 
1300         while (enumKey.hasMoreElements())
1301         {
1302             String strName = (String) enumKey.nextElement();
1303             String strValue = (String) ht.get(strName);
1304 
1305             this.email.addHeader(strName, strValue);
1306         }
1307 
1308         assertEquals(ht.size(), this.email.getHeaders().size());
1309         assertEquals(ht, this.email.getHeaders());
1310     }
1311 
1312     /** */
1313     public void testAddHeaderEx()
1314     {
1315         // ====================================================================
1316         // Test Exceptions
1317         // ====================================================================
1318         Hashtable htBad = new Hashtable();
1319         htBad.put("X-Mailer", "");
1320         htBad.put("X-Priority", "");
1321         htBad.put("", "me@home.com");
1322 
1323         Hashtable arrExpected = new Hashtable();
1324         Enumeration enumKeyBad = htBad.keys();
1325 
1326         while (enumKeyBad.hasMoreElements())
1327         {
1328             try
1329             {
1330                 String strName = (String) enumKeyBad.nextElement();
1331                 String strValue = (String) htBad.get(strName);
1332 
1333                 this.email.addHeader(strName, strValue);
1334                 fail("Should have thrown an exception");
1335             }
1336             catch (IllegalArgumentException e)
1337             {
1338                 assertTrue(true);
1339             }
1340         }
1341 
1342         assertEquals(arrExpected.size(), this.email.getHeaders().size());
1343         assertEquals(
1344             arrExpected.toString(),
1345             this.email.getHeaders().toString());
1346     }
1347 
1348     /** */
1349     public void testSetHeaders()
1350     {
1351         // ====================================================================
1352         // Test Success
1353         // ====================================================================
1354         Hashtable ht = new Hashtable();
1355         ht.put("X-Priority", "1");
1356         ht.put("Disposition-Notification-To", "me@home.com");
1357         ht.put("X-Mailer", "Sendmail");
1358 
1359         this.email.setHeaders(ht);
1360 
1361         assertEquals(ht.size(), this.email.getHeaders().size());
1362         assertEquals(ht, this.email.getHeaders());
1363     }
1364 
1365     /** */
1366     public void testSetHeadersEx()
1367     {
1368         // ====================================================================
1369         // Test Exceptions
1370         // ====================================================================
1371         // first test
1372         Hashtable htBad = new Hashtable();
1373         htBad.put("X-Mailer", "");
1374 
1375         Hashtable arrExpected = new Hashtable();
1376 
1377         try
1378         {
1379             this.email.setHeaders(htBad);
1380             fail("Should have thrown an exception");
1381         }
1382         catch (IllegalArgumentException e)
1383         {
1384             assertTrue(true);
1385         }
1386 
1387         assertEquals(arrExpected.size(), this.email.getHeaders().size());
1388         assertEquals(
1389             arrExpected.toString(),
1390             this.email.getHeaders().toString());
1391 
1392         // ====================================================================
1393         // second test
1394         htBad = new Hashtable();
1395         htBad.put("", "me@home.com");
1396 
1397         try
1398         {
1399             this.email.setHeaders(htBad);
1400             fail("Should have thrown an exception");
1401         }
1402         catch (IllegalArgumentException e)
1403         {
1404             assertTrue(true);
1405         }
1406 
1407         assertEquals(arrExpected.size(), this.email.getHeaders().size());
1408         assertEquals(
1409             arrExpected.toString(),
1410             this.email.getHeaders().toString());
1411     }
1412 
1413     /** */
1414     public void testSetSubject()
1415     {
1416 
1417         for (int i = 0; i < testCharsValid.length; i++)
1418         {
1419             this.email.setSubject(testCharsValid[i]);
1420             assertEquals(testCharsValid[i], this.email.getSubject());
1421         }
1422     }
1423 
1424     /** */
1425     public void testSendEx()
1426     {
1427         // ====================================================================
1428         // Test Exceptions (in getMailSession)
1429         // ====================================================================
1430         // hostname not set
1431         try
1432         {
1433             this.getMailServer();
1434 
1435             this.email = new MockEmailConcrete();
1436             this.email.send();
1437             fail("Should have thrown an exception");
1438         }
1439         catch (EmailException e)
1440         {
1441             this.fakeMailServer.stop();
1442             assertTrue(true);
1443         }
1444         catch (Exception e)
1445         {
1446             e.printStackTrace();
1447             fail("Unexpected exception thrown");
1448         }
1449 
1450         // bad hostname
1451         try
1452         {
1453             this.getMailServer();
1454 
1455             this.email = new MockEmailConcrete();
1456             this.email.setSubject("Test Email #1 Subject");
1457             this.email.setHostName("bad.host.com");
1458             this.email.setFrom("me@home.com");
1459             this.email.addTo("me@home.com");
1460             this.email.addCc("me@home.com");
1461             this.email.addBcc("me@home.com");
1462             this.email.addReplyTo("me@home.com");
1463 
1464             this.email.setContent(
1465                 "test string object",
1466                 " ; charset=" + Email.US_ASCII);
1467 
1468             this.email.send();
1469             fail("Should have thrown an exception");
1470         }
1471         catch (EmailException e)
1472         {
1473             assertTrue(e.getCause() instanceof ParseException);
1474             this.fakeMailServer.stop();
1475             assertTrue(true);
1476         }
1477         catch (Exception e)
1478         {
1479             e.printStackTrace();
1480             fail("Unexpected exception thrown");
1481         }
1482 
1483         // ====================================================================
1484         // Test Exceptions (in send)
1485         // ====================================================================
1486         // from add not set
1487         try
1488         {
1489             this.getMailServer();
1490 
1491             this.email = new MockEmailConcrete();
1492             this.email.setHostName(this.strTestMailServer);
1493             this.email.setSmtpPort(this.getMailServerPort());
1494 
1495             this.email.send();
1496             fail("Should have thrown an exception");
1497         }
1498         catch (EmailException e)
1499         {
1500             this.fakeMailServer.stop();
1501             assertTrue(true);
1502         }
1503         catch (Exception e)
1504         {
1505             e.printStackTrace();
1506             fail("Unexpected exception thrown");
1507         }
1508 
1509         // destination (to/cc/bcc) dd not set
1510         try
1511         {
1512             this.getMailServer();
1513 
1514             this.email = new MockEmailConcrete();
1515             this.email.setHostName(this.strTestMailServer);
1516             this.email.setSmtpPort(this.getMailServerPort());
1517             this.email.setFrom("me@home.com");
1518             this.email.send();
1519             fail("Should have thrown an exception");
1520         }
1521         catch (EmailException e)
1522         {
1523             this.fakeMailServer.stop();
1524             assertTrue(true);
1525         }
1526         catch (Exception e)
1527         {
1528             e.printStackTrace();
1529             fail("Unexpected exception thrown");
1530         }
1531 
1532         // bad auth set
1533         try
1534         {
1535             this.getMailServer();
1536 
1537             this.email = new MockEmailConcrete();
1538             this.email.setHostName(this.strTestMailServer);
1539             this.email.setSmtpPort(this.getMailServerPort());
1540             this.email.setFrom(this.strTestMailFrom);
1541             this.email.addTo(this.strTestMailTo);
1542             this.email.setAuthentication(null, null);
1543             this.email.send();
1544             fail("Should have thrown an exception");
1545         }
1546         catch (EmailException e)
1547         {
1548             this.fakeMailServer.stop();
1549             assertTrue(true);
1550         }
1551         catch (Exception e)
1552         {
1553             e.printStackTrace();
1554             fail("Unexpected exception thrown");
1555         }
1556     }
1557 
1558     /** */
1559     public void testGetSetSentDate()
1560     {
1561         // with input date
1562 
1563         Date dtTest = Calendar.getInstance().getTime();
1564         this.email.setSentDate(dtTest);
1565         assertEquals(dtTest, this.email.getSentDate());
1566 
1567         // with null input (this is a fudge :D)
1568         this.email.setSentDate(null);
1569 
1570         Date sentDate = this.email.getSentDate();
1571 
1572         // Date objects are millisecond specific. If you have a slow processor,
1573         // time passes between the generation of dtTest and the new Date() in
1574         // getSentDate() and this test fails. Make sure that the difference
1575         // is less than a second...
1576         assertTrue(Math.abs(sentDate.getTime() - dtTest.getTime()) < 1000);
1577     }
1578 
1579     /** */
1580     public void testToInternetAddressArray()
1581     {
1582         ArrayList testInetEmailValid = new ArrayList();
1583 
1584         try
1585         {
1586             testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
1587             testInetEmailValid.add(
1588                 new InternetAddress(
1589                     "joe.doe@apache.org",
1590                     "joe.doe@apache.org"));
1591             testInetEmailValid.add(
1592                 new InternetAddress(
1593                     "someone_here@work-address.com.au",
1594                     "someone_here@work-address.com.au"));
1595 
1596             this.email.setBcc(testInetEmailValid);
1597             assertEquals(
1598                 testInetEmailValid.size(),
1599                 this.email.toInternetAddressArray(
1600                     this.email.getBccList()).length);
1601         }
1602         catch (UnsupportedEncodingException e)
1603         {
1604             e.printStackTrace();
1605             fail("Unexpected exception thrown");
1606         }
1607         catch (EmailException e)
1608         {
1609             e.printStackTrace();
1610             fail("Unexpected exception thrown");
1611         }
1612     }
1613 
1614     /** */
1615     public void testSetPopBeforeSmtp()
1616     {
1617         // simple test (can be improved)
1618         boolean boolPopBeforeSmtp = true;
1619         String strHost = "mail.home.com";
1620         String strUsername = "user.name";
1621         String strPassword = "user.passwd";
1622 
1623         this.email.setPopBeforeSmtp(
1624             boolPopBeforeSmtp,
1625             strHost,
1626             strUsername,
1627             strPassword);
1628 
1629         // retrieve and verify
1630         assertEquals(boolPopBeforeSmtp, this.email.isPopBeforeSmtp());
1631         assertEquals(strHost, this.email.getPopHost());
1632         assertEquals(strUsername, this.email.getPopUsername());
1633         assertEquals(strPassword, this.email.getPopPassword());
1634     }
1635 
1636 }