KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RequiredNameTest.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22
23 package org.apache.commons.validator;
24
25 import java.io.IOException JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException JavaDoc;
31
32                                                           
33 /**
34  * Performs Validation Test.
35  */

36 public class RequiredNameTest extends TestCommon {
37    
38    /**
39     * The key used to retrieve the set of validation
40     * rules from the xml file.
41     */

42    protected static String JavaDoc FORM_KEY = "nameForm";
43
44    /**
45     * The key used to retrieve the validator action.
46     */

47    protected static String JavaDoc ACTION = "required";
48
49    public RequiredNameTest(String JavaDoc name) {
50        super(name);
51    }
52
53    /**
54     * Start the tests.
55     *
56     * @param theArgs the arguments. Not used
57     */

58    public static void main(String JavaDoc[] theArgs) {
59        junit.awtui.TestRunner.main(new String JavaDoc[] {RequiredNameTest.class.getName()});
60    }
61
62    /**
63     * @return a test suite (<code>TestSuite</code>) that includes all methods
64     * starting with "test"
65     */

66    public static Test suite() {
67        // All methods starting with "test" will be executed in the test suite.
68
return new TestSuite(RequiredNameTest.class);
69    }
70
71    /**
72     * Load <code>ValidatorResources</code> from
73     * validator-name-required.xml.
74     */

75    protected void setUp() throws IOException JavaDoc, SAXException JavaDoc {
76       // Load resources
77
loadResources("validator-name-required.xml");
78    }
79
80    protected void tearDown() {
81    }
82
83    /**
84     * Tests the required validation failure.
85     */

86    public void testRequired() throws ValidatorException {
87       // Create bean to run test on.
88
NameBean name = new NameBean();
89       
90       // Construct validator based on the loaded resources
91
// and the form key
92
Validator validator = new Validator(resources, FORM_KEY);
93       // add the name bean to the validator as a resource
94
// for the validations to be performed on.
95
validator.setParameter(Validator.BEAN_PARAM, name);
96
97       // Get results of the validation.
98
ValidatorResults results = null;
99       
100       // throws ValidatorException,
101
// but we aren't catching for testing
102
// since no validation methods we use
103
// throw this
104
results = validator.validate();
105       
106       assertNotNull("Results are null.", results);
107       
108       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
109       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
110       
111       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
112       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
113       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
114       
115       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
116       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
117       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
118    }
119
120    /**
121     * Tests the required validation for first name if it is blank.
122     */

123    public void testRequiredFirstNameBlank() throws ValidatorException {
124       // Create bean to run test on.
125
NameBean name = new NameBean();
126       name.setFirstName("");
127       
128       // Construct validator based on the loaded resources
129
// and the form key
130
Validator validator = new Validator(resources, FORM_KEY);
131       // add the name bean to the validator as a resource
132
// for the validations to be performed on.
133
validator.setParameter(Validator.BEAN_PARAM, name);
134
135       // Get results of the validation.
136
ValidatorResults results = null;
137       
138       results = validator.validate();
139       
140       assertNotNull("Results are null.", results);
141       
142       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
143       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
144       
145       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
146       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
147       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
148       
149       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
150       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
151       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
152    }
153
154    /**
155     * Tests the required validation for first name.
156     */

157    public void testRequiredFirstName() throws ValidatorException {
158       // Create bean to run test on.
159
NameBean name = new NameBean();
160       name.setFirstName("Joe");
161       
162       // Construct validator based on the loaded resources
163
// and the form key
164
Validator validator = new Validator(resources, FORM_KEY);
165       // add the name bean to the validator as a resource
166
// for the validations to be performed on.
167
validator.setParameter(Validator.BEAN_PARAM, name);
168
169       // Get results of the validation.
170
ValidatorResults results = null;
171       
172       results = validator.validate();
173       
174       assertNotNull("Results are null.", results);
175       
176       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
177       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
178       
179       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
180       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
181       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
182       
183       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
184       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
185       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
186    }
187
188    /**
189     * Tests the required validation for last name if it is blank.
190     */

191    public void testRequiredLastNameBlank() throws ValidatorException {
192       // Create bean to run test on.
193
NameBean name = new NameBean();
194       name.setLastName("");
195       
196       // Construct validator based on the loaded resources
197
// and the form key
198
Validator validator = new Validator(resources, FORM_KEY);
199       // add the name bean to the validator as a resource
200
// for the validations to be performed on.
201
validator.setParameter(Validator.BEAN_PARAM, name);
202
203       // Get results of the validation.
204
ValidatorResults results = null;
205       
206       results = validator.validate();
207       
208       assertNotNull("Results are null.", results);
209       
210       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
211       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
212       
213       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
214       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
215       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
216       
217       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
218       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
219       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
220    }
221
222    /**
223     * Tests the required validation for last name.
224     */

225    public void testRequiredLastName() throws ValidatorException {
226       // Create bean to run test on.
227
NameBean name = new NameBean();
228       name.setLastName("Smith");
229       
230       // Construct validator based on the loaded resources
231
// and the form key
232
Validator validator = new Validator(resources, FORM_KEY);
233       // add the name bean to the validator as a resource
234
// for the validations to be performed on.
235
validator.setParameter(Validator.BEAN_PARAM, name);
236
237       // Get results of the validation.
238
ValidatorResults results = null;
239       
240       results = validator.validate();
241       
242       assertNotNull("Results are null.", results);
243       
244       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
245       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
246       
247       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
248       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
249       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
250       
251       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
252       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
253       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
254
255    }
256
257    /**
258     * Tests the required validation for first and last name.
259     */

260    public void testRequiredName() throws ValidatorException {
261       // Create bean to run test on.
262
NameBean name = new NameBean();
263       name.setFirstName("Joe");
264       name.setLastName("Smith");
265       
266       // Construct validator based on the loaded resources
267
// and the form key
268
Validator validator = new Validator(resources, FORM_KEY);
269       // add the name bean to the validator as a resource
270
// for the validations to be performed on.
271
validator.setParameter(Validator.BEAN_PARAM, name);
272
273       // Get results of the validation.
274
ValidatorResults results = null;
275       
276       results = validator.validate();
277       
278       assertNotNull("Results are null.", results);
279       
280       ValidatorResult firstNameResult = results.getValidatorResult("firstName");
281       ValidatorResult lastNameResult = results.getValidatorResult("lastName");
282       
283       assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
284       assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
285       assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
286       
287       assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
288       assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
289       assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
290    }
291    
292 }
Popular Tags