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