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.mail2.jakarta;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.util.stream.IntStream;
22  
23  import org.apache.commons.mail2.core.EmailException;
24  import org.apache.commons.mail2.jakarta.mocks.MockEmailConcrete;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * JUnit test case for invalid Addresses in Email Class
30   */
31  public class InvalidAddressTest extends AbstractEmailTest {
32  
33      // @formatter:off
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              // "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      // @formatter:on
67  
68      private MockEmailConcrete email;
69  
70      @BeforeEach
71      public void setUpInvalidAddressTest() {
72          // reusable objects to be used across multiple tests
73          email = new MockEmailConcrete();
74      }
75  
76      @Test
77      public void testAddInvalidBcc() throws Exception {
78          // Test adding invalid 'BCC' addresses
79          // @formatter:off
80          IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> assertThrows(EmailException.class,
81                  () -> email.addBcc(ARR_INVALID_EMAILS[i], "Joe"),
82                  () -> "addBcc " + i + " passed: " + ARR_INVALID_EMAILS[i]));
83          // @formatter:on
84      }
85  
86      @Test
87      public void testAddInvalidCc() throws Exception {
88          // Test adding invalid 'CC' addresses
89          // @formatter:off
90          IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> assertThrows(EmailException.class,
91                  () -> email.addCc(ARR_INVALID_EMAILS[i], "Joe"),
92                  () -> "addCc " + i + " passed: " + ARR_INVALID_EMAILS[i]));
93          // @formatter:on
94      }
95  
96      @Test
97      public void testAddInvalidTo() throws Exception {
98          // Test adding invalid 'to' addresses
99          // @formatter:off
100         IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> assertThrows(EmailException.class,
101                 () -> email.addTo(ARR_INVALID_EMAILS[i], "Joe"),
102                 () -> "addTo " + i + " passed: " + ARR_INVALID_EMAILS[i]));
103         // @formatter:on
104     }
105 
106     @Test
107     public void testSetInvalidFrom() throws Exception {
108         // Test setting invalid 'from' addresses
109         // @formatter:off
110         IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> assertThrows(EmailException.class,
111                 () -> email.setFrom(ARR_INVALID_EMAILS[i], "Joe"),
112                 () -> "setFrom " + i + " passed: " + ARR_INVALID_EMAILS[i]));
113         // @formatter:on
114     }
115 }