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 RequiredIfTest 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 = "requiredif";
44  
45      /**
46       * Load {@code ValidatorResources} from validator-requiredif.xml.
47       */
48      @BeforeEach
49      protected void setUp() throws IOException, SAXException {
50          // Load resources
51          loadResources("RequiredIfTest-config.xml");
52      }
53  
54      @AfterEach
55      protected void tearDown() {
56      }
57  
58      /**
59       * With nothing provided, we should pass since the fields only fail on null if the other field is non-blank.
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          assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
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          assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
92      }
93  
94      /**
95       * Tests the required validation for last 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("Test");
102         name.setLastName("Test");
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 firstNameResult = results.getValidatorResult("firstName");
117         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
118 
119         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
120         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
121         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
122 
123         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
124         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
125         assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
126     }
127 
128     /**
129      * Tests the required validation for first name if it is blank.
130      */
131     @Test
132     void testRequiredFirstNameBlank() throws ValidatorException {
133         // Create bean to run test on.
134         final NameBean name = new NameBean();
135         name.setFirstName("");
136         name.setLastName("Test");
137 
138         // Construct validator based on the loaded resources
139         // and the form key
140         final Validator validator = new Validator(resources, FORM_KEY);
141         // add the name bean to the validator as a resource
142         // for the validations to be performed on.
143         validator.setParameter(Validator.BEAN_PARAM, name);
144 
145         // Get results of the validation.
146         final ValidatorResults results = validator.validate();
147 
148         assertNotNull(results, "Results are null.");
149 
150         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
151         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
152 
153         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
154         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
155         assertFalse(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
156 
157         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
158         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
159         assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
160     }
161 
162     /**
163      * Tests the required validation for last name.
164      */
165     @Test
166     void testRequiredLastName() throws ValidatorException {
167         // Create bean to run test on.
168         final NameBean name = new NameBean();
169         name.setFirstName("Joe");
170         name.setLastName("Smith");
171 
172         // Construct validator based on the loaded resources
173         // and the form key
174         final Validator validator = new Validator(resources, FORM_KEY);
175         // add the name bean to the validator as a resource
176         // for the validations to be performed on.
177         validator.setParameter(Validator.BEAN_PARAM, name);
178 
179         // Get results of the validation.
180         final ValidatorResults results = validator.validate();
181 
182         assertNotNull(results, "Results are null.");
183 
184         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
185         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
186 
187         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
188         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
189         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
190 
191         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
192         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
193         assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
194 
195     }
196 
197     /**
198      * Tests the required validation for last name if it is blank.
199      */
200     @Test
201     void testRequiredLastNameBlank() throws ValidatorException {
202         // Create bean to run test on.
203         final NameBean name = new NameBean();
204         name.setFirstName("Joe");
205         name.setLastName("");
206 
207         // Construct validator based on the loaded resources
208         // and the form key
209         final Validator validator = new Validator(resources, FORM_KEY);
210         // add the name bean to the validator as a resource
211         // for the validations to be performed on.
212         validator.setParameter(Validator.BEAN_PARAM, name);
213 
214         // Get results of the validation.
215         final ValidatorResults results = validator.validate();
216 
217         assertNotNull(results, "Results are null.");
218 
219         final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
220         final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
221 
222         assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
223         assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
224         assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
225 
226         assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
227         assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
228         assertFalse(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
229     }
230 
231 }