Source Code Cross Referenced for MultipleTests.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.
028:         *
029:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
030:         */
031:        public class MultipleTests extends TestCommon {
032:
033:            /**
034:             * The key used to retrieve the set of validation
035:             * rules from the xml file.
036:             */
037:            protected static String FORM_KEY = "nameForm";
038:
039:            /**
040:             * The key used to retrieve the validator action.
041:             */
042:            protected static String ACTION = "required";
043:
044:            public MultipleTests(String name) {
045:                super (name);
046:            }
047:
048:            /**
049:             * Start the tests.
050:             *
051:             * @param theArgs the arguments. Not used
052:             */
053:            public static void main(String[] theArgs) {
054:                junit.awtui.TestRunner.main(new String[] { MultipleTests.class
055:                        .getName() });
056:            }
057:
058:            /**
059:             * @return a test suite (<code>TestSuite</code>) that includes all methods
060:             *         starting with "test"
061:             */
062:            public static Test suite() {
063:                // All methods starting with "test" will be executed in the test suite.
064:                return new TestSuite(MultipleTests.class);
065:            }
066:
067:            /**
068:             * Load <code>ValidatorResources</code> from
069:             * validator-multipletest.xml.
070:             */
071:            protected void setUp() throws IOException, SAXException {
072:                // Load resources
073:                loadResources("MultipleTests-config.xml");
074:            }
075:
076:            protected void tearDown() {
077:            }
078:
079:            /**
080:             * With nothing provided, we should fail both because both are required.
081:             */
082:            public void testBothBlank() throws ValidatorException {
083:                // Create bean to run test on.
084:                NameBean name = new NameBean();
085:
086:                // Construct validator based on the loaded resources
087:                // and the form key
088:                Validator validator = new Validator(resources, FORM_KEY);
089:                // add the name bean to the validator as a resource
090:                // for the validations to be performed on.
091:                validator.setParameter(Validator.BEAN_PARAM, name);
092:
093:                // Get results of the validation.
094:                ValidatorResults results = null;
095:
096:                // throws ValidatorException,
097:                // but we aren't catching for testing
098:                // since no validation methods we use
099:                // throw this
100:                results = validator.validate();
101:
102:                assertNotNull("Results are null.", results);
103:
104:                ValidatorResult firstNameResult = results
105:                        .getValidatorResult("firstName");
106:                ValidatorResult lastNameResult = results
107:                        .getValidatorResult("lastName");
108:
109:                assertNotNull("First Name ValidatorResult should not be null.",
110:                        firstNameResult);
111:                assertTrue("First Name ValidatorResult should contain the '"
112:                        + ACTION + "' action.", firstNameResult
113:                        .containsAction(ACTION));
114:                assertTrue("First Name ValidatorResult for the '" + ACTION
115:                        + "' action should have failed.", !firstNameResult
116:                        .isValid(ACTION));
117:
118:                assertNotNull("Last Name ValidatorResult should not be null.",
119:                        lastNameResult);
120:                assertTrue("Last Name ValidatorResult should contain the '"
121:                        + ACTION + "' action.", lastNameResult
122:                        .containsAction(ACTION));
123:                assertTrue("Last Name ValidatorResult for the '" + ACTION
124:                        + "' action should have failed.", !lastNameResult
125:                        .isValid(ACTION));
126:                assertTrue(
127:                        "Last Name ValidatorResults should not contain the 'int' action.",
128:                        !lastNameResult.containsAction("int"));
129:            }
130:
131:            /**
132:             * If the first name fails required, and the second test fails int, we should get two errors.
133:             */
134:            public void testRequiredFirstNameBlankLastNameShort()
135:                    throws ValidatorException {
136:                // Create bean to run test on.
137:                NameBean name = new NameBean();
138:                name.setFirstName("");
139:                name.setLastName("Test");
140:
141:                // Construct validator based on the loaded resources
142:                // and the form key
143:                Validator validator = new Validator(resources, FORM_KEY);
144:                // add the name bean to the validator as a resource
145:                // for the validations to be performed on.
146:                validator.setParameter(Validator.BEAN_PARAM, name);
147:
148:                // Get results of the validation.
149:                ValidatorResults results = null;
150:
151:                results = validator.validate();
152:
153:                assertNotNull("Results are null.", results);
154:
155:                ValidatorResult firstNameResult = results
156:                        .getValidatorResult("firstName");
157:                ValidatorResult lastNameResult = results
158:                        .getValidatorResult("lastName");
159:
160:                assertNotNull("First Name ValidatorResult should not be null.",
161:                        firstNameResult);
162:                assertTrue("First Name ValidatorResult should contain the '"
163:                        + ACTION + "' action.", firstNameResult
164:                        .containsAction(ACTION));
165:                assertTrue("First Name ValidatorResult for the '" + ACTION
166:                        + "' action should have failed.", !firstNameResult
167:                        .isValid(ACTION));
168:
169:                assertNotNull("Last Name ValidatorResult should not be null.",
170:                        lastNameResult);
171:                assertTrue(
172:                        "Last Name ValidatorResult should contain the 'int' action.",
173:                        lastNameResult.containsAction("int"));
174:                assertTrue(
175:                        "Last Name ValidatorResult for the 'int' action should have failed.",
176:                        !lastNameResult.isValid("int"));
177:            }
178:
179:            /**
180:             * If the first name is there, and the last name fails int, we should get one error.
181:             */
182:            public void testRequiredLastNameShort() throws ValidatorException {
183:                // Create bean to run test on.
184:                NameBean name = new NameBean();
185:                name.setFirstName("Test");
186:                name.setLastName("Test");
187:
188:                // Construct validator based on the loaded resources
189:                // and the form key
190:                Validator validator = new Validator(resources, FORM_KEY);
191:                // add the name bean to the validator as a resource
192:                // for the validations to be performed on.
193:                validator.setParameter(Validator.BEAN_PARAM, name);
194:
195:                // Get results of the validation.
196:                ValidatorResults results = null;
197:
198:                results = validator.validate();
199:
200:                assertNotNull("Results are null.", results);
201:
202:                ValidatorResult firstNameResult = results
203:                        .getValidatorResult("firstName");
204:                ValidatorResult lastNameResult = results
205:                        .getValidatorResult("lastName");
206:
207:                assertNotNull("First Name ValidatorResult should not be null.",
208:                        firstNameResult);
209:                assertTrue("First Name ValidatorResult should contain the '"
210:                        + ACTION + "' action.", firstNameResult
211:                        .containsAction(ACTION));
212:                assertTrue("First Name ValidatorResult for the '" + ACTION
213:                        + "' action should have passed.", firstNameResult
214:                        .isValid(ACTION));
215:
216:                assertNotNull("Last Name ValidatorResult should not be null.",
217:                        lastNameResult);
218:                assertTrue(
219:                        "Last Name ValidatorResult should contain the 'int' action.",
220:                        lastNameResult.containsAction("int"));
221:                assertTrue(
222:                        "Last Name ValidatorResult for the 'int' action should have failed.",
223:                        !lastNameResult.isValid("int"));
224:            }
225:
226:            /**
227:             * If first name is ok and last name is ok and is an int, no errors.
228:             */
229:            public void testRequiredLastNameLong() throws ValidatorException {
230:                // Create bean to run test on.
231:                NameBean name = new NameBean();
232:                name.setFirstName("Joe");
233:                name.setLastName("12345678");
234:
235:                // Construct validator based on the loaded resources
236:                // and the form key
237:                Validator validator = new Validator(resources, FORM_KEY);
238:                // add the name bean to the validator as a resource
239:                // for the validations to be performed on.
240:                validator.setParameter(Validator.BEAN_PARAM, name);
241:
242:                // Get results of the validation.
243:                ValidatorResults results = null;
244:
245:                results = validator.validate();
246:
247:                assertNotNull("Results are null.", results);
248:
249:                ValidatorResult firstNameResult = results
250:                        .getValidatorResult("firstName");
251:                ValidatorResult lastNameResult = results
252:                        .getValidatorResult("lastName");
253:
254:                assertNotNull("First Name ValidatorResult should not be null.",
255:                        firstNameResult);
256:                assertTrue("First Name ValidatorResult should contain the '"
257:                        + ACTION + "' action.", firstNameResult
258:                        .containsAction(ACTION));
259:                assertTrue("First Name ValidatorResult for the '" + ACTION
260:                        + "' action should have passed.", firstNameResult
261:                        .isValid(ACTION));
262:
263:                assertNotNull("Last Name ValidatorResult should not be null.",
264:                        lastNameResult);
265:                assertTrue(
266:                        "Last Name ValidatorResult should contain the 'int' action.",
267:                        lastNameResult.containsAction("int"));
268:                assertTrue(
269:                        "Last Name ValidatorResult for the 'int' action should have passed.",
270:                        lastNameResult.isValid("int"));
271:            }
272:
273:            /**
274:             * If middle name is not there, then the required dependent test should fail.
275:             * No other tests should run
276:             *
277:             * @throws ValidatorException
278:             */
279:            public void testFailingFirstDependentValidator()
280:                    throws ValidatorException {
281:                // Create bean to run test on.
282:                NameBean name = new NameBean();
283:
284:                // Construct validator based on the loaded resources
285:                // and the form key
286:                Validator validator = new Validator(resources, FORM_KEY);
287:                // add the name bean to the validator as a resource
288:                // for the validations to be performed on.
289:                validator.setParameter(Validator.BEAN_PARAM, name);
290:
291:                // Get results of the validation.
292:                ValidatorResults results = null;
293:
294:                results = validator.validate();
295:
296:                assertNotNull("Results are null.", results);
297:
298:                ValidatorResult middleNameResult = results
299:                        .getValidatorResult("middleName");
300:
301:                assertNotNull(
302:                        "Middle Name ValidatorResult should not be null.",
303:                        middleNameResult);
304:
305:                assertTrue(
306:                        "Middle Name ValidatorResult should contain the 'required' action.",
307:                        middleNameResult.containsAction("required"));
308:                assertTrue(
309:                        "Middle Name ValidatorResult for the 'required' action should have failed",
310:                        !middleNameResult.isValid("required"));
311:
312:                assertTrue(
313:                        "Middle Name ValidatorResult should not contain the 'int' action.",
314:                        !middleNameResult.containsAction("int"));
315:
316:                assertTrue(
317:                        "Middle Name ValidatorResult should not contain the 'positive' action.",
318:                        !middleNameResult.containsAction("positive"));
319:            }
320:
321:            /**
322:             * If middle name is there but not int, then the required dependent test
323:             * should pass, but the int dependent test should fail. No other tests should
324:             * run.
325:             *
326:             * @throws ValidatorException
327:             */
328:            public void testFailingNextDependentValidator()
329:                    throws ValidatorException {
330:                // Create bean to run test on.
331:                NameBean name = new NameBean();
332:                name.setMiddleName("TEST");
333:
334:                // Construct validator based on the loaded resources
335:                // and the form key
336:                Validator validator = new Validator(resources, FORM_KEY);
337:                // add the name bean to the validator as a resource
338:                // for the validations to be performed on.
339:                validator.setParameter(Validator.BEAN_PARAM, name);
340:
341:                // Get results of the validation.
342:                ValidatorResults results = null;
343:
344:                results = validator.validate();
345:
346:                assertNotNull("Results are null.", results);
347:
348:                ValidatorResult middleNameResult = results
349:                        .getValidatorResult("middleName");
350:
351:                assertNotNull(
352:                        "Middle Name ValidatorResult should not be null.",
353:                        middleNameResult);
354:
355:                assertTrue(
356:                        "Middle Name ValidatorResult should contain the 'required' action.",
357:                        middleNameResult.containsAction("required"));
358:                assertTrue(
359:                        "Middle Name ValidatorResult for the 'required' action should have passed",
360:                        middleNameResult.isValid("required"));
361:
362:                assertTrue(
363:                        "Middle Name ValidatorResult should contain the 'int' action.",
364:                        middleNameResult.containsAction("int"));
365:                assertTrue(
366:                        "Middle Name ValidatorResult for the 'int' action should have failed",
367:                        !middleNameResult.isValid("int"));
368:
369:                assertTrue(
370:                        "Middle Name ValidatorResult should not contain the 'positive' action.",
371:                        !middleNameResult.containsAction("positive"));
372:            }
373:
374:            /**
375:             * If middle name is there and a negative int, then the required and int
376:             * dependent tests should pass, but the positive test should fail.
377:             *
378:             * @throws ValidatorException
379:             */
380:            public void testPassingDependentsFailingMain()
381:                    throws ValidatorException {
382:                // Create bean to run test on.
383:                NameBean name = new NameBean();
384:                name.setMiddleName("-2534");
385:
386:                // Construct validator based on the loaded resources
387:                // and the form key
388:                Validator validator = new Validator(resources, FORM_KEY);
389:                // add the name bean to the validator as a resource
390:                // for the validations to be performed on.
391:                validator.setParameter(Validator.BEAN_PARAM, name);
392:
393:                // Get results of the validation.
394:                ValidatorResults results = null;
395:
396:                results = validator.validate();
397:
398:                assertNotNull("Results are null.", results);
399:
400:                ValidatorResult middleNameResult = results
401:                        .getValidatorResult("middleName");
402:
403:                assertNotNull(
404:                        "Middle Name ValidatorResult should not be null.",
405:                        middleNameResult);
406:
407:                assertTrue(
408:                        "Middle Name ValidatorResult should contain the 'required' action.",
409:                        middleNameResult.containsAction("required"));
410:                assertTrue(
411:                        "Middle Name ValidatorResult for the 'required' action should have passed",
412:                        middleNameResult.isValid("required"));
413:
414:                assertTrue(
415:                        "Middle Name ValidatorResult should contain the 'int' action.",
416:                        middleNameResult.containsAction("int"));
417:                assertTrue(
418:                        "Middle Name ValidatorResult for the 'int' action should have passed",
419:                        middleNameResult.isValid("int"));
420:
421:                assertTrue(
422:                        "Middle Name ValidatorResult should contain the 'positive' action.",
423:                        middleNameResult.containsAction("positive"));
424:                assertTrue(
425:                        "Middle Name ValidatorResult for the 'positive' action should have failed",
426:                        !middleNameResult.isValid("positive"));
427:            }
428:
429:            /**
430:             * If middle name is there and a positve int, then the required and int
431:             * dependent tests should pass, and the positive test should pass.
432:             *
433:             * @throws ValidatorException
434:             */
435:            public void testPassingDependentsPassingMain()
436:                    throws ValidatorException {
437:                // Create bean to run test on.
438:                NameBean name = new NameBean();
439:                name.setMiddleName("2534");
440:
441:                // Construct validator based on the loaded resources
442:                // and the form key
443:                Validator validator = new Validator(resources, FORM_KEY);
444:                // add the name bean to the validator as a resource
445:                // for the validations to be performed on.
446:                validator.setParameter(Validator.BEAN_PARAM, name);
447:
448:                // Get results of the validation.
449:                ValidatorResults results = null;
450:
451:                results = validator.validate();
452:
453:                assertNotNull("Results are null.", results);
454:
455:                ValidatorResult middleNameResult = results
456:                        .getValidatorResult("middleName");
457:
458:                assertNotNull(
459:                        "Middle Name ValidatorResult should not be null.",
460:                        middleNameResult);
461:
462:                assertTrue(
463:                        "Middle Name ValidatorResult should contain the 'required' action.",
464:                        middleNameResult.containsAction("required"));
465:                assertTrue(
466:                        "Middle Name ValidatorResult for the 'required' action should have passed",
467:                        middleNameResult.isValid("required"));
468:
469:                assertTrue(
470:                        "Middle Name ValidatorResult should contain the 'int' action.",
471:                        middleNameResult.containsAction("int"));
472:                assertTrue(
473:                        "Middle Name ValidatorResult for the 'int' action should have passed",
474:                        middleNameResult.isValid("int"));
475:
476:                assertTrue(
477:                        "Middle Name ValidatorResult should contain the 'positive' action.",
478:                        middleNameResult.containsAction("positive"));
479:                assertTrue(
480:                        "Middle Name ValidatorResult for the 'positive' action should have passed",
481:                        middleNameResult.isValid("positive"));
482:            }
483:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.