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: import java.util.Iterator;
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: import org.xml.sax.SAXException;
025:
026: /**
027: * Test ValidatorResults.
028: *
029: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
030: */
031: public class ValidatorResultsTest extends TestCommon {
032:
033: private static final String FORM_KEY = "nameForm";
034: private static final String firstNameField = "firstName";
035: private static final String middleNameField = "middleName";
036: private static final String lastNameField = "lastName";
037:
038: private String firstName;
039: private String middleName;
040: private String lastName;
041:
042: /**
043: * Constructor.
044: */
045: public ValidatorResultsTest(String name) {
046: super (name);
047: }
048:
049: /**
050: * Start the tests.
051: *
052: * @param theArgs the arguments. Not used
053: */
054: public static void main(String[] theArgs) {
055: junit.awtui.TestRunner
056: .main(new String[] { ValidatorResultsTest.class
057: .getName() });
058: }
059:
060: /**
061: * @return a test suite (<code>TestSuite</code>) that includes all methods
062: * starting with "test"
063: */
064: public static Test suite() {
065: // All methods starting with "test" will be executed in the test suite.
066: return new TestSuite(ValidatorResultsTest.class);
067: }
068:
069: /**
070: * Load <code>ValidatorResources</code> from
071: * ValidatorResultsTest-config.xml.
072: */
073: protected void setUp() throws IOException, SAXException {
074: // Load resources
075: loadResources("ValidatorResultsTest-config.xml");
076:
077: // initialize values
078: firstName = "foo";
079: middleName = "123";
080: lastName = "456";
081:
082: }
083:
084: protected void tearDown() {
085: }
086:
087: /**
088: * Test all validations ran and passed.
089: */
090: public void testAllValid() throws ValidatorException {
091:
092: // Create bean to run test on.
093: NameBean bean = createNameBean();
094:
095: // Validate.
096: ValidatorResults results = validate(bean);
097:
098: // Check results
099: checkValidatorResult(results, firstNameField, "required", true);
100: checkValidatorResult(results, middleNameField, "required", true);
101: checkValidatorResult(results, middleNameField, "int", true);
102: checkValidatorResult(results, middleNameField, "positive", true);
103: checkValidatorResult(results, lastNameField, "required", true);
104: checkValidatorResult(results, lastNameField, "int", true);
105:
106: }
107:
108: /**
109: * Test some validations failed and some didn't run.
110: */
111: public void testErrors() throws ValidatorException {
112:
113: middleName = "XXX";
114: lastName = null;
115:
116: // Create bean to run test on.
117: NameBean bean = createNameBean();
118:
119: // Validate.
120: ValidatorResults results = validate(bean);
121:
122: // Check results
123: checkValidatorResult(results, firstNameField, "required", true);
124: checkValidatorResult(results, middleNameField, "required", true);
125: checkValidatorResult(results, middleNameField, "int", false);
126: checkNotRun(results, middleNameField, "positive");
127: checkValidatorResult(results, lastNameField, "required", false);
128: checkNotRun(results, lastNameField, "int");
129:
130: }
131:
132: /**
133: * Check a validator has not been run for a field and the result.
134: */
135: private void checkNotRun(ValidatorResults results, String field,
136: String action) {
137: ValidatorResult result = results.getValidatorResult(field);
138: assertNotNull(field + " result", result);
139: assertFalse(field + "[" + action + "] run", result
140: .containsAction(action));
141: // System.out.println(field + "[" + action + "] not run");
142: }
143:
144: /**
145: * Check a validator has run for a field and the result.
146: */
147: private void checkValidatorResult(ValidatorResults results,
148: String field, String action, boolean expected) {
149: ValidatorResult result = results.getValidatorResult(field);
150: // System.out.println(field + "[" + action + "]=" + result.isValid(action));
151: assertNotNull(field + " result", result);
152: assertTrue(field + "[" + action + "] not run", result
153: .containsAction(action));
154: assertEquals(field + "[" + action + "] result", expected,
155: result.isValid(action));
156: }
157:
158: /**
159: * Create a NameBean.
160: */
161: private NameBean createNameBean() {
162: NameBean name = new NameBean();
163: name.setFirstName(firstName);
164: name.setMiddleName(middleName);
165: name.setLastName(lastName);
166: return name;
167: }
168:
169: /**
170: * Validate results.
171: */
172: private ValidatorResults validate(Object bean)
173: throws ValidatorException {
174:
175: // Construct validator based on the loaded resources
176: // and the form key
177: Validator validator = new Validator(resources, FORM_KEY);
178:
179: // add the name bean to the validator as a resource
180: // for the validations to be performed on.
181: validator.setParameter(Validator.BEAN_PARAM, bean);
182:
183: // Get results of the validation.
184: ValidatorResults results = validator.validate();
185:
186: return results;
187:
188: }
189:
190: }
|