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