1 /* 2 * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/EmailTest.java,v 1.25 2004/03/30 02:42:06 dgraham Exp $ 3 * $Revision: 1.25 $ 4 * $Date: 2004/03/30 02:42:06 $ 5 * 6 * ==================================================================== 7 * Copyright 2001-2004 The Apache Software Foundation 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package org.apache.commons.validator; 23 24 import java.io.IOException; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 import org.xml.sax.SAXException; 30 31 /*** 32 * Performs Validation Test for e-mail validations. 33 */ 34 public class EmailTest extends TestCommon { 35 36 /*** 37 * The key used to retrieve the set of validation 38 * rules from the xml file. 39 */ 40 protected static String FORM_KEY = "emailForm"; 41 42 /*** 43 * The key used to retrieve the validator action. 44 */ 45 protected static String ACTION = "email"; 46 47 48 public EmailTest(String name) { 49 super(name); 50 } 51 52 /*** 53 * Start the tests. 54 * 55 * @param theArgs the arguments. Not used 56 */ 57 public static void main(String[] theArgs) { 58 junit.awtui.TestRunner.main(new String[] {EmailTest.class.getName()}); 59 } 60 61 /*** 62 * @return a test suite (<code>TestSuite</code>) that includes all methods 63 * starting with "test" 64 */ 65 public static Test suite() { 66 // All methods starting with "test" will be executed in the test suite. 67 return new TestSuite(EmailTest.class); 68 } 69 70 /*** 71 * Load <code>ValidatorResources</code> from 72 * validator-regexp.xml. 73 */ 74 protected void setUp() throws IOException, SAXException { 75 loadResources("validator-regexp.xml"); 76 } 77 78 protected void tearDown() { 79 } 80 81 /*** 82 * Tests the e-mail validation. 83 */ 84 public void testEmail() throws ValidatorException { 85 // Create bean to run test on. 86 ValueBean info = new ValueBean(); 87 88 info.setValue("jsmith@apache.org"); 89 valueTest(info, true); 90 } 91 92 /*** 93 * Tests the e-mail validation. 94 */ 95 public void testEmailExtension() throws ValidatorException { 96 // Create bean to run test on. 97 ValueBean info = new ValueBean(); 98 99 info.setValue("jsmith@apache.org"); 100 valueTest(info, true); 101 102 info.setValue("jsmith@apache.com"); 103 valueTest(info, true); 104 105 info.setValue("jsmith@apache.net"); 106 valueTest(info, true); 107 108 info.setValue("jsmith@apache.info"); 109 valueTest(info, true); 110 111 info.setValue("jsmith@apache.infoo"); 112 valueTest(info, false); 113 114 info.setValue("jsmith@apache."); 115 valueTest(info, false); 116 117 info.setValue("jsmith@apache.c"); 118 valueTest(info, false); 119 } 120 121 /*** 122 * <p>Tests the e-mail validation with a dash in 123 * the address.</p> 124 */ 125 public void testEmailWithDash() throws ValidatorException { 126 // Create bean to run test on. 127 ValueBean info = new ValueBean(); 128 129 info.setValue("andy.noble@data-workshop.com"); 130 valueTest(info, true); 131 132 info.setValue("andy-noble@data-workshop.-com"); 133 valueTest(info, true); 134 info.setValue("andy-noble@data-workshop.c-om"); 135 valueTest(info,true); 136 info.setValue("andy-noble@data-workshop.co-m"); 137 valueTest(info, true); 138 139 140 } 141 142 /*** 143 * <p>Tests the e-mail validation with a dot at the end of 144 * the address.</p> 145 */ 146 public void testEmailWithDotEnd() throws ValidatorException { 147 // Create bean to run test on. 148 ValueBean info = new ValueBean(); 149 150 info.setValue("andy.noble@data-workshop.com."); 151 valueTest(info, false); 152 153 } 154 155 /*** 156 * <p>Tests the e-mail validation with an RCS-noncompliant character in 157 * the address.</p> 158 */ 159 public void testEmailWithBogusCharacter() throws ValidatorException { 160 // Create bean to run test on. 161 ValueBean info = new ValueBean(); 162 163 info.setValue("andy.noble@\u008fdata-workshop.com"); 164 valueTest(info, false); 165 166 // The ' character is valid in an email address. 167 info.setValue("andy.o'reilly@data-workshop.com"); 168 valueTest(info, true); 169 170 info.setValue("foo+bar@i.am.not.in.us.example.com"); 171 valueTest(info, true); 172 } 173 174 /*** 175 * Tests the email validation with commas. 176 */ 177 public void testEmailWithCommas() throws ValidatorException { 178 ValueBean info = new ValueBean(); 179 info.setValue("joeblow@apa,che.org"); 180 valueTest(info, false); 181 info.setValue("joeblow@apache.o,rg"); 182 valueTest(info, false); 183 info.setValue("joeblow@apache,org"); 184 valueTest(info, false); 185 186 } 187 188 /*** 189 * Write this test according to parts of RFC, as opposed to the type of character 190 * that is being tested. 191 * @throws ValidatorException 192 * 193 * FIXME This test fails so disable it with a leading _ for 1.1.2 release. 194 * The real solution is to fix the email parsing. 195 */ 196 public void _testEmailUserName() throws ValidatorException { 197 ValueBean info = new ValueBean(); 198 info.setValue("joe1blow@apache.org"); 199 valueTest(info, true); 200 info.setValue("joe$blow@apache.org"); 201 valueTest(info, true); 202 info.setValue("joe-@apache.org"); 203 valueTest(info, true); 204 info.setValue("joe_@apache.org"); 205 valueTest(info, true); 206 207 //UnQuoted Special characters are invalid 208 209 info.setValue("joe.@apache.org"); 210 valueTest(info, false); 211 info.setValue("joe+@apache.org"); 212 valueTest(info, false); 213 info.setValue("joe!@apache.org"); 214 valueTest(info, false); 215 info.setValue("joe*@apache.org"); 216 valueTest(info, false); 217 info.setValue("joe'@apache.org"); 218 valueTest(info, false); 219 info.setValue("joe(@apache.org"); 220 valueTest(info, false); 221 info.setValue("joe)@apache.org"); 222 valueTest(info, false); 223 info.setValue("joe,@apache.org"); 224 valueTest(info, false); 225 info.setValue("joe%45@apache.org"); 226 valueTest(info, false); 227 info.setValue("joe;@apache.org"); 228 valueTest(info, false); 229 info.setValue("joe?@apache.org"); 230 valueTest(info, false); 231 info.setValue("joe&@apache.org"); 232 valueTest(info, false); 233 info.setValue("joe=@apache.org"); 234 valueTest(info, false); 235 236 //Quoted Special characters are valid 237 info.setValue("\"joe.\"@apache.org"); 238 valueTest(info, true); 239 info.setValue("\"joe+\"@apache.org"); 240 valueTest(info, true); 241 info.setValue("\"joe!\"@apache.org"); 242 valueTest(info, true); 243 info.setValue("\"joe*\"@apache.org"); 244 valueTest(info, true); 245 info.setValue("\"joe'\"@apache.org"); 246 valueTest(info, true); 247 info.setValue("\"joe(\"@apache.org"); 248 valueTest(info, true); 249 info.setValue("\"joe)\"@apache.org"); 250 valueTest(info, true); 251 info.setValue("\"joe,\"@apache.org"); 252 valueTest(info, true); 253 info.setValue("\"joe%45\"@apache.org"); 254 valueTest(info, true); 255 info.setValue("\"joe;\"@apache.org"); 256 valueTest(info, true); 257 info.setValue("\"joe?\"@apache.org"); 258 valueTest(info, true); 259 info.setValue("\"joe&\"@apache.org"); 260 valueTest(info, true); 261 info.setValue("\"joe=\"@apache.org"); 262 valueTest(info, true); 263 264 } 265 266 /*** 267 * These test values derive directly from RFC 822 & 268 * Mail::RFC822::Address & RFC::RFC822::Address perl test.pl 269 * For traceability don't combine these test values with other tests. 270 */ 271 TestPair[] testEmailFromPerl = { 272 new TestPair("abigail@example.com", true), 273 new TestPair("abigail@example.com ", true), 274 new TestPair(" abigail@example.com", true), 275 new TestPair("abigail @example.com ", true), 276 new TestPair("*@example.net", true), 277 new TestPair("\"//\"\"@foo.bar", true), 278 new TestPair("fred&barny@example.com", true), 279 new TestPair("---@example.com", true), 280 new TestPair("foo-bar@example.net", true), 281 new TestPair("\"127.0.0.1\"@[127.0.0.1]", true), 282 new TestPair("Abigail <abigail@example.com>", true), 283 new TestPair("Abigail<abigail@example.com>", true), 284 new TestPair("Abigail<@a,@b,@c:abigail@example.com>", true), 285 new TestPair("\"This is a phrase\"<abigail@example.com>", true), 286 new TestPair("\"Abigail \"<abigail@example.com>", true), 287 new TestPair("\"Joe & J. Harvey\" <example @Org>", true), 288 new TestPair("Abigail <abigail @ example.com>", true), 289 new TestPair("Abigail made this < abigail @ example . com >", true), 290 new TestPair("Abigail(the bitch)@example.com", true), 291 new TestPair("Abigail <abigail @ example . (bar) com >", true), 292 new TestPair("Abigail < (one) abigail (two) @(three)example . (bar) com (quz) >", true), 293 new TestPair("Abigail (foo) (((baz)(nested) (comment)) ! ) < (one) abigail (two) @(three)example . (bar) com (quz) >", true), 294 new TestPair("Abigail <abigail(fo//(o)@example.com>", true), 295 new TestPair("Abigail <abigail(fo//)o)@example.com> ", true), 296 new TestPair("(foo) abigail@example.com", true), 297 new TestPair("abigail@example.com (foo)", true), 298 new TestPair("\"Abi//\"gail\" <abigail@example.com>", true), 299 new TestPair("abigail@[example.com]", true), 300 new TestPair("abigail@[exa//[ple.com]", true), 301 new TestPair("abigail@[exa//]ple.com]", true), 302 new TestPair("\":sysmail\"@ Some-Group. Some-Org", true), 303 new TestPair("Muhammed.(I am the greatest) Ali @(the)Vegas.WBA", true), 304 new TestPair("mailbox.sub1.sub2@this-domain", true), 305 new TestPair("sub-net.mailbox@sub-domain.domain", true), 306 new TestPair("name:;", true), 307 new TestPair("':;", true), 308 new TestPair("name: ;", true), 309 new TestPair("Alfred Neuman <Neuman@BBN-TENEXA>", true), 310 new TestPair("Neuman@BBN-TENEXA", true), 311 new TestPair("\"George, Ted\" <Shared@Group.Arpanet>", true), 312 new TestPair("Wilt . (the Stilt) Chamberlain@NBA.US", true), 313 new TestPair("Cruisers: Port@Portugal, Jones@SEA;", true), 314 new TestPair("$@[]", true), 315 new TestPair("*()@[]", true), 316 new TestPair("\"quoted ( brackets\" ( a comment )@example.com", true), 317 new TestPair("\"Joe & J. Harvey\"//x0D//x0A <ddd//@ Org>", true), 318 new TestPair("\"Joe &//x0D//x0A J. Harvey\" <ddd //@ Org>", true), 319 new TestPair("Gourmets: Pompous Person <WhoZiWhatZit//@Cordon-Bleu>,//x0D//x0A" + 320 " Childs//@WGBH.Boston, \"Galloping Gourmet\"//@//x0D//x0A" + 321 " ANT.Down-Under (Australian National Television),//x0D//x0A" + 322 " Cheapie//@Discount-Liquors;", true), 323 new TestPair(" Just a string", false), 324 new TestPair("string", false), 325 new TestPair("(comment)", false), 326 new TestPair("()@example.com", false), 327 new TestPair("fred(&)barny@example.com", false), 328 new TestPair("fred// barny@example.com", false), 329 new TestPair("Abigail <abi gail @ example.com>", false), 330 new TestPair("Abigail <abigail(fo(o)@example.com>", false), 331 new TestPair("Abigail <abigail(fo)o)@example.com>", false), 332 new TestPair("\"Abi\"gail\" <abigail@example.com>", false), 333 new TestPair("abigail@[exa]ple.com]", false), 334 new TestPair("abigail@[exa[ple.com]", false), 335 new TestPair("abigail@[exaple].com]", false), 336 new TestPair("abigail@", false), 337 new TestPair("@example.com", false), 338 new TestPair("phrase: abigail@example.com abigail@example.com ;", false), 339 new TestPair("invalid£char@example.com", false) 340 }; 341 342 /*** 343 * Write this test based on perl Mail::RFC822::Address 344 * which takes its example email address directly from RFC822 345 * 346 * @throws ValidatorException 347 * 348 * FIXME This test fails so disable it with a leading _ for 1.1.2 release. 349 * The real solution is to fix the email parsing. 350 */ 351 public void _testEmailFromPerl() throws ValidatorException { 352 ValueBean info = new ValueBean(); 353 for (int index = 0; index < testEmailFromPerl.length; index++) { 354 info.setValue(testEmailFromPerl[index].item); 355 valueTest(info, testEmailFromPerl[index].valid); 356 } 357 } 358 359 /*** 360 * Utlity class to run a test on a value. 361 * 362 * @param info Value to run test on. 363 * @param passed Whether or not the test is expected to pass. 364 */ 365 private void valueTest(ValueBean info, boolean passed) throws ValidatorException { 366 // Construct validator based on the loaded resources 367 // and the form key 368 Validator validator = new Validator(resources, FORM_KEY); 369 // add the name bean to the validator as a resource 370 // for the validations to be performed on. 371 validator.setParameter(Validator.BEAN_PARAM, info); 372 373 // Get results of the validation. 374 ValidatorResults results = null; 375 376 // throws ValidatorException, 377 // but we aren't catching for testing 378 // since no validation methods we use 379 // throw this 380 results = validator.validate(); 381 382 assertNotNull("Results are null.", results); 383 384 ValidatorResult result = results.getValidatorResult("value"); 385 386 assertNotNull(ACTION + " value ValidatorResult should not be null.", result); 387 assertTrue("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION)); 388 assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION))); 389 } 390 }

This page was automatically generated by Maven