View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.util.Map;
12  
13  import javax.mail.Part;
14  import javax.mail.internet.AddressException;
15  import javax.mail.internet.InternetAddress;
16  import javax.mail.internet.MimeBodyPart;
17  
18  import org.apache.commons.collections.map.HashedMap;
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  public class MailUtilsTestCase extends AbstractMuleTestCase
26  {
27  
28      private static final String EMAIL_1 = "vasya@pupkin.com";
29      private static final String EMAIL_2 = "zhora@buryakov.com";
30      private InternetAddress inetAddress1;
31      private InternetAddress inetAddress2;
32      private static final String MULTIPLE_EMAILS_WITH_WHITESPACE = EMAIL_1 + ", " + EMAIL_2;
33      private static final String MULTIPLE_EMAILS_WITHOUT_WHITESPACE = EMAIL_1 + "," + EMAIL_2;
34  
35      @Before
36      public void createInternetAddresses() throws AddressException
37      {
38          inetAddress1 = new InternetAddress(EMAIL_1);
39          inetAddress2 = new InternetAddress(EMAIL_2);
40      }
41  
42      @Test
43      public void testSingleInternetAddressToString() throws Exception
44      {
45          String result = MailUtils.internetAddressesToString(inetAddress1);
46          assertEquals("Wrong internet address conversion.", EMAIL_1, result);
47      }
48  
49      @Test
50      public void testMultipleInternetAddressesToString()
51      {
52          String result = MailUtils.internetAddressesToString(new InternetAddress[]{inetAddress1, inetAddress2});
53          assertEquals("Wrong internet address conversion.", MULTIPLE_EMAILS_WITH_WHITESPACE, result);
54      }
55  
56      @Test
57      public void testStringToSingleInternetAddresses() throws Exception
58      {
59          InternetAddress[] result = MailUtils.stringToInternetAddresses(EMAIL_1);
60          assertNotNull(result);
61          assertEquals("Wrong number of addresses parsed.", 1, result.length);
62          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
63      }
64  
65      @Test
66      public void testStringWithWhitespaceToMultipleInternetAddresses() throws Exception
67      {
68          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITH_WHITESPACE);
69          assertNotNull(result);
70          assertEquals("Wrong number of addresses parsed.", 2, result.length);
71          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
72          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
73      }
74  
75      @Test
76      public void testStringWithoutWhitespaceToMultipleInternetAddresses() throws Exception
77      {
78          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITHOUT_WHITESPACE);
79          assertNotNull(result);
80          assertEquals("Wrong number of addresses parsed.", 2, result.length);
81          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
82          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
83      }
84  
85      @Test
86      public void testGetAttachmentName() throws Exception
87      {
88          @SuppressWarnings("unchecked")
89          Map<String, Part> attachments = new HashedMap();
90  
91          String key = "test.txt";
92          assertEquals(key, MailUtils.getAttachmentName(key, attachments));
93  
94          attachments.put(key, new MimeBodyPart());
95          assertEquals("0_" + key, MailUtils.getAttachmentName(key, attachments));
96      }
97  }