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