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.Locale;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.xml.sax.SAXException;
026:
027: /**
028: * This TestCase is a confirmation of the parameter of the validator's method.
029: *
030: * @version $Revision: 478345 $ $Date: 2006-11-22 22:25:19 +0000 (Wed, 22 Nov 2006) $
031: */
032: public class ParameterTest extends TestCommon {
033:
034: private static final String FORM_KEY = "nameForm";
035:
036: private static final String firstNameField = "firstName";
037:
038: private static final String middleNameField = "middleName";
039:
040: private static final String lastNameField = "lastName";
041:
042: private String firstName;
043:
044: private String middleName;
045:
046: private String lastName;
047:
048: /**
049: * Constructor.
050: */
051: public ParameterTest(String name) {
052: super (name);
053: }
054:
055: /**
056: * Start the tests.
057: *
058: * @param theArgs the arguments. Not used
059: */
060: public static void main(String[] theArgs) {
061: junit.awtui.TestRunner.main(new String[] { ParameterTest.class
062: .getName() });
063: }
064:
065: /**
066: * Create a Test Suite
067: * @return a test suite (<code>TestSuite</code>) that includes all
068: * methods starting with "test"
069: */
070: public static Test suite() {
071: return new TestSuite(ParameterTest.class);
072: }
073:
074: /**
075: * Load <code>ValidatorResources</code> from
076: * ValidatorResultsTest-config.xml.
077: */
078: protected void setUp() throws IOException, SAXException {
079: // Load resources
080: loadResources("ParameterTest-config.xml");
081:
082: // initialize values
083: firstName = "foo";
084: middleName = "123";
085: lastName = "456";
086:
087: }
088:
089: protected void tearDown() {
090: }
091:
092: /**
093: * Test all validations ran and passed.
094: */
095: public void testAllValid() throws ValidatorException {
096:
097: // Create bean to run test on.
098: NameBean bean = createNameBean();
099:
100: Validator validator = new Validator(resources, FORM_KEY);
101:
102: // add the name bean to the validator as a resource
103: // for the validations to be performed on.
104: validator.setParameter(Validator.BEAN_PARAM, bean);
105: validator.setParameter(Validator.LOCALE_PARAM, Locale
106: .getDefault());
107:
108: // Get results of the validation.
109: ValidatorResults results = null;
110: try {
111: results = validator.validate();
112: } catch (Exception e) {
113: fail("Validator.validate() threw " + e);
114: }
115: assertParameterValue(validator, Validator.BEAN_PARAM,
116: Object.class);
117: assertParameterValue(validator, Validator.FIELD_PARAM,
118: Field.class);
119: assertParameterValue(validator, Validator.FORM_PARAM,
120: Form.class);
121: assertParameterValue(validator, Validator.LOCALE_PARAM,
122: Locale.class);
123: assertParameterValue(validator,
124: Validator.VALIDATOR_ACTION_PARAM, ValidatorAction.class);
125: assertParameterValue(validator, Validator.VALIDATOR_PARAM,
126: Validator.class);
127: assertParameterValue(validator,
128: Validator.VALIDATOR_RESULTS_PARAM,
129: ValidatorResults.class);
130: }
131:
132: private void assertParameterValue(Validator validator, String name,
133: Class type) {
134: Object value = validator.getParameterValue(name);
135: assertNotNull("Expected '" + type.getName() + "' but was null",
136: value);
137: assertTrue("Expected '" + type.getName() + "' but was '"
138: + value.getClass().getName() + "'", type
139: .isInstance(value));
140: }
141:
142: /**
143: * Create a NameBean.
144: */
145: private NameBean createNameBean() {
146: NameBean name = new NameBean();
147: name.setFirstName(firstName);
148: name.setMiddleName(middleName);
149: name.setLastName(lastName);
150: return name;
151: }
152: }
|