View Javadoc

1   /*
2    * $Id: MailUtilsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.email;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.transport.email.MailUtils;
15  
16  import javax.mail.internet.InternetAddress;
17  
18  public class MailUtilsTestCase extends AbstractMuleTestCase
19  {
20      private static final String EMAIL_1 = "vasya@pupkin.com";
21      private static final String EMAIL_2 = "zhora@buryakov.com";
22      private InternetAddress inetAddress1;
23      private InternetAddress inetAddress2;
24      private static final String MULTIPLE_EMAILS_WITH_WHITESPACE = EMAIL_1 + ", " + EMAIL_2;
25      private static final String MULTIPLE_EMAILS_WITHOUT_WHITESPACE = EMAIL_1 + "," + EMAIL_2;
26  
27      protected void doSetUp() throws Exception
28      {
29          inetAddress1 = new InternetAddress(EMAIL_1);
30          inetAddress2 = new InternetAddress(EMAIL_2);
31      }
32  
33      public void testSingleInternetAddressToString() throws Exception
34      {
35          String result = MailUtils.internetAddressesToString(inetAddress1);
36          assertEquals("Wrong internet address conversion.", EMAIL_1, result);
37      }
38  
39      public void testMultipleInternetAddressesToString()
40      {
41          String result = MailUtils.internetAddressesToString(new InternetAddress[]{inetAddress1, inetAddress2});
42          assertEquals("Wrong internet address conversion.", MULTIPLE_EMAILS_WITH_WHITESPACE, result);
43      }
44  
45      public void testStringToSingleInternetAddresses() throws Exception
46      {
47          InternetAddress[] result = MailUtils.stringToInternetAddresses(EMAIL_1);
48          assertNotNull(result);
49          assertEquals("Wrong number of addresses parsed.", 1, result.length);
50          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
51      }
52  
53      public void testStringWithWhitespaceToMultipleInternetAddresses() throws Exception
54      {
55          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITH_WHITESPACE);
56          assertNotNull(result);
57          assertEquals("Wrong number of addresses parsed.", 2, result.length);
58          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
59          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
60      }
61  
62      public void testStringWithoutWhitespaceToMultipleInternetAddresses() throws Exception
63      {
64          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITHOUT_WHITESPACE);
65          assertNotNull(result);
66          assertEquals("Wrong number of addresses parsed.", 2, result.length);
67          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
68          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
69      }
70  
71  }