View Javadoc

1   /*
2    * $Id: MailUtilsTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.junit4.AbstractMuleTestCase;
14  
15  import java.util.Map;
16  
17  import javax.mail.Part;
18  import javax.mail.internet.AddressException;
19  import javax.mail.internet.InternetAddress;
20  import javax.mail.internet.MimeBodyPart;
21  
22  import org.apache.commons.collections.map.HashedMap;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  
29  public class MailUtilsTestCase extends AbstractMuleTestCase
30  {
31  
32      private static final String EMAIL_1 = "vasya@pupkin.com";
33      private static final String EMAIL_2 = "zhora@buryakov.com";
34      private InternetAddress inetAddress1;
35      private InternetAddress inetAddress2;
36      private static final String MULTIPLE_EMAILS_WITH_WHITESPACE = EMAIL_1 + ", " + EMAIL_2;
37      private static final String MULTIPLE_EMAILS_WITHOUT_WHITESPACE = EMAIL_1 + "," + EMAIL_2;
38  
39      @Before
40      public void createInternetAddresses() throws AddressException
41      {
42          inetAddress1 = new InternetAddress(EMAIL_1);
43          inetAddress2 = new InternetAddress(EMAIL_2);
44      }
45  
46      @Test
47      public void testSingleInternetAddressToString() throws Exception
48      {
49          String result = MailUtils.internetAddressesToString(inetAddress1);
50          assertEquals("Wrong internet address conversion.", EMAIL_1, result);
51      }
52  
53      @Test
54      public void testMultipleInternetAddressesToString()
55      {
56          String result = MailUtils.internetAddressesToString(new InternetAddress[]{inetAddress1, inetAddress2});
57          assertEquals("Wrong internet address conversion.", MULTIPLE_EMAILS_WITH_WHITESPACE, result);
58      }
59  
60      @Test
61      public void testStringToSingleInternetAddresses() throws Exception
62      {
63          InternetAddress[] result = MailUtils.stringToInternetAddresses(EMAIL_1);
64          assertNotNull(result);
65          assertEquals("Wrong number of addresses parsed.", 1, result.length);
66          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
67      }
68  
69      @Test
70      public void testStringWithWhitespaceToMultipleInternetAddresses() throws Exception
71      {
72          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITH_WHITESPACE);
73          assertNotNull(result);
74          assertEquals("Wrong number of addresses parsed.", 2, result.length);
75          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
76          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
77      }
78  
79      @Test
80      public void testStringWithoutWhitespaceToMultipleInternetAddresses() throws Exception
81      {
82          InternetAddress[] result = MailUtils.stringToInternetAddresses(MULTIPLE_EMAILS_WITHOUT_WHITESPACE);
83          assertNotNull(result);
84          assertEquals("Wrong number of addresses parsed.", 2, result.length);
85          assertEquals("Wrong internet address conversion.", inetAddress1, result[0]);
86          assertEquals("Wrong internet address conversion.", inetAddress2, result[1]);
87      }
88  
89      @Test
90      public void testGetAttachmentName() throws Exception
91      {
92          @SuppressWarnings("unchecked")
93          Map<String, Part> attachments = new HashedMap();
94  
95          String key = "test.txt";
96          assertEquals(key, MailUtils.getAttachmentName(key, attachments));
97  
98          attachments.put(key, new MimeBodyPart());
99          assertEquals("0_" + key, MailUtils.getAttachmentName(key, attachments));
100     }
101 }