1 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 34 public class EmailTest extends TestCommon { 35 36 40 protected static String FORM_KEY = "emailForm"; 41 42 45 protected static String ACTION = "email"; 46 47 48 public EmailTest(String name) { 49 super(name); 50 } 51 52 57 public static void main(String [] theArgs) { 58 junit.awtui.TestRunner.main(new String [] {EmailTest.class.getName()}); 59 } 60 61 65 public static Test suite() { 66 return new TestSuite(EmailTest.class); 68 } 69 70 74 protected void setUp() throws IOException , SAXException { 75 loadResources("validator-regexp.xml"); 76 } 77 78 protected void tearDown() { 79 } 80 81 84 public void testEmail() throws ValidatorException { 85 ValueBean info = new ValueBean(); 87 88 info.setValue("jsmith@apache.org"); 89 valueTest(info, true); 90 } 91 92 95 public void testEmailWithNumericAddress() throws ValidatorException { 96 ValueBean info = new ValueBean(); 97 info.setValue("someone@[216.109.118.76]"); 98 valueTest(info, true); 99 info.setValue("someone@yahoo.com"); 100 valueTest(info, true); 101 } 102 103 106 public void testEmailExtension() throws ValidatorException { 107 ValueBean info = new ValueBean(); 109 110 info.setValue("jsmith@apache.org"); 111 valueTest(info, true); 112 113 info.setValue("jsmith@apache.com"); 114 valueTest(info, true); 115 116 info.setValue("jsmith@apache.net"); 117 valueTest(info, true); 118 119 info.setValue("jsmith@apache.info"); 120 valueTest(info, true); 121 122 info.setValue("jsmith@apache.infoo"); 123 valueTest(info, false); 124 125 info.setValue("jsmith@apache."); 126 valueTest(info, false); 127 128 info.setValue("jsmith@apache.c"); 129 valueTest(info, false); 130 } 131 132 136 public void testEmailWithDash() throws ValidatorException { 137 ValueBean info = new ValueBean(); 139 140 info.setValue("andy.noble@data-workshop.com"); 141 valueTest(info, true); 142 143 info.setValue("andy-noble@data-workshop.-com"); 144 valueTest(info, true); 145 info.setValue("andy-noble@data-workshop.c-om"); 146 valueTest(info,true); 147 info.setValue("andy-noble@data-workshop.co-m"); 148 valueTest(info, true); 149 150 151 } 152 153 157 public void testEmailWithDotEnd() throws ValidatorException { 158 ValueBean info = new ValueBean(); 160 161 info.setValue("andy.noble@data-workshop.com."); 162 valueTest(info, false); 163 164 } 165 166 170 public void testEmailWithBogusCharacter() throws ValidatorException { 171 ValueBean info = new ValueBean(); 173 174 info.setValue("andy.noble@\u008fdata-workshop.com"); 175 valueTest(info, false); 176 177 info.setValue("andy.o'reilly@data-workshop.com"); 179 valueTest(info, true); 180 181 info.setValue("foo+bar@i.am.not.in.us.example.com"); 182 valueTest(info, true); 183 } 184 185 188 public void testEmailWithCommas() throws ValidatorException { 189 ValueBean info = new ValueBean(); 190 info.setValue("joeblow@apa,che.org"); 191 valueTest(info, false); 192 info.setValue("joeblow@apache.o,rg"); 193 valueTest(info, false); 194 info.setValue("joeblow@apache,org"); 195 valueTest(info, false); 196 197 } 198 199 208 public void _testEmailUserName() throws ValidatorException { 209 ValueBean info = new ValueBean(); 210 info.setValue("joe1blow@apache.org"); 211 valueTest(info, true); 212 info.setValue("joe$blow@apache.org"); 213 valueTest(info, true); 214 info.setValue("joe-@apache.org"); 215 valueTest(info, true); 216 info.setValue("joe_@apache.org"); 217 valueTest(info, true); 218 219 221 info.setValue("joe.@apache.org"); 222 valueTest(info, false); 223 info.setValue("joe+@apache.org"); 224 valueTest(info, false); 225 info.setValue("joe!@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 info.setValue("joe,@apache.org"); 236 valueTest(info, false); 237 info.setValue("joe%45@apache.org"); 238 valueTest(info, false); 239 info.setValue("joe;@apache.org"); 240 valueTest(info, false); 241 info.setValue("joe?@apache.org"); 242 valueTest(info, false); 243 info.setValue("joe&@apache.org"); 244 valueTest(info, false); 245 info.setValue("joe=@apache.org"); 246 valueTest(info, false); 247 248 info.setValue("\"joe.\"@apache.org"); 250 valueTest(info, true); 251 info.setValue("\"joe+\"@apache.org"); 252 valueTest(info, true); 253 info.setValue("\"joe!\"@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 info.setValue("\"joe,\"@apache.org"); 264 valueTest(info, true); 265 info.setValue("\"joe%45\"@apache.org"); 266 valueTest(info, true); 267 info.setValue("\"joe;\"@apache.org"); 268 valueTest(info, true); 269 info.setValue("\"joe?\"@apache.org"); 270 valueTest(info, true); 271 info.setValue("\"joe&\"@apache.org"); 272 valueTest(info, true); 273 info.setValue("\"joe=\"@apache.org"); 274 valueTest(info, true); 275 276 } 277 278 283 TestPair[] testEmailFromPerl = { 284 new TestPair("abigail@example.com", true), 285 new TestPair("abigail@example.com ", true), 286 new TestPair(" abigail@example.com", true), 287 new TestPair("abigail @example.com ", true), 288 new TestPair("*@example.net", true), 289 new TestPair("\"\\\"\"@foo.bar", true), 290 new TestPair("fred&barny@example.com", true), 291 new TestPair("---@example.com", true), 292 new TestPair("foo-bar@example.net", true), 293 new TestPair("\"127.0.0.1\"@[127.0.0.1]", true), 294 new TestPair("Abigail <abigail@example.com>", true), 295 new TestPair("Abigail<abigail@example.com>", true), 296 new TestPair("Abigail<@a,@b,@c:abigail@example.com>", true), 297 new TestPair("\"This is a phrase\"<abigail@example.com>", true), 298 new TestPair("\"Abigail \"<abigail@example.com>", true), 299 new TestPair("\"Joe & J. Harvey\" <example @Org>", true), 300 new TestPair("Abigail <abigail @ example.com>", true), 301 new TestPair("Abigail made this < abigail @ example . com >", true), 302 new TestPair("Abigail(the bitch)@example.com", true), 303 new TestPair("Abigail <abigail @ example . (bar) com >", true), 304 new TestPair("Abigail < (one) abigail (two) @(three)example . (bar) com (quz) >", true), 305 new TestPair("Abigail (foo) (((baz)(nested) (comment)) ! ) < (one) abigail (two) @(three)example . (bar) com (quz) >", true), 306 new TestPair("Abigail <abigail(fo\\(o)@example.com>", true), 307 new TestPair("Abigail <abigail(fo\\)o)@example.com> ", true), 308 new TestPair("(foo) abigail@example.com", true), 309 new TestPair("abigail@example.com (foo)", true), 310 new TestPair("\"Abi\\\"gail\" <abigail@example.com>", true), 311 new TestPair("abigail@[example.com]", true), 312 new TestPair("abigail@[exa\\[ple.com]", true), 313 new TestPair("abigail@[exa\\]ple.com]", true), 314 new TestPair("\":sysmail\"@ Some-Group. Some-Org", true), 315 new TestPair("Muhammed.(I am the greatest) Ali @(the)Vegas.WBA", true), 316 new TestPair("mailbox.sub1.sub2@this-domain", true), 317 new TestPair("sub-net.mailbox@sub-domain.domain", true), 318 new TestPair("name:;", true), 319 new TestPair("':;", true), 320 new TestPair("name: ;", true), 321 new TestPair("Alfred Neuman <Neuman@BBN-TENEXA>", true), 322 new TestPair("Neuman@BBN-TENEXA", true), 323 new TestPair("\"George, Ted\" <Shared@Group.Arpanet>", true), 324 new TestPair("Wilt . (the Stilt) Chamberlain@NBA.US", true), 325 new TestPair("Cruisers: Port@Portugal, Jones@SEA;", true), 326 new TestPair("$@[]", true), 327 new TestPair("*()@[]", true), 328 new TestPair("\"quoted ( brackets\" ( a comment )@example.com", true), 329 new TestPair("\"Joe & J. Harvey\"\\x0D\\x0A <ddd\\@ Org>", true), 330 new TestPair("\"Joe &\\x0D\\x0A J. Harvey\" <ddd \\@ Org>", true), 331 new TestPair("Gourmets: Pompous Person <WhoZiWhatZit\\@Cordon-Bleu>,\\x0D\\x0A" + 332 " Childs\\@WGBH.Boston, \"Galloping Gourmet\"\\@\\x0D\\x0A" + 333 " ANT.Down-Under (Australian National Television),\\x0D\\x0A" + 334 " Cheapie\\@Discount-Liquors;", true), 335 new TestPair(" Just a string", false), 336 new TestPair("string", false), 337 new TestPair("(comment)", false), 338 new TestPair("()@example.com", false), 339 new TestPair("fred(&)barny@example.com", false), 340 new TestPair("fred\\ barny@example.com", false), 341 new TestPair("Abigail <abi gail @ example.com>", false), 342 new TestPair("Abigail <abigail(fo(o)@example.com>", false), 343 new TestPair("Abigail <abigail(fo)o)@example.com>", false), 344 new TestPair("\"Abi\"gail\" <abigail@example.com>", false), 345 new TestPair("abigail@[exa]ple.com]", false), 346 new TestPair("abigail@[exa[ple.com]", false), 347 new TestPair("abigail@[exaple].com]", false), 348 new TestPair("abigail@", false), 349 new TestPair("@example.com", false), 350 new TestPair("phrase: abigail@example.com abigail@example.com ;", false), 351 new TestPair("invalid£char@example.com", false) 352 }; 353 354 363 public void _testEmailFromPerl() throws ValidatorException { 364 ValueBean info = new ValueBean(); 365 for (int index = 0; index < testEmailFromPerl.length; index++) { 366 info.setValue(testEmailFromPerl[index].item); 367 valueTest(info, testEmailFromPerl[index].valid); 368 } 369 } 370 371 377 private void valueTest(ValueBean info, boolean passed) throws ValidatorException { 378 Validator validator = new Validator(resources, FORM_KEY); 381 validator.setParameter(Validator.BEAN_PARAM, info); 384 385 ValidatorResults results = null; 387 388 results = validator.validate(); 393 394 assertNotNull("Results are null.", results); 395 396 ValidatorResult result = results.getValidatorResult("value"); 397 398 assertNotNull(ACTION + " value ValidatorResult should not be null.", result); 399 assertTrue("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION)); 400 assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION))); 401 } 402 } 403 | Popular Tags |