1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.jsf.core;
20
21 import java.io.IOException;
22 import java.lang.reflect.Field;
23
24 import javax.faces.component.UIInput;
25 import javax.faces.component.UIViewRoot;
26 import javax.faces.validator.BeanValidator;
27 import javax.faces.validator.Validator;
28
29 import org.apache.myfaces.util.ExternalSpecifications;
30 import org.apache.myfaces.view.facelets.FaceletTestCase;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34
35
36
37
38
39
40 public class ValidateBeanTestCase extends FaceletTestCase
41 {
42
43 @Override
44 public void setUp() throws Exception
45 {
46 super.setUp();
47
48
49 _setBeanValidationAvailable(true);
50 }
51
52
53
54
55
56
57
58 private void _setBeanValidationAvailable(boolean available)
59 {
60 try
61 {
62 Field field = ExternalSpecifications.class.getDeclaredField("beanValidationAvailable");
63 field.setAccessible(true);
64 field.set(ExternalSpecifications.class, available);
65 }
66 catch (Exception e)
67 {
68 throw new IllegalStateException("Could not configure BeanValidation for the test case.", e);
69 }
70 }
71
72
73
74
75
76
77
78
79
80 @SuppressWarnings("unchecked")
81 private <T> T _getValidator(UIInput input, Class<T> validatorClass)
82 {
83 Validator[] validators = input.getValidators();
84 if (validators != null)
85 {
86 for (Validator validator : validators)
87 {
88 if (validatorClass.isAssignableFrom(validator.getClass()))
89 {
90 return (T) validator;
91 }
92 }
93 }
94
95 return null;
96 }
97
98
99
100
101
102
103
104 private boolean _hasValidator(UIInput input, Class<?> validatorClass)
105 {
106 return (_getValidator(input, validatorClass) != null);
107 }
108
109
110
111
112
113
114
115 private String _getValidationGroups(UIInput input)
116 {
117 BeanValidator validator = _getValidator(input, BeanValidator.class);
118 if (validator != null)
119 {
120 return validator.getValidationGroups();
121 }
122 return null;
123 }
124
125
126
127
128
129
130
131 @Test
132 @SuppressWarnings("unchecked")
133 public void testBeanValidatorInstalledManually() throws IOException
134 {
135
136 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.FALSE);
137
138
139 UIViewRoot root = facesContext.getViewRoot();
140 vdl.buildView(facesContext, root, "testValidateBean.xhtml");
141
142
143 UIInput input = (UIInput) root.findComponent("form:input");
144
145
146 Assert.assertTrue(_hasValidator(input, BeanValidator.class));
147 }
148
149
150
151
152
153
154
155 @Test
156 public void testBeanValidatorInstalledAutomatically() throws IOException
157 {
158
159 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
160
161
162 UIViewRoot root = facesContext.getViewRoot();
163 vdl.buildView(facesContext, root, "testValidateBeanEmptyInput.xhtml");
164
165
166 UIInput input = (UIInput) root.findComponent("form:input");
167
168
169 Assert.assertTrue(_hasValidator(input, BeanValidator.class));
170 }
171
172
173
174
175
176
177
178 @Test
179 @SuppressWarnings("unchecked")
180 public void testDisabledBeanValidatorNotInstalled() throws IOException
181 {
182
183 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
184
185
186 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.TRUE);
187
188
189 UIViewRoot root = facesContext.getViewRoot();
190 vdl.buildView(facesContext, root, "testValidateBean.xhtml");
191
192
193 UIInput input = (UIInput) root.findComponent("form:input");
194
195
196 Assert.assertFalse(_hasValidator(input, BeanValidator.class));
197 }
198
199
200
201
202
203
204
205
206
207 @Test
208 @SuppressWarnings("unchecked")
209 public void testNestingValidateBean() throws IOException
210 {
211
212 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.FALSE);
213
214
215 UIViewRoot root = facesContext.getViewRoot();
216 vdl.buildView(facesContext, root, "testValidateBeanNesting.xhtml");
217
218
219 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
220 UIInput doublenestedinput = (UIInput) root.findComponent("form:doublenestedinput");
221 UIInput nestedinput2 = (UIInput) root.findComponent("form:nestedinput2");
222 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
223
224
225 Assert.assertTrue(_hasValidator(nestedinput, BeanValidator.class));
226 Assert.assertTrue(_hasValidator(doublenestedinput, BeanValidator.class));
227 Assert.assertTrue(_hasValidator(nestedinput2, BeanValidator.class));
228 Assert.assertFalse(_hasValidator(nonnestedinput, BeanValidator.class));
229 }
230
231
232
233
234
235
236
237
238
239 @Test
240 @SuppressWarnings("unchecked")
241 public void testNestingValidateBeanDisabled() throws IOException
242 {
243
244 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.TRUE);
245
246
247 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
248
249
250 UIViewRoot root = facesContext.getViewRoot();
251 vdl.buildView(facesContext, root, "testValidateBeanNesting.xhtml");
252
253
254 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
255 UIInput doublenestedinput = (UIInput) root.findComponent("form:doublenestedinput");
256 UIInput nestedinput2 = (UIInput) root.findComponent("form:nestedinput2");
257 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
258
259
260 Assert.assertFalse(_hasValidator(nestedinput, BeanValidator.class));
261 Assert.assertFalse(_hasValidator(doublenestedinput, BeanValidator.class));
262 Assert.assertFalse(_hasValidator(nestedinput2, BeanValidator.class));
263 Assert.assertTrue(_hasValidator(nonnestedinput, BeanValidator.class));
264 }
265
266
267
268
269
270
271
272
273
274 @Test
275 public void testValidateBeanDisabledAndEnabled() throws IOException
276 {
277
278 UIViewRoot root = facesContext.getViewRoot();
279 vdl.buildView(facesContext, root, "testValidateBeanDisabledAndEnabled.xhtml");
280
281
282 UIInput input = (UIInput) root.findComponent("form:input");
283
284
285 Assert.assertTrue(_hasValidator(input, BeanValidator.class));
286 }
287
288
289
290
291
292
293
294
295
296
297 @Test
298 @SuppressWarnings("unchecked")
299 public void testValidateBeanNestingAndNested() throws IOException
300 {
301
302 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
303
304
305 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.TRUE);
306
307
308 UIViewRoot root = facesContext.getViewRoot();
309 vdl.buildView(facesContext, root, "testValidateBeanNestingAndNested.xhtml");
310
311
312 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
313 UIInput nestedinputWithValidator = (UIInput) root.findComponent("form:nestedinputWithValidator");
314 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
315
316
317
318 Assert.assertFalse(_hasValidator(nestedinput, BeanValidator.class));
319 Assert.assertTrue(_hasValidator(nestedinputWithValidator, BeanValidator.class));
320 Assert.assertTrue(_hasValidator(nonnestedinput, BeanValidator.class));
321 }
322
323
324
325
326
327 @Test
328 @SuppressWarnings("unchecked")
329 public void testValidateBeanValidationGroups() throws IOException
330 {
331 final String validationGroups = "org.apache.myfaces.beanvalidation.Group1," +
332 "org.apache.myfaces.beanvalidation.Group2";
333
334
335 externalContext.getRequestMap().put("validationGroups", validationGroups);
336
337
338 UIViewRoot root = facesContext.getViewRoot();
339 vdl.buildView(facesContext, root, "testValidateBean.xhtml");
340
341
342 UIInput input = (UIInput) root.findComponent("form:input");
343
344
345 Assert.assertEquals(validationGroups, _getValidationGroups(input));
346 }
347
348
349
350
351
352
353
354 @Test
355 public void testValidateBeanDefaultValidationGroup() throws IOException
356 {
357
358 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
359
360
361 UIViewRoot root = facesContext.getViewRoot();
362 vdl.buildView(facesContext, root, "testValidateBeanEmptyInput.xhtml");
363
364
365 UIInput input = (UIInput) root.findComponent("form:input");
366
367
368 Assert.assertEquals(javax.validation.groups.Default.class.getName(),
369 _getValidationGroups(input));
370 }
371
372
373
374
375
376
377
378
379
380 @Test
381 @SuppressWarnings("unchecked")
382 public void testValidateBeanValidationGroupsNested() throws IOException
383 {
384 final String validationGroups = "org.apache.myfaces.beanvalidation.Group1";
385
386
387 externalContext.getRequestMap().put("validationGroups", validationGroups);
388
389
390 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
391
392
393 UIViewRoot root = facesContext.getViewRoot();
394 vdl.buildView(facesContext, root, "testValidateBeanNesting.xhtml");
395
396
397 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
398 UIInput doublenestedinput = (UIInput) root.findComponent("form:doublenestedinput");
399 UIInput nestedinput2 = (UIInput) root.findComponent("form:nestedinput2");
400 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
401
402
403
404
405 Assert.assertEquals(validationGroups, _getValidationGroups(nestedinput));
406 Assert.assertEquals(validationGroups, _getValidationGroups(doublenestedinput));
407 Assert.assertEquals(validationGroups, _getValidationGroups(nestedinput2));
408 Assert.assertEquals(javax.validation.groups.Default.class.getName(),
409 _getValidationGroups(nonnestedinput));
410 }
411
412
413
414
415
416
417
418
419
420 @Test
421 @SuppressWarnings("unchecked")
422 public void testValidateBeanValidationGroupsNestingAndNested() throws IOException
423 {
424 final String wrappingValidationGroups = "org.apache.myfaces.beanvalidation.Group1";
425 final String componentValidationGroups = "org.apache.myfaces.beanvalidation.ACompletelyOtherGroup";
426
427
428 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
429
430
431 externalContext.getRequestMap().put("validateBeanDisabled", Boolean.FALSE);
432
433
434 externalContext.getRequestMap().put("validationGroups", wrappingValidationGroups);
435 externalContext.getRequestMap().put("validationGroupsComponent", componentValidationGroups);
436
437
438 UIViewRoot root = facesContext.getViewRoot();
439 vdl.buildView(facesContext, root, "testValidateBeanNestingAndNested.xhtml");
440
441
442 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
443 UIInput nestedinputWithValidator = (UIInput) root.findComponent("form:nestedinputWithValidator");
444 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
445
446
447
448
449 Assert.assertEquals(wrappingValidationGroups, _getValidationGroups(nestedinput));
450 Assert.assertEquals(componentValidationGroups, _getValidationGroups(nestedinputWithValidator));
451 Assert.assertEquals(javax.validation.groups.Default.class.getName(),
452 _getValidationGroups(nonnestedinput));
453 }
454
455
456
457
458
459
460
461
462
463 @Test
464 @SuppressWarnings("unchecked")
465 public void testValidateBeanValidationGroupsDoubleNesting() throws IOException
466 {
467 final String validationGroupsOuter = "org.apache.myfaces.beanvalidation.OuterGroup";
468 final String validationGroupsInner = "org.apache.myfaces.beanvalidation.InnerGroup";
469
470
471 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
472
473
474 externalContext.getRequestMap().put("validationGroupsOuter", validationGroupsOuter);
475 externalContext.getRequestMap().put("validationGroupsInner", validationGroupsInner);
476
477
478 UIViewRoot root = facesContext.getViewRoot();
479 vdl.buildView(facesContext, root, "testValidateBeanDoubleNesting.xhtml");
480
481
482 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
483 UIInput doublenestedinput = (UIInput) root.findComponent("form:doublenestedinput");
484 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
485
486
487
488
489 Assert.assertEquals(validationGroupsOuter, _getValidationGroups(nestedinput));
490 Assert.assertEquals(validationGroupsInner, _getValidationGroups(doublenestedinput));
491 Assert.assertEquals(javax.validation.groups.Default.class.getName(),
492 _getValidationGroups(nonnestedinput));
493 }
494
495
496
497
498
499
500
501
502 @Test
503 public void testValidateBeanWithBeanValidationNotAvailable() throws IOException
504 {
505
506 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
507
508
509 _setBeanValidationAvailable(false);
510
511
512 UIViewRoot root = facesContext.getViewRoot();
513 vdl.buildView(facesContext, root, "testValidateBeanEmptyInput.xhtml");
514
515
516 UIInput input = (UIInput) root.findComponent("form:input");
517
518
519
520 Assert.assertFalse(_hasValidator(input, BeanValidator.class));
521 }
522
523
524
525
526
527 @Test
528 @SuppressWarnings("unchecked")
529 public void testValidateBeanValidationDisabledDoubleNesting() throws IOException
530 {
531 final String validationGroupsOuter = "org.apache.myfaces.beanvalidation.OuterGroup";
532 final String validationGroupsInner = "org.apache.myfaces.beanvalidation.InnerGroup";
533
534
535 application.addDefaultValidatorId(BeanValidator.VALIDATOR_ID);
536
537
538 externalContext.getRequestMap().put("validationGroupsOuter", validationGroupsOuter);
539 externalContext.getRequestMap().put("validationGroupsInner", validationGroupsInner);
540
541
542 UIViewRoot root = facesContext.getViewRoot();
543 vdl.buildView(facesContext, root, "testValidateBeanDisableDoubleNesting.xhtml");
544
545
546 UIInput nestedinput = (UIInput) root.findComponent("form:nestedinput");
547 UIInput doublenestedinput = (UIInput) root.findComponent("form:doublenestedinput");
548 UIInput nonnestedinput = (UIInput) root.findComponent("form:nonnestedinput");
549 UIInput nesteouterdisabledinput = (UIInput) root.findComponent("form:nesteouterdisabledinput");
550
551
552
553
554 Assert.assertEquals(validationGroupsOuter, _getValidationGroups(nestedinput));
555 Assert.assertFalse(_hasValidator(doublenestedinput, BeanValidator.class));
556 Assert.assertEquals(validationGroupsInner, _getValidationGroups(nesteouterdisabledinput));
557 Assert.assertEquals(javax.validation.groups.Default.class.getName(),
558 _getValidationGroups(nonnestedinput));
559 }
560
561 }