1   /*
2    * $Id: MailUtilsTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.email;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  
15  import javax.mail.internet.InternetAddress;
16  
17  public class MailUtilsTestCase extends AbstractMuleTestCase
18  {
19      private static final String EMAIL_1 = "vasya@pupkin.com";
20      private static final String EMAIL_2 = "zhora@buryakov.com";
21      private InternetAddress inetAddress1;
22      private InternetAddress inetAddress2;
23      private static final String MULTIPLE_EMAILS_WITH_WHITESPACE = EMAIL_1 + ", " + EMAIL_2;
24      private static final String MULTIPLE_EMAILS_WITHOUT_WHITESPACE = EMAIL_1 + "," + EMAIL_2;
25  
26      protected void doSetUp() throws Exception
27      {
28          inetAddress1 = new InternetAddress(EMAIL_1);
29          inetAddress2 = new InternetAddress(EMAIL_2);
30      }
31  
32      public void testSingleInternetAddressToString() throws Exception
33      {
34          String result = MailUtils.internetAddressesToString(inetAddress1);
35          assertEquals("Wrong internet address conversion.", EMAIL_1, result);
36      }
37  
38      public void testMultipleInternetAddressesToString()
39      {
40          String result = MailUtils.internetAddressesToString(new InternetAddress[]{inetAddress1, inetAddress2});
41          assertEquals("Wrong internet address conversion.", MULTIPLE_EMAILS_WITH_WHITESPACE, result);
42      }
43  
44      public void testStringToSingleInternetAddresses() throws Exception
45      {
46          InternetAddress[] result = MailUtils.stringToInternetAddresses(EMAIL_1);
47          assertNotNull(result);
48          assertEquals("Wrong number of addresses parsed.", 1, result.length);
49          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
50      }
51  
52      public void testStringWithWhitespaceToMultipleInternetAddresses() throws Exception
53      {
54          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITH_WHITESPACE);
55          assertNotNull(result);
56          assertEquals("Wrong number of addresses parsed.", 2, result.length);
57          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
58          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
59      }
60  
61      public void testStringWithoutWhitespaceToMultipleInternetAddresses() throws Exception
62      {
63          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITHOUT_WHITESPACE);
64          assertNotNull(result);
65          assertEquals("Wrong number of addresses parsed.", 2, result.length);
66          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
67          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
68      }
69  
70  }