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