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.assertFalse;
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  
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  import org.xml.sax.SAXException;
29  
30  /**
31   * Performs Validation Test.
32   */
33  class MultipleTest extends AbstractCommonTest {
34  
35      /**
36       * The key used to retrieve the set of validation rules from the xml file.
37       */
38      protected static final String FORM_KEY = "nameForm";
39  
40      /**
41       * The key used to retrieve the validator action.
42       */
43      protected static final String ACTION = "required";
44  
45      /**
46       * Load {@code ValidatorResources} from validator-multipletest.xml.
47       */
48      @BeforeEach
49      protected void setUp() throws IOException, SAXException {
50          // Load resources
51          loadResources("MultipleTests-config.xml");
52      }
53  
54      @AfterEach
55      protected void tearDown() {
56      }
57  
58      /**
59       * With nothing provided, we should fail both because both are required.
60       */
61      @Test
62      void testBothBlank() throws ValidatorException {
63          // Create bean to run test on.
64          final NameBean name = new NameBean();
65  
66          // Construct validator based on the loaded resources
67          // and the form key
68          final Validator validator = new Validator(resources, FORM_KEY);
69          // add the name bean to the validator as a resource
70          // for the validations to be performed on.
71          validator.setParameter(Validator.BEAN_PARAM, name);
72  
73          // Get results of the validation.
74          // throws ValidatorException,
75          // but we aren't catching for testing
76          // since no validation methods we use
77          // throw this
78          final ValidatorResults results = validator.validate();
79  
80          assertNotNull(results, "Results are null.");
81  
82          final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
83          final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
84  
85          assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
86          assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
87          assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
88  
89          assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
90          assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
91          assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
92          assertFalse(lastNameResult.containsAction("int"), "Last Name ValidatorResults should not contain the 'int' action.");
93      }
94  
95      /**
96       * If middle name is not there, then the required dependent test should fail. No other tests should run
97       *
98       * @throws ValidatorException
99       */
100     @Test
101     void testFailingFirstDependentValidator() throws ValidatorException {
102         // Create bean to run test on.
103         final NameBean name = new NameBean();
104 
105         // Construct validator based on the loaded resources
106         // and the form key
107         final Validator validator = new Validator(resources, FORM_KEY);
108         // add the name bean to the validator as a resource
109         // for the validations to be performed on.
110         validator.setParameter(Validator.BEAN_PARAM, name);
111 
112         // Get results of the validation.
113         final ValidatorResults results = validator.validate();
114 
115         assertNotNull(results, "Results are null.");
116 
117         final ValidatorResult middleNameResult = results.getValidatorResult("middleName");
118 
119         assertNotNull(middleNameResult, "Middle Name ValidatorResult should not be null.");
120 
121         assertTrue(middleNameResult.containsAction("required"), "Middle Name ValidatorResult should contain the 'required' action.");
122         assertFalse(middleNameResult.isValid("required"), "Middle Name ValidatorResult for the 'required' action should have failed");
123 
124         assertFalse(middleNameResult.containsAction("int"), "Middle Name ValidatorResult should not contain the 'int' action.");
125 
126         assertFalse(middleNameResult.containsAction("positive"), "Middle Name ValidatorResult should not contain the 'positive' action.");
127     }
128 
129     /**
130      * If middle name is there but not int, then the required dependent test should pass, but the int dependent test should fail. No other tests should run.
131      *
132      * @throws ValidatorException
133      */
134     @Test
135     void testFailingNextDependentValidator() throws ValidatorException {
136         // Create bean to run test on.
137         final NameBean name = new NameBean();
138         name.setMiddleName("TEST");
139 
140         // Construct validator based on the loaded resources
141         // and the form key
142         final Validator validator = new Validator(resources, FORM_KEY);
143         // add the name bean to the validator as a resource
144         // for the validations to be performed on.
145         validator.setParameter(Validator.BEAN_PARAM, name);
146 
147         // Get results of the validation.
148         final ValidatorResults results = validator.validate();
149 
150         assertNotNull(results, "Results are null.");
151 
152         final ValidatorResult middleNameResult = results.getValidatorResult("middleName");
153 
154         assertNotNull(middleNameResult, "Middle Name ValidatorResult should not be null.");
155 
156         assertTrue(middleNameResult.containsAction("required"), "Middle Name ValidatorResult should contain the 'required' action.");
157         assertTrue(middleNameResult.isValid("required"), "Middle Name ValidatorResult for the 'required' action should have passed");
158 
159         assertTrue(middleNameResult.containsAction("int"), "Middle Name ValidatorResult should contain the 'int' action.");
160         assertFalse(middleNameResult.isValid("int"), "Middle Name ValidatorResult for the 'int' action should have failed");
161 
162         assertFalse(middleNameResult.containsAction("positive"), "Middle Name ValidatorResult should not contain the 'positive' action.");
163     }
164 
165     /**
166      * If middle name is there and a negative int, then the required and int dependent tests should pass, but the positive test should fail.
167      *
168      * @throws ValidatorException
169      */
170     @Test
171     void testPassingDependentsFailingMain() throws ValidatorException {
172         // Create bean to run test on.
173         final NameBean name = new NameBean();
174         name.setMiddleName("-2534");
175 
176         // Construct validator based on the loaded resources
177         // and the form key
178         final Validator validator = new Validator(resources, FORM_KEY);
179         // add the name bean to the validator as a resource
180         // for the validations to be performed on.
181         validator.setParameter(Validator.BEAN_PARAM, name);
182 
183         // Get results of the validation.
184         final ValidatorResults results = validator.validate();
185 
186         assertNotNull(results, "Results are null.");
187 
188         final ValidatorResult middleNameResult = results.getValidatorResult("middleName");
189 
190         assertNotNull(middleNameResult, "Middle Name ValidatorResult should not be null.");
191 
192         assertTrue(middleNameResult.containsAction("required"), "Middle Name ValidatorResult should contain the 'required' action.");
193         assertTrue(middleNameResult.isValid("required"), "Middle Name ValidatorResult for the 'required' action should have passed");
194 
195         assertTrue(middleNameResult.containsAction("int"), "Middle Name ValidatorResult should contain the 'int' action.");
196         assertTrue(middleNameResult.isValid("int"), "Middle Name ValidatorResult for the 'int' action should have passed");
197 
198         assertTrue(middleNameResult.containsAction("positive"), "Middle Name ValidatorResult should contain the 'positive' action.");
199         assertFalse(middleNameResult.isValid("positive"), "Middle Name ValidatorResult for the 'positive' action should have failed");
200     }
201 
202     /**
203      * If middle name is there and a positive int, then the required and int dependent tests should pass, and the positive test should pass.
204      *
205      * @throws ValidatorException
206      */
207     @Test
208     void testPassingDependentsPassingMain() throws ValidatorException {
209         // Create bean to run test on.
210         final NameBean name = new NameBean();
211         name.setMiddleName("2534");
212 
213         // Construct validator based on the loaded resources
214         // and the form key
215         final Validator validator = new Validator(resources, FORM_KEY);
216         // add the name bean to the validator as a resource
217         // for the validations to be performed on.
218         validator.setParameter(Validator.BEAN_PARAM, name);
219 
220         // Get results of the validation.
221         final ValidatorResults results = validator.validate();
222 
223         assertNotNull(results, "Results are null.");
224 
225         final ValidatorResult middleNameResult = results.getValidatorResult("middleName");
226 
227         assertNotNull(middleNameResult, "Middle Name ValidatorResult should not be null.");
228 
229         assertTrue(middleNameResult.containsAction("required"), "Middle Name ValidatorResult should contain the 'required' action.");
230         assertTrue(middleNameResult.isValid("required"), "Middle Name ValidatorResult for the 'required' action should have passed");
231 
232         assertTrue(middleNameResult.containsAction("int"), "Middle Name ValidatorResult should contain the 'int' action.");
233         assertTrue(middleNameResult.isValid("int"), "Middle Name ValidatorResult for the 'int' action should have passed");
234 
235         assertTrue(middleNameResult.containsAction("positive"), "Middle Name ValidatorResult should contain the 'positive' action.");
236         assertTrue(middleNameResult.isValid("positive"), "Middle Name ValidatorResult for the 'positive' action should have passed");
237     }
238 
239     /**
240      * If the first name fails required, and the second test fails int, we should get two errors.
241      */
242     @Test
243     void testRequiredFirstNameBlankLastNameShort() throws ValidatorException {
244         // Create bean to run test on.
245         final NameBean name = new NameBean();
246         name.setFirstName("");
247         name.setLastName("Test");
248 
249         // Construct validator based on the loaded resources
250         // and the form key
251         final Validator validator = new Validator(resources, FORM_KEY);
252         // add the name bean to the validator as a resource
253         // for the validations to be performed on.
254         validator.setParameter(Validator.BEAN_PARAM, name);
255 
256         // Get results of the validation.
257         final ValidatorResults results = validator.validate();
258 
259         assertNotNull(results, "Results are null.");
260 
261         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
262         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
263 
264         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
265         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
266         assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
267 
268         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
269         assertTrue(lastNameResult.containsAction("int"), "Last Name ValidatorResult should contain the 'int' action.");
270         assertFalse(lastNameResult.isValid("int"), "Last Name ValidatorResult for the 'int' action should have failed.");
271     }
272 
273     /**
274      * If first name is ok and last name is ok and is an int, no errors.
275      */
276     @Test
277     void testRequiredLastNameLong() throws ValidatorException {
278         // Create bean to run test on.
279         final NameBean name = new NameBean();
280         name.setFirstName("Joe");
281         name.setLastName("12345678");
282 
283         // Construct validator based on the loaded resources
284         // and the form key
285         final Validator validator = new Validator(resources, FORM_KEY);
286         // add the name bean to the validator as a resource
287         // for the validations to be performed on.
288         validator.setParameter(Validator.BEAN_PARAM, name);
289 
290         // Get results of the validation.
291         final ValidatorResults results = validator.validate();
292 
293         assertNotNull(results, "Results are null.");
294 
295         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
296         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
297 
298         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
299         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
300         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
301 
302         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
303         assertTrue(lastNameResult.containsAction("int"), "Last Name ValidatorResult should contain the 'int' action.");
304         assertTrue(lastNameResult.isValid("int"), "Last Name ValidatorResult for the 'int' action should have passed.");
305     }
306 
307     /**
308      * If the first name is there, and the last name fails int, we should get one error.
309      */
310     @Test
311     void testRequiredLastNameShort() throws ValidatorException {
312         // Create bean to run test on.
313         final NameBean name = new NameBean();
314         name.setFirstName("Test");
315         name.setLastName("Test");
316 
317         // Construct validator based on the loaded resources
318         // and the form key
319         final Validator validator = new Validator(resources, FORM_KEY);
320         // add the name bean to the validator as a resource
321         // for the validations to be performed on.
322         validator.setParameter(Validator.BEAN_PARAM, name);
323 
324         // Get results of the validation.
325         final ValidatorResults results = validator.validate();
326 
327         assertNotNull(results, "Results are null.");
328 
329         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
330         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
331 
332         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
333         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
334         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
335 
336         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
337         assertTrue(lastNameResult.containsAction("int"), "Last Name ValidatorResult should contain the 'int' action.");
338         assertFalse(lastNameResult.isValid("int"), "Last Name ValidatorResult for the 'int' action should have failed.");
339     }
340 }