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.fail;
20  
21  import org.apache.commons.mail.mocks.MockEmailConcrete;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  /**
26   * JUnit test case for invalid Addresses in Email Class
27   *
28   * @since 1.0
29   * @version $Id: InvalidAddressTest.java 1460036 2013-03-22 22:38:21Z tn $
30   */
31  public class InvalidAddressTest extends AbstractEmailTest
32  {
33      /** */
34      private static final String [] ARR_INVALID_EMAILS = {
35          "local name@domain.com",
36          "local(name@domain.com",
37          "local)name@domain.com",
38          "local<name@domain.com",
39          "local>name@domain.com",
40          "local,name@domain.com",
41          "local;name@domain.com",
42          "local:name@domain.com",
43          "local[name@domain.com",
44          "local]name@domain.com",
45          // "local\\name@domain.com", is considered valid for mail-1.4.1
46          "local\"name@domain.com",
47          "local\tname@domain.com",
48          "local\nname@domain.com",
49          "local\rname@domain.com",
50          "local.name@domain com",
51          "local.name@domain(com",
52          "local.name@domain)com",
53          "local.name@domain<com",
54          "local.name@domain>com",
55          "local.name@domain,com",
56          "local.name@domain;com",
57          "local.name@domain:com",
58  
59          //      "local.name@domain[com",
60          "local.name@domain]com",
61          "local.name@domain\\com",
62          "local.name@domain\tcom",
63          "local.name@domain\ncom",
64          "local.name@domain\rcom",
65          "local.name@",
66          "@domain.com"
67      };
68  
69      private MockEmailConcrete email;
70  
71      @Before
72      public void setUpInvalidAddressTest()
73      {
74          // reusable objects to be used across multiple tests
75          this.email = new MockEmailConcrete();
76      }
77  
78      @Test
79      public void testSetInvalidFrom()
80              throws Exception
81      {
82          // ====================================================================
83          // Test setting invalid 'from' addresses
84          // ====================================================================
85          for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
86          {
87              try
88              {
89                  // set from
90                  email.setFrom(ARR_INVALID_EMAILS[i]);
91  
92                  // Expected an exception to be thrown
93                  fail("setFrom " + i + " passed: " + ARR_INVALID_EMAILS[i]);
94              }
95              catch (EmailException ignore)
96              {
97                  // Expected Result
98              }
99          }
100     }
101 
102     @Test
103     public void testAddInvalidTo()
104             throws Exception
105     {
106         // ====================================================================
107         // Test adding invalid 'to' addresses
108         // ====================================================================
109         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
110         {
111             try
112             {
113                 // Add To
114                 email.addTo(ARR_INVALID_EMAILS[i], "Joe");
115 
116                 // Expected an exception to be thrown
117                 fail("addTo " + i + " passed: " + ARR_INVALID_EMAILS[i]);
118             }
119             catch (EmailException ignore)
120             {
121                 // Expected Result
122             }
123         }
124     }
125 
126     @Test
127     public void testAddInvalidCc()
128             throws Exception
129     {
130         // ====================================================================
131         // Test adding invalid 'cc' addresses
132         // ====================================================================
133         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
134         {
135             try
136             {
137                 // add cc
138                 email.addCc(ARR_INVALID_EMAILS[i], "Joe");
139 
140                 // Expected an exception to be thrown
141                 fail("addCc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
142             }
143             catch (EmailException ignore)
144             {
145                 // Expected Result
146             }
147         }
148     }
149 
150     @Test
151     public void testAddInvalidBcc()
152             throws Exception
153     {
154         // ====================================================================
155         // Test adding invalid 'Bcc' addresses
156         // ====================================================================
157         for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
158         {
159             try
160             {
161                 // add bcc
162                 email.addBcc(ARR_INVALID_EMAILS[i], "Joe");
163 
164                 // Expected an exception to be thrown
165                 fail("addBcc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
166             }
167             catch (EmailException ignore)
168             {
169                 // Expected Result
170             }
171         }
172     }
173 }