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 static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.InputStream;
25
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34
35
36 public class ExtensionTest {
37
38
39
40
41 protected static String FORM_KEY = "nameForm";
42
43
44
45
46 protected static String FORM_KEY2 = "nameForm2";
47
48
49
50
51 protected static String CHECK_MSG_KEY = "nameForm.lastname.displayname";
52
53
54
55
56 protected static String ACTION = "required";
57
58
59
60
61 private ValidatorResources resources;
62
63
64
65
66 @BeforeEach
67 protected void setUp() throws Exception {
68
69 try (InputStream in = this.getClass().getResourceAsStream("ExtensionTest-config.xml")) {
70 resources = new ValidatorResources(in);
71 }
72 }
73
74 @AfterEach
75 protected void tearDown() {
76 }
77
78
79
80
81 @Test
82 public void testOrder() {
83
84 final Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY);
85 final Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2);
86
87 assertNotNull(form, FORM_KEY + " is null.");
88 assertEquals(2, form.getFields().size(), "There should only be 2 fields in " + FORM_KEY);
89
90 assertNotNull(form2, FORM_KEY2 + " is null.");
91 assertEquals(2, form2.getFields().size(), "There should only be 2 fields in " + FORM_KEY2);
92
93
94 Field fieldFirstName = form.getFields().get(0);
95
96 Field fieldLastName = form.getFields().get(1);
97 assertTrue(fieldFirstName.getKey().equals("firstName"), "firstName in " + FORM_KEY + " should be the first in the list");
98 assertTrue(fieldLastName.getKey().equals("lastName"), "lastName in " + FORM_KEY + " should be the first in the list");
99
100
101 fieldLastName = form2.getFields().get(0);
102
103 fieldFirstName = form2.getFields().get(1);
104 assertTrue(fieldFirstName.getKey().equals("firstName"), "firstName in " + FORM_KEY2 + " should be the first in the list");
105 assertTrue(fieldLastName.getKey().equals("lastName"), "lastName in " + FORM_KEY2 + " should be the first in the list");
106
107 }
108
109
110
111
112 @Test
113 public void testOverrideRule() throws ValidatorException {
114
115
116 final NameBean name = new NameBean();
117 name.setLastName("Smith");
118
119
120
121 final Validator validator = new Validator(resources, FORM_KEY2);
122
123
124 validator.setParameter(Validator.BEAN_PARAM, name);
125
126
127 final ValidatorResults results = validator.validate();
128
129 assertNotNull(results, "Results are null.");
130
131 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
132 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
133 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
134 assertTrue(firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY),
135 "First Name ValidatorResult for the '" + ACTION + "' action should have '" + CHECK_MSG_KEY + " as a key.");
136
137 assertNull(lastNameResult, "Last Name ValidatorResult should be null.");
138 }
139
140
141
142
143 @Test
144 public void testRequired() throws ValidatorException {
145
146 final NameBean name = new NameBean();
147
148
149
150 final Validator validator = new Validator(resources, FORM_KEY);
151
152
153 validator.setParameter(Validator.BEAN_PARAM, name);
154
155
156
157
158
159
160 final ValidatorResults results = validator.validate();
161
162 assertNotNull(results, "Results are null.");
163
164 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
165 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
166
167 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
168 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
169 assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
170
171 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
172 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
173 assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
174 }
175
176
177
178
179 @Test
180 public void testRequiredFirstName() throws ValidatorException {
181
182 final NameBean name = new NameBean();
183 name.setFirstName("Joe");
184
185
186
187 final Validator validator = new Validator(resources, FORM_KEY);
188
189
190 validator.setParameter(Validator.BEAN_PARAM, name);
191
192
193 final ValidatorResults results = validator.validate();
194
195 assertNotNull(results, "Results are null.");
196
197 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
198 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
199
200 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
201 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
202 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
203
204 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
205 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
206 assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
207 }
208
209
210
211
212 @Test
213 public void testRequiredFirstNameBlank() throws ValidatorException {
214
215 final NameBean name = new NameBean();
216 name.setFirstName("");
217
218
219
220 final Validator validator = new Validator(resources, FORM_KEY);
221
222
223 validator.setParameter(Validator.BEAN_PARAM, name);
224
225
226 final ValidatorResults results = validator.validate();
227
228 assertNotNull(results, "Results are null.");
229
230 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
231 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
232
233 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
234 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
235 assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
236
237 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
238 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
239 assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
240 }
241
242
243
244
245 @Test
246 public void testRequiredLastName() throws ValidatorException {
247
248 final NameBean name = new NameBean();
249 name.setLastName("Smith");
250
251
252
253 final Validator validator = new Validator(resources, FORM_KEY);
254
255
256 validator.setParameter(Validator.BEAN_PARAM, name);
257
258
259 final ValidatorResults results = validator.validate();
260
261 assertNotNull(results, "Results are null.");
262
263 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
264 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
265
266 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
267 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
268 assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
269
270 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
271 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
272 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
273
274 }
275
276
277
278
279 @Test
280 public void testRequiredLastNameBlank() throws ValidatorException {
281
282 final NameBean name = new NameBean();
283 name.setLastName("");
284
285
286
287 final Validator validator = new Validator(resources, FORM_KEY);
288
289
290 validator.setParameter(Validator.BEAN_PARAM, name);
291
292
293 final ValidatorResults results = validator.validate();
294
295 assertNotNull(results, "Results are null.");
296
297 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
298 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
299
300 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
301 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
302 assertTrue(!firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have failed.");
303
304 assertNotNull(lastNameResult, "First Name ValidatorResult should not be null.");
305 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
306 assertTrue(!lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have failed.");
307 }
308
309
310
311
312 @Test
313 public void testRequiredName() throws ValidatorException {
314
315 final NameBean name = new NameBean();
316 name.setFirstName("Joe");
317 name.setLastName("Smith");
318
319
320
321 final Validator validator = new Validator(resources, FORM_KEY);
322
323
324 validator.setParameter(Validator.BEAN_PARAM, name);
325
326
327 final ValidatorResults results = validator.validate();
328
329 assertNotNull(results, "Results are null.");
330
331 final ValidatorResult firstNameResult = results.getValidatorResult("firstName");
332 final ValidatorResult lastNameResult = results.getValidatorResult("lastName");
333
334 assertNotNull(firstNameResult, "First Name ValidatorResult should not be null.");
335 assertTrue(firstNameResult.containsAction(ACTION), "First Name ValidatorResult should contain the '" + ACTION + "' action.");
336 assertTrue(firstNameResult.isValid(ACTION), "First Name ValidatorResult for the '" + ACTION + "' action should have passed.");
337
338 assertNotNull(lastNameResult, "Last Name ValidatorResult should not be null.");
339 assertTrue(lastNameResult.containsAction(ACTION), "Last Name ValidatorResult should contain the '" + ACTION + "' action.");
340 assertTrue(lastNameResult.isValid(ACTION), "Last Name ValidatorResult for the '" + ACTION + "' action should have passed.");
341 }
342 }