~ubuntu-branches/ubuntu/oneiric/libcommons-validator-java/oneiric

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/validator/EmailTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Kumar Appaiah, Varun Hiremath
  • Date: 2007-09-16 00:57:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070916005746-r5xwnjm12ng9fbdf
Tags: 1:1.3.1-1
[ Kumar Appaiah ]
* New upstream release.
* New uploaders: Varun Hiremath and Kumar Appaiah, removed Wolfgang Baer
* Use upstream's conf directory for configuration and DTDs.
* Use a custom ant.properties to avoid downloads.
* Use DEB_UPSTREAM_VERSION in rules instead of versions in rules for symlinking.

[ Varun Hiremath ]
* debian/control:
  + Add rhino to Build-Depends-Indep and Depends.
  + Add XS-Vcs-{Svn, Browser} headers.
  + Depend on java-gcj-compat instead of kaffe.
* debian/compat: switch to 5
* Remove debian/patches and remove RELEASE-NOTES.txt in debian/rules.
* Add debian/orig-tar.sh to remove CRLF line terminators from upstream files.
* debian/rules: implement get-orig-source
* debian/watch: switch to version 3 and call debian/orig-tar.sh

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
}                                                         
 
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 junit.framework.Test;
 
22
import junit.framework.TestSuite;
 
23
 
 
24
import org.xml.sax.SAXException;
 
25
 
 
26
/**                                                       
 
27
 * Performs Validation Test for e-mail validations.
 
28
 *
 
29
 *
 
30
 * @version $Revision: 478560 $ $Date: 2006-11-23 13:09:27 +0000 (Thu, 23 Nov 2006) $
 
31
 */
 
