001: /*
002: * Copyright 2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.mail;
017:
018: import java.lang.reflect.Method;
019:
020: import javax.mail.internet.InternetAddress;
021:
022: /**
023: * JUnit test case demonstrating InternetAddress validation.
024: *
025: * @since 1.0
026: * @author Niall Pemberton
027: * @version $Id: InvalidInternetAddressTest.java 279295 2005-09-07 10:56:41Z henning $
028: */
029:
030: public class InvalidInternetAddressTest extends BaseEmailTestCase {
031:
032: /** */
033: private static final String VALID_QUOTED_EMAIL = "\"John O'Groats\"@domain.com";
034:
035: /** JavaMail 1.2. does not know about this */
036: private static Method validateMethod = null;
037:
038: /** */
039: private static final String[] ARR_INVALID_EMAILS = {
040: "local name@domain.com", "local(name@domain.com",
041: "local)name@domain.com", "local<name@domain.com",
042: "local>name@domain.com", "local,name@domain.com",
043: "local;name@domain.com", "local:name@domain.com",
044: "local[name@domain.com", "local]name@domain.com",
045: "local\\name@domain.com", "local\"name@domain.com",
046: "local\tname@domain.com", "local\nname@domain.com",
047: "local\rname@domain.com", "local.name@domain com",
048: "local.name@domain(com", "local.name@domain)com",
049: "local.name@domain<com", "local.name@domain>com",
050: "local.name@domain,com", "local.name@domain;com",
051: "local.name@domain:com", "local.name@domain[com",
052: "local.name@domain]com", "local.name@domain\\com",
053: "local.name@domain\tcom", "local.name@domain\ncom",
054: "local.name@domain\rcom", "local.name@", "@domain.com" };
055:
056: /**
057: * @param name name
058: */
059: public InvalidInternetAddressTest(String name) {
060: super (name);
061: }
062:
063: protected void setUp() {
064: super .setUp();
065:
066: try {
067: validateMethod = InternetAddress.class.getMethod(
068: "validate", new Class[0]);
069: } catch (Exception e) {
070: assertEquals(
071: "Got wrong Exception when looking for validate()",
072: NoSuchMethodException.class, e.getClass());
073: }
074: }
075:
076: /**
077: *
078: * @throws Exception Exception
079: */
080: public void testStrictConstructor() throws Exception {
081: // ====================================================================
082: // Prove InternetAddress constructor is throwing exception.
083: // ====================================================================
084:
085: // test Invalid Email addresses
086: for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
087:
088: try {
089:
090: // Create Internet Address using "strict" constructor
091: new InternetAddress(ARR_INVALID_EMAILS[i]);
092:
093: // Expected an exception to be thrown
094: fail("Strict " + i + " passed: "
095: + ARR_INVALID_EMAILS[i]);
096:
097: } catch (Exception ex) {
098: // Expected Result
099: }
100:
101: }
102:
103: // test valid 'quoted' Email addresses
104: try {
105:
106: // Create Internet Address using "strict" constructor
107: new InternetAddress(VALID_QUOTED_EMAIL);
108:
109: } catch (Exception ex) {
110: fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
111: + " - " + ex.getMessage());
112: }
113: }
114:
115: /**
116: *
117: * @throws Exception Exception
118: */
119: public void testValidateMethod() throws Exception {
120: if (validateMethod == null) {
121: return;
122: }
123:
124: // ====================================================================
125: // Prove InternetAddress constructor isn't throwing exception and
126: // the validate() method is
127: // ====================================================================
128:
129: for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
130:
131: InternetAddress address = new InternetAddress(
132: ARR_INVALID_EMAILS[i], "Joe");
133:
134: // N.B. validate() doesn't check addresses containing quotes or '['
135: boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0);
136: int atIndex = ARR_INVALID_EMAILS[i].indexOf("@");
137: boolean domainBracket = (atIndex >= 0)
138: && (ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0);
139: try {
140:
141: validateMethod.invoke(address, null);
142:
143: if (!(quoted || domainBracket)) {
144: fail("Validate " + i + " passed: "
145: + ARR_INVALID_EMAILS[i]);
146: }
147:
148: } catch (Exception ex) {
149:
150: if (quoted || domainBracket) {
151: fail("Validate " + i + " failed: "
152: + ARR_INVALID_EMAILS[i] + " - "
153: + ex.getMessage());
154: }
155:
156: }
157:
158: }
159:
160: // test valid 'quoted' Email addresses
161: try {
162:
163: validateMethod.invoke(new InternetAddress(
164: VALID_QUOTED_EMAIL, "Joe"), null);
165:
166: } catch (Exception ex) {
167: fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
168: + " - " + ex.getMessage());
169: }
170:
171: }
172:
173: /**
174: *
175: * @throws Exception Exception
176: */
177: public void testValidateMethodCharset() throws Exception {
178: if (validateMethod == null) {
179: return;
180: }
181:
182: // ====================================================================
183: // Prove InternetAddress constructor isn't throwing exception and
184: // the validate() method is
185: // ====================================================================
186:
187: for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
188:
189: InternetAddress address = new InternetAddress(
190: ARR_INVALID_EMAILS[i], "Joe", "UTF-8");
191:
192: // N.B. validate() doesn't check addresses containing quotes or '['
193: boolean quoted = (ARR_INVALID_EMAILS[i].indexOf("\"") >= 0);
194: int atIndex = ARR_INVALID_EMAILS[i].indexOf("@");
195: boolean domainBracket = (atIndex >= 0)
196: && (ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0);
197:
198: try {
199:
200: validateMethod.invoke(address, null);
201:
202: if (!(quoted || domainBracket)) {
203: fail("Validate " + i + " passed: "
204: + ARR_INVALID_EMAILS[i]);
205: }
206:
207: } catch (Exception ex) {
208:
209: if (quoted || domainBracket) {
210: fail("Validate " + i + " failed: "
211: + ARR_INVALID_EMAILS[i] + " - "
212: + ex.getMessage());
213: }
214:
215: }
216:
217: }
218:
219: // test valid 'quoted' Email addresses
220: try {
221:
222: validateMethod.invoke(new InternetAddress(
223: VALID_QUOTED_EMAIL, "Joe", "UTF-8"), null);
224:
225: } catch (Exception ex) {
226: fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL
227: + " - " + ex.getMessage());
228: }
229: }
230:
231: }
|