1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator;
18  
19  import java.io.IOException;
20  import java.util.Locale;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.xml.sax.SAXException;
26  
27  /***
28   * This TestCase is a confirmation of the parameter of the validator's method.
29   *
30   * @version $Revision: 478345 $ $Date: 2006-11-22 22:25:19 +0000 (Wed, 22 Nov 2006) $
31   */
32  public class ParameterTest extends TestCommon {
33  
34      private static final String FORM_KEY = "nameForm";
35  
36      private static final String firstNameField = "firstName";
37  
38      private static final String middleNameField = "middleName";
39  
40      private static final String lastNameField = "lastName";
41  
42      private String firstName;
43  
44      private String middleName;
45  
46      private String lastName;
47  
48      /***
49       * Constructor.
50       */
51      public ParameterTest(String name) {
52          super(name);
53      }
54  
55      /***
56       * Start the tests.
57       * 
58       * @param theArgs the arguments. Not used
59       */
60      public static void main(String[] theArgs) {
61          junit.awtui.TestRunner.main(new String[] {
62              ParameterTest.class.getName()
63          });
64      }
65  
66      /***
67       * Create a Test Suite
68       * @return a test suite (<code>TestSuite</code>) that includes all
69       *         methods starting with "test"
70       */
71      public static Test suite() {
72          return new TestSuite(ParameterTest.class);
73      }
74  
75      /***
76       * Load <code>ValidatorResources</code> from
77       * ValidatorResultsTest-config.xml.
78       */
79      protected void setUp() throws IOException, SAXException {
80          // Load resources
81          loadResources("ParameterTest-config.xml");
82  
83          // initialize values
84          firstName = "foo";
85          middleName = "123";
86          lastName = "456";
87  
88      }
89  
90      protected void tearDown() {
91      }
92  
93      /***
94       * Test all validations ran and passed.
95       */
96      public void testAllValid() throws ValidatorException {
97  
98          // Create bean to run test on.
99          NameBean bean = createNameBean();
100 
101         Validator validator = new Validator(resources, FORM_KEY);
102 
103         // add the name bean to the validator as a resource
104         // for the validations to be performed on.
105         validator.setParameter(Validator.BEAN_PARAM, bean);
106         validator.setParameter(Validator.LOCALE_PARAM, Locale.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, Object.class);
116         assertParameterValue(validator, Validator.FIELD_PARAM, Field.class);
117         assertParameterValue(validator, Validator.FORM_PARAM, Form.class);
118         assertParameterValue(validator, Validator.LOCALE_PARAM, Locale.class);
119         assertParameterValue(validator, Validator.VALIDATOR_ACTION_PARAM,
120                 ValidatorAction.class);
121         assertParameterValue(validator, Validator.VALIDATOR_PARAM,
122                 Validator.class);
123         assertParameterValue(validator, Validator.VALIDATOR_RESULTS_PARAM,
124                 ValidatorResults.class);
125     }
126 
127     private void assertParameterValue(Validator validator, String name,
128             Class type) {
129         Object value = validator.getParameterValue(name);
130         assertNotNull("Expected '" + type.getName() + "' but was null", value);
131         assertTrue("Expected '" + type.getName() + "' but was '" + value.getClass().getName() + "'",
132                    type.isInstance(value));
133     }
134 
135     /***
136      * Create a NameBean.
137      */
138     private NameBean createNameBean() {
139         NameBean name = new NameBean();
140         name.setFirstName(firstName);
141         name.setMiddleName(middleName);
142         name.setLastName(lastName);
143         return name;
144     }
145 }