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