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: }
|