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