1 /*
2 * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/MultipleTests.java,v 1.15 2004/02/21 17:10:30 rleland Exp $
3 * $Revision: 1.15 $
4 * $Date: 2004/02/21 17:10:30 $
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
23 package org.apache.commons.validator;
24
25 import java.io.IOException;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException;
31
32 /***
33 * Performs Validation Test.
34 */
35 public class MultipleTests extends TestCommon {
36
37 /***
38 * The key used to retrieve the set of validation
39 * rules from the xml file.
40 */
41 protected static String FORM_KEY = "nameForm";
42
43 /***
44 * The key used to retrieve the validator action.
45 */
46 protected static String ACTION = "required";
47
48
49
50 public MultipleTests(String name) {
51 super(name);
52 }
53
54 /***
55 * Start the tests.
56 *
57 * @param theArgs the arguments. Not used
58 */
59 public static void main(String[] theArgs) {
60 junit.awtui.TestRunner.main(new String[] {MultipleTests.class.getName()});
61 }
62
63 /***
64 * @return a test suite (<code>TestSuite</code>) that includes all methods
65 * starting with "test"
66 */
67 public static Test suite() {
68 // All methods starting with "test" will be executed in the test suite.
69 return new TestSuite(MultipleTests.class);
70 }
71
72 /***
73 * Load <code>ValidatorResources</code> from
74 * validator-multipletest.xml.
75 */
76 protected void setUp() throws IOException, SAXException {
77 // Load resources
78 loadResources("validator-multipletest.xml");
79 }
80
81 protected void tearDown() {
82 }
83
84 /***
85 * With nothing provided, we should fail both because both are required.
86 */
87 public void testBothBlank() throws ValidatorException {
88 // Create bean to run test on.
89 NameBean name = new NameBean();
90
91 // Construct validator based on the loaded resources
92 // and the form key
93 Validator validator = new Validator(resources, FORM_KEY);
94 // add the name bean to the validator as a resource
95 // for the validations to be performed on.
96 validator.setParameter(Validator.BEAN_PARAM, name);
97
98 // Get results of the validation.
99 ValidatorResults results = null;
100
101 // throws ValidatorException,
102 // but we aren't catching for testing
103 // since no validation methods we use
104 // throw this
105 results = validator.validate();
106
107 assertNotNull("Results are null.", results);
108
109 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
110 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
111
112 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
113 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
114 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
115
116 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
117 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
118 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
119 assertTrue("Last Name ValidatorResults should not contain the 'int' action.", !lastNameResult.containsAction("int"));
120 }
121
122 /***
123 * If the first name fails required, and the second test fails int, we should get two errors.
124 */
125 public void testRequiredFirstNameBlankLastNameShort() throws ValidatorException {
126 // Create bean to run test on.
127 NameBean name = new NameBean();
128 name.setFirstName("");
129 name.setLastName("Test");
130
131 // Construct validator based on the loaded resources
132 // and the form key
133 Validator validator = new Validator(resources, FORM_KEY);
134 // add the name bean to the validator as a resource
135 // for the validations to be performed on.
136 validator.setParameter(Validator.BEAN_PARAM, name);
137
138 // Get results of the validation.
139 ValidatorResults results = null;
140
141 results = validator.validate();
142
143 assertNotNull("Results are null.", results);
144
145 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
146 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
147
148 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
149 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
150 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
151
152 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
153 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
154 assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int"));
155 }
156
157 /***
158 * If the first name is there, and the last name fails int, we should get one error.
159 */
160 public void testRequiredLastNameShort() throws ValidatorException {
161 // Create bean to run test on.
162 NameBean name = new NameBean();
163 name.setFirstName("Test");
164 name.setLastName("Test");
165
166 // Construct validator based on the loaded resources
167 // and the form key
168 Validator validator = new Validator(resources, FORM_KEY);
169 // add the name bean to the validator as a resource
170 // for the validations to be performed on.
171 validator.setParameter(Validator.BEAN_PARAM, name);
172
173 // Get results of the validation.
174 ValidatorResults results = null;
175
176 results = validator.validate();
177
178 assertNotNull("Results are null.", results);
179
180 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
181 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
182
183 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
184 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
185 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
186
187 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
188 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
189 assertTrue("Last Name ValidatorResult for the 'int' action should have failed.", !lastNameResult.isValid("int"));
190 }
191
192 /***
193 * If first name is ok and last name is ok and is an int, no errors.
194 */
195 public void testRequiredLastNameLong() throws ValidatorException {
196 // Create bean to run test on.
197 NameBean name = new NameBean();
198 name.setFirstName("Joe");
199 name.setLastName("12345678");
200
201 // Construct validator based on the loaded resources
202 // and the form key
203 Validator validator = new Validator(resources, FORM_KEY);
204 // add the name bean to the validator as a resource
205 // for the validations to be performed on.
206 validator.setParameter(Validator.BEAN_PARAM, name);
207
208 // Get results of the validation.
209 ValidatorResults results = null;
210
211 results = validator.validate();
212
213 assertNotNull("Results are null.", results);
214
215 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
216 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
217
218 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
219 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
220 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
221
222 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
223 assertTrue("Last Name ValidatorResult should contain the 'int' action.", lastNameResult.containsAction("int"));
224 assertTrue("Last Name ValidatorResult for the 'int' action should have passed.", lastNameResult.isValid("int"));
225 }
226
227 /***
228 * If middle name is not there, then the required dependent test should fail.
229 * No other tests should run
230 *
231 * @throws ValidatorException
232 */
233 public void testFailingFirstDependentValidator() throws ValidatorException {
234 // Create bean to run test on.
235 NameBean name = new NameBean();
236
237 // Construct validator based on the loaded resources
238 // and the form key
239 Validator validator = new Validator(resources, FORM_KEY);
240 // add the name bean to the validator as a resource
241 // for the validations to be performed on.
242 validator.setParameter(Validator.BEAN_PARAM, name);
243
244 // Get results of the validation.
245 ValidatorResults results = null;
246
247 results = validator.validate();
248
249 assertNotNull("Results are null.", results);
250
251 ValidatorResult middleNameResult = results.getValidatorResult("middleName");
252
253 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
254
255 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
256 assertTrue("Middle Name ValidatorResult for the 'required' action should have failed", !middleNameResult.isValid("required"));
257
258 assertTrue("Middle Name ValidatorResult should not contain the 'int' action.", !middleNameResult.containsAction("int"));
259
260 assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
261 }
262
263 /***
264 * If middle name is there but not int, then the required dependent test
265 * should pass, but the int dependent test should fail. No other tests should
266 * run.
267 *
268 * @throws ValidatorException
269 */
270 public void testFailingNextDependentValidator() throws ValidatorException {
271 // Create bean to run test on.
272 NameBean name = new NameBean();
273 name.setMiddleName("TEST");
274
275 // Construct validator based on the loaded resources
276 // and the form key
277 Validator validator = new Validator(resources, FORM_KEY);
278 // add the name bean to the validator as a resource
279 // for the validations to be performed on.
280 validator.setParameter(Validator.BEAN_PARAM, name);
281
282 // Get results of the validation.
283 ValidatorResults results = null;
284
285 results = validator.validate();
286
287 assertNotNull("Results are null.", results);
288
289 ValidatorResult middleNameResult = results.getValidatorResult("middleName");
290
291 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
292
293 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
294 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
295
296 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
297 assertTrue("Middle Name ValidatorResult for the 'int' action should have failed", !middleNameResult.isValid("int"));
298
299 assertTrue("Middle Name ValidatorResult should not contain the 'positive' action.", !middleNameResult.containsAction("positive"));
300 }
301
302 /***
303 * If middle name is there and a negative int, then the required and int
304 * dependent tests should pass, but the positive test should fail.
305 *
306 * @throws ValidatorException
307 */
308 public void testPassingDependentsFailingMain() throws ValidatorException {
309 // Create bean to run test on.
310 NameBean name = new NameBean();
311 name.setMiddleName("-2534");
312
313 // Construct validator based on the loaded resources
314 // and the form key
315 Validator validator = new Validator(resources, FORM_KEY);
316 // add the name bean to the validator as a resource
317 // for the validations to be performed on.
318 validator.setParameter(Validator.BEAN_PARAM, name);
319
320 // Get results of the validation.
321 ValidatorResults results = null;
322
323 results = validator.validate();
324
325 assertNotNull("Results are null.", results);
326
327 ValidatorResult middleNameResult = results.getValidatorResult("middleName");
328
329 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
330
331 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
332 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
333
334 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
335 assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
336
337 assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
338 assertTrue("Middle Name ValidatorResult for the 'positive' action should have failed", !middleNameResult.isValid("positive"));
339 }
340
341 /***
342 * If middle name is there and a positve int, then the required and int
343 * dependent tests should pass, and the positive test should pass.
344 *
345 * @throws ValidatorException
346 */
347 public void testPassingDependentsPassingMain() throws ValidatorException {
348 // Create bean to run test on.
349 NameBean name = new NameBean();
350 name.setMiddleName("2534");
351
352 // Construct validator based on the loaded resources
353 // and the form key
354 Validator validator = new Validator(resources, FORM_KEY);
355 // add the name bean to the validator as a resource
356 // for the validations to be performed on.
357 validator.setParameter(Validator.BEAN_PARAM, name);
358
359 // Get results of the validation.
360 ValidatorResults results = null;
361
362 results = validator.validate();
363
364 assertNotNull("Results are null.", results);
365
366 ValidatorResult middleNameResult = results.getValidatorResult("middleName");
367
368 assertNotNull("Middle Name ValidatorResult should not be null.", middleNameResult);
369
370 assertTrue("Middle Name ValidatorResult should contain the 'required' action.", middleNameResult.containsAction("required"));
371 assertTrue("Middle Name ValidatorResult for the 'required' action should have passed", middleNameResult.isValid("required"));
372
373 assertTrue("Middle Name ValidatorResult should contain the 'int' action.", middleNameResult.containsAction("int"));
374 assertTrue("Middle Name ValidatorResult for the 'int' action should have passed", middleNameResult.isValid("int"));
375
376 assertTrue("Middle Name ValidatorResult should contain the 'positive' action.", middleNameResult.containsAction("positive"));
377 assertTrue("Middle Name ValidatorResult for the 'positive' action should have passed", middleNameResult.isValid("positive"));
378 }
379 }
This page was automatically generated by Maven