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