32
public class EmailTest extends TestCommon {
 
33
 
 
34
    /**
 
35
     * The key used to retrieve the set of validation
 
36
     * rules from the xml file.
 
37
     */
 
38
    protected static String FORM_KEY = "emailForm";
 
39
 
 
40
   /**
 
41
    * The key used to retrieve the validator action.
 
42
    */
 
43
   protected static String ACTION = "email";
 
44
 
 
45
 
 
46
   public EmailTest(String name) {                  
 
47
       super(name);                                      
 
48
   }                                                     
 
49
 
 
50
   /**
 
51
    * Start the tests.
 
52
    *
 
53
    * @param theArgs the arguments. Not used
 
54
    */
 
55
   public static void main(String[] theArgs) {
 
56
       junit.awtui.TestRunner.main(new String[] {EmailTest.class.getName()});
 
57
   }
 
58
 
 
59
   /**
 
60
    * @return a test suite (<code>TestSuite</code>) that includes all methods
 
61
    *         starting with "test"
 
62
    */
 
63
   public static Test suite() {
 
64
       // All methods starting with "test" will be executed in the test suite.
 
65
       return new TestSuite(EmailTest.class);
 
66
   }
 
67
 
 
68
   /**
 
69
    * Load <code>ValidatorResources</code> from 
 
70
    * validator-regexp.xml.
 
71
    */
 
72
   protected void setUp() throws IOException, SAXException {
 
73
      loadResources("EmailTest-config.xml");
 
74
   }
 
75
 
 
76
   protected void tearDown() {
 
77
   }
 
78
 
 
79
   /**
 
80
    * Tests the e-mail validation.
 
81
    */
 
82
   public void testEmail() throws ValidatorException {
 
83
      // Create bean to run test on.
 
84
      ValueBean info = new ValueBean();
 
85
 
 
86
      info.setValue("jsmith@apache.org");
 
87
      valueTest(info, true);
 
88
   }
 
89
    
 
90
   /**
 
91
    * Tests the email validation with numeric domains.
 
92
    */
 
93
    public void testEmailWithNumericAddress() throws ValidatorException {
 
94
        ValueBean info = new ValueBean();
 
95
        info.setValue("someone@[216.109.118.76]");
 
96
        valueTest(info, true);
 
97
        info.setValue("someone@yahoo.com");
 
98
        valueTest(info, true);
 
99
    }
 
100
 
 
101
    /**
 
102
     * Tests the e-mail validation.
 
103
     */
 
104
    public void testEmailExtension() throws ValidatorException {
 
105
        // Create bean to run test on.
 
106
        ValueBean info = new ValueBean();
 
107
 
 
108
        info.setValue("jsmith@apache.org");
 
109
        valueTest(info, true);
 
110
 
 
111
        info.setValue("jsmith@apache.com");
 
112
        valueTest(info, true);
 
113
 
 
114
        info.setValue("jsmith@apache.net");
 
115
        valueTest(info, true);
 
116
 
 
117
        info.setValue("jsmith@apache.info");
 
118
        valueTest(info, true);
 
119
 
 
120
        info.setValue("jsmith@apache.");
 
121
        valueTest(info, false);
 
122
 
 
123
        info.setValue("jsmith@apache.c");
 
124
        valueTest(info, false);
 
125
        
 
126
        info.setValue("someone@yahoo.museum");
 
127
        valueTest(info, true);
 
128
        
 
129
        info.setValue("someone@yahoo.mu-seum");
 
130
        valueTest(info, false);
 
131
    }
 
132
 
 
133
   /**
 
134
    * <p>Tests the e-mail validation with a dash in 
 
135
    * the address.</p>
 
136
    */
 
137
   public void testEmailWithDash() throws ValidatorException {
 
138
      // Create bean to run test on.
 
139
      ValueBean info = new ValueBean();
 
140
 
 
141
      info.setValue("andy.noble@data-workshop.com");
 
142
      valueTest(info, true);
 
143
 
 
144
      info.setValue("andy-noble@data-workshop.-com");
 
145
       valueTest(info, false);
 
146
       info.setValue("andy-noble@data-workshop.c-om");
 
147
       valueTest(info,false);
 
148
       info.setValue("andy-noble@data-workshop.co-m");
 
149
       valueTest(info, false);
 
150
 
 
151
 
 
152
   }
 
153
 
 
154
   /**
 
155
    * Tests the e-mail validation with a dot at the end of 
 
156
    * the address.
 
157
    */
 
158
   public void testEmailWithDotEnd() throws ValidatorException {
 
159
      // Create bean to run test on.
 
160
      ValueBean info = new ValueBean();
 
161
 
 
162
      info.setValue("andy.noble@data-workshop.com.");
 
163
      valueTest(info, false);
 
164
 
 
165
   }
 
166
 
 
167
    /**
 
168
     * Tests the e-mail validation with an RCS-noncompliant character in
 
169
     * the address.
 
170
     */
 
171
    public void testEmailWithBogusCharacter() throws ValidatorException {
 
172
        // Create bean to run test on.
 
173
        ValueBean info = new ValueBean();
 
174
 
 
175
        info.setValue("andy.noble@\u008fdata-workshop.com");
 
176
        valueTest(info, false);
 
177
    
 
178
        // The ' character is valid in an email username.
 
179
        info.setValue("andy.o'reilly@data-workshop.com");
 
180
        valueTest(info, true);
 
181
        
 
182
        // But not in the domain name.
 
183
        info.setValue("andy@o'reilly.data-workshop.com");
 
184
        valueTest(info, false);
 
185
 
 
186
        info.setValue("foo+bar@i.am.not.in.us.example.com");
 
187
        valueTest(info, true);
 
188
    }
 
189
   
 
190
   /**
 
191
    * Tests the email validation with commas.
 
192
    */
 
193
    public void testEmailWithCommas() throws ValidatorException {
 
194
        ValueBean info = new ValueBean();
 
195
        info.setValue("joeblow@apa,che.org");
 
196
        valueTest(info, false);
 
197
        info.setValue("joeblow@apache.o,rg");
 
198
        valueTest(info, false);
 
199
        info.setValue("joeblow@apache,org");
 
200
        valueTest(info, false);
 
201
 
 
202
    }
 
203
   
 
204
   /**
 
205
    * Tests the email validation with spaces.
 
206
    */
 
207
    public void testEmailWithSpaces() throws ValidatorException {
 
208
        ValueBean info = new ValueBean();
 
209
        info.setValue("joeblow @apache.org");
 
210
        valueTest(info, false);
 
211
        info.setValue("joeblow@ apache.org");
 
212
        valueTest(info, false);
 
213
        info.setValue(" joeblow@apache.org");
 
214
        valueTest(info, true);
 
215
        info.setValue("joeblow@apache.org ");
 
216
        valueTest(info, true);
 
217
        info.setValue("joe blow@apache.org ");
 
218
        valueTest(info, false);
 
219
        info.setValue("joeblow@apa che.org ");
 
220
        valueTest(info, false);
 
221
 
 
222
    }
 
223
 
 
224
   /**
 
225
    * Tests the email validation with ascii control characters.
 
226
    * (i.e. Ascii chars 0 - 31 and 127)
 
227
    */
 
228
    public void testEmailWithControlChars() throws ValidatorException {
 
229
        EmailValidator validator = new EmailValidator();
 
230
        for (char c = 0; c < 32; c++) {
 
231
            assertFalse("Test control char " + ((int)c), validator.isValid("foo" + c + "bar@domain.com"));
 
232
        }
 
233
        assertFalse("Test control char 127", validator.isValid("foo" + ((char)127) + "bar@domain.com"));
 
234
    }
 
235
 
 
236
    /**
 
237
     * Write this test according to parts of RFC, as opposed to the type of character
 
238
     * that is being tested.
 
239
     *
 
240
     * <p><b>FIXME</b>: This test fails so disable it with a leading _ for 1.1.4 release.
 
241
     * The real solution is to fix the email parsing.
 
242
     *
 
243
     * @throws ValidatorException
 
244
     */
 
245
    public void _testEmailUserName() throws ValidatorException {
 
246
        ValueBean info = new ValueBean();
 
247
        info.setValue("joe1blow@apache.org");
 
248
        valueTest(info, true);
 
249
        info.setValue("joe$blow@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
 
 
256
        //UnQuoted Special characters are invalid
 
257
 
 
258
        info.setValue("joe.@apache.org");
 
259
        valueTest(info, false);
 
260
        info.setValue("joe+@apache.org");
 
261
        valueTest(info, false);
 
262
        info.setValue("joe!@apache.org");
 
263
        valueTest(info, false);
 
264
        info.setValue("joe*@apache.org");
 
265
        valueTest(info, false);
 
266
        info.setValue("joe'@apache.org");
 
267
        valueTest(info, false);
 
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%45@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
 
 
285
        //Quoted Special characters are valid
 
286
        info.setValue("\"joe.\"@apache.org");
 
287
        valueTest(info, true);
 
288
        info.setValue("\"joe+\"@apache.org");
 
289
        valueTest(info, true);
 
290
        info.setValue("\"joe!\"@apache.org");
 
291
        valueTest(info, true);
 
292
        info.setValue("\"joe*\"@apache.org");
 
293
        valueTest(info, true);
 
294
        info.setValue("\"joe'\"@apache.org");
 
295
        valueTest(info, true);
 
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%45\"@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
 
 
313
    }
 
314
 
 
315
    /**
 
316
     * These test values derive directly from RFC 822 &
 
317
     * Mail::RFC822::Address & RFC::RFC822::Address perl test.pl
 
318
     * For traceability don't combine these test values with other tests.
 
319
     */
 
320
    TestPair[] testEmailFromPerl = {
 
321
        new TestPair("abigail@example.com", true),
 
322
        new TestPair("abigail@example.com ", true),
 
323
        new TestPair(" abigail@example.com", true),
 
324
        new TestPair("abigail @example.com ", true),
 
325
        new TestPair("*@example.net", true),
 
326
        new TestPair("\"\\\"\"@foo.bar", true),
 
327
        new TestPair("fred&barny@example.com", true),
 
328
        new TestPair("---@example.com", true),
 
329
        new TestPair("foo-bar@example.net", true),
 
330
        new TestPair("\"127.0.0.1\"@[127.0.0.1]", true),
 
331
        new TestPair("Abigail <abigail@example.com>", true),
 
332
        new TestPair("Abigail<abigail@example.com>", true),
 
333
        new TestPair("Abigail<@a,@b,@c:abigail@example.com>", true),
 
334
        new TestPair("\"This is a phrase\"<abigail@example.com>", true),
 
335
        new TestPair("\"Abigail \"<abigail@example.com>", true),
 
336
        new TestPair("\"Joe & J. Harvey\" <example @Org>", true),
 
337
        new TestPair("Abigail <abigail @ example.com>", true),
 
338
        new TestPair("Abigail made this <  abigail   @   example  .    com    >", true),
 
339
        new TestPair("Abigail(the bitch)@example.com", true),
 
340
        new TestPair("Abigail <abigail @ example . (bar) com >", true),
 
341
        new TestPair("Abigail < (one)  abigail (two) @(three)example . (bar) com (quz) >", true),
 
342
        new TestPair("Abigail (foo) (((baz)(nested) (comment)) ! ) < (one)  abigail (two) @(three)example . (bar) com (quz) >", true),
 
343
        new TestPair("Abigail <abigail(fo\\(o)@example.com>", true),
 
344
        new TestPair("Abigail <abigail(fo\\)o)@example.com> ", true),
 
345
        new TestPair("(foo) abigail@example.com", true),
 
346
        new TestPair("abigail@example.com (foo)", true),
 
347
        new TestPair("\"Abi\\\"gail\" <abigail@example.com>", true),
 
348
        new TestPair("abigail@[example.com]", true),
 
349
        new TestPair("abigail@[exa\\[ple.com]", true),
 
350
        new TestPair("abigail@[exa\\]ple.com]", true),
 
351
        new TestPair("\":sysmail\"@  Some-Group. Some-Org", true),
 
352
        new TestPair("Muhammed.(I am  the greatest) Ali @(the)Vegas.WBA", true),
 
353
        new TestPair("mailbox.sub1.sub2@this-domain", true),
 
354
        new TestPair("sub-net.mailbox@sub-domain.domain", true),
 
355
        new TestPair("name:;", true),
 
356
        new TestPair("':;", true),
 
357
        new TestPair("name:   ;", true),
 
358
        new TestPair("Alfred Neuman <Neuman@BBN-TENEXA>", true),
 
359
        new TestPair("Neuman@BBN-TENEXA", true),
 
360
        new TestPair("\"George, Ted\" <Shared@Group.Arpanet>", true),
 
361
        new TestPair("Wilt . (the  Stilt) Chamberlain@NBA.US", true),
 
362
        new TestPair("Cruisers:  Port@Portugal, Jones@SEA;", true),
 
363
        new TestPair("$@[]", true),
 
364
        new TestPair("*()@[]", true),
 
365
        new TestPair("\"quoted ( brackets\" ( a comment )@example.com", true),
 
366
        new TestPair("\"Joe & J. Harvey\"\\x0D\\x0A     <ddd\\@ Org>", true),
 
367
        new TestPair("\"Joe &\\x0D\\x0A J. Harvey\" <ddd \\@ Org>", true),
 
368
        new TestPair("Gourmets:  Pompous Person <WhoZiWhatZit\\@Cordon-Bleu>,\\x0D\\x0A" +
 
369
            "        Childs\\@WGBH.Boston, \"Galloping Gourmet\"\\@\\x0D\\x0A" +
 
370
            "        ANT.Down-Under (Australian National Television),\\x0D\\x0A" +
 
371
            "        Cheapie\\@Discount-Liquors;", true),
 
372
        new TestPair("   Just a string", false),
 
373
        new TestPair("string", false),
 
374
        new TestPair("(comment)", false),
 
375
        new TestPair("()@example.com", false),
 
376
        new TestPair("fred(&)barny@example.com", false),
 
377
        new TestPair("fred\\ barny@example.com", false),
 
378
        new TestPair("Abigail <abi gail @ example.com>", false),
 
379
        new TestPair("Abigail <abigail(fo(o)@example.com>", false),
 
380
        new TestPair("Abigail <abigail(fo)o)@example.com>", false),
 
381
        new TestPair("\"Abi\"gail\" <abigail@example.com>", false),
 
382
        new TestPair("abigail@[exa]ple.com]", false),
 
383
        new TestPair("abigail@[exa[ple.com]", false),
 
384
        new TestPair("abigail@[exaple].com]", false),
 
385
        new TestPair("abigail@", false),
 
386
        new TestPair("@example.com", false),
 
387
        new TestPair("phrase: abigail@example.com abigail@example.com ;", false),
 
388
        new TestPair("invalid�char@example.com", false)
 
389
    };
 
390
 
 
391
    /**
 
392
     * Write this test based on perl Mail::RFC822::Address
 
393
     * which takes its example email address directly from RFC822
 
394
     * 
 
395
     * @throws ValidatorException
 
396
     * 
 
397
     * FIXME This test fails so disable it with a leading _ for 1.1.4 release.
 
398
     * The real solution is to fix the email parsing.
 
399
     */
 
400
    public void _testEmailFromPerl() throws ValidatorException {
 
401
        ValueBean info = new ValueBean();
 
402
        for (int index = 0; index < testEmailFromPerl.length; index++) {
 
403
            info.setValue(testEmailFromPerl[index].item);
 
404
            valueTest(info, testEmailFromPerl[index].valid);
 
405
        }
 
406
    }
 
407
 
 
408
   /**
 
409
    * Utlity class to run a test on a value.
 
410
    *
 
411
    * @param info       Value to run test on.
 
412
    * @param passed     Whether or not the test is expected to pass.
 
413
    */
 
414
   private void valueTest(ValueBean info, boolean passed) throws ValidatorException {
 
415
      // Construct validator based on the loaded resources 
 
416
      // and the form key
 
417
      Validator validator = new Validator(resources, FORM_KEY);
 
418
      // add the name bean to the validator as a resource 
 
419
      // for the validations to be performed on.
 
420
      validator.setParameter(Validator.BEAN_PARAM, info);
 
421
 
 
422
      // Get results of the validation.
 
423
      ValidatorResults results = null;
 
424
      
 
425
      // throws ValidatorException, 
 
426
      // but we aren't catching for testing 
 
427
      // since no validation methods we use 
 
428
      // throw this
 
429
      results = validator.validate();
 
430
      
 
431
      assertNotNull("Results are null.", results);
 
432
      
 
433
      ValidatorResult result = results.getValidatorResult("value");
 
434
 
 
435
      assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
 
436
      assertTrue("Value "+info.getValue()+" ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
 
437
      assertTrue("Value "+info.getValue()+"ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
 
438
    }
 
439
}