Source Code Cross Referenced for ValidatorResultsTest.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:        import java.util.Iterator;
021:        import junit.framework.Test;
022:        import junit.framework.TestSuite;
023:
024:        import org.xml.sax.SAXException;
025:
026:        /**
027:         * Test ValidatorResults.
028:         *
029:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
030:         */
031:        public class ValidatorResultsTest extends TestCommon {
032:
033:            private static final String FORM_KEY = "nameForm";
034:            private static final String firstNameField = "firstName";
035:            private static final String middleNameField = "middleName";
036:            private static final String lastNameField = "lastName";
037:
038:            private String firstName;
039:            private String middleName;
040:            private String lastName;
041:
042:            /**
043:             * Constructor.
044:             */
045:            public ValidatorResultsTest(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
056:                        .main(new String[] { ValidatorResultsTest.class
057:                                .getName() });
058:            }
059:
060:            /**
061:             * @return a test suite (<code>TestSuite</code>) that includes all methods
062:             *         starting with "test"
063:             */
064:            public static Test suite() {
065:                // All methods starting with "test" will be executed in the test suite.
066:                return new TestSuite(ValidatorResultsTest.class);
067:            }
068:
069:            /**
070:             * Load <code>ValidatorResources</code> from
071:             * ValidatorResultsTest-config.xml.
072:             */
073:            protected void setUp() throws IOException, SAXException {
074:                // Load resources
075:                loadResources("ValidatorResultsTest-config.xml");
076:
077:                // initialize values
078:                firstName = "foo";
079:                middleName = "123";
080:                lastName = "456";
081:
082:            }
083:
084:            protected void tearDown() {
085:            }
086:
087:            /**
088:             * Test all validations ran and passed.
089:             */
090:            public void testAllValid() throws ValidatorException {
091:
092:                // Create bean to run test on.
093:                NameBean bean = createNameBean();
094:
095:                // Validate.
096:                ValidatorResults results = validate(bean);
097:
098:                // Check results
099:                checkValidatorResult(results, firstNameField, "required", true);
100:                checkValidatorResult(results, middleNameField, "required", true);
101:                checkValidatorResult(results, middleNameField, "int", true);
102:                checkValidatorResult(results, middleNameField, "positive", true);
103:                checkValidatorResult(results, lastNameField, "required", true);
104:                checkValidatorResult(results, lastNameField, "int", true);
105:
106:            }
107:
108:            /**
109:             * Test some validations failed and some didn't run.
110:             */
111:            public void testErrors() throws ValidatorException {
112:
113:                middleName = "XXX";
114:                lastName = null;
115:
116:                // Create bean to run test on.
117:                NameBean bean = createNameBean();
118:
119:                // Validate.
120:                ValidatorResults results = validate(bean);
121:
122:                // Check results
123:                checkValidatorResult(results, firstNameField, "required", true);
124:                checkValidatorResult(results, middleNameField, "required", true);
125:                checkValidatorResult(results, middleNameField, "int", false);
126:                checkNotRun(results, middleNameField, "positive");
127:                checkValidatorResult(results, lastNameField, "required", false);
128:                checkNotRun(results, lastNameField, "int");
129:
130:            }
131:
132:            /**
133:             * Check a validator has not been run for a field and the result.
134:             */
135:            private void checkNotRun(ValidatorResults results, String field,
136:                    String action) {
137:                ValidatorResult result = results.getValidatorResult(field);
138:                assertNotNull(field + " result", result);
139:                assertFalse(field + "[" + action + "] run", result
140:                        .containsAction(action));
141:                // System.out.println(field + "[" + action + "] not run");
142:            }
143:
144:            /**
145:             * Check a validator has run for a field and the result.
146:             */
147:            private void checkValidatorResult(ValidatorResults results,
148:                    String field, String action, boolean expected) {
149:                ValidatorResult result = results.getValidatorResult(field);
150:                // System.out.println(field + "[" + action + "]=" + result.isValid(action));
151:                assertNotNull(field + " result", result);
152:                assertTrue(field + "[" + action + "] not run", result
153:                        .containsAction(action));
154:                assertEquals(field + "[" + action + "] result", expected,
155:                        result.isValid(action));
156:            }
157:
158:            /**
159:             * Create a NameBean.
160:             */
161:            private NameBean createNameBean() {
162:                NameBean name = new NameBean();
163:                name.setFirstName(firstName);
164:                name.setMiddleName(middleName);
165:                name.setLastName(lastName);
166:                return name;
167:            }
168:
169:            /**
170:             * Validate results.
171:             */
172:            private ValidatorResults validate(Object bean)
173:                    throws ValidatorException {
174:
175:                // Construct validator based on the loaded resources
176:                // and the form key
177:                Validator validator = new Validator(resources, FORM_KEY);
178:
179:                // add the name bean to the validator as a resource
180:                // for the validations to be performed on.
181:                validator.setParameter(Validator.BEAN_PARAM, bean);
182:
183:                // Get results of the validation.
184:                ValidatorResults results = validator.validate();
185:
186:                return results;
187:
188:            }
189:
190:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.