View Javadoc
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 org.xml.sax.SAXException;
23  
24  /**
25   * This TestCase is a confirmation of the parameter of the validator's method.
26   *
27   * @version $Revision$
28   */
29  public class ParameterTest extends AbstractCommonTest {
30  
31      private static final String FORM_KEY = "nameForm";
32  
33      private String firstName;
34  
35      private String middleName;
36  
37      private String lastName;
38  
39      /**
40       * Constructor.
41       */
42      public ParameterTest(String name) {
43          super(name);
44      }
45  
46      /**
47       * Load <code>ValidatorResources</code> from
48       * ValidatorResultsTest-config.xml.
49       */
50      @Override
51      protected void setUp() throws IOException, SAXException {
52          // Load resources
53          loadResources("ParameterTest-config.xml");
54  
55          // initialize values
56          firstName = "foo";
57          middleName = "123";
58          lastName = "456";
59  
60      }
61  
62      @Override
63      protected void tearDown() {
64      }
65  
66      /**
67       * Test all validations ran and passed.
68       */
69      public void testAllValid() {
70  
71          // Create bean to run test on.
72          NameBean bean = createNameBean();
73  
74          Validator validator = new Validator(resources, FORM_KEY);
75  
76          // add the name bean to the validator as a resource
77          // for the validations to be performed on.
78          validator.setParameter(Validator.BEAN_PARAM, bean);
79          validator.setParameter(Validator.LOCALE_PARAM, Locale.getDefault());
80  
81          // Get results of the validation.
82          try {
83              validator.validate();
84          } catch(Exception e) {
85              fail("Validator.validate() threw " + e);
86          }
87          assertParameterValue(validator, Validator.BEAN_PARAM, Object.class);
88          assertParameterValue(validator, Validator.FIELD_PARAM, Field.class);
89          assertParameterValue(validator, Validator.FORM_PARAM, Form.class);
90          assertParameterValue(validator, Validator.LOCALE_PARAM, Locale.class);
91          assertParameterValue(validator, Validator.VALIDATOR_ACTION_PARAM,
92                  ValidatorAction.class);
93          assertParameterValue(validator, Validator.VALIDATOR_PARAM,
94                  Validator.class);
95          assertParameterValue(validator, Validator.VALIDATOR_RESULTS_PARAM,
96                  ValidatorResults.class);
97      }
98  
99      private void assertParameterValue(Validator validator, String name,
100             Class<?> type) {
101         Object value = validator.getParameterValue(name);
102         assertNotNull("Expected '" + type.getName() + "' but was null", value);
103         assertTrue("Expected '" + type.getName() + "' but was '" + value.getClass().getName() + "'",
104                    type.isInstance(value));
105     }
106 
107     /**
108      * Create a NameBean.
109      */
110     private NameBean createNameBean() {
111         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
112         name.setFirstName(firstName);
113         name.setMiddleName(middleName);
114         name.setLastName(lastName);
115         return name;
116     }
117 }