1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.IOException;
20
21 import org.xml.sax.SAXException;
22
23
24
25
26
27
28
29
30 @Deprecated
31 public class EmailTest extends AbstractCommonTest {
32
33
34
35
36
37 protected static String FORM_KEY = "emailForm";
38
39
40
41
42 protected static String ACTION = "email";
43
44
45 public EmailTest(String name) {
46 super(name);
47 }
48
49
50
51
52
53 @Override
54 protected void setUp() throws IOException, SAXException {
55 loadResources("EmailTest-config.xml");
56 }
57
58
59
60
61 public void testEmail() throws ValidatorException {
62
63 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
64
65 info.setValue("jsmith@apache.org");
66 valueTest(info, true);
67 }
68
69
70
71
72 public void testEmailWithNumericAddress() throws ValidatorException {
73 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
74 info.setValue("someone@[216.109.118.76]");
75 valueTest(info, true);
76 info.setValue("someone@yahoo.com");
77 valueTest(info, true);
78 }
79
80
81
82
83 public void testEmailExtension() throws ValidatorException {
84
85 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
86
87 info.setValue("jsmith@apache.org");
88 valueTest(info, true);
89
90 info.setValue("jsmith@apache.com");
91 valueTest(info, true);
92
93 info.setValue("jsmith@apache.net");
94 valueTest(info, true);
95
96 info.setValue("jsmith@apache.info");
97 valueTest(info, true);
98
99 info.setValue("jsmith@apache.");
100 valueTest(info, false);
101
102 info.setValue("jsmith@apache.c");
103 valueTest(info, false);
104
105 info.setValue("someone@yahoo.museum");
106 valueTest(info, true);
107
108 info.setValue("someone@yahoo.mu-seum");
109 valueTest(info, false);
110 }
111
112
113
114
115
116 public void testEmailWithDash() throws ValidatorException {
117
118 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
119
120 info.setValue("andy.noble@data-workshop.com");
121 valueTest(info, true);
122
123 info.setValue("andy-noble@data-workshop.-com");
124 valueTest(info, false);
125 info.setValue("andy-noble@data-workshop.c-om");
126 valueTest(info,false);
127 info.setValue("andy-noble@data-workshop.co-m");
128 valueTest(info, false);
129
130
131 }
132
133
134
135
136
137 public void testEmailWithDotEnd() throws ValidatorException {
138
139 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
140
141 info.setValue("andy.noble@data-workshop.com.");
142 valueTest(info, false);
143
144 }
145
146
147
148
149
150 public void testEmailWithBogusCharacter() throws ValidatorException {
151
152 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
153
154 info.setValue("andy.noble@\u008fdata-workshop.com");
155 valueTest(info, false);
156
157
158 info.setValue("andy.o'reilly@data-workshop.com");
159 valueTest(info, true);
160
161
162 info.setValue("andy@o'reilly.data-workshop.com");
163 valueTest(info, false);
164
165 info.setValue("foo+bar@i.am.not.in.us.example.com");
166 valueTest(info, true);
167 }
168
169
170
171
172 public void testEmailWithCommas() throws ValidatorException {
173 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
174 info.setValue("joeblow@apa,che.org");
175 valueTest(info, false);
176 info.setValue("joeblow@apache.o,rg");
177 valueTest(info, false);
178 info.setValue("joeblow@apache,org");
179 valueTest(info, false);
180
181 }
182
183
184
185
186 public void testEmailWithSpaces() throws ValidatorException {
187 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
188 info.setValue("joeblow @apache.org");
189 valueTest(info, false);
190 info.setValue("joeblow@ apache.org");
191 valueTest(info, false);
192 info.setValue(" joeblow@apache.org");
193 valueTest(info, false);
194 info.setValue("joeblow@apache.org ");
195 valueTest(info, false);
196 info.setValue("joe blow@apache.org ");
197 valueTest(info, false);
198 info.setValue("joeblow@apa che.org ");
199 valueTest(info, false);
200 info.setValue("\"joe blow\"@apache.org");
201 valueTest(info, true);
202
203 }
204
205
206
207
208
209 public void testEmailWithControlChars() {
210 EmailValidator validator = new EmailValidator();
211 for (char c = 0; c < 32; c++) {
212 assertFalse("Test control char " + ((int)c), validator.isValid("foo" + c + "bar@domain.com"));
213 }
214 assertFalse("Test control char 127", validator.isValid("foo" + ((char)127) + "bar@domain.com"));
215 }
216
217
218
219
220 public void testEmailAtTLD() throws ValidatorException {
221
222 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
223
224 info.setValue("m@de");
225 valueTest(info, false);
226
227 org.apache.commons.validator.routines.EmailValidator validator =
228 org.apache.commons.validator.routines.EmailValidator.getInstance(true, true);
229 boolean result = validator.isValid("m@de");
230 assertTrue("Result should have been true", result);
231
232 }
233
234
235
236
237
238 public void testEmailLocalhost() throws ValidatorException {
239 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
240 info.setValue("joe@localhost");
241 valueTest(info, false);
242 info.setValue("joe@localhost.localdomain");
243 valueTest(info, false);
244 }
245
246
247
248
249
250
251
252
253
254
255 public void _testEmailUserName() throws ValidatorException {
256 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
257 info.setValue("joe1blow@apache.org");
258 valueTest(info, true);
259 info.setValue("joe$blow@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
266
267
268 info.setValue("joe.@apache.org");
269 valueTest(info, false);
270 info.setValue("joe+@apache.org");
271 valueTest(info, false);
272 info.setValue("joe!@apache.org");
273 valueTest(info, false);
274 info.setValue("joe*@apache.org");
275 valueTest(info, false);
276 info.setValue("joe'@apache.org");
277 valueTest(info, false);
278 info.setValue("joe(@apache.org");
279 valueTest(info, false);
280 info.setValue("joe)@apache.org");
281 valueTest(info, false);
282 info.setValue("joe,@apache.org");
283 valueTest(info, false);
284 info.setValue("joe%45@apache.org");
285 valueTest(info, false);
286 info.setValue("joe;@apache.org");
287 valueTest(info, false);
288 info.setValue("joe?@apache.org");
289 valueTest(info, false);
290 info.setValue("joe&@apache.org");
291 valueTest(info, false);
292 info.setValue("joe=@apache.org");
293 valueTest(info, false);
294
295
296 info.setValue("\"joe.\"@apache.org");
297 valueTest(info, true);
298 info.setValue("\"joe+\"@apache.org");
299 valueTest(info, true);
300 info.setValue("\"joe!\"@apache.org");
301 valueTest(info, true);
302 info.setValue("\"joe*\"@apache.org");
303 valueTest(info, true);
304 info.setValue("\"joe'\"@apache.org");
305 valueTest(info, true);
306 info.setValue("\"joe(\"@apache.org");
307 valueTest(info, true);
308 info.setValue("\"joe)\"@apache.org");
309 valueTest(info, true);
310 info.setValue("\"joe,\"@apache.org");
311 valueTest(info, true);
312 info.setValue("\"joe%45\"@apache.org");
313 valueTest(info, true);
314 info.setValue("\"joe;\"@apache.org");
315 valueTest(info, true);
316 info.setValue("\"joe?\"@apache.org");
317 valueTest(info, true);
318 info.setValue("\"joe&\"@apache.org");
319 valueTest(info, true);
320 info.setValue("\"joe=\"@apache.org");
321 valueTest(info, true);
322
323 }
324
325
326
327
328
329
330 ResultPair[] testEmailFromPerl = {
331 new ResultPair("abigail@example.com", true),
332 new ResultPair("abigail@example.com ", true),
333 new ResultPair(" abigail@example.com", true),
334 new ResultPair("abigail @example.com ", true),
335 new ResultPair("*@example.net", true),
336 new ResultPair("\"\\\"\"@foo.bar", true),
337 new ResultPair("fred&barny@example.com", true),
338 new ResultPair("---@example.com", true),
339 new ResultPair("foo-bar@example.net", true),
340 new ResultPair("\"127.0.0.1\"@[127.0.0.1]", true),
341 new ResultPair("Abigail <abigail@example.com>", true),
342 new ResultPair("Abigail<abigail@example.com>", true),
343 new ResultPair("Abigail<@a,@b,@c:abigail@example.com>", true),
344 new ResultPair("\"This is a phrase\"<abigail@example.com>", true),
345 new ResultPair("\"Abigail \"<abigail@example.com>", true),
346 new ResultPair("\"Joe & J. Harvey\" <example @Org>", true),
347 new ResultPair("Abigail <abigail @ example.com>", true),
348 new ResultPair("Abigail made this < abigail @ example . com >", true),
349 new ResultPair("Abigail(the bitch)@example.com", true),
350 new ResultPair("Abigail <abigail @ example . (bar) com >", true),
351 new ResultPair("Abigail < (one) abigail (two) @(three)example . (bar) com (quz) >", true),
352 new ResultPair("Abigail (foo) (((baz)(nested) (comment)) ! ) < (one) abigail (two) @(three)example . (bar) com (quz) >", true),
353 new ResultPair("Abigail <abigail(fo\\(o)@example.com>", true),
354 new ResultPair("Abigail <abigail(fo\\)o)@example.com> ", true),
355 new ResultPair("(foo) abigail@example.com", true),
356 new ResultPair("abigail@example.com (foo)", true),
357 new ResultPair("\"Abi\\\"gail\" <abigail@example.com>", true),
358 new ResultPair("abigail@[example.com]", true),
359 new ResultPair("abigail@[exa\\[ple.com]", true),
360 new ResultPair("abigail@[exa\\]ple.com]", true),
361 new ResultPair("\":sysmail\"@ Some-Group. Some-Org", true),
362 new ResultPair("Muhammed.(I am the greatest) Ali @(the)Vegas.WBA", true),
363 new ResultPair("mailbox.sub1.sub2@this-domain", true),
364 new ResultPair("sub-net.mailbox@sub-domain.domain", true),
365 new ResultPair("name:;", true),
366 new ResultPair("':;", true),
367 new ResultPair("name: ;", true),
368 new ResultPair("Alfred Neuman <Neuman@BBN-TENEXA>", true),
369 new ResultPair("Neuman@BBN-TENEXA", true),
370 new ResultPair("\"George, Ted\" <Shared@Group.Arpanet>", true),
371 new ResultPair("Wilt . (the Stilt) Chamberlain@NBA.US", true),
372 new ResultPair("Cruisers: Port@Portugal, Jones@SEA;", true),
373 new ResultPair("$@[]", true),
374 new ResultPair("*()@[]", true),
375 new ResultPair("\"quoted ( brackets\" ( a comment )@example.com", true),
376 new ResultPair("\"Joe & J. Harvey\"\\x0D\\x0A <ddd\\@ Org>", true),
377 new ResultPair("\"Joe &\\x0D\\x0A J. Harvey\" <ddd \\@ Org>", true),
378 new ResultPair("Gourmets: Pompous Person <WhoZiWhatZit\\@Cordon-Bleu>,\\x0D\\x0A" +
379 " Childs\\@WGBH.Boston, \"Galloping Gourmet\"\\@\\x0D\\x0A" +
380 " ANT.Down-Under (Australian National Television),\\x0D\\x0A" +
381 " Cheapie\\@Discount-Liquors;", true),
382 new ResultPair(" Just a string", false),
383 new ResultPair("string", false),
384 new ResultPair("(comment)", false),
385 new ResultPair("()@example.com", false),
386 new ResultPair("fred(&)barny@example.com", false),
387 new ResultPair("fred\\ barny@example.com", false),
388 new ResultPair("Abigail <abi gail @ example.com>", false),
389 new ResultPair("Abigail <abigail(fo(o)@example.com>", false),
390 new ResultPair("Abigail <abigail(fo)o)@example.com>", false),
391 new ResultPair("\"Abi\"gail\" <abigail@example.com>", false),
392 new ResultPair("abigail@[exa]ple.com]", false),
393 new ResultPair("abigail@[exa[ple.com]", false),
394 new ResultPair("abigail@[exaple].com]", false),
395 new ResultPair("abigail@", false),
396 new ResultPair("@example.com", false),
397 new ResultPair("phrase: abigail@example.com abigail@example.com ;", false),
398 new ResultPair("invalid�char@example.com", false)
399 };
400
401
402
403
404
405
406
407
408
409
410 public void _testEmailFromPerl() throws ValidatorException {
411 ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
412 for (int index = 0; index < testEmailFromPerl.length; index++) {
413 info.setValue(testEmailFromPerl[index].item);
414 valueTest(info, testEmailFromPerl[index].valid);
415 }
416 }
417
418
419
420
421
422
423
424 private void valueTest(ValueBean info, boolean passed) throws ValidatorException {
425
426
427 Validator validator = new Validator(resources, FORM_KEY);
428
429
430 validator.setParameter(Validator.BEAN_PARAM, info);
431
432
433 ValidatorResults results = null;
434
435
436
437
438
439 results = validator.validate();
440
441 assertNotNull("Results are null.", results);
442
443 ValidatorResult result = results.getValidatorResult("value");
444
445 assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
446 assertTrue("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
447 assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
448 }
449 }