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.text.DateFormat;
020: import java.text.ParseException;
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.List;
024: import java.util.Locale;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.validator.util.ValidatorUtils;
031:
032: /**
033: * Performs Validation Test.
034: *
035: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
036: */
037: public class ValidatorTest extends TestCase {
038:
039: public ValidatorTest(String name) {
040: super (name);
041: }
042:
043: /**
044: * Start the tests.
045: *
046: * @param theArgs the arguments. Not used
047: */
048: public static void main(String[] theArgs) {
049: junit.awtui.TestRunner.main(new String[] { ValidatorTest.class
050: .getName() });
051: }
052:
053: /**
054: * @return a test suite (<code>TestSuite</code>) that includes all methods
055: * starting with "test"
056: */
057: public static Test suite() {
058: // All methods starting with "test" will be executed in the test suite.
059: return new TestSuite(ValidatorTest.class);
060: }
061:
062: protected void setUp() {
063: }
064:
065: protected void tearDown() {
066: }
067:
068: /**
069: * Verify that one value generates an error and the other passes. The validation
070: * method being tested returns an object (<code>null</code> will be considered an error).
071: */
072: public void testManualObject() {
073: // property name of the method we are validating
074: String property = "date";
075: // name of ValidatorAction
076: String action = "date";
077: ValidatorResources resources = setupDateResources(property,
078: action);
079:
080: TestBean bean = new TestBean();
081: bean.setDate("2/3/1999");
082:
083: Validator validator = new Validator(resources, "testForm");
084: validator.setParameter(Validator.BEAN_PARAM, bean);
085:
086: try {
087: ValidatorResults results = validator.validate();
088:
089: assertNotNull("Results are null.", results);
090:
091: ValidatorResult result = results
092: .getValidatorResult(property);
093:
094: assertNotNull("Results are null.", results);
095:
096: assertTrue("ValidatorResult does not contain '" + action
097: + "' validator result.", result
098: .containsAction(action));
099:
100: assertTrue("Validation of the date formatting has failed.",
101: result.isValid(action));
102: } catch (Exception e) {
103: fail("An exception was thrown while calling Validator.validate()");
104: }
105:
106: bean.setDate("2/30/1999");
107:
108: try {
109: ValidatorResults results = validator.validate();
110:
111: assertNotNull("Results are null.", results);
112:
113: ValidatorResult result = results
114: .getValidatorResult(property);
115:
116: assertNotNull("Results are null.", results);
117:
118: assertTrue("ValidatorResult does not contain '" + action
119: + "' validator result.", result
120: .containsAction(action));
121:
122: assertTrue(
123: "Validation of the date formatting has passed when it should have failed.",
124: !result.isValid(action));
125: } catch (Exception e) {
126: fail("An exception was thrown while calling Validator.validate()");
127: }
128:
129: }
130:
131: public void testOnlyReturnErrors() throws ValidatorException {
132: // property name of the method we are validating
133: String property = "date";
134: // name of ValidatorAction
135: String action = "date";
136: ValidatorResources resources = setupDateResources(property,
137: action);
138:
139: TestBean bean = new TestBean();
140: bean.setDate("2/3/1999");
141:
142: Validator validator = new Validator(resources, "testForm");
143: validator.setParameter(Validator.BEAN_PARAM, bean);
144:
145: ValidatorResults results = validator.validate();
146:
147: assertNotNull(results);
148:
149: // Field passed and should be in results
150: assertTrue(results.getPropertyNames().contains(property));
151:
152: // Field passed but should not be in results
153: validator.setOnlyReturnErrors(true);
154: results = validator.validate();
155: assertFalse(results.getPropertyNames().contains(property));
156: }
157:
158: public void testOnlyValidateField() throws ValidatorException {
159: // property name of the method we are validating
160: String property = "date";
161: // name of ValidatorAction
162: String action = "date";
163: ValidatorResources resources = setupDateResources(property,
164: action);
165:
166: TestBean bean = new TestBean();
167: bean.setDate("2/3/1999");
168:
169: Validator validator = new Validator(resources, "testForm",
170: property);
171: validator.setParameter(Validator.BEAN_PARAM, bean);
172:
173: ValidatorResults results = validator.validate();
174:
175: assertNotNull(results);
176:
177: // Field passed and should be in results
178: assertTrue(results.getPropertyNames().contains(property));
179: }
180:
181: private ValidatorResources setupDateResources(String property,
182: String action) {
183:
184: ValidatorResources resources = new ValidatorResources();
185:
186: ValidatorAction va = new ValidatorAction();
187: va.setName(action);
188: va.setClassname("org.apache.commons.validator.ValidatorTest");
189: va.setMethod("formatDate");
190: va
191: .setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
192:
193: FormSet fs = new FormSet();
194: Form form = new Form();
195: form.setName("testForm");
196: Field field = new Field();
197: field.setProperty(property);
198: field.setDepends(action);
199: form.addField(field);
200: fs.addForm(form);
201:
202: resources.addValidatorAction(va);
203: resources.addFormSet(fs);
204: resources.process();
205:
206: return resources;
207: }
208:
209: /**
210: * Verify that one value generates an error and the other passes. The validation
211: * method being tested returns a <code>boolean</code> value.
212: */
213: public void testManualBoolean() {
214: ValidatorResources resources = new ValidatorResources();
215:
216: ValidatorAction va = new ValidatorAction();
217: va.setName("capLetter");
218: va.setClassname("org.apache.commons.validator.ValidatorTest");
219: va.setMethod("isCapLetter");
220: va
221: .setMethodParams("java.lang.Object,org.apache.commons.validator.Field,java.util.List");
222:
223: FormSet fs = new FormSet();
224: Form form = new Form();
225: form.setName("testForm");
226: Field field = new Field();
227: field.setProperty("letter");
228: field.setDepends("capLetter");
229: form.addField(field);
230: fs.addForm(form);
231:
232: resources.addValidatorAction(va);
233: resources.addFormSet(fs);
234: resources.process();
235:
236: List l = new ArrayList();
237:
238: TestBean bean = new TestBean();
239: bean.setLetter("A");
240:
241: Validator validator = new Validator(resources, "testForm");
242: validator.setParameter(Validator.BEAN_PARAM, bean);
243: validator.setParameter("java.util.List", l);
244:
245: try {
246: validator.validate();
247: } catch (Exception e) {
248: fail("An exception was thrown while calling Validator.validate()");
249: }
250:
251: assertEquals("Validation of the letter 'A'.", 0, l.size());
252:
253: l.clear();
254: bean.setLetter("AA");
255:
256: try {
257: validator.validate();
258: } catch (Exception e) {
259: fail("An exception was thrown while calling Validator.validate()");
260: }
261:
262: assertEquals("Validation of the letter 'AA'.", 1, l.size());
263: }
264:
265: /**
266: * Checks if the field is one upper case letter between 'A' and 'Z'.
267: */
268: public static boolean isCapLetter(Object bean, Field field, List l) {
269: String value = ValidatorUtils.getValueAsString(bean, field
270: .getProperty());
271:
272: if (value != null && value.length() == 1) {
273: if (value.charAt(0) >= 'A' && value.charAt(0) <= 'Z') {
274: return true;
275: } else {
276: l.add("Error");
277: return false;
278: }
279: } else {
280: l.add("Error");
281: return false;
282: }
283: }
284:
285: /**
286: * Formats a <code>String</code> to a <code>Date</code>.
287: * The <code>Validator</code> will interpret a <code>null</code>
288: * as validation having failed.
289: */
290: public static Date formatDate(Object bean, Field field) {
291: String value = ValidatorUtils.getValueAsString(bean, field
292: .getProperty());
293: Date date = null;
294:
295: try {
296: DateFormat formatter = null;
297: formatter = DateFormat.getDateInstance(DateFormat.SHORT,
298: Locale.US);
299:
300: formatter.setLenient(false);
301:
302: date = formatter.parse(value);
303: } catch (ParseException e) {
304: System.out.println("ValidatorTest.formatDate() - "
305: + e.getMessage());
306: }
307:
308: return date;
309: }
310:
311: public class TestBean {
312: private String letter = null;
313: private String date = null;
314:
315: public String getLetter() {
316: return letter;
317: }
318:
319: public void setLetter(String letter) {
320: this .letter = letter;
321: }
322:
323: public String getDate() {
324: return date;
325: }
326:
327: public void setDate(String date) {
328: this.date = date;
329: }
330: }
331:
332: }
|