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