View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail;
18  
19  import static org.junit.Assert.*;
20  
21  import java.io.File;
22  import java.nio.charset.Charset;
23  import java.nio.charset.IllegalCharsetNameException;
24  import java.util.ArrayList;
25  import java.util.Calendar;
26  import java.util.Collections;
27  import java.util.Date;
28  import java.util.HashMap;
29  import java.util.Hashtable;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  import javax.mail.Session;
35  import javax.mail.internet.InternetAddress;
36  import javax.mail.internet.MimeMessage;
37  import javax.mail.internet.MimeMultipart;
38  import javax.mail.internet.ParseException;
39  
40  import org.apache.commons.mail.mocks.MockEmailConcrete;
41  import org.junit.Before;
42  import org.junit.Test;
43  
44  /**
45   * JUnit test case for Email Class
46   *
47   * @since 1.0
48   * @version $Id: EmailTest.java 1665699 2015-03-10 21:12:09Z tn $
49   */
50  public class EmailTest extends AbstractEmailTest
51  {
52      private static final String[] VALID_EMAILS =
53          {
54              "me@home.com",
55              "joe.doe@apache.org",
56              "someone_here@work-address.com.au"
57          };
58  
59      /** mock for testing */
60      private MockEmailConcrete email;
61  
62      @Before
63      public void setUpEmailTest()
64      {
65          // reusable objects to be used across multiple tests
66          email = new MockEmailConcrete();
67      }
68  
69      @Test
70      public void testGetSetDebug()
71      {
72          email.setDebug(true);
73          assertTrue(email.isDebug());
74          email.setDebug(false);
75          assertFalse(email.isDebug());
76      }
77  
78      @Test
79      public void testGetSetSession() throws Exception
80      {
81  
82          final Properties properties = new Properties(System.getProperties());
83          properties.setProperty(EmailConstants.MAIL_TRANSPORT_PROTOCOL, EmailConstants.SMTP);
84  
85          properties.setProperty(
86              EmailConstants.MAIL_PORT,
87              String.valueOf(getMailServerPort()));
88          properties.setProperty(EmailConstants.MAIL_HOST, strTestMailServer);
89          properties.setProperty(EmailConstants.MAIL_DEBUG, String.valueOf(false));
90  
91          final Session mySession = Session.getInstance(properties, null);
92  
93          email.setMailSession(mySession);
94          assertEquals(mySession, email.getMailSession());
95  
96      }
97  
98      @Test
99      public void testGetSetAuthentication()
100     {
101         // setup
102         final String strUsername = "user.name";
103         final String strPassword = "user.pwd";
104         email.setAuthentication(strUsername, strPassword);
105 
106         // this is cast into DefaultAuthenticator for convenience
107         // and give us access to the getPasswordAuthentication fn
108         final DefaultAuthenticator retrievedAuth =
109             (DefaultAuthenticator) email.getAuthenticator();
110 
111         // tests
112         assertEquals(
113             strUsername,
114             retrievedAuth.getPasswordAuthentication().getUserName());
115         assertEquals(
116             strPassword,
117             retrievedAuth.getPasswordAuthentication().getPassword());
118     }
119 
120     @Test
121     public void testGetSetAuthenticator()
122     {
123         // setup
124         final String strUsername = "user.name";
125         final String strPassword = "user.pwd";
126         final DefaultAuthenticator authenticator =
127             new DefaultAuthenticator(strUsername, strPassword);
128         email.setAuthenticator(authenticator);
129 
130         // this is cast into DefaultAuthenticator for convenience
131         // and give us access to the getPasswordAuthentication fn
132         final DefaultAuthenticator retrievedAuth =
133             (DefaultAuthenticator) email.getAuthenticator();
134 
135         // tests
136         assertEquals(
137                 strUsername,
138                 retrievedAuth.getPasswordAuthentication().getUserName());
139         assertEquals(
140             strPassword,
141             retrievedAuth.getPasswordAuthentication().getPassword());
142     }
143 
144     @Test
145     public void testGetSetCharset()
146     {
147         // test ASCII and UTF-8 charsets; since every JVM is required
148         // to support these, testing them should always succeed.
149         Charset set = Charset.forName("US-ASCII");
150         email.setCharset(set.name());
151         assertEquals(set.name(), email.getCharset());
152 
153         set = Charset.forName("UTF-8");
154         email.setCharset(set.name());
155         assertEquals(set.name(), email.getCharset());
156     }
157 
158     @Test
159     public void testSetContentEmptyMimeMultipart()
160     {
161         final MimeMultipart part = new MimeMultipart();
162         email.setContent(part);
163 
164         assertEquals(part, email.getContentMimeMultipart());
165     }
166 
167     @Test
168     public void testSetContentMimeMultipart()
169     {
170         final MimeMultipart part = new MimeMultipart("abc123");
171         email.setContent(part);
172 
173         assertEquals(part, email.getContentMimeMultipart());
174     }
175 
176     @Test
177     public void testSetContentNull() throws Exception
178     {
179         email.setContent(null);
180         assertNull(email.getContentMimeMultipart());
181     }
182 
183     @Test
184     public void testSetContentObject()
185     {
186         // ====================================================================
187         // test (string object and valid content type)
188         String testObject = "test string object";
189         String testContentType = " ; charset=" + EmailConstants.US_ASCII;
190 
191         email.setContent(testObject, testContentType);
192         assertEquals(testObject, email.getContentObject());
193         assertEquals(testContentType, email.getContentType());
194 
195         // ====================================================================
196         // test (null string object and valid content type)
197         testObject = null;
198         testContentType = " ; charset=" + EmailConstants.US_ASCII + " some more here";
199 
200         email.setContent(testObject, testContentType);
201         assertEquals(testObject, email.getContentObject());
202         assertEquals(testContentType, email.getContentType());
203 
204         // ====================================================================
205         // test (string object and null content type)
206         testObject = "test string object";
207         testContentType = null;
208 
209         email.setContent(testObject, testContentType);
210         assertEquals(testObject, email.getContentObject());
211         assertEquals(testContentType, email.getContentType());
212 
213         // ====================================================================
214         // test (string object and invalid content type)
215         testObject = "test string object";
216         testContentType = " something incorrect ";
217 
218         email.setContent(testObject, testContentType);
219         assertEquals(testObject, email.getContentObject());
220         assertEquals(testContentType, email.getContentType());
221     }
222 
223     @Test
224     public void testGetSetHostName()
225     {
226         for (final String validChar : testCharsValid) {
227             email.setHostName(validChar);
228             assertEquals(validChar, email.getHostName());
229         }
230     }
231 
232     @Test
233     public void testGetSetSmtpPort()
234     {
235         email.setSmtpPort(1);
236         assertEquals(
237             1,
238             Integer.valueOf(email.getSmtpPort()).intValue());
239 
240         email.setSmtpPort(Integer.MAX_VALUE);
241         assertEquals(
242                 Integer.MAX_VALUE,
243                 Integer.valueOf(email.getSmtpPort()).intValue());
244     }
245 
246     @Test(expected = IllegalArgumentException.class)
247     public void testSetSmtpPortZero()
248     {
249         email.setSmtpPort(0);
250     }
251 
252     @Test(expected = IllegalArgumentException.class)
253     public void testSetSmptPortNegative()
254     {
255         email.setSmtpPort(-1);
256     }
257 
258     @Test(expected = IllegalArgumentException.class)
259     public void testSetSmtpPortMinValue()
260     {
261         email.setSmtpPort(Integer.MIN_VALUE);
262     }
263 
264     @Test
265     public void testSetFrom() throws Exception
266     {
267         // ====================================================================
268         // Test Success
269         // ====================================================================
270 
271         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
272         arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
273         arrExpected.add(
274             new InternetAddress(
275                 "joe.doe@apache.org",
276                 "joe.doe@apache.org"));
277         arrExpected.add(
278                 new InternetAddress(
279                         "someone_here@work-address.com.au",
280                         "someone_here@work-address.com.au"));
281 
282         for (int i = 0; i < VALID_EMAILS.length; i++)
283         {
284 
285             // set from
286             email.setFrom(VALID_EMAILS[i]);
287 
288             // retrieve and verify
289             assertEquals(arrExpected.get(i), email.getFromAddress());
290         }
291     }
292 
293     @Test
294     public void testSetFromWithEncoding() throws Exception
295     {
296         // ====================================================================
297         // Test Success (with charset set)
298         // ====================================================================
299         final String testValidEmail = "me@home.com";
300 
301         final InternetAddress inetExpected =
302             new InternetAddress("me@home.com", "me@home.com", EmailConstants.ISO_8859_1);
303 
304         // set from
305         email.setFrom(testValidEmail, testValidEmail, EmailConstants.ISO_8859_1);
306 
307         // retrieve and verify
308         assertEquals(inetExpected, email.getFromAddress());
309 
310     }
311 
312     @Test
313     public void testSetFrom2() throws Exception
314     {
315         // ====================================================================
316         // Test Success
317         // ====================================================================
318         final String[] testEmailNames = {"Name1", "", null};
319         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
320         arrExpected.add(new InternetAddress("me@home.com", "Name1"));
321         arrExpected.add(
322             new InternetAddress(
323                 "joe.doe@apache.org",
324                 "joe.doe@apache.org"));
325         arrExpected.add(
326                 new InternetAddress(
327                         "someone_here@work-address.com.au",
328                         "someone_here@work-address.com.au"));
329 
330         for (int i = 0; i < VALID_EMAILS.length; i++)
331         {
332             // set from
333             email.setFrom(VALID_EMAILS[i], testEmailNames[i]);
334 
335             // retrieve and verify
336             assertEquals(arrExpected.get(i), email.getFromAddress());
337 
338         }
339     }
340 
341     @Test(expected = IllegalCharsetNameException.class)
342     public void testSetFromBadEncoding() throws Exception {
343         email.setFrom("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
344     }
345 
346     @Test    
347     public void testAddTo() throws Exception
348     {
349         // ====================================================================
350         // Test Success
351         // ====================================================================
352 
353         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
354         arrExpected.add(new InternetAddress("me@home.com"));
355         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
356         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
357 
358         for (final String address : VALID_EMAILS) {
359             email.addTo(address);
360         }
361 
362         // retrieve and verify
363         assertEquals(arrExpected.size(), email.getToAddresses().size());
364         assertEquals(arrExpected.toString(), email.getToAddresses().toString());
365     }
366 
367     @Test
368     public void testAddToArray() throws Exception
369     {
370         // ====================================================================
371         // Test Success
372         // ====================================================================
373 
374         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
375         arrExpected.add(new InternetAddress("me@home.com"));
376         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
377         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
378 
379         //set To
380         email.addTo(VALID_EMAILS);
381 
382         // retrieve and verify
383         assertEquals(arrExpected.size(), email.getToAddresses().size());
384         assertEquals(arrExpected.toString(), email.getToAddresses().toString());
385     }
386 
387     @Test
388     public void testAddToWithEncoding() throws Exception
389     {
390         // ====================================================================
391         // Test Success
392         // ====================================================================
393         final String testCharset = EmailConstants.ISO_8859_1;
394         final String[] testEmailNames = {"Name1", "", null};
395 
396         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
397         arrExpected.add(
398             new InternetAddress(
399                 "me@home.com",
400                 "Name1",
401                 testCharset));
402         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
403         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
404 
405         for (int i = 0; i < VALID_EMAILS.length; i++)
406         {
407             // set from
408             email.addTo(VALID_EMAILS[i], testEmailNames[i], testCharset);
409         }
410 
411         // retrieve and verify
412         assertEquals(arrExpected.size(), email.getToAddresses().size());
413         assertEquals(arrExpected.toString(), email.getToAddresses().toString());
414     }
415 
416     @Test
417     public void testAddTo2() throws Exception
418     {
419         // ====================================================================
420         // Test Success
421         // ====================================================================
422 
423         final String[] testEmailNames = {"Name1", "", null};
424 
425         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
426         arrExpected.add(new InternetAddress("me@home.com", "Name1"));
427         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
428         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
429 
430         for (int i = 0; i < VALID_EMAILS.length; i++)
431         {
432             // set from
433             email.addTo(VALID_EMAILS[i], testEmailNames[i]);
434         }
435 
436         // retrieve and verify
437         assertEquals(arrExpected.size(), email.getToAddresses().size());
438         assertEquals(arrExpected.toString(), email.getToAddresses().toString());
439     }
440 
441     @Test(expected = IllegalCharsetNameException.class)
442     public void testAddToBadEncoding() throws Exception
443     {
444         email.addTo("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
445     }
446 
447     @Test
448     public void testSetTo() throws Exception
449     {
450         // ====================================================================
451         // Test Success
452         // ====================================================================
453         final List<InternetAddress> testEmailValid2 = new ArrayList<InternetAddress>();
454         testEmailValid2.add(new InternetAddress("me@home.com", "Name1"));
455         testEmailValid2.add(
456             new InternetAddress(
457                 "joe.doe@apache.org",
458                 "joe.doe@apache.org"));
459         testEmailValid2.add(
460             new InternetAddress(
461                 "someone_here@work-address.com.au",
462                 "someone_here@work-address.com.au"));
463 
464         email.setTo(testEmailValid2);
465 
466         // retrieve and verify
467         assertEquals(testEmailValid2.size(), email.getToAddresses().size());
468         assertEquals(
469                 testEmailValid2.toString(),
470                 email.getToAddresses().toString());
471     }
472 
473     @Test(expected = EmailException.class)
474     public void testSetToNull() throws Exception
475     {
476         email.setTo(null);
477     }
478 
479     @Test(expected = EmailException.class)
480     public void testSetToEmpty() throws Exception
481     {
482         email.setTo(Collections.<InternetAddress>emptyList());
483     }
484 
485     @Test
486     public void testAddCc() throws Exception
487     {
488         // ====================================================================
489         // Test Success
490         // ====================================================================
491 
492         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
493         arrExpected.add(new InternetAddress("me@home.com"));
494         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
495         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
496 
497         for (final String address : VALID_EMAILS) {
498             email.addCc(address);
499         }
500 
501         // retrieve and verify
502         assertEquals(arrExpected.size(), email.getCcAddresses().size());
503         assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
504     }
505 
506     @Test
507     public void testAddCcArray() throws Exception
508     {
509         // ====================================================================
510         // Test Success
511         // ====================================================================
512 
513         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
514         arrExpected.add(new InternetAddress("me@home.com"));
515         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
516         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
517 
518         //set Cc array
519         email.addCc(VALID_EMAILS);
520 
521         // retrieve and verify
522         assertEquals(arrExpected.size(), email.getCcAddresses().size());
523         assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
524     }
525 
526     @Test
527     public void testAddCcWithEncoding() throws Exception
528     {
529         // ====================================================================
530         // Test Success
531         // ====================================================================
532         final String testCharset = EmailConstants.ISO_8859_1;
533         final String[] testEmailNames = {"Name1", "", null};
534 
535         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
536         arrExpected.add(
537             new InternetAddress("me@home.com", "Name1", testCharset));
538         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
539         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
540 
541         // add valid ccs
542         for (int i = 0; i < VALID_EMAILS.length; i++)
543         {
544             email.addCc(VALID_EMAILS[i], testEmailNames[i], testCharset);
545         }
546 
547         // retrieve and verify
548         assertEquals(arrExpected.size(), email.getCcAddresses().size());
549         assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
550     }
551 
552     @Test
553     public void testAddCc2() throws Exception
554     {
555         // ====================================================================
556         // Test Success
557         // ====================================================================
558 
559         final String[] testEmailNames = {"Name1", "", null};
560 
561         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
562         arrExpected.add(new InternetAddress("me@home.com", "Name1"));
563         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
564         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
565 
566         for (int i = 0; i < VALID_EMAILS.length; i++)
567         {
568             // set from
569             email.addCc(VALID_EMAILS[i], testEmailNames[i]);
570         }
571 
572         // retrieve and verify
573         assertEquals(arrExpected.size(), email.getCcAddresses().size());
574         assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
575     }
576 
577     @Test(expected = IllegalCharsetNameException.class)
578     public void testAddCcBadEncoding() throws Exception
579     {
580         email.addCc("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
581     }
582 
583     @Test
584     public void testSetCc() throws Exception
585     {
586         // ====================================================================
587         // Test Success
588         // ====================================================================
589         final List<InternetAddress> testEmailValid2 = new ArrayList<InternetAddress>();
590         testEmailValid2.add(new InternetAddress("Name1 <me@home.com>"));
591         testEmailValid2.add(new InternetAddress("\"joe.doe@apache.org\" <joe.doe@apache.org>"));
592         testEmailValid2.add(
593                 new InternetAddress("\"someone_here@work.com.au\" <someone_here@work.com.au>"));
594 
595         email.setCc(testEmailValid2);
596         assertEquals(testEmailValid2, email.getCcAddresses());
597     }
598 
599     @Test(expected = EmailException.class)
600     public void testSetCcNull() throws Exception
601     {
602         email.setCc(null);
603     }
604 
605     @Test(expected = EmailException.class)
606     public void testSetCcEmpty() throws Exception
607     {
608         email.setCc(Collections.<InternetAddress>emptyList());
609     }
610 
611     @Test
612     public void testAddBcc() throws Exception
613     {
614         // ====================================================================
615         // Test Success
616         // ====================================================================
617 
618         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
619         arrExpected.add(new InternetAddress("me@home.com"));
620         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
621         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
622 
623         for (final String address : VALID_EMAILS) {
624             email.addBcc(address);
625         }
626 
627         // retrieve and verify
628         assertEquals(arrExpected.size(), email.getBccAddresses().size());
629         assertEquals(
630             arrExpected.toString(),
631             email.getBccAddresses().toString());
632     }
633 
634     @Test
635     public void testAddBccArray() throws Exception
636     {
637         // ====================================================================
638         // Test Success
639         // ====================================================================
640 
641         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
642         arrExpected.add(new InternetAddress("me@home.com"));
643         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
644         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
645 
646         // add a valid bcc
647         email.addBcc(VALID_EMAILS);
648 
649         // retrieve and verify
650         assertEquals(arrExpected.size(), email.getBccAddresses().size());
651         assertEquals(
652             arrExpected.toString(),
653             email.getBccAddresses().toString());
654     }
655 
656     @Test
657     public void testAddBccWithEncoding() throws Exception
658     {
659         // ====================================================================
660         // Test Success
661         // ====================================================================
662         final String testCharset = EmailConstants.ISO_8859_1;
663         final String[] testEmailNames = {"Name1", "", null};
664 
665         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
666         arrExpected.add(new InternetAddress("me@home.com", "Name1", testCharset));
667         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
668         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
669 
670         for (int i = 0; i < VALID_EMAILS.length; i++)
671         {
672             // set bccs
673             email.addBcc(VALID_EMAILS[i], testEmailNames[i], testCharset);
674         }
675 
676         // retrieve and verify
677         assertEquals(arrExpected.size(), email.getBccAddresses().size());
678         assertEquals(
679             arrExpected.toString(),
680             email.getBccAddresses().toString());
681     }
682 
683     @Test
684     public void testAddBcc2() throws Exception
685     {
686         // ====================================================================
687         // Test Success
688         // ====================================================================
689 
690         final String[] testEmailNames = {"Name1", "", null};
691 
692 
693         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
694         arrExpected.add(new InternetAddress("me@home.com", "Name1"));
695         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
696         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
697 
698         for (int i = 0; i < VALID_EMAILS.length; i++)
699         {
700             // set from
701             email.addBcc(VALID_EMAILS[i], testEmailNames[i]);
702         }
703 
704         // retrieve and verify
705         assertEquals(arrExpected.size(), email.getBccAddresses().size());
706         assertEquals(
707             arrExpected.toString(),
708             email.getBccAddresses().toString());
709     }
710 
711     @Test(expected = IllegalCharsetNameException.class)
712     public void testAddBccBadEncoding() throws Exception
713     {
714         email.addBcc("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
715     }
716 
717     @Test
718     public void testSetBcc() throws Exception
719     {
720         // ====================================================================
721         // Test Success
722         // ====================================================================
723         final List<InternetAddress> testInetEmailValid = new ArrayList<InternetAddress>();
724         testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
725         testInetEmailValid.add(
726             new InternetAddress(
727                 "joe.doe@apache.org",
728                 "joe.doe@apache.org"));
729         testInetEmailValid.add(
730             new InternetAddress(
731                 "someone_here@work-address.com.au",
732                 "someone_here@work-address.com.au"));
733 
734         email.setBcc(testInetEmailValid);
735         assertEquals(testInetEmailValid, email.getBccAddresses());
736     }
737 
738     @Test(expected = EmailException.class)
739     public void testSetBccNull() throws Exception
740     {
741         email.setBcc(null);
742     }
743 
744     @Test(expected = EmailException.class)
745     public void testSetBccEmpty() throws Exception
746     {
747         email.setBcc(Collections.<InternetAddress>emptyList());
748     }
749 
750     @Test
751     public void testAddReplyTo() throws Exception
752     {
753         // ====================================================================
754         // Test Success
755         // ====================================================================
756 
757         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
758         arrExpected.add(new InternetAddress("me@home.com"));
759         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
760         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
761 
762         for (final String address : VALID_EMAILS) {
763             email.addReplyTo(address);
764         }
765 
766         // retrieve and verify
767         assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
768         assertEquals(
769             arrExpected.toString(),
770             email.getReplyToAddresses().toString());
771     }
772 
773     @Test
774     public void testAddReplyToWithEncoding() throws Exception
775     {
776         // ====================================================================
777         // Test Success
778         // ====================================================================
779         final String testCharset = EmailConstants.ISO_8859_1;
780         final String[] testEmailNames = {"Name1", "", null};
781 
782         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
783         arrExpected.add(new InternetAddress("me@home.com", "Name1", testCharset));
784         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
785         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
786 
787         for (int i = 0; i < VALID_EMAILS.length; i++)
788         {
789             // set replyTo
790             email.addReplyTo(VALID_EMAILS[i], testEmailNames[i], testCharset);
791         }
792 
793         // retrieve and verify
794         assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
795         assertEquals(
796             arrExpected.toString(),
797             email.getReplyToAddresses().toString());
798     }
799 
800     @Test
801     public void testAddReplyTo2() throws Exception
802     {
803         // ====================================================================
804         // Test Success
805         // ====================================================================
806 
807         final String[] testEmailNames = {"Name1", "", null};
808 
809         final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
810         arrExpected.add(new InternetAddress("me@home.com", "Name1"));
811         arrExpected.add(new InternetAddress("joe.doe@apache.org"));
812         arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
813 
814         for (int i = 0; i < VALID_EMAILS.length; i++)
815         {
816             // set replyTo
817             email.addReplyTo(VALID_EMAILS[i], testEmailNames[i]);
818         }
819 
820         // retrieve and verify
821         assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
822         assertEquals(
823             arrExpected.toString(),
824             email.getReplyToAddresses().toString());
825     }
826 
827     @Test(expected = IllegalCharsetNameException.class)
828     public void testAddReplyToBadEncoding() throws Exception
829     {
830         email.addReplyTo("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
831     }
832 
833     @Test
834     public void testAddHeader()
835     {
836         // ====================================================================
837         // Test Success
838         // ====================================================================
839         final Map<String, String> headers = new HashMap<String, String>();
840         headers.put("X-Priority", "1");
841         headers.put("Disposition-Notification-To", "me@home.com");
842         headers.put("X-Mailer", "Sendmail");
843 
844         for (final Map.Entry<String, String> header : headers.entrySet()) {
845             final String name = header.getKey();
846             final String value = header.getValue();
847             email.addHeader(name, value);
848         }
849 
850         assertEquals(headers.size(), email.getHeaders().size());
851         assertEquals(headers, email.getHeaders());
852     }
853 
854     @Test(expected = IllegalArgumentException.class)
855     public void testAddHeaderEmptyName() throws Exception
856     {
857         email.addHeader("", "me@home.com");
858     }
859 
860     @Test(expected = IllegalArgumentException.class)
861     public void testAddHeaderNullName() throws Exception
862     {
863         email.addHeader(null, "me@home.com");
864     }
865 
866     @Test(expected = IllegalArgumentException.class)
867     public void testAddHeaderEmptyValue() throws Exception
868     {
869         email.addHeader("X-Mailer", "");
870     }
871 
872     @Test(expected = IllegalArgumentException.class)
873     public void testAddHeaderNullValue() throws Exception
874     {
875         email.addHeader("X-Mailer", null);
876     }
877 
878     @Test
879     public void testSetHeaders()
880     {
881         final Map<String, String> ht = new Hashtable<String, String>();
882         ht.put("X-Priority", "1");
883         ht.put("Disposition-Notification-To", "me@home.com");
884         ht.put("X-Mailer", "Sendmail");
885 
886         email.setHeaders(ht);
887 
888         assertEquals(ht.size(), email.getHeaders().size());
889         assertEquals(ht, email.getHeaders());
890     }
891 
892     @Test
893     public void testFoldingHeaders() throws Exception
894     {
895         email.setHostName(strTestMailServer);
896         email.setSmtpPort(getMailServerPort());
897         email.setFrom("a@b.com");
898         email.addTo("c@d.com");
899         email.setSubject("test mail");
900 
901         final String headerValue = "1234567890 1234567890 123456789 01234567890 123456789 0123456789 01234567890 01234567890";
902         email.addHeader("X-LongHeader", headerValue);
903         
904         assertTrue(email.getHeaders().size() == 1);
905         // the header should not yet be folded -> will be done by buildMimeMessage()
906         assertFalse(email.getHeaders().get("X-LongHeader").contains("\r\n"));
907         
908         email.buildMimeMessage();
909 
910         final MimeMessage msg = email.getMimeMessage();
911         msg.saveChanges();
912         
913         final String[] values = msg.getHeader("X-LongHeader");
914         assertEquals(1, values.length);
915         
916         // the header should be split in two lines
917         final String[] lines = values[0].split("\\r\\n");
918         assertEquals(2, lines.length);
919         
920         // there should only be one line-break
921         assertTrue(values[0].indexOf("\n") == values[0].lastIndexOf("\n"));
922     }
923 
924     @Test(expected = IllegalArgumentException.class)
925     public void testSetHeaderEmptyValue() throws Exception
926     {
927         email.setHeaders(Collections.singletonMap("X-Mailer", ""));
928     }
929 
930     @Test(expected = IllegalArgumentException.class)
931     public void testSetHeaderNullValue() throws Exception
932     {
933         email.setHeaders(Collections.singletonMap("X-Mailer", (String) null));
934     }
935 
936     @Test(expected = IllegalArgumentException.class)
937     public void testSetHeaderEmptyName() throws Exception
938     {
939         email.setHeaders(Collections.singletonMap("", "me@home.com"));
940     }
941 
942     @Test(expected = IllegalArgumentException.class)
943     public void testSetHeaderNullName() throws Exception
944     {
945         email.setHeaders(Collections.singletonMap((String) null, "me@home.com"));
946     }
947 
948     @Test
949     public void testSetSubject()
950     {
951         for (final String validChar : testCharsValid) {
952             email.setSubject(validChar);
953             assertEquals(validChar, email.getSubject());
954         }
955     }
956 
957     @Test(expected = EmailException.class)
958     public void testSendNoHostName() throws Exception
959     {
960         getMailServer();
961 
962         email = new MockEmailConcrete();
963         email.send();
964     }
965 
966     @Test
967     public void testSendBadHostName()
968     {
969         try
970         {
971             getMailServer();
972 
973             email = new MockEmailConcrete();
974             email.setSubject("Test Email #1 Subject");
975             email.setHostName("bad.host.com");
976             email.setFrom("me@home.com");
977             email.addTo("me@home.com");
978             email.addCc("me@home.com");
979             email.addBcc("me@home.com");
980             email.addReplyTo("me@home.com");
981 
982             email.setContent(
983                     "test string object",
984                     " ; charset=" + EmailConstants.US_ASCII);
985 
986             email.send();
987             fail("Should have thrown an exception");
988         }
989         catch (final EmailException e)
990         {
991             assertTrue(e.getCause() instanceof ParseException);
992             fakeMailServer.stop();
993         }
994     }
995 
996     @Test(expected = EmailException.class)
997     public void testSendFromNotSet() throws Exception
998     {
999          getMailServer();
1000 
1001          email = new MockEmailConcrete();
1002          email.setHostName(strTestMailServer);
1003          email.setSmtpPort(getMailServerPort());
1004          email.addTo("me@home.com");
1005 
1006          email.send();
1007     }
1008 
1009     @Test
1010     public void testSendFromSetInSession() throws Exception
1011     {
1012          getMailServer();
1013 
1014          email = new MockEmailConcrete();
1015          email.setHostName(strTestMailServer);
1016          email.setSmtpPort(getMailServerPort());
1017          email.addTo("me@home.com");
1018          email.getSession().getProperties().setProperty(EmailConstants.MAIL_FROM, "me@home.com");
1019 
1020          email.send();
1021     }
1022 
1023     @Test(expected = EmailException.class)
1024     public void testSendDestinationNotSet() throws Exception
1025     {
1026         getMailServer();
1027 
1028         email = new MockEmailConcrete();
1029         email.setHostName(strTestMailServer);
1030         email.setSmtpPort(getMailServerPort());
1031         email.setFrom("me@home.com");
1032 
1033         email.send();
1034     }
1035 
1036     @Test(expected = EmailException.class)
1037     public void testSendBadAuthSet() throws Exception
1038     {
1039         getMailServer();
1040 
1041         email = new MockEmailConcrete();
1042         email.setHostName(strTestMailServer);
1043         email.setSmtpPort(getMailServerPort());
1044         email.setFrom(strTestMailFrom);
1045         email.addTo(strTestMailTo);
1046         email.setAuthentication(null, null);
1047 
1048         email.send();
1049     }
1050 
1051     @Test
1052     public void testSendCorrectSmtpPortContainedInException()
1053     {
1054         try
1055         {
1056             getMailServer();
1057 
1058             email = new MockEmailConcrete();
1059             email.setHostName("bad.host.com");
1060             email.setSSLOnConnect(true);
1061             email.setFrom(strTestMailFrom);
1062             email.addTo(strTestMailTo);
1063             email.setAuthentication(null, null);
1064             email.send();
1065             fail("Should have thrown an exception");
1066         }
1067         catch (final EmailException e)
1068         {
1069             assertTrue(e.getMessage().contains("bad.host.com:465"));
1070             fakeMailServer.stop();
1071         }
1072     }
1073 
1074     @Test
1075     public void testGetSetSentDate()
1076     {
1077         // with input date
1078 
1079         final Date dtTest = Calendar.getInstance().getTime();
1080         email.setSentDate(dtTest);
1081         assertEquals(dtTest, email.getSentDate());
1082 
1083         // with null input (this is a fudge :D)
1084         email.setSentDate(null);
1085 
1086         final Date sentDate = email.getSentDate();
1087 
1088         // Date objects are millisecond specific. If you have a slow processor,
1089         // time passes between the generation of dtTest and the new Date() in
1090         // getSentDate() and this test fails. Make sure that the difference
1091         // is less than a second...
1092         assertTrue(Math.abs(sentDate.getTime() - dtTest.getTime()) < 1000);
1093     }
1094 
1095     @Test
1096     public void testToInternetAddressArray() throws Exception
1097     {
1098         final List<InternetAddress> testInetEmailValid = new ArrayList<InternetAddress>();
1099 
1100         testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
1101         testInetEmailValid.add(
1102                 new InternetAddress(
1103                         "joe.doe@apache.org",
1104                         "joe.doe@apache.org"));
1105         testInetEmailValid.add(
1106                 new InternetAddress(
1107                         "someone_here@work-address.com.au",
1108                         "someone_here@work-address.com.au"));
1109 
1110         email.setBcc(testInetEmailValid);
1111         assertEquals(
1112                 testInetEmailValid.size(),
1113                 email.getBccAddresses().size());
1114     }
1115 
1116     @Test
1117     public void testSetPopBeforeSmtp()
1118     {
1119         // simple test (can be improved)
1120         final boolean boolPopBeforeSmtp = true;
1121         final String strHost = "mail.home.com";
1122         final String strUsername = "user.name";
1123         final String strPassword = "user.passwd";
1124 
1125         email.setPopBeforeSmtp(
1126             boolPopBeforeSmtp,
1127             strHost,
1128             strUsername,
1129             strPassword);
1130 
1131         // retrieve and verify
1132         assertEquals(boolPopBeforeSmtp, email.isPopBeforeSmtp());
1133         assertEquals(strHost, email.getPopHost());
1134         assertEquals(strUsername, email.getPopUsername());
1135         assertEquals(strPassword, email.getPopPassword());
1136     }
1137 
1138     /**
1139      * Test: When Email.setCharset() is called, a subsequent setContent()
1140      * should use that charset for text content types unless overridden
1141      * by the contentType parameter.
1142      * See https://issues.apache.org/jira/browse/EMAIL-1.
1143      *
1144      *
1145      * Case 1:
1146      * Setting a default charset results in adding that charset info to
1147      * to the content type of a text/based content object.
1148      * @throws Exception on any error
1149      */
1150     @Test
1151     public void testDefaultCharsetAppliesToTextContent() throws Exception
1152     {
1153         email.setHostName(strTestMailServer);
1154         email.setSmtpPort(getMailServerPort());
1155         email.setFrom("a@b.com");
1156         email.addTo("c@d.com");
1157         email.setSubject("test mail");
1158 
1159         email.setCharset("ISO-8859-1");
1160         email.setContent("test content", "text/plain");
1161         email.buildMimeMessage();
1162         final MimeMessage msg = email.getMimeMessage();
1163         msg.saveChanges();
1164         assertEquals("text/plain; charset=ISO-8859-1", msg.getContentType());
1165     }
1166 
1167     /**
1168      * Case 2:
1169      * A default charset is overridden by an explicitly specified
1170      * charset in setContent().
1171      * @throws Exception on any error
1172      */
1173     @Test
1174     public void testDefaultCharsetCanBeOverriddenByContentType()
1175         throws Exception
1176     {
1177         email.setHostName(strTestMailServer);
1178         email.setSmtpPort(getMailServerPort());
1179         email.setFrom("a@b.com");
1180         email.addTo("c@d.com");
1181         email.setSubject("test mail");
1182 
1183         email.setCharset("ISO-8859-1");
1184         email.setContent("test content", "text/plain; charset=US-ASCII");
1185         email.buildMimeMessage();
1186         final MimeMessage msg = email.getMimeMessage();
1187         msg.saveChanges();
1188         assertEquals("text/plain; charset=US-ASCII", msg.getContentType());
1189     }
1190 
1191     /**
1192      * Case 3:
1193      * A non-text content object ignores a default charset entirely.
1194      * @throws Exception on any error
1195      */
1196     @Test
1197     public void testDefaultCharsetIgnoredByNonTextContent()
1198         throws Exception
1199     {
1200         email.setHostName(strTestMailServer);
1201         email.setSmtpPort(getMailServerPort());
1202         email.setFrom("a@b.com");
1203         email.addTo("c@d.com");
1204         email.setSubject("test mail");
1205 
1206         email.setCharset("ISO-8859-1");
1207         email.setContent("test content", "application/octet-stream");
1208         email.buildMimeMessage();
1209         final MimeMessage msg = email.getMimeMessage();
1210         msg.saveChanges();
1211         assertEquals("application/octet-stream", msg.getContentType());
1212     }
1213 
1214     @Test
1215     public void testCorrectContentTypeForPNG() throws Exception
1216     {
1217         email.setHostName(strTestMailServer);
1218         email.setSmtpPort(getMailServerPort());
1219         email.setFrom("a@b.com");
1220         email.addTo("c@d.com");
1221         email.setSubject("test mail");
1222 
1223         email.setCharset("ISO-8859-1");
1224         final File png = new File("./target/test-classes/images/logos/maven-feather.png");
1225         email.setContent(png, "image/png");
1226         email.buildMimeMessage();
1227         final MimeMessage msg = email.getMimeMessage();
1228         msg.saveChanges();
1229         assertEquals("image/png", msg.getContentType());
1230     }
1231 
1232     @Test
1233     public void testGetSetBounceAddress()
1234     {
1235         assertNull(email.getBounceAddress());
1236 
1237         final String bounceAddress = "test_bounce@apache.org";
1238         email.setBounceAddress(bounceAddress);
1239 
1240         // tests
1241         assertEquals(bounceAddress, email.getBounceAddress());        
1242     }
1243 
1244 }