Source Code Cross Referenced for EmailTest.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.validator;
018:
019:        import java.io.IOException;
020:
021:        import junit.framework.Test;
022:        import junit.framework.TestSuite;
023:
024:        import org.xml.sax.SAXException;
025:
026:        /**                                                       
027:         * Performs Validation Test for e-mail validations.
028:         *
029:         *
030:         * @version $Revision: 478560 $ $Date: 2006-11-23 13:09:27 +0000 (Thu, 23 Nov 2006) $
031:         */
032:        public class EmailTest extends TestCommon {
033:
034:            /**
035:             * The key used to retrieve the set of validation
036:             * rules from the xml file.
037:             */
038:            protected static String FORM_KEY = "emailForm";
039:
040:            /**
041:             * The key used to retrieve the validator action.
042:             */
043:            protected static String ACTION = "email";
044:
045:            public EmailTest(String name) {
046:                super (name);
047:            }
048:
049:            /**
050:             * Start the tests.
051:             *
052:             * @param theArgs the arguments. Not used
053:             */
054:            public static void main(String[] theArgs) {
055:                junit.awtui.TestRunner.main(new String[] { EmailTest.class
056:                        .getName() });
057:            }
058:
059:            /**
060:             * @return a test suite (<code>TestSuite</code>) that includes all methods
061:             *         starting with "test"
062:             */
063:            public static Test suite() {
064:                // All methods starting with "test" will be executed in the test suite.
065:                return new TestSuite(EmailTest.class);
066:            }
067:
068:            /**
069:             * Load <code>ValidatorResources</code> from 
070:             * validator-regexp.xml.
071:             */
072:            protected void setUp() throws IOException, SAXException {
073:                loadResources("EmailTest-config.xml");
074:            }
075:
076:            protected void tearDown() {
077:            }
078:
079:            /**
080:             * Tests the e-mail validation.
081:             */
082:            public void testEmail() throws ValidatorException {
083:                // Create bean to run test on.
084:                ValueBean info = new ValueBean();
085:
086:                info.setValue("jsmith@apache.org");
087:                valueTest(info, true);
088:            }
089:
090:            /**
091:             * Tests the email validation with numeric domains.
092:             */
093:            public void testEmailWithNumericAddress() throws ValidatorException {
094:                ValueBean info = new ValueBean();
095:                info.setValue("someone@[216.109.118.76]");
096:                valueTest(info, true);
097:                info.setValue("someone@yahoo.com");
098:                valueTest(info, true);
099:            }
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:             * 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 username.
178:                info.setValue("andy.o'reilly@data-workshop.com");
179:                valueTest(info, true);
180:
181:                // But not in the domain name.
182:                info.setValue("andy@o'reilly.data-workshop.com");
183:                valueTest(info, false);
184:
185:                info.setValue("foo+bar@i.am.not.in.us.example.com");
186:                valueTest(info, true);
187:            }
188:
189:            /**
190:             * Tests the email validation with commas.
191:             */
192:            public void testEmailWithCommas() throws ValidatorException {
193:                ValueBean info = new ValueBean();
194:                info.setValue("joeblow@apa,che.org");
195:                valueTest(info, false);
196:                info.setValue("joeblow@apache.o,rg");
197:                valueTest(info, false);
198:                info.setValue("joeblow@apache,org");
199:                valueTest(info, false);
200:
201:            }
202:
203:            /**
204:             * Tests the email validation with spaces.
205:             */
206:            public void testEmailWithSpaces() throws ValidatorException {
207:                ValueBean info = new ValueBean();
208:                info.setValue("joeblow @apache.org");
209:                valueTest(info, false);
210:                info.setValue("joeblow@ apache.org");
211:                valueTest(info, false);
212:                info.setValue(" joeblow@apache.org");
213:                valueTest(info, true);
214:                info.setValue("joeblow@apache.org ");
215:                valueTest(info, true);
216:                info.setValue("joe blow@apache.org ");
217:                valueTest(info, false);
218:                info.setValue("joeblow@apa che.org ");
219:                valueTest(info, false);
220:
221:            }
222:
223:            /**
224:             * Tests the email validation with ascii control characters.
225:             * (i.e. Ascii chars 0 - 31 and 127)
226:             */
227:            public void testEmailWithControlChars() throws ValidatorException {
228:                EmailValidator validator = new EmailValidator();
229:                for (char c = 0; c < 32; c++) {
230:                    assertFalse("Test control char " + ((int) c), validator
231:                            .isValid("foo" + c + "bar@domain.com"));
232:                }
233:                assertFalse("Test control char 127", validator.isValid("foo"
234:                        + ((char) 127) + "bar@domain.com"));
235:            }
236:
237:            /**
238:             * Write this test according to parts of RFC, as opposed to the type of character
239:             * that is being tested.
240:             *
241:             * <p><b>FIXME</b>: This test fails so disable it with a leading _ for 1.1.4 release.
242:             * The real solution is to fix the email parsing.
243:             *
244:             * @throws ValidatorException
245:             */
246:            public void _testEmailUserName() throws ValidatorException {
247:                ValueBean info = new ValueBean();
248:                info.setValue("joe1blow@apache.org");
249:                valueTest(info, true);
250:                info.setValue("joe$blow@apache.org");
251:                valueTest(info, true);
252:                info.setValue("joe-@apache.org");
253:                valueTest(info, true);
254:                info.setValue("joe_@apache.org");
255:                valueTest(info, true);
256:
257:                //UnQuoted Special characters are invalid
258:
259:                info.setValue("joe.@apache.org");
260:                valueTest(info, false);
261:                info.setValue("joe+@apache.org");
262:                valueTest(info, false);
263:                info.setValue("joe!@apache.org");
264:                valueTest(info, false);
265:                info.setValue("joe*@apache.org");
266:                valueTest(info, false);
267:                info.setValue("joe'@apache.org");
268:                valueTest(info, false);
269:                info.setValue("joe(@apache.org");
270:                valueTest(info, false);
271:                info.setValue("joe)@apache.org");
272:                valueTest(info, false);
273:                info.setValue("joe,@apache.org");
274:                valueTest(info, false);
275:                info.setValue("joe%45@apache.org");
276:                valueTest(info, false);
277:                info.setValue("joe;@apache.org");
278:                valueTest(info, false);
279:                info.setValue("joe?@apache.org");
280:                valueTest(info, false);
281:                info.setValue("joe&@apache.org");
282:                valueTest(info, false);
283:                info.setValue("joe=@apache.org");
284:                valueTest(info, false);
285:
286:                //Quoted Special characters are valid
287:                info.setValue("\"joe.\"@apache.org");
288:                valueTest(info, true);
289:                info.setValue("\"joe+\"@apache.org");
290:                valueTest(info, true);
291:                info.setValue("\"joe!\"@apache.org");
292:                valueTest(info, true);
293:                info.setValue("\"joe*\"@apache.org");
294:                valueTest(info, true);
295:                info.setValue("\"joe'\"@apache.org");
296:                valueTest(info, true);
297:                info.setValue("\"joe(\"@apache.org");
298:                valueTest(info, true);
299:                info.setValue("\"joe)\"@apache.org");
300:                valueTest(info, true);
301:                info.setValue("\"joe,\"@apache.org");
302:                valueTest(info, true);
303:                info.setValue("\"joe%45\"@apache.org");
304:                valueTest(info, true);
305:                info.setValue("\"joe;\"@apache.org");
306:                valueTest(info, true);
307:                info.setValue("\"joe?\"@apache.org");
308:                valueTest(info, true);
309:                info.setValue("\"joe&\"@apache.org");
310:                valueTest(info, true);
311:                info.setValue("\"joe=\"@apache.org");
312:                valueTest(info, true);
313:
314:            }
315:
316:            /**
317:             * These test values derive directly from RFC 822 &
318:             * Mail::RFC822::Address & RFC::RFC822::Address perl test.pl
319:             * For traceability don't combine these test values with other tests.
320:             */
321:            TestPair[] testEmailFromPerl = {
322:                    new TestPair("abigail@example.com", true),
323:                    new TestPair("abigail@example.com ", true),
324:                    new TestPair(" abigail@example.com", true),
325:                    new TestPair("abigail @example.com ", true),
326:                    new TestPair("*@example.net", true),
327:                    new TestPair("\"\\\"\"@foo.bar", true),
328:                    new TestPair("fred&barny@example.com", true),
329:                    new TestPair("---@example.com", true),
330:                    new TestPair("foo-bar@example.net", true),
331:                    new TestPair("\"127.0.0.1\"@[127.0.0.1]", true),
332:                    new TestPair("Abigail <abigail@example.com>", true),
333:                    new TestPair("Abigail<abigail@example.com>", true),
334:                    new TestPair("Abigail<@a,@b,@c:abigail@example.com>", true),
335:                    new TestPair("\"This is a phrase\"<abigail@example.com>",
336:                            true),
337:                    new TestPair("\"Abigail \"<abigail@example.com>", true),
338:                    new TestPair("\"Joe & J. Harvey\" <example @Org>", true),
339:                    new TestPair("Abigail <abigail @ example.com>", true),
340:                    new TestPair(
341:                            "Abigail made this <  abigail   @   example  .    com    >",
342:                            true),
343:                    new TestPair("Abigail(the bitch)@example.com", true),
344:                    new TestPair("Abigail <abigail @ example . (bar) com >",
345:                            true),
346:                    new TestPair(
347:                            "Abigail < (one)  abigail (two) @(three)example . (bar) com (quz) >",
348:                            true),
349:                    new TestPair(
350:                            "Abigail (foo) (((baz)(nested) (comment)) ! ) < (one)  abigail (two) @(three)example . (bar) com (quz) >",
351:                            true),
352:                    new TestPair("Abigail <abigail(fo\\(o)@example.com>", true),
353:                    new TestPair("Abigail <abigail(fo\\)o)@example.com> ", true),
354:                    new TestPair("(foo) abigail@example.com", true),
355:                    new TestPair("abigail@example.com (foo)", true),
356:                    new TestPair("\"Abi\\\"gail\" <abigail@example.com>", true),
357:                    new TestPair("abigail@[example.com]", true),
358:                    new TestPair("abigail@[exa\\[ple.com]", true),
359:                    new TestPair("abigail@[exa\\]ple.com]", true),
360:                    new TestPair("\":sysmail\"@  Some-Group. Some-Org", true),
361:                    new TestPair(
362:                            "Muhammed.(I am  the greatest) Ali @(the)Vegas.WBA",
363:                            true),
364:                    new TestPair("mailbox.sub1.sub2@this-domain", true),
365:                    new TestPair("sub-net.mailbox@sub-domain.domain", true),
366:                    new TestPair("name:;", true),
367:                    new TestPair("':;", true),
368:                    new TestPair("name:   ;", true),
369:                    new TestPair("Alfred Neuman <Neuman@BBN-TENEXA>", true),
370:                    new TestPair("Neuman@BBN-TENEXA", true),
371:                    new TestPair("\"George, Ted\" <Shared@Group.Arpanet>", true),
372:                    new TestPair("Wilt . (the  Stilt) Chamberlain@NBA.US", true),
373:                    new TestPair("Cruisers:  Port@Portugal, Jones@SEA;", true),
374:                    new TestPair("$@[]", true),
375:                    new TestPair("*()@[]", true),
376:                    new TestPair(
377:                            "\"quoted ( brackets\" ( a comment )@example.com",
378:                            true),
379:                    new TestPair(
380:                            "\"Joe & J. Harvey\"\\x0D\\x0A     <ddd\\@ Org>",
381:                            true),
382:                    new TestPair("\"Joe &\\x0D\\x0A J. Harvey\" <ddd \\@ Org>",
383:                            true),
384:                    new TestPair(
385:                            "Gourmets:  Pompous Person <WhoZiWhatZit\\@Cordon-Bleu>,\\x0D\\x0A"
386:                                    + "        Childs\\@WGBH.Boston, \"Galloping Gourmet\"\\@\\x0D\\x0A"
387:                                    + "        ANT.Down-Under (Australian National Television),\\x0D\\x0A"
388:                                    + "        Cheapie\\@Discount-Liquors;",
389:                            true),
390:                    new TestPair("   Just a string", false),
391:                    new TestPair("string", false),
392:                    new TestPair("(comment)", false),
393:                    new TestPair("()@example.com", false),
394:                    new TestPair("fred(&)barny@example.com", false),
395:                    new TestPair("fred\\ barny@example.com", false),
396:                    new TestPair("Abigail <abi gail @ example.com>", false),
397:                    new TestPair("Abigail <abigail(fo(o)@example.com>", false),
398:                    new TestPair("Abigail <abigail(fo)o)@example.com>", false),
399:                    new TestPair("\"Abi\"gail\" <abigail@example.com>", false),
400:                    new TestPair("abigail@[exa]ple.com]", false),
401:                    new TestPair("abigail@[exa[ple.com]", false),
402:                    new TestPair("abigail@[exaple].com]", false),
403:                    new TestPair("abigail@", false),
404:                    new TestPair("@example.com", false),
405:                    new TestPair(
406:                            "phrase: abigail@example.com abigail@example.com ;",
407:                            false),
408:                    new TestPair("invalid£char@example.com", false) };
409:
410:            /**
411:             * Write this test based on perl Mail::RFC822::Address
412:             * which takes its example email address directly from RFC822
413:             * 
414:             * @throws ValidatorException
415:             * 
416:             * FIXME This test fails so disable it with a leading _ for 1.1.4 release.
417:             * The real solution is to fix the email parsing.
418:             */
419:            public void _testEmailFromPerl() throws ValidatorException {
420:                ValueBean info = new ValueBean();
421:                for (int index = 0; index < testEmailFromPerl.length; index++) {
422:                    info.setValue(testEmailFromPerl[index].item);
423:                    valueTest(info, testEmailFromPerl[index].valid);
424:                }
425:            }
426:
427:            /**
428:             * Utlity class to run a test on a value.
429:             *
430:             * @param info	Value to run test on.
431:             * @param passed	Whether or not the test is expected to pass.
432:             */
433:            private void valueTest(ValueBean info, boolean passed)
434:                    throws ValidatorException {
435:                // Construct validator based on the loaded resources 
436:                // and the form key
437:                Validator validator = new Validator(resources, FORM_KEY);
438:                // add the name bean to the validator as a resource 
439:                // for the validations to be performed on.
440:                validator.setParameter(Validator.BEAN_PARAM, info);
441:
442:                // Get results of the validation.
443:                ValidatorResults results = null;
444:
445:                // throws ValidatorException, 
446:                // but we aren't catching for testing 
447:                // since no validation methods we use 
448:                // throw this
449:                results = validator.validate();
450:
451:                assertNotNull("Results are null.", results);
452:
453:                ValidatorResult result = results.getValidatorResult("value");
454:
455:                assertNotNull(ACTION
456:                        + " value ValidatorResult should not be null.", result);
457:                assertTrue("Value " + info.getValue()
458:                        + " ValidatorResult should contain the '" + ACTION
459:                        + "' action.", result.containsAction(ACTION));
460:                assertTrue("Value " + info.getValue()
461:                        + "ValidatorResult for the '" + ACTION
462:                        + "' action should have "
463:                        + (passed ? "passed" : "failed") + ".",
464:                        (passed ? result.isValid(ACTION) : !result
465:                                .isValid(ACTION)));
466:            }
467:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.