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 /***
28 * Performs Validation Test.
29 *
30 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
31 */
32 public class RequiredNameTest extends TestCommon {
33
34 /***
35 * The key used to retrieve the set of validation
36 * rules from the xml file.
37 */
38 protected static String FORM_KEY = "nameForm";
39
40 /***
41 * The key used to retrieve the validator action.
42 */
43 protected static String ACTION = "required";
44
45 public RequiredNameTest(String name) {
46 super(name);
47 }
48
49 /***
50 * Start the tests.
51 *
52 * @param theArgs the arguments. Not used
53 */
54 public static void main(String[] theArgs) {
55 junit.awtui.TestRunner.main(new String[] {RequiredNameTest.class.getName()});
56 }
57
58 /***
59 * @return a test suite (<code>TestSuite</code>) that includes all methods
60 * starting with "test"
61 */
62 public static Test suite() {
63
64 return new TestSuite(RequiredNameTest.class);
65 }
66
67 /***
68 * Load <code>ValidatorResources</code> from
69 * validator-name-required.xml.
70 */
71 protected void setUp() throws IOException, SAXException {
72
73 loadResources("RequiredNameTest-config.xml");
74 }
75
76 protected void tearDown() {
77 }
78
79 /***
80 * Tests the required validation failure.
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 failed.", !firstNameResult.isValid(ACTION));
110
111 assertNotNull("First 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 failed.", !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
124
125
126 Validator validator = new Validator(resources, FORM_KEY);
127
128
129 validator.setParameter(Validator.BEAN_PARAM, name);
130
131
132 ValidatorResults results = null;
133
134 results = validator.validate();
135
136 assertNotNull("Results are null.", results);
137
138 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
139 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
140
141 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
142 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
143 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
144
145 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
146 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
147 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
148 }
149
150 /***
151 * Tests the required validation for first name.
152 */
153 public void testRequiredFirstName() throws ValidatorException {
154
155 NameBean name = new NameBean();
156 name.setFirstName("Joe");
157
158
159
160 Validator validator = new Validator(resources, FORM_KEY);
161
162
163 validator.setParameter(Validator.BEAN_PARAM, name);
164
165
166 ValidatorResults results = null;
167
168 results = validator.validate();
169
170 assertNotNull("Results are null.", results);
171
172 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
173 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
174
175 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
176 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
177 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
178
179 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
180 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
181 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
182 }
183
184 /***
185 * Tests the required validation for last name if it is blank.
186 */
187 public void testRequiredLastNameBlank() throws ValidatorException {
188
189 NameBean name = new NameBean();
190 name.setLastName("");
191
192
193
194 Validator validator = new Validator(resources, FORM_KEY);
195
196
197 validator.setParameter(Validator.BEAN_PARAM, name);
198
199
200 ValidatorResults results = null;
201
202 results = validator.validate();
203
204 assertNotNull("Results are null.", results);
205
206 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
207 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
208
209 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
210 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
211 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
212
213 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
214 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
215 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have failed.", !lastNameResult.isValid(ACTION));
216 }
217
218 /***
219 * Tests the required validation for last name.
220 */
221 public void testRequiredLastName() throws ValidatorException {
222
223 NameBean name = new NameBean();
224 name.setLastName("Smith");
225
226
227
228 Validator validator = new Validator(resources, FORM_KEY);
229
230
231 validator.setParameter(Validator.BEAN_PARAM, name);
232
233
234 ValidatorResults results = null;
235
236 results = validator.validate();
237
238 assertNotNull("Results are null.", results);
239
240 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
241 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
242
243 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
244 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
245 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have failed.", !firstNameResult.isValid(ACTION));
246
247 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
248 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
249 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
250
251 }
252
253 /***
254 * Tests the required validation for first and last name.
255 */
256 public void testRequiredName() throws ValidatorException {
257
258 NameBean name = new NameBean();
259 name.setFirstName("Joe");
260 name.setLastName("Smith");
261
262
263
264 Validator validator = new Validator(resources, FORM_KEY);
265
266
267 validator.setParameter(Validator.BEAN_PARAM, name);
268
269
270 ValidatorResults results = null;
271
272 results = validator.validate();
273
274 assertNotNull("Results are null.", results);
275
276 ValidatorResult firstNameResult = results.getValidatorResult("firstName");
277 ValidatorResult lastNameResult = results.getValidatorResult("lastName");
278
279 assertNotNull("First Name ValidatorResult should not be null.", firstNameResult);
280 assertTrue("First Name ValidatorResult should contain the '" + ACTION +"' action.", firstNameResult.containsAction(ACTION));
281 assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should have passed.", firstNameResult.isValid(ACTION));
282
283 assertNotNull("First Name ValidatorResult should not be null.", lastNameResult);
284 assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"' action.", lastNameResult.containsAction(ACTION));
285 assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should have passed.", lastNameResult.isValid(ACTION));
286 }
287
288 }