KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > EmailTest


1 /*
2  * $Id: EmailTest.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 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 JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc FORM_KEY = "emailForm";
41
42    /**
43     * The key used to retrieve the validator action.
44     */

45    protected static String JavaDoc ACTION = "email";
46
47
48    public EmailTest(String JavaDoc 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 JavaDoc[] theArgs) {
58        junit.awtui.TestRunner.main(new String JavaDoc[] {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 JavaDoc, SAXException JavaDoc {
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 email validation with numeric domains.
94     */

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     /**
104      * Tests the e-mail validation.
105      */

106     public void testEmailExtension() throws ValidatorException {
107         // Create bean to run test on.
108
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    /**
133     * <p>Tests the e-mail validation with a dash in
134     * the address.</p>
135     */

136    public void testEmailWithDash() throws ValidatorException {
137       // Create bean to run test on.
138
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    /**
154     * Tests the e-mail validation with a dot at the end of
155     * the address.
156     */

157    public void testEmailWithDotEnd() throws ValidatorException {
158       // Create bean to run test on.
159
ValueBean info = new ValueBean();
160
161       info.setValue("andy.noble@data-workshop.com.");
162       valueTest(info, false);
163
164    }
165
166     /**
167      * Tests the e-mail validation with an RCS-noncompliant character in
168      * the address.
169      */

170     public void testEmailWithBogusCharacter() throws ValidatorException {
171         // Create bean to run test on.
172
ValueBean info = new ValueBean();
173
174         info.setValue("andy.noble@\u008fdata-workshop.com");
175         valueTest(info, false);
176     
177         // The ' character is valid in an email address.
178
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    /**
186     * Tests the email validation with commas.
187     */

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     /**
200      * Write this test according to parts of RFC, as opposed to the type of character
201      * that is being tested.
202      *
203      * <p><b>FIXME</b>: This test fails so disable it with a leading _ for 1.1.4 release.
204      * The real solution is to fix the email parsing.
205      *
206      * @throws ValidatorException
207      */

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         //UnQuoted Special characters are invalid
220

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         //Quoted Special characters are valid
249
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     /**
279      * These test values derive directly from RFC 822 &
280      * Mail::RFC822::Address & RFC::RFC822::Address perl test.pl
281      * For traceability don't combine these test values with other tests.
282      */

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     /**
355      * Write this test based on perl Mail::RFC822::Address
356      * which takes its example email address directly from RFC822
357      *
358      * @throws ValidatorException
359      *
360      * FIXME This test fails so disable it with a leading _ for 1.1.4 release.
361      * The real solution is to fix the email parsing.
362      */

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    /**
372     * Utlity class to run a test on a value.
373     *
374     * @param info Value to run test on.
375     * @param passed Whether or not the test is expected to pass.
376     */

377    private void valueTest(ValueBean info, boolean passed) throws ValidatorException {
378       // Construct validator based on the loaded resources
379
// and the form key
380
Validator validator = new Validator(resources, FORM_KEY);
381       // add the name bean to the validator as a resource
382
// for the validations to be performed on.
383
validator.setParameter(Validator.BEAN_PARAM, info);
384
385       // Get results of the validation.
386
ValidatorResults results = null;
387       
388       // throws ValidatorException,
389
// but we aren't catching for testing
390
// since no validation methods we use
391
// throw this
392
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