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.io.InputStream;
21  
22  import junit.framework.TestCase;
23  
24  import org.xml.sax.SAXException;
25  
26  /**
27   * Tests that validator rules split between 2 different XML files get 
28   * merged properly.
29   *
30   * @version $Revision$
31   */
32  public class MultipleConfigFilesTest extends TestCase {
33  
34      /**
35       * Resources used for validation tests.
36       */
37      private ValidatorResources resources = null;
38      
39      /**
40       * The key used to retrieve the set of validation
41       * rules from the xml file.
42       */
43      private static final String FORM_KEY = "nameForm";
44  
45      /**
46       * The key used to retrieve the validator action.
47       */
48      private static final String ACTION = "required";
49  
50      /**
51       * Constructor for MultipleConfigFilesTest.
52       * @param name
53       */
54      public MultipleConfigFilesTest(String name) {
55          super(name);
56      }
57  
58      /** 
59       * Load <code>ValidatorResources</code> from multiple xml files.
60       */
61      @Override
62      protected void setUp() throws IOException, SAXException {
63          InputStream[] streams =
64              new InputStream[] {
65                  this.getClass().getResourceAsStream(
66                      "MultipleConfigFilesTest-1-config.xml"),
67                  this.getClass().getResourceAsStream(
68                      "MultipleConfigFilesTest-2-config.xml")};
69  
70          this.resources = new ValidatorResources(streams);
71  
72          for (int i = 0; i < streams.length; i++) {
73              streams[i].close();
74          }
75      }
76  
77     /**
78      * Check the forms and constants from different config files have
79      * been merged into the same FormSet.
80      */
81      public void testMergedConfig() {
82  
83          // *********** Default Locale *******************
84  
85          // Check the form from the first config file exists
86          Form form1 = resources.getForm("", "", "", "testForm1");
87          assertNotNull("Form 'testForm1' not found", form1);
88  
89          // Check the form from the second config file exists
90          Form form2 = resources.getForm("", "", "", "testForm2");
91          assertNotNull("Form 'testForm2' not found", form2);
92  
93          // Check the Constants  for the form from the first config file
94          Field field1 = form1.getField("testProperty1");
95          assertEquals("testProperty1 - const 1", "testConstValue1", field1.getVarValue("var11"));
96          assertEquals("testProperty1 - const 2", "testConstValue2", field1.getVarValue("var12"));
97  
98          // Check the Constants  for the form from the second config file
99          Field field2 = form2.getField("testProperty2");
100         assertEquals("testProperty2 - const 1", "testConstValue1", field2.getVarValue("var21"));
101         assertEquals("testProperty2 - const 2", "testConstValue2", field2.getVarValue("var22"));
102  
103         // *********** 'fr' locale *******************
104 
105         // Check the form from the first config file exists
106         Form form1_fr = resources.getForm("fr", "", "", "testForm1_fr");
107         assertNotNull("Form 'testForm1_fr' not found", form1_fr);
108 
109         // Check the form from the second config file exists
110         Form form2_fr = resources.getForm("fr", "", "", "testForm2_fr");
111         assertNotNull("Form 'testForm2_fr' not found", form2_fr);
112 
113         // Check the Constants  for the form from the first config file
114         Field field1_fr = form1_fr.getField("testProperty1_fr");
115         assertEquals("testProperty1_fr - const 1", "testConstValue1_fr", field1_fr.getVarValue("var11_fr"));
116         assertEquals("testProperty1_fr - const 2", "testConstValue2_fr", field1_fr.getVarValue("var12_fr"));
117 
118         // Check the Constants  for the form from the second config file
119         Field field2_fr = form2_fr.getField("testProperty2_fr");
120         assertEquals("testProperty2_fr - const 1", "testConstValue1_fr", field2_fr.getVarValue("var21_fr"));
121         assertEquals("testProperty2_fr - const 2", "testConstValue2_fr", field2_fr.getVarValue("var22_fr"));
122     }
123 
124     /**
125     * With nothing provided, we should fail both because both are required.
126     */
127     public void testBothBlank() throws ValidatorException {
128         // Create bean to run test on.
129         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
130 
131         // Construct validator based on the loaded resources
132         // and the form key
133         Validator validator = new Validator(resources, FORM_KEY);
134         // add the name bean to the validator as a resource
135         // for the validations to be performed on.
136         validator.setParameter(Validator.BEAN_PARAM, name);
137 
138         // Get results of the validation.
139         ValidatorResults results = null;
140 
141         // throws ValidatorException,
142         // but we aren't catching for testing
143         // since no validation methods we use
144         // throw this
145         results = validator.validate();
146 
147         assertNotNull("Results are null.", results);
148 
149         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
150         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
151 
152         assertNotNull(firstNameResult);
153         assertTrue(firstNameResult.containsAction(ACTION));
154         assertTrue(!firstNameResult.isValid(ACTION));
155 
156         assertNotNull(lastNameResult);
157         assertTrue(lastNameResult.containsAction(ACTION));
158         assertTrue(!lastNameResult.isValid(ACTION));
159         assertTrue(!lastNameResult.containsAction("int"));
160     }
161 
162     /**
163      * If the first name fails required, and the second test fails int, we should get two errors.
164     */
165     public void testRequiredFirstNameBlankLastNameShort()
166         throws ValidatorException {
167         // Create bean to run test on.
168         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
169         name.setFirstName("");
170         name.setLastName("Test");
171 
172         // Construct validator based on the loaded resources
173         // and the form key
174         Validator validator = new Validator(resources, FORM_KEY);
175         // add the name bean to the validator as a resource
176         // for the validations to be performed on.
177         validator.setParameter(Validator.BEAN_PARAM, name);
178 
179         // Get results of the validation.
180         ValidatorResults results = null;
181 
182         results = validator.validate();
183 
184         assertNotNull("Results are null.", results);
185 
186         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
187         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
188 
189         assertNotNull(firstNameResult);
190         assertTrue(firstNameResult.containsAction(ACTION));
191         assertTrue(!firstNameResult.isValid(ACTION));
192 
193         assertNotNull(lastNameResult);
194         assertTrue(lastNameResult.containsAction("int"));
195         assertTrue(!lastNameResult.isValid("int"));
196     }
197 
198     /**
199      * If the first name is there, and the last name fails int, we should get one error.
200     */
201     public void testRequiredLastNameShort() throws ValidatorException {
202         // Create bean to run test on.
203         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
204         name.setFirstName("Test");
205         name.setLastName("Test");
206 
207         // Construct validator based on the loaded resources
208         // and the form key
209         Validator validator = new Validator(resources, FORM_KEY);
210         // add the name bean to the validator as a resource
211         // for the validations to be performed on.
212         validator.setParameter(Validator.BEAN_PARAM, name);
213 
214         // Get results of the validation.
215         ValidatorResults results = null;
216 
217         results = validator.validate();
218 
219         assertNotNull("Results are null.", results);
220 
221         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
222         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
223 
224         assertNotNull(firstNameResult);
225         assertTrue(firstNameResult.containsAction(ACTION));
226         assertTrue(firstNameResult.isValid(ACTION));
227 
228         assertNotNull(lastNameResult);
229         assertTrue(lastNameResult.containsAction("int"));
230         assertTrue(!lastNameResult.isValid("int"));
231     }
232 
233     /**
234      * If first name is ok and last name is ok and is an int, no errors.
235     */
236     public void testRequiredLastNameLong() throws ValidatorException {
237         // Create bean to run test on.
238         NameBean/NameBean.html#NameBean">NameBean name = new NameBean();
239         name.setFirstName("Joe");
240         name.setLastName("12345678");
241 
242         // Construct validator based on the loaded resources
243         // and the form key
244         Validator validator = new Validator(resources, FORM_KEY);
245         // add the name bean to the validator as a resource
246         // for the validations to be performed on.
247         validator.setParameter(Validator.BEAN_PARAM, name);
248 
249         // Get results of the validation.
250         ValidatorResults results = null;
251 
252         results = validator.validate();
253 
254         assertNotNull("Results are null.", results);
255 
256         ValidatorResult firstNameResult = results.getValidatorResult("firstName");
257         ValidatorResult lastNameResult = results.getValidatorResult("lastName");
258 
259         assertNotNull(firstNameResult);
260         assertTrue(firstNameResult.containsAction(ACTION));
261         assertTrue(firstNameResult.isValid(ACTION));
262 
263         assertNotNull(lastNameResult);
264         assertTrue(lastNameResult.containsAction("int"));
265         assertTrue(lastNameResult.isValid("int"));
266     }
267 
268 }