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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.InputStream;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * <p>
32   * Performs tests for extension in form definitions. Performs the same tests RequiredNameTest does but with an equivalent validation definition with extension
33   * definitions (validator-extension.xml), plus an extra check on overriding rules and another one checking it mantains correct order when extending.
34   * </p>
35   */
36  public class ExtensionTest {
37  
38      /**
39       * The key used to retrieve the set of validation rules from the xml file.
40       */
41      protected static String FORM_KEY = "nameForm";
42  
43      /**
44       * The key used to retrieve the set of validation rules from the xml file.
45       */
46      protected static String FORM_KEY2 = "nameForm2";
47  
48      /**
49       * The key used to retrieve the set of validation rules from the xml file.
50       */
51      protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname";
52  
53      /**
54       * The key used to retrieve the validator action.
55       */
56      protected static String ACTION = "required";
57  
58      /**
59       * Resources used for validation tests.
60       */
61      private ValidatorResources resources;
62  
63      /**
64       * Load <code>ValidatorResources</code> from validator-extension.xml.
65       */
66      @BeforeEach
67      protected void setUp() throws Exception {
68          // Load resources
69          try (InputStream in = this.getClass().getResourceAsStream("ExtensionTest-config.xml")) {
70              resources = new ValidatorResources(in);
71          }
72      }
73  
74      @AfterEach
75      protected void tearDown() {
76      }
77  
78      /**
79       * Tests if the order is mantained when extending a form. Parent form fields should preceed self form fields, except if we override the rules.
80       */
81      @Test
82      public void testOrder() {
83  
84          final Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY);
85          final Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2);
86  
87          assertNotNull(form, FORM_KEY + " is null.");
88          assertEquals(2, form.getFields().size(), "There should only be 2 fields in " + FORM_KEY);
89  
90          assertNotNull(form2, FORM_KEY2 + " is null.");
91          assertEquals(2, form2.getFields().size(), "There should only be 2 fields in " + FORM_KEY2);
92  
93          // get the first field
94          Field fieldFirstName = form.getFields().get(0);
95          // get the second field
96          Field fieldLastName = form.getFields().get(1);
97          assertTrue(fieldFirstName.getKey().equals("firstName"), "firstName in " + FORM_KEY + " should be the first in the list");
98          assertTrue(fieldLastName.getKey().equals("lastName"), "lastName in " + FORM_KEY + " should be the first in the list");
99  
100 //     get the second field
101         fieldLastName = form2.getFields().get(0);
102         // get the first field
103         fieldFirstName = form2.getFields().get(1);
104         assertTrue(fieldFirstName.getKey().equals("firstName"), "firstName in " + FORM_KEY2 + " should be the first in the list");
105         assertTrue(fieldLastName.getKey().equals("lastName"), "lastName in " + FORM_KEY2 + " should be the first in the list");
106 
107     }
108 
109     /**
110      * Tests if we can override a rule. We "can" override a rule if the message shown when the firstName required test fails and the lastName test is null.
111      */
112     @Test
113     public void testOverrideRule() throws ValidatorException {
114 
115         // Create bean to run test on.
116         final NameBean name = new NameBean();
117         name.setLastName("Smith");
118 
119         // Construct validator based on the loaded resources
120         // and the form key
121         final Validator validator = new Validator(resources, FORM_KEY2);
122         // add the name bean to the validator as a resource
123         // for the validations to be performed on.
124         validator.setParameter(Validator.BEAN_PARAM, name);
125 
126         // Get results of the validation.
127         final ValidatorResults results = validator.validate();
128 
129         assertNotNull(results, "Results are null.");
130 
131         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
132         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
133         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
134         assertTrue(firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY),
135                 "First Name ValidatorResult for the '" + ACTION + "' action should have '" + CHECK_MSG_KEY + " as a key.");
136 
137         assertNull(lastNameResult, "Last Name ValidatorResult should be null.");
138     }
139 
140     /**
141      * Tests the required validation failure.
142      */
143     @Test
144     public void testRequired() throws ValidatorException {
145         // Create bean to run test on.
146         final NameBean name = new NameBean();
147 
148         // Construct validator based on the loaded resources
149         // and the form key
150         final Validator validator = new Validator(resources, FORM_KEY);
151         // add the name bean to the validator as a resource
152         // for the validations to be performed on.
153         validator.setParameter(Validator.BEAN_PARAM, name);
154 
155         // Get results of the validation.
156         // throws ValidatorException,
157         // but we aren't catching for testing
158         // since no validation methods we use
159         // throw this
160         final ValidatorResults results = validator.validate();
161 
162         assertNotNull(results, "Results are null.");
163 
164         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
165         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
166 
167         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
168         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
169         assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
170 
171         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
172         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
173         assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
174     }
175 
176     /**
177      * Tests the required validation for first name.
178      */
179     @Test
180     public void testRequiredFirstName() throws ValidatorException {
181         // Create bean to run test on.
182         final NameBean name = new NameBean();
183         name.setFirstName("Joe");
184 
185         // Construct validator based on the loaded resources
186         // and the form key
187         final Validator validator = new Validator(resources, FORM_KEY);
188         // add the name bean to the validator as a resource
189         // for the validations to be performed on.
190         validator.setParameter(Validator.BEAN_PARAM, name);
191 
192         // Get results of the validation.
193         final ValidatorResults results = validator.validate();
194 
195         assertNotNull(results, "Results are null.");
196 
197         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
198         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
199 
200         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
201         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
202         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
203 
204         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
205         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
206         assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
207     }
208 
209     /**
210      * Tests the required validation for first name if it is blank.
211      */
212     @Test
213     public void testRequiredFirstNameBlank() throws ValidatorException {
214         // Create bean to run test on.
215         final NameBean name = new NameBean();
216         name.setFirstName("");
217 
218         // Construct validator based on the loaded resources
219         // and the form key
220         final Validator validator = new Validator(resources, FORM_KEY);
221         // add the name bean to the validator as a resource
222         // for the validations to be performed on.
223         validator.setParameter(Validator.BEAN_PARAM, name);
224 
225         // Get results of the validation.
226         final ValidatorResults results = validator.validate();
227 
228         assertNotNull(results, "Results are null.");
229 
230         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
231         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
232 
233         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
234         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
235         assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
236 
237         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
238         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
239         assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
240     }
241 
242     /**
243      * Tests the required validation for last name.
244      */
245     @Test
246     public void testRequiredLastName() throws ValidatorException {
247         // Create bean to run test on.
248         final NameBean name = new NameBean();
249         name.setLastName("Smith");
250 
251         // Construct validator based on the loaded resources
252         // and the form key
253         final Validator validator = new Validator(resources, FORM_KEY);
254         // add the name bean to the validator as a resource
255         // for the validations to be performed on.
256         validator.setParameter(Validator.BEAN_PARAM, name);
257 
258         // Get results of the validation.
259         final ValidatorResults results = validator.validate();
260 
261         assertNotNull(results, "Results are null.");
262 
263         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
264         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
265 
266         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
267         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
268         assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
269 
270         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
271         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
272         assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
273 
274     }
275 
276     /**
277      * Tests the required validation for last name if it is blank.
278      */
279     @Test
280     public void testRequiredLastNameBlank() throws ValidatorException {
281         // Create bean to run test on.
282         final NameBean name = new NameBean();
283         name.setLastName("");
284 
285         // Construct validator based on the loaded resources
286         // and the form key
287         final Validator validator = new Validator(resources, FORM_KEY);
288         // add the name bean to the validator as a resource
289         // for the validations to be performed on.
290         validator.setParameter(Validator.BEAN_PARAM, name);
291 
292         // Get results of the validation.
293         final ValidatorResults results = validator.validate();
294 
295         assertNotNull(results, "Results are null.");
296 
297         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
298         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
299 
300         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
301         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
302         assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
303 
304         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
305         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
306         assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
307     }
308 
309     /**
310      * Tests the required validation for first and last name.
311      */
312     @Test
313     public void testRequiredName() throws ValidatorException {
314         // Create bean to run test on.
315         final NameBean name = new NameBean();
316         name.setFirstName("Joe");
317         name.setLastName("Smith");
318 
319         // Construct validator based on the loaded resources
320         // and the form key
321         final Validator validator = new Validator(resources, FORM_KEY);
322         // add the name bean to the validator as a resource
323         // for the validations to be performed on.
324         validator.setParameter(Validator.BEAN_PARAM, name);
325 
326         // Get results of the validation.
327         final ValidatorResults results = validator.validate();
328 
329         assertNotNull(results, "Results are null.");
330 
331         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
332         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
333 
334         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
335         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
336         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
337 
338         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
339         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
340         assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
341     }
342 }