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 RequiredIfTest 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 = "requiredif";
043:
044: public RequiredIfTest(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[] { RequiredIfTest.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(RequiredIfTest.class);
065: }
066:
067: /**
068: * Load <code>ValidatorResources</code> from
069: * validator-requiredif.xml.
070: */
071: protected void setUp() throws IOException, SAXException {
072: // Load resources
073: loadResources("RequiredIfTest-config.xml");
074: }
075:
076: protected void tearDown() {
077: }
078:
079: /**
080: * With nothing provided, we should pass since the fields only fail on
081: * null if the other field is non-blank.
082: */
083: public void testRequired() throws ValidatorException {
084: // Create bean to run test on.
085: NameBean name = new NameBean();
086:
087: // Construct validator based on the loaded resources
088: // and the form key
089: Validator validator = new Validator(resources, FORM_KEY);
090: // add the name bean to the validator as a resource
091: // for the validations to be performed on.
092: validator.setParameter(Validator.BEAN_PARAM, name);
093:
094: // Get results of the validation.
095: ValidatorResults results = null;
096:
097: // throws ValidatorException,
098: // but we aren't catching for testing
099: // since no validation methods we use
100: // throw this
101: results = validator.validate();
102:
103: assertNotNull("Results are null.", results);
104:
105: ValidatorResult firstNameResult = results
106: .getValidatorResult("firstName");
107: ValidatorResult lastNameResult = results
108: .getValidatorResult("lastName");
109:
110: assertNotNull("First Name ValidatorResult should not be null.",
111: firstNameResult);
112: assertTrue("First Name ValidatorResult should contain the '"
113: + ACTION + "' action.", firstNameResult
114: .containsAction(ACTION));
115: assertTrue("First Name ValidatorResult for the '" + ACTION
116: + "' action should have passed.", firstNameResult
117: .isValid(ACTION));
118:
119: assertNotNull("Last Name ValidatorResult should not be null.",
120: lastNameResult);
121: assertTrue("Last Name ValidatorResult should contain the '"
122: + ACTION + "' action.", lastNameResult
123: .containsAction(ACTION));
124: assertTrue("Last Name ValidatorResult for the '" + ACTION
125: + "' action should have passed.", lastNameResult
126: .isValid(ACTION));
127: }
128:
129: /**
130: * Tests the required validation for first name if it is blank.
131: */
132: public void testRequiredFirstNameBlank() throws ValidatorException {
133: // Create bean to run test on.
134: NameBean name = new NameBean();
135: name.setFirstName("");
136: name.setLastName("Test");
137:
138: // Construct validator based on the loaded resources
139: // and the form key
140: Validator validator = new Validator(resources, FORM_KEY);
141: // add the name bean to the validator as a resource
142: // for the validations to be performed on.
143: validator.setParameter(Validator.BEAN_PARAM, name);
144:
145: // Get results of the validation.
146: ValidatorResults results = null;
147:
148: results = validator.validate();
149:
150: assertNotNull("Results are null.", results);
151:
152: ValidatorResult firstNameResult = results
153: .getValidatorResult("firstName");
154: ValidatorResult lastNameResult = results
155: .getValidatorResult("lastName");
156:
157: assertNotNull("First Name ValidatorResult should not be null.",
158: firstNameResult);
159: assertTrue("First Name ValidatorResult should contain the '"
160: + ACTION + "' action.", firstNameResult
161: .containsAction(ACTION));
162: assertTrue("First Name ValidatorResult for the '" + ACTION
163: + "' action should have failed.", !firstNameResult
164: .isValid(ACTION));
165:
166: assertNotNull("Last Name ValidatorResult should not be null.",
167: lastNameResult);
168: assertTrue("Last Name ValidatorResult should contain the '"
169: + ACTION + "' action.", lastNameResult
170: .containsAction(ACTION));
171: assertTrue("Last Name ValidatorResult for the '" + ACTION
172: + "' action should have passed.", lastNameResult
173: .isValid(ACTION));
174: }
175:
176: /**
177: * Tests the required validation for last name.
178: */
179: public void testRequiredFirstName() throws ValidatorException {
180: // Create bean to run test on.
181: NameBean name = new NameBean();
182: name.setFirstName("Test");
183: name.setLastName("Test");
184:
185: // Construct validator based on the loaded resources
186: // and the form key
187: Validator validator = new Validator(resources, FORM_KEY);
188: // add the name bean to the validator as a resource
189: // for the validations to be performed on.
190: validator.setParameter(Validator.BEAN_PARAM, name);
191:
192: // Get results of the validation.
193: ValidatorResults results = null;
194:
195: results = validator.validate();
196:
197: assertNotNull("Results are null.", results);
198:
199: ValidatorResult firstNameResult = results
200: .getValidatorResult("firstName");
201: ValidatorResult lastNameResult = results
202: .getValidatorResult("lastName");
203:
204: assertNotNull("First Name ValidatorResult should not be null.",
205: firstNameResult);
206: assertTrue("First Name ValidatorResult should contain the '"
207: + ACTION + "' action.", firstNameResult
208: .containsAction(ACTION));
209: assertTrue("First Name ValidatorResult for the '" + ACTION
210: + "' action should have passed.", firstNameResult
211: .isValid(ACTION));
212:
213: assertNotNull("Last Name ValidatorResult should not be null.",
214: lastNameResult);
215: assertTrue("Last Name ValidatorResult should contain the '"
216: + ACTION + "' action.", lastNameResult
217: .containsAction(ACTION));
218: assertTrue("Last Name ValidatorResult for the '" + ACTION
219: + "' action should have passed.", lastNameResult
220: .isValid(ACTION));
221: }
222:
223: /**
224: * Tests the required validation for last name if it is blank.
225: */
226: public void testRequiredLastNameBlank() throws ValidatorException {
227: // Create bean to run test on.
228: NameBean name = new NameBean();
229: name.setFirstName("Joe");
230: name.setLastName("");
231:
232: // Construct validator based on the loaded resources
233: // and the form key
234: Validator validator = new Validator(resources, FORM_KEY);
235: // add the name bean to the validator as a resource
236: // for the validations to be performed on.
237: validator.setParameter(Validator.BEAN_PARAM, name);
238:
239: // Get results of the validation.
240: ValidatorResults results = null;
241:
242: results = validator.validate();
243:
244: assertNotNull("Results are null.", results);
245:
246: ValidatorResult firstNameResult = results
247: .getValidatorResult("firstName");
248: ValidatorResult lastNameResult = results
249: .getValidatorResult("lastName");
250:
251: assertNotNull("First Name ValidatorResult should not be null.",
252: firstNameResult);
253: assertTrue("First Name ValidatorResult should contain the '"
254: + ACTION + "' action.", firstNameResult
255: .containsAction(ACTION));
256: assertTrue("First Name ValidatorResult for the '" + ACTION
257: + "' action should have passed.", firstNameResult
258: .isValid(ACTION));
259:
260: assertNotNull("Last Name ValidatorResult should not be null.",
261: lastNameResult);
262: assertTrue("Last Name ValidatorResult should contain the '"
263: + ACTION + "' action.", lastNameResult
264: .containsAction(ACTION));
265: assertTrue("Last Name ValidatorResult for the '" + ACTION
266: + "' action should have failed.", !lastNameResult
267: .isValid(ACTION));
268: }
269:
270: /**
271: * Tests the required validation for last name.
272: */
273: public void testRequiredLastName() throws ValidatorException {
274: // Create bean to run test on.
275: NameBean name = new NameBean();
276: name.setFirstName("Joe");
277: name.setLastName("Smith");
278:
279: // Construct validator based on the loaded resources
280: // and the form key
281: Validator validator = new Validator(resources, FORM_KEY);
282: // add the name bean to the validator as a resource
283: // for the validations to be performed on.
284: validator.setParameter(Validator.BEAN_PARAM, name);
285:
286: // Get results of the validation.
287: ValidatorResults results = null;
288:
289: results = validator.validate();
290:
291: assertNotNull("Results are null.", results);
292:
293: ValidatorResult firstNameResult = results
294: .getValidatorResult("firstName");
295: ValidatorResult lastNameResult = results
296: .getValidatorResult("lastName");
297:
298: assertNotNull("First Name ValidatorResult should not be null.",
299: firstNameResult);
300: assertTrue("First Name ValidatorResult should contain the '"
301: + ACTION + "' action.", firstNameResult
302: .containsAction(ACTION));
303: assertTrue("First Name ValidatorResult for the '" + ACTION
304: + "' action should have passed.", firstNameResult
305: .isValid(ACTION));
306:
307: assertNotNull("Last Name ValidatorResult should not be null.",
308: lastNameResult);
309: assertTrue("Last Name ValidatorResult should contain the '"
310: + ACTION + "' action.", lastNameResult
311: .containsAction(ACTION));
312: assertTrue("Last Name ValidatorResult for the '" + ACTION
313: + "' action should have passed.", lastNameResult
314: .isValid(ACTION));
315:
316: }
317:
318: }
|