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 org.xml.sax.SAXException;
022:
023: /**
024: * Abstracts number unit tests methods.
025: *
026: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
027: */
028: abstract public class TestNumber extends TestCommon {
029:
030: /**
031: * The key used to retrieve the set of validation
032: * rules from the xml file.
033: */
034: protected String FORM_KEY;
035:
036: /**
037: * The key used to retrieve the validator action.
038: */
039: protected String ACTION;
040:
041: public TestNumber(String name) {
042: super (name);
043: }
044:
045: /**
046: * Load <code>ValidatorResources</code> from
047: * validator-numeric.xml.
048: */
049: protected void setUp() throws IOException, SAXException {
050: // Load resources
051: loadResources("TestNumber-config.xml");
052: }
053:
054: protected void tearDown() {
055: }
056:
057: /**
058: * Tests the number validation.
059: */
060: public void testNumber() throws ValidatorException {
061: // Create bean to run test on.
062: ValueBean info = new ValueBean();
063: info.setValue("0");
064: valueTest(info, true);
065: }
066:
067: /**
068: * Tests the float validation failure.
069: */
070: public void testNumberFailure() throws ValidatorException {
071: // Create bean to run test on.
072: ValueBean info = new ValueBean();
073: valueTest(info, false);
074: }
075:
076: /**
077: * Utlity class to run a test on a value.
078: *
079: * @param info Value to run test on.
080: * @param passed Whether or not the test is expected to pass.
081: */
082: protected void valueTest(Object info, boolean passed)
083: throws ValidatorException {
084: // Construct validator based on the loaded resources
085: // and the form key
086: Validator validator = new Validator(resources, FORM_KEY);
087: // add the name bean to the validator as a resource
088: // for the validations to be performed on.
089: validator.setParameter(Validator.BEAN_PARAM, info);
090:
091: // Get results of the validation.
092: ValidatorResults results = null;
093:
094: // throws ValidatorException,
095: // but we aren't catching for testing
096: // since no validation methods we use
097: // throw this
098: results = validator.validate();
099:
100: assertNotNull("Results are null.", results);
101:
102: ValidatorResult result = results.getValidatorResult("value");
103:
104: assertNotNull(ACTION
105: + " value ValidatorResult should not be null.", result);
106: assertTrue(ACTION
107: + " value ValidatorResult should contain the '"
108: + ACTION + "' action.", result.containsAction(ACTION));
109: assertTrue(ACTION + " value ValidatorResult for the '" + ACTION
110: + "' action should have "
111: + (passed ? "passed" : "failed") + ".",
112: (passed ? result.isValid(ACTION) : !result
113: .isValid(ACTION)));
114: }
115:
116: }
|