1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.commons.validator;
24
25 import java.io.IOException;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.xml.sax.SAXException;
31
32
33 /***
34 * Performs Validation Test.
35 */
36 public class RequiredNameTest extends TestCommon {
37
38 /***
39 * The key used to retrieve the set of validation
40 * rules from the xml file.
41 */
42 protected static String FORM_KEY = "nameForm";
43
44 /***
45 * The key used to retrieve the validator action.
46 */
47 protected static String ACTION = "required";
48
49 public RequiredNameTest(String name) {
50 super(name);
51 }
52
53 /***
54 * Start the tests.
55 *
56 * @param theArgs the arguments. Not used
57 */
58 public static void main(String[] theArgs) {
59 junit.awtui.TestRunner.main(new String[] {RequiredNameTest.class.getName()});
60 }
61
62 /***
63 * @return a test suite (<code>TestSuite</code>) that includes all methods
64 * starting with "test"
65 */
66 public static Test suite() {
67
68 return new TestSuite(RequiredNameTest.class);
69 }
70
71 /***
72 * Load <code>ValidatorResources</code> from
73 * validator-name-required.xml.
74 */
75 protected void setUp() throws IOException, SAXException {
76
77 loadResources("RequiredNameTest-config.xml");
78 }
79
80 protected void tearDown() {
81 }
82
83 /***
84 * Tests the required validation failure.
85 */
86 public void testRequired() throws ValidatorException {
87
88 NameBean name = new NameBean();
89
90
91
92 Validator validator = new Validator(resources, FORM_KEY);
93
94
95 validator.setParameter(Validator.BEAN_PARAM, name);
96
97
98 ValidatorResults results = null;
99
100
101
102
103
104 results = validator.validate();
105
106 assertNotNull("Results are null.", results);
107
108 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
109 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
110
111 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
112 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
113 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
114
115 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
116 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
117 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
118 }
119
120 /***
121 * Tests the required validation for first name if it is blank.
122 */
123 public void testRequiredFirstNameBlank() throws ValidatorException {
124
125 NameBean name = new NameBean();
126 name.setFirstName("");
127
128
129
130 Validator validator = new Validator(resources, FORM_KEY);
131
132
133 validator.setParameter(Validator.BEAN_PARAM, name);
134
135
136 ValidatorResults results = null;
137
138 results = validator.validate();
139
140 assertNotNull("Results are null.", results);
141
142 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
143 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
144
145 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
146 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
147 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
148
149 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
150 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
151 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
152 }
153
154 /***
155 * Tests the required validation for first name.
156 */
157 public void testRequiredFirstName() throws ValidatorException {
158
159 NameBean name = new NameBean();
160 name.setFirstName("Joe");
161
162
163
164 Validator validator = new Validator(resources, FORM_KEY);
165
166
167 validator.setParameter(Validator.BEAN_PARAM, name);
168
169
170 ValidatorResults results = null;
171
172 results = validator.validate();
173
174 assertNotNull("Results are null.", results);
175
176 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
177 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
178
179 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
180 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
181 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
182
183 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
184 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
185 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
186 }
187
188 /***
189 * Tests the required validation for last name if it is blank.
190 */
191 public void testRequiredLastNameBlank() throws ValidatorException {
192
193 NameBean name = new NameBean();
194 name.setLastName("");
195
196
197
198 Validator validator = new Validator(resources, FORM_KEY);
199
200
201 validator.setParameter(Validator.BEAN_PARAM, name);
202
203
204 ValidatorResults results = null;
205
206 results = validator.validate();
207
208 assertNotNull("Results are null.", results);
209
210 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
211 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
212
213 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
214 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
215 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
216
217 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
218 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
219 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
220 }
221
222 /***
223 * Tests the required validation for last name.
224 */
225 public void testRequiredLastName() throws ValidatorException {
226
227 NameBean name = new NameBean();
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 failed.", !firstNameResult.isValid(ACTION));
250
251 assertNotNull("First 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 /***
258 * Tests the required validation for first and last name.
259 */
260 public void testRequiredName() throws ValidatorException {
261
262 NameBean name = new NameBean();
263 name.setFirstName("Joe");
264 name.setLastName("Smith");
265
266
267
268 Validator validator = new Validator(resources, FORM_KEY);
269
270
271 validator.setParameter(Validator.BEAN_PARAM, name);
272
273
274 ValidatorResults results = null;
275
276 results = validator.validate();
277
278 assertNotNull("Results are null.", results);
279
280 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
281 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
282
283 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
284 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
285 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
286
287 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
288 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
289 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
290 }
291
292 }