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 RequiredNameTest 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-name-required.xml.
46       */
47      @BeforeEach
48      protected void setUp() throws IOException, SAXException {
49          // Load resources
50          loadResources("RequiredNameTest-config.xml");
51      }
52  
53      @AfterEach
54      protected void tearDown() {
55      }
56  
57      /**
58       * Tests the required validation failure.
59       */
60      @Test
61      public void testRequired() 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, "First 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      }
92  
93      /**
94       * Tests the required validation for first name.
95       */
96      @Test
97      public void testRequiredFirstName() throws ValidatorException {
98          // Create bean to run test on.
99          final NameBean name = new NameBean();
100         name.setFirstName("Joe");
101 
102         // Construct validator based on the loaded resources
103         // and the form key
104         final Validator validator = new Validator(resources, FORM_KEY);
105         // add the name bean to the validator as a resource
106         // for the validations to be performed on.
107         validator.setParameter(Validator.BEAN_PARAM, name);
108 
109         // Get results of the validation.
110         final ValidatorResults results = validator.validate();
111 
112         assertNotNull(results, "Results are null.");
113 
114         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
115         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
116 
117         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
118         assertTrue(firstNameResult.containsAction(ACTION), () -> "First Name ValidatorResult should contain the '" + ACTION + "' action.");
119         assertTrue(firstNameResult.isValid(ACTION), () -> "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
120 
121         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
122         assertTrue(lastNameResult.containsAction(ACTION), () -> "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
123         assertTrue(!lastNameResult.isValid(ACTION), () -> "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
124     }
125 
126     /**
127      * Tests the required validation for first name if it is blank.
128      */
129     @Test
130     public void testRequiredFirstNameBlank() throws ValidatorException {
131         // Create bean to run test on.
132         final NameBean name = new NameBean();
133         name.setFirstName("");
134 
135         // Construct validator based on the loaded resources
136         // and the form key
137         final Validator validator = new Validator(resources, FORM_KEY);
138         // add the name bean to the validator as a resource
139         // for the validations to be performed on.
140         validator.setParameter(Validator.BEAN_PARAM, name);
141 
142         // Get results of the validation.
143         final ValidatorResults results = validator.validate();
144 
145         assertNotNull(results, "Results are null.");
146 
147         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
148         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
149 
150         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
151         assertTrue(firstNameResult.containsAction(ACTION), () -> "First Name ValidatorResult should contain the '" + ACTION + "' action.");
152         assertTrue(!firstNameResult.isValid(ACTION), () -> "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
153 
154         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
155         assertTrue(lastNameResult.containsAction(ACTION), () -> "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
156         assertTrue(!lastNameResult.isValid(ACTION), () -> "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
157     }
158 
159     /**
160      * Tests the required validation for last name.
161      */
162     @Test
163     public void testRequiredLastName() throws ValidatorException {
164         // Create bean to run test on.
165         final NameBean name = new NameBean();
166         name.setLastName("Smith");
167 
168         // Construct validator based on the loaded resources
169         // and the form key
170         final Validator validator = new Validator(resources, FORM_KEY);
171         // add the name bean to the validator as a resource
172         // for the validations to be performed on.
173         validator.setParameter(Validator.BEAN_PARAM, name);
174 
175         // Get results of the validation.
176         final ValidatorResults results = validator.validate();
177 
178         assertNotNull(results, "Results are null.");
179 
180         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
181         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
182 
183         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
184         assertTrue(firstNameResult.containsAction(ACTION), () -> "First Name ValidatorResult should contain the '" + ACTION + "' action.");
185         assertTrue(!firstNameResult.isValid(ACTION), () -> "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
186 
187         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
188         assertTrue(lastNameResult.containsAction(ACTION), () -> "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
189         assertTrue(lastNameResult.isValid(ACTION), () -> "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
190 
191     }
192 
193     /**
194      * Tests the required validation for last name if it is blank.
195      */
196     @Test
197     public void testRequiredLastNameBlank() throws ValidatorException {
198         // Create bean to run test on.
199         final NameBean name = new NameBean();
200         name.setLastName("");
201 
202         // Construct validator based on the loaded resources
203         // and the form key
204         final Validator validator = new Validator(resources, FORM_KEY);
205         // add the name bean to the validator as a resource
206         // for the validations to be performed on.
207         validator.setParameter(Validator.BEAN_PARAM, name);
208 
209         // Get results of the validation.
210         final ValidatorResults results = validator.validate();
211 
212         assertNotNull(results, "Results are null.");
213 
214         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
215         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
216 
217         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
218         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
219         assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
220 
221         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
222         assertTrue(lastNameResult.containsAction(ACTION), () -> "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
223         assertTrue(!lastNameResult.isValid(ACTION), () -> "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
224     }
225 
226     /**
227      * Tests the required validation for first and last name.
228      */
229     @Test
230     public void testRequiredName() throws ValidatorException {
231         // Create bean to run test on.
232         final NameBean name = new NameBean();
233         name.setFirstName("Joe");
234         name.setLastName("Smith");
235 
236         // Construct validator based on the loaded resources
237         // and the form key
238         final Validator validator = new Validator(resources, FORM_KEY);
239         // add the name bean to the validator as a resource
240         // for the validations to be performed on.
241         validator.setParameter(Validator.BEAN_PARAM, name);
242 
243         // Get results of the validation.
244         final ValidatorResults results = validator.validate();
245 
246         assertNotNull(results, "Results are null.");
247 
248         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
249         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
250 
251         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
252         assertTrue(firstNameResult.containsAction(ACTION), () -> "First Name ValidatorResult should contain the '" + ACTION + "' action.");
253         assertTrue(firstNameResult.isValid(ACTION), () -> "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
254 
255         assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
256         assertTrue(lastNameResult.containsAction(ACTION), () -> "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
257         assertTrue(lastNameResult.isValid(ACTION), () -> "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
258     }
259 
260 }