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