1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.IOException;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.xml.sax.SAXException;
25
26 /***
27 * Performs Validation Test.
28 *
29 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
30 */
31 public class RequiredIfTest extends TestCommon {
32
33 /***
34 * The key used to retrieve the set of validation
35 * 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 public RequiredIfTest(String name) {
45 super(name);
46 }
47
48 /***
49 * Start the tests.
50 *
51 * @param theArgs the arguments. Not used
52 */
53 public static void main(String[] theArgs) {
54 junit.awtui.TestRunner.main(new String[] {RequiredIfTest.class.getName()});
55 }
56
57 /***
58 * @return a test suite (<code>TestSuite</code>) that includes all methods
59 * starting with "test"
60 */
61 public static Test suite() {
62
63 return new TestSuite(RequiredIfTest.class);
64 }
65
66 /***
67 * Load <code>ValidatorResources</code> from
68 * validator-requiredif.xml.
69 */
70 protected void setUp() throws IOException, SAXException {
71
72 loadResources("RequiredIfTest-config.xml");
73 }
74
75 protected void tearDown() {
76 }
77
78 /***
79 * With nothing provided, we should pass since the fields only fail on
80 * null if the other field is non-blank.
81 */
82 public void testRequired() throws ValidatorException {
83
84 NameBean name = new NameBean();
85
86
87
88 Validator validator = new Validator(resources, FORM_KEY);
89
90
91 validator.setParameter(Validator.BEAN_PARAM, name);
92
93
94 ValidatorResults results = null;
95
96
97
98
99
100 results = validator.validate();
101
102 assertNotNull("Results are null.", results);
103
104 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
105 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
106
107 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
108 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
109 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
110
111 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
112 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
113 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
114 }
115
116 /***
117 * Tests the required validation for first name if it is blank.
118 */
119 public void testRequiredFirstNameBlank() throws ValidatorException {
120
121 NameBean name = new NameBean();
122 name.setFirstName("");
123 name.setLastName("Test");
124
125
126
127 Validator validator = new Validator(resources, FORM_KEY);
128
129
130 validator.setParameter(Validator.BEAN_PARAM, name);
131
132
133 ValidatorResults results = null;
134
135 results = validator.validate();
136
137 assertNotNull("Results are null.", results);
138
139 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
140 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
141
142 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
143 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
144 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
145
146 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
147 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
148 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
149 }
150
151 /***
152 * Tests the required validation for last name.
153 */
154 public void testRequiredFirstName() throws ValidatorException {
155
156 NameBean name = new NameBean();
157 name.setFirstName("Test");
158 name.setLastName("Test");
159
160
161
162 Validator validator = new Validator(resources, FORM_KEY);
163
164
165 validator.setParameter(Validator.BEAN_PARAM, name);
166
167
168 ValidatorResults results = null;
169
170 results = validator.validate();
171
172 assertNotNull("Results are null.", results);
173
174 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
175 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
176
177 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
178 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
179 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
180
181 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
182 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
183 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
184 }
185
186 /***
187 * Tests the required validation for last name if it is blank.
188 */
189 public void testRequiredLastNameBlank() throws ValidatorException {
190
191 NameBean name = new NameBean();
192 name.setFirstName("Joe");
193 name.setLastName("");
194
195
196
197 Validator validator = new Validator(resources, FORM_KEY);
198
199
200 validator.setParameter(Validator.BEAN_PARAM, name);
201
202
203 ValidatorResults results = null;
204
205 results = validator.validate();
206
207 assertNotNull("Results are null.", results);
208
209 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
210 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
211
212 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
213 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
214 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
215
216 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
217 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
218 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
219 }
220
221 /***
222 * Tests the required validation for last name.
223 */
224 public void testRequiredLastName() throws ValidatorException {
225
226 NameBean name = new NameBean();
227 name.setFirstName("Joe");
228 name.setLastName("Smith");
229
230
231
232 Validator validator = new Validator(resources, FORM_KEY);
233
234
235 validator.setParameter(Validator.BEAN_PARAM, name);
236
237
238 ValidatorResults results = null;
239
240 results = validator.validate();
241
242 assertNotNull("Results are null.", results);
243
244 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
245 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
246
247 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
248 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
249 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
250
251 assertNotNull("Last Name ValidatorResult should not be null.", lastNameResult);
252 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
253 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
254
255 }
256
257 }