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.validator;
18  
19  import java.io.IOException;
20  
21  import org.xml.sax.SAXException;
22  
23  /**                                                       
24   * Performs Validation Test for e-mail validations.
25   *
26   *
27   * @version $Revision$
28   * @deprecated to be removed when target class is removed
29   */
30  @Deprecated
31  public class EmailTest extends AbstractCommonTest {
32  
33      /**
34       * The key used to retrieve the set of validation
35       * rules from the xml file.
36       */
37      protected static String FORM_KEY = "emailForm";
38  
39     /**
40      * The key used to retrieve the validator action.
41      */
42     protected static String ACTION = "email";
43  
44  
45     public EmailTest(String name) {                  
46         super(name);                                      
47     }                                                     
48  
49     /**
50      * Load <code>ValidatorResources</code> from 
51      * validator-regexp.xml.
52      */
53     @Override
54  protected void setUp() throws IOException, SAXException {
55        loadResources("EmailTest-config.xml");
56     }
57  
58     /**
59      * Tests the e-mail validation.
60      */
61     public void testEmail() throws ValidatorException {
62        // Create bean to run test on.
63        ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
64  
65        info.setValue("jsmith@apache.org");
66        valueTest(info, true);
67     }
68      
69     /**
70      * Tests the email validation with numeric domains.
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       * Tests the e-mail validation.
82       */
83      public void testEmailExtension() throws ValidatorException {
84          // Create bean to run test on.
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     * <p>Tests the e-mail validation with a dash in 
114     * the address.</p>
115     */
116    public void testEmailWithDash() throws ValidatorException {
117       // Create bean to run test on.
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     * Tests the e-mail validation with a dot at the end of 
135     * the address.
136     */
137    public void testEmailWithDotEnd() throws ValidatorException {
138       // Create bean to run test on.
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      * Tests the e-mail validation with an RCS-noncompliant character in
148      * the address.
149      */
150     public void testEmailWithBogusCharacter() throws ValidatorException {
151         // Create bean to run test on.
152         ValueBeanValueBean.html#ValueBean">ValueBean info = new ValueBean();
153 
154         info.setValue("andy.noble@\u008fdata-workshop.com");
155         valueTest(info, false);
156     
157         // The ' character is valid in an email username.
158         info.setValue("andy.o'reilly@data-workshop.com");
159         valueTest(info, true);
160         
161         // But not in the domain name.
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     * Tests the email validation with commas.
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     * Tests the email validation with spaces.
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     * Tests the email validation with ascii control characters.
207     * (i.e. Ascii chars 0 - 31 and 127)
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     * Tests the e-mail validation with a user at a TLD
219     */
220    public void testEmailAtTLD() throws ValidatorException {
221       // Create bean to run test on.
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      * Test that @localhost and @localhost.localdomain
236      *  addresses aren't declared valid by default 
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      * Write this test according to parts of RFC, as opposed to the type of character
248      * that is being tested.
249      *
250      * <p><b>FIXME</b>: This test fails so disable it with a leading _ for 1.1.4 release.
251      * The real solution is to fix the email parsing.
252      *
253      * @throws ValidatorException
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         //UnQuoted Special characters are invalid
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         //Quoted Special characters are valid
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      * These test values derive directly from RFC 822 &
327      * Mail::RFC822::Address & RFC::RFC822::Address perl test.pl
328      * For traceability don't combine these test values with other tests.
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      * Write this test based on perl Mail::RFC822::Address
403      * which takes its example email address directly from RFC822
404      * 
405      * @throws ValidatorException
406      * 
407      * FIXME This test fails so disable it with a leading _ for 1.1.4 release.
408      * The real solution is to fix the email parsing.
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     * Utlity class to run a test on a value.
420     *
421     * @param info    Value to run test on.
422     * @param passed    Whether or not the test is expected to pass.
423     */
424    private void valueTest(ValueBean info, boolean passed) throws ValidatorException {
425       // Construct validator based on the loaded resources 
426       // and the form key
427       Validator validator = new Validator(resources, FORM_KEY);
428       // add the name bean to the validator as a resource 
429       // for the validations to be performed on.
430       validator.setParameter(Validator.BEAN_PARAM, info);
431 
432       // Get results of the validation.
433       ValidatorResults results = null;
434       
435       // throws ValidatorException, 
436       // but we aren't catching for testing 
437       // since no validation methods we use 
438       // throw this
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 }