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 RequiredNameTest 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 RequiredNameTest(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
055: .main(new String[] { RequiredNameTest.class.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(RequiredNameTest.class);
065: }
066:
067: /**
068: * Load <code>ValidatorResources</code> from
069: * validator-name-required.xml.
070: */
071: protected void setUp() throws IOException, SAXException {
072: // Load resources
073: loadResources("RequiredNameTest-config.xml");
074: }
075:
076: protected void tearDown() {
077: }
078:
079: /**
080: * Tests the required validation failure.
081: */
082: public void testRequired() 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("First 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: }
127:
128: /**
129: * Tests the required validation for first name if it is blank.
130: */
131: public void testRequiredFirstNameBlank() throws ValidatorException {
132: // Create bean to run test on.
133: NameBean name = new NameBean();
134: name.setFirstName("");
135:
136: // Construct validator based on the loaded resources
137: // and the form key
138: Validator validator = new Validator(resources, FORM_KEY);
139: // add the name bean to the validator as a resource
140: // for the validations to be performed on.
141: validator.setParameter(Validator.BEAN_PARAM, name);
142:
143: // Get results of the validation.
144: ValidatorResults results = null;
145:
146: results = validator.validate();
147:
148: assertNotNull("Results are null.", results);
149:
150: ValidatorResult firstNameResult = results
151: .getValidatorResult("firstName");
152: ValidatorResult lastNameResult = results
153: .getValidatorResult("lastName");
154:
155: assertNotNull("First Name ValidatorResult should not be null.",
156: firstNameResult);
157: assertTrue("First Name ValidatorResult should contain the '"
158: + ACTION + "' action.", firstNameResult
159: .containsAction(ACTION));
160: assertTrue("First Name ValidatorResult for the '" + ACTION
161: + "' action should have failed.", !firstNameResult
162: .isValid(ACTION));
163:
164: assertNotNull("First Name ValidatorResult should not be null.",
165: lastNameResult);
166: assertTrue("Last Name ValidatorResult should contain the '"
167: + ACTION + "' action.", lastNameResult
168: .containsAction(ACTION));
169: assertTrue("Last Name ValidatorResult for the '" + ACTION
170: + "' action should have failed.", !lastNameResult
171: .isValid(ACTION));
172: }
173:
174: /**
175: * Tests the required validation for first name.
176: */
177: public void testRequiredFirstName() throws ValidatorException {
178: // Create bean to run test on.
179: NameBean name = new NameBean();
180: name.setFirstName("Joe");
181:
182: // Construct validator based on the loaded resources
183: // and the form key
184: Validator validator = new Validator(resources, FORM_KEY);
185: // add the name bean to the validator as a resource
186: // for the validations to be performed on.
187: validator.setParameter(Validator.BEAN_PARAM, name);
188:
189: // Get results of the validation.
190: ValidatorResults results = null;
191:
192: results = validator.validate();
193:
194: assertNotNull("Results are null.", results);
195:
196: ValidatorResult firstNameResult = results
197: .getValidatorResult("firstName");
198: ValidatorResult lastNameResult = results
199: .getValidatorResult("lastName");
200:
201: assertNotNull("First Name ValidatorResult should not be null.",
202: firstNameResult);
203: assertTrue("First Name ValidatorResult should contain the '"
204: + ACTION + "' action.", firstNameResult
205: .containsAction(ACTION));
206: assertTrue("First Name ValidatorResult for the '" + ACTION
207: + "' action should have passed.", firstNameResult
208: .isValid(ACTION));
209:
210: assertNotNull("First Name ValidatorResult should not be null.",
211: lastNameResult);
212: assertTrue("Last Name ValidatorResult should contain the '"
213: + ACTION + "' action.", lastNameResult
214: .containsAction(ACTION));
215: assertTrue("Last Name ValidatorResult for the '" + ACTION
216: + "' action should have failed.", !lastNameResult
217: .isValid(ACTION));
218: }
219:
220: /**
221: * Tests the required validation for last name if it is blank.
222: */
223: public void testRequiredLastNameBlank() throws ValidatorException {
224: // Create bean to run test on.
225: NameBean name = new NameBean();
226: name.setLastName("");
227:
228: // Construct validator based on the loaded resources
229: // and the form key
230: Validator validator = new Validator(resources, FORM_KEY);
231: // add the name bean to the validator as a resource
232: // for the validations to be performed on.
233: validator.setParameter(Validator.BEAN_PARAM, name);
234:
235: // Get results of the validation.
236: ValidatorResults results = null;
237:
238: results = validator.validate();
239:
240: assertNotNull("Results are null.", results);
241:
242: ValidatorResult firstNameResult = results
243: .getValidatorResult("firstName");
244: ValidatorResult lastNameResult = results
245: .getValidatorResult("lastName");
246:
247: assertNotNull("First Name ValidatorResult should not be null.",
248: firstNameResult);
249: assertTrue("First Name ValidatorResult should contain the '"
250: + ACTION + "' action.", firstNameResult
251: .containsAction(ACTION));
252: assertTrue("First Name ValidatorResult for the '" + ACTION
253: + "' action should have failed.", !firstNameResult
254: .isValid(ACTION));
255:
256: assertNotNull("First Name ValidatorResult should not be null.",
257: lastNameResult);
258: assertTrue("Last Name ValidatorResult should contain the '"
259: + ACTION + "' action.", lastNameResult
260: .containsAction(ACTION));
261: assertTrue("Last Name ValidatorResult for the '" + ACTION
262: + "' action should have failed.", !lastNameResult
263: .isValid(ACTION));
264: }
265:
266: /**
267: * Tests the required validation for last name.
268: */
269: public void testRequiredLastName() throws ValidatorException {
270: // Create bean to run test on.
271: NameBean name = new NameBean();
272: name.setLastName("Smith");
273:
274: // Construct validator based on the loaded resources
275: // and the form key
276: Validator validator = new Validator(resources, FORM_KEY);
277: // add the name bean to the validator as a resource
278: // for the validations to be performed on.
279: validator.setParameter(Validator.BEAN_PARAM, name);
280:
281: // Get results of the validation.
282: ValidatorResults results = null;
283:
284: results = validator.validate();
285:
286: assertNotNull("Results are null.", results);
287:
288: ValidatorResult firstNameResult = results
289: .getValidatorResult("firstName");
290: ValidatorResult lastNameResult = results
291: .getValidatorResult("lastName");
292:
293: assertNotNull("First Name ValidatorResult should not be null.",
294: firstNameResult);
295: assertTrue("First Name ValidatorResult should contain the '"
296: + ACTION + "' action.", firstNameResult
297: .containsAction(ACTION));
298: assertTrue("First Name ValidatorResult for the '" + ACTION
299: + "' action should have failed.", !firstNameResult
300: .isValid(ACTION));
301:
302: assertNotNull("First Name ValidatorResult should not be null.",
303: lastNameResult);
304: assertTrue("Last Name ValidatorResult should contain the '"
305: + ACTION + "' action.", lastNameResult
306: .containsAction(ACTION));
307: assertTrue("Last Name ValidatorResult for the '" + ACTION
308: + "' action should have passed.", lastNameResult
309: .isValid(ACTION));
310:
311: }
312:
313: /**
314: * Tests the required validation for first and last name.
315: */
316: public void testRequiredName() throws ValidatorException {
317: // Create bean to run test on.
318: NameBean name = new NameBean();
319: name.setFirstName("Joe");
320: name.setLastName("Smith");
321:
322: // Construct validator based on the loaded resources
323: // and the form key
324: Validator validator = new Validator(resources, FORM_KEY);
325: // add the name bean to the validator as a resource
326: // for the validations to be performed on.
327: validator.setParameter(Validator.BEAN_PARAM, name);
328:
329: // Get results of the validation.
330: ValidatorResults results = null;
331:
332: results = validator.validate();
333:
334: assertNotNull("Results are null.", results);
335:
336: ValidatorResult firstNameResult = results
337: .getValidatorResult("firstName");
338: ValidatorResult lastNameResult = results
339: .getValidatorResult("lastName");
340:
341: assertNotNull("First Name ValidatorResult should not be null.",
342: firstNameResult);
343: assertTrue("First Name ValidatorResult should contain the '"
344: + ACTION + "' action.", firstNameResult
345: .containsAction(ACTION));
346: assertTrue("First Name ValidatorResult for the '" + ACTION
347: + "' action should have passed.", firstNameResult
348: .isValid(ACTION));
349:
350: assertNotNull("First Name ValidatorResult should not be null.",
351: lastNameResult);
352: assertTrue("Last Name ValidatorResult should contain the '"
353: + ACTION + "' action.", lastNameResult
354: .containsAction(ACTION));
355: assertTrue("Last Name ValidatorResult for the '" + ACTION
356: + "' action should have passed.", lastNameResult
357: .isValid(ACTION));
358: }
359:
360: }
|