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