2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator;
19
20 import java.io.StringWriter;
21 import java.util.Iterator;
22 import java.util.Locale;
23 import java.util.StringTokenizer;
24
25 import javax.faces.application.FacesMessage;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIInput;
28 import javax.faces.component.UINamingContainer;
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.component.html.HtmlForm;
31 import javax.faces.component.html.HtmlInputText;
32 import javax.faces.context.ResponseWriter;
33 import javax.servlet.ServletContextEvent;
34
35 import junit.framework.Assert;
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38
39 import org.apache.commons.validator.ValidatorAction;
40 import org.apache.shale.validator.faces.ValidatorLifecycleListener;
41 import org.apache.shale.validator.faces.ValidatorScript;
42 import org.apache.shale.test.base.AbstractJsfTestCase;
43 import org.apache.shale.util.Messages;
44
45
46 /***
47 * <p>Test case for <code>CommonsValidator</code>.</p>
48 */
49 public class CommonsValidatorTestCase extends AbstractJsfTestCase {
50
51 /***
52 * <p>Validator message resources for this class.</p>
53 */
54 private static Messages messages =
55 new Messages("org.apache.shale.validator.messages",
56 CommonsValidatorTestCase.class.getClassLoader());
57
58 protected static final Object[] COMPONENTS = {
59 new String[] {"javax.faces.NamingContainer", "javax.faces.component.UINamingContainer"},
60 new String[] {"javax.faces.HtmlInputText", "javax.faces.component.html.HtmlInputText"},
61 new String[] {"org.apache.shale.ValidatorScript", "org.apache.shale.validator.faces.ValidatorScript"},
62 new String[] {"javax.faces.HtmlForm", "javax.faces.component.html.HtmlForm"},
63
64 };
65
66
67 protected void loadComponents() {
68 for (int i = 0; i < COMPONENTS.length; i++) {
69 application.addComponent(((String[])COMPONENTS[i])[0], ((String[])COMPONENTS[i])[1]);
70 }
71 }
72
73
74 protected static final Object[] CONVERTERS = {
75 new String[] {"javax.faces.DateTime", "javax.faces.convert.DateTimeConverter"}
76 };
77
78 protected void loadConverters() {
79 for (int i = 0; i < CONVERTERS.length; i++) {
80 application.addConverter(((String[])CONVERTERS[i])[0], ((String[])CONVERTERS[i])[1]);
81 }
82 }
83
84 public static final Object[] VALIDATORS = {
85 new String[] {"org.apache.shale.CommonsValidator", "org.apache.shale.validator.CommonsValidator"}
86 };
87
88 protected void loadValidators() {
89 for (int i = 0; i < VALIDATORS.length; i++) {
90 application.addValidator(((String[])VALIDATORS[i])[0], ((String[])VALIDATORS[i])[1]);
91 }
92 }
93
94
95
96
97
98
99 public CommonsValidatorTestCase(String name) {
100 super(name);
101 }
102
103
104
105
106
107
108 ValidatorLifecycleListener listener = null;
109
110
111
112
113
114
115 protected void setUp() throws Exception {
116
117 super.setUp();
118 servletContext.addInitParameter
119 (Globals.VALIDATOR_RULES,
120 Globals.DEFAULT_VALIDATOR_RULES +
121 ", /org/apache/shale/validator/custom-rules.xml");
122 listener = new ValidatorLifecycleListener();
123 listener.contextInitialized(new ServletContextEvent(servletContext));
124
125 loadComponents();
126 loadConverters();
127 loadValidators();
128
129 facesContext.getViewRoot().setLocale(new Locale("de", "DE"));
130 }
131
132
133
134 public static Test suite() {
135
136 return (new TestSuite(CommonsValidatorTestCase.class));
137
138 }
139
140
141
142 protected void tearDown() throws Exception {
143
144 listener.contextDestroyed(new ServletContextEvent(servletContext));
145 listener = null;
146
147 super.tearDown();
148
149 }
150
151
152
153
154
155
156
157
158
159 public void testRequired() {
160
161 ValidatorAction va = CommonsValidator.getValidatorAction("required");
162 assertNotNull(va);
163 assertEquals("required", va.getName());
164 assertEquals("org.apache.shale.validator.CommonsValidator",
165 va.getClassname());
166 assertEquals("isSupplied",va.getMethod());
167
168
169 }
170
171
172
173 public static boolean isValid(String value) {
174 if (value != null && value.equals("blue") ||
175 value.equals("red"))
176 return true;
177 else
178 return false;
179 }
180
181
182 public static boolean isValid(String value, String colors) {
183 StringTokenizer tokenizer = new StringTokenizer(colors, ",");
184
185 while (tokenizer.hasMoreTokens()) {
186 String color = tokenizer.nextToken().trim();
187 if (value.equals(color))
188 return true;
189 }
190
191 return false;
192 }
193
194
195
196
197 public void testCustom1() {
198
199 application.setMessageBundle("org.apache.shale.validator.messages");
200
201
202 ValidatorAction va = CommonsValidator.getValidatorAction("testRule1");
203 assertNotNull(va);
204 assertEquals("testRule1", va.getName());
205 assertEquals("org.apache.shale.validator.CommonsValidatorTestCase",
206 va.getClassname());
207 assertEquals("isValid", va.getMethod());
208
209
210
211 UIViewRoot root = facesContext.getViewRoot();
212 assertNotNull(root);
213
214
215 UIComponent form1 = this.createForm("test1", root);
216
217
218 HtmlInputText component1 = createInputText("testRule1", form1);
219 component1.setRequired(true);
220
221
222 CommonsValidator validator1 = createValidator(component1, "testRule1");
223
224
225 component1.setSubmittedValue("blue");
226
227
228 component1.validate(facesContext);
229
230
231 checkMessages(component1);
232
233 }
234
235
236
237 public void testCustom2() {
238
239 ValidatorAction va = CommonsValidator.getValidatorAction("testRule2");
240 assertNotNull(va);
241 assertEquals("testRule2", va.getName());
242 assertEquals("org.apache.shale.validator.CommonsValidatorTestCase",
243 va.getClassname());
244 assertEquals("isValid", va.getMethod());
245
246
247
248 UIViewRoot root = facesContext.getViewRoot();
249 assertNotNull(root);
250
251
252 UIComponent form1 = this.createForm("test1", root);
253
254
255 HtmlInputText component1 = createInputText("testRule2", form1);
256 component1.setRequired(true);
257
258
259 CommonsValidator validator1 = createValidator(component1, "testRule2");
260
261
262
263
264 validator1.getVars().put("enumerations", "black, yellow, red");
265 validator1.setMessage("{0} must be one of the following: {2}.");
266 validator1.setArg("Favorite Color");
267
268
269 component1.setSubmittedValue("blue");
270
271
272 component1.validate(facesContext);
273
274
275 checkMessage("Favorite Color must be one of the following: black, yellow, red.", component1);
276
277 }
278
279
280
281 public void testValidateRequired() throws Exception {
282
283
284 UIViewRoot root = facesContext.getViewRoot();
285 assertNotNull(root);
286
287
288 UIComponent form1 = this.createForm("test1", root);
289
290
291 HtmlInputText component1 = createInputText("requiredField", form1);
292 component1.setRequired(true);
293
294
295 CommonsValidator validator1 = createValidator(component1, "required");
296
297
298
299 UIComponent form2 = this.createForm("test2", root);
300
301
302 HtmlInputText component2 = createInputText("requiredField", form2);
303 component2.setRequired(true);
304
305
306 CommonsValidator validator2 = createValidator(component2, "required");
307
308
309
310 ValidatorScript script = createValidatorScript(root);
311
312
313 component1.setSubmittedValue(" ");
314
315 component1.validate(facesContext);
316
317
318 ValidatorAction va = CommonsValidator.getValidatorAction("required");
319 assertNotNull("validator action", va);
320 String actualMsg = messages.getMessage(va.getMsg(),
321 facesContext.getViewRoot().getLocale(),
322 new Object[] {component1.getId()});
323
324
325 checkMessage(actualMsg, component1);
326
327
328 StringBuffer htmlSnippet = encode(script);
329
330
331 String[] searchTokens = {
332 "test1_required()",
333 "test2_required()",
334 "validateRequired(form)",
335 "function validateRequired(form)",
336 "function jcv_retrieveFormName(form)"
337 };
338
339
340 checkScript(htmlSnippet, searchTokens);
341
342
343 }
344
345
346
347 public void testValidateMaxlength() throws Exception {
348
349
350 UIViewRoot root = facesContext.getViewRoot();
351 assertNotNull(root);
352
353
354 UIComponent form1 = this.createForm("test1", root);
355
356
357 HtmlInputText component1 = createInputText("maxlength", form1);
358
359
360 CommonsValidator validator1 = createValidator(component1, "maxlength");
361 validator1.setMaxLength(new Integer(5).toString());
362
363
364
365 UIComponent form2 = this.createForm("test2", root);
366
367
368 HtmlInputText component2 = createInputText("maxlength", form2);
369
370
371 CommonsValidator validator2 = createValidator(component2, "maxlength");
372 validator2.setMaxLength(new Integer(5).toString());
373
374
375 ValidatorScript script = createValidatorScript(root);
376
377
378 component1.setSubmittedValue("0123456789");
379
380 component1.validate(facesContext);
381
382 ValidatorAction va = CommonsValidator.getValidatorAction("maxlength");
383 assertNotNull("validator action", va);
384 String actualMsg = messages.getMessage(va.getMsg(),
385 facesContext.getViewRoot().getLocale(),
386 new Object[] {component1.getId(),
387 validator1.getVars().get(CommonsValidator.MAX_LENGTH_VARNAME)});
388
389
390 checkMessage(actualMsg, component1);
391
392
393 StringBuffer htmlSnippet = encode(script);
394
395
396 String[] searchTokens = {
397 "test1_maxlength()",
398 "test2_maxlength()",
399 "validateMaxLength(form)",
400 "function validateMaxLength(form)",
401 "function jcv_retrieveFormName(form)"
402 };
403
404
405 checkScript(htmlSnippet, searchTokens);
406
407
408 }
409
410
411
412 public void testValidateMinlength() throws Exception {
413
414
415 UIViewRoot root = facesContext.getViewRoot();
416 assertNotNull(root);
417
418
419 UIComponent form1 = this.createForm("test1", root);
420
421
422 HtmlInputText component1 = createInputText("minlength", form1);
423
424
425 CommonsValidator validator1 = createValidator(component1, "minlength");
426 validator1.setMinLength(new Integer(5).toString());
427
428
429
430 UIComponent form2 = this.createForm("test2", root);
431
432
433 HtmlInputText component2 = createInputText("minlength", form2);
434
435
436 CommonsValidator validator2 = createValidator(component2, "minlength");
437 validator2.setMinLength(new Integer(5).toString());
438
439
440 ValidatorScript script = createValidatorScript(root);
441
442
443 component1.setSubmittedValue("0");
444
445 component1.validate(facesContext);
446
447 ValidatorAction va = CommonsValidator.getValidatorAction("minlength");
448 assertNotNull("validator action", va);
449 String actualMsg = messages.getMessage(va.getMsg(),
450 facesContext.getViewRoot().getLocale(),
451 new Object[] {component1.getId(),
452 validator1.getVars().get(CommonsValidator.MIN_LENGTH_VARNAME)});
453
454
455 checkMessage(actualMsg, component1);
456
457
458 StringBuffer htmlSnippet = encode(script);
459
460
461 String[] searchTokens = {
462 "test1_minlength()",
463 "test2_minlength()",
464 "validateMinLength(form)",
465 "function validateMinLength(form)",
466 "function jcv_retrieveFormName(form)"
467 };
468
469
470 checkScript(htmlSnippet, searchTokens);
471
472
473 }
474
475
476
477 public void testValidateMask() throws Exception {
478
479
480 UIViewRoot root = facesContext.getViewRoot();
481 assertNotNull(root);
482
483
484 UIComponent form1 = this.createForm("test1", root);
485
486
487 HtmlInputText component1 = createInputText("mask", form1);
488
489
490 CommonsValidator validator1 = createValidator(component1, "mask");
491 validator1.setMask("^[a-zA-Z]*$");
492
493
494
495 UIComponent form2 = this.createForm("test2", root);
496
497
498 HtmlInputText component2 = createInputText("mask", form2);
499
500
501 CommonsValidator validator2 = createValidator(component2, "mask");
502 validator2.setMask("^[a-zA-Z]*$");
503
504
505 ValidatorScript script = createValidatorScript(root);
506
507
508 component1.setSubmittedValue("123Test");
509
510 component1.validate(facesContext);
511
512 ValidatorAction va = CommonsValidator.getValidatorAction("mask");
513 assertNotNull("validator action", va);
514 String actualMsg = messages.getMessage(va.getMsg(),
515 facesContext.getViewRoot().getLocale(),
516 new Object[] {component1.getId()});
517
518
519 checkMessage(actualMsg, component1);
520
521
522 StringBuffer htmlSnippet = encode(script);
523
524
525 String[] searchTokens = {
526 "test1_mask()",
527 "test2_mask()",
528 "validateMask(form)",
529 "function validateMask(form)",
530 "function jcv_retrieveFormName(form)"
531 };
532
533
534 checkScript(htmlSnippet, searchTokens);
535
536
537 }
538
539
540
541 public void testValidateByte() throws Exception {
542
543
544 UIViewRoot root = facesContext.getViewRoot();
545 assertNotNull(root);
546
547
548 UIComponent form1 = this.createForm("test1", root);
549
550
551 HtmlInputText component1 = createInputText("byte", form1);
552
553
554 CommonsValidator validator1 = createValidator(component1, "byte");
555
556
557
558 UIComponent form2 = this.createForm("test2", root);
559
560
561 HtmlInputText component2 = createInputText("byte", form2);
562
563
564 CommonsValidator validator2 = createValidator(component2, "byte");
565
566
567 ValidatorScript script = createValidatorScript(root);
568
569
570 component1.setSubmittedValue("129");
571
572 component1.validate(facesContext);
573
574 ValidatorAction va = CommonsValidator.getValidatorAction("byte");
575 assertNotNull("validator action", va);
576 String actualMsg = messages.getMessage(va.getMsg(),
577 facesContext.getViewRoot().getLocale(),
578 new Object[] {component1.getId()});
579
580
581 checkMessage(actualMsg, component1);
582
583
584 StringBuffer htmlSnippet = encode(script);
585
586
587 String[] searchTokens = {
588 "test1_ByteValidations()",
589 "test2_ByteValidations()",
590 "validateByte(form)",
591 "function validateByte(form)",
592 "function jcv_retrieveFormName(form)"
593 };
594
595
596 checkScript(htmlSnippet, searchTokens);
597
598
599 }
600
601
602
603 public void testValidateShort() throws Exception {
604
605
606 UIViewRoot root = facesContext.getViewRoot();
607 assertNotNull(root);
608
609
610 UIComponent form1 = this.createForm("test1", root);
611
612
613 HtmlInputText component1 = createInputText("short", form1);
614
615
616 CommonsValidator validator1 = createValidator(component1, "short");
617
618
619
620 UIComponent form2 = this.createForm("test2", root);
621
622
623 HtmlInputText component2 = createInputText("short", form2);
624
625
626 CommonsValidator validator2 = createValidator(component2, "short");
627
628
629 ValidatorScript script = createValidatorScript(root);
630
631
632 component1.setSubmittedValue("32768");
633
634 component1.validate(facesContext);
635
636 ValidatorAction va = CommonsValidator.getValidatorAction("short");
637 assertNotNull("validator action", va);
638 String actualMsg = messages.getMessage(va.getMsg(),
639 facesContext.getViewRoot().getLocale(),
640 new Object[] {component1.getId()});
641
642
643 checkMessage(actualMsg, component1);
644
645
646 StringBuffer htmlSnippet = encode(script);
647
648
649 String[] searchTokens = {
650 "test1_ShortValidations()",
651 "test2_ShortValidations()",
652 "validateShort(form)",
653 "function validateShort(form)",
654 "function jcv_retrieveFormName(form)"
655 };
656
657
658 checkScript(htmlSnippet, searchTokens);
659
660
661 }
662
663
664 public void testValidateInteger() throws Exception {
665
666
667 UIViewRoot root = facesContext.getViewRoot();
668 assertNotNull(root);
669
670
671 UIComponent form1 = this.createForm("test1", root);
672
673
674 HtmlInputText component1 = createInputText("integer", form1);
675
676
677 CommonsValidator validator1 = createValidator(component1, "integer");
678
679
680
681 UIComponent form2 = this.createForm("test2", root);
682
683
684 HtmlInputText component2 = createInputText("integer", form2);
685
686
687 CommonsValidator validator2 = createValidator(component2, "integer");
688
689
690 ValidatorScript script = createValidatorScript(root);
691
692
693 component1.setSubmittedValue("2147483648");
694
695 component1.validate(facesContext);
696
697 ValidatorAction va = CommonsValidator.getValidatorAction("integer");
698 assertNotNull("validator action", va);
699 String actualMsg = messages.getMessage(va.getMsg(),
700 facesContext.getViewRoot().getLocale(),
701 new Object[] {component1.getId()});
702
703
704 checkMessage(actualMsg, component1);
705
706
707 StringBuffer htmlSnippet = encode(script);
708
709
710 String[] searchTokens = {
711 "test1_IntegerValidations()",
712 "test2_IntegerValidations()",
713 "validateInteger(form)",
714 "function validateInteger(form)",
715 "function jcv_retrieveFormName(form)"
716 };
717
718
719 checkScript(htmlSnippet, searchTokens);
720
721
722 }
723
724
725
726 public void testValidateFloat() throws Exception {
727
728
729 UIViewRoot root = facesContext.getViewRoot();
730 assertNotNull(root);
731
732
733 UIComponent form1 = this.createForm("test1", root);
734
735
736 HtmlInputText component1 = createInputText("float", form1);
737
738
739 CommonsValidator validator1 = createValidator(component1, "float");
740
741
742
743 UIComponent form2 = this.createForm("test2", root);
744
745
746 HtmlInputText component2 = createInputText("float", form2);
747
748
749 CommonsValidator validator2 = createValidator(component2, "float");
750
751
752 ValidatorScript script = createValidatorScript(root);
753
754
755 component1.setSubmittedValue("ABCD");
756
757 component1.validate(facesContext);
758
759 ValidatorAction va = CommonsValidator.getValidatorAction("float");
760 assertNotNull("validator action", va);
761 String actualMsg = messages.getMessage(va.getMsg(),
762 facesContext.getViewRoot().getLocale(),
763 new Object[] {component1.getId()});
764
765
766 checkMessage(actualMsg, component1);
767
768
769 StringBuffer htmlSnippet = encode(script);
770
771
772 String[] searchTokens = {
773 "test1_FloatValidations()",
774 "test2_FloatValidations()",
775 "validateFloat(form)",
776 "function validateFloat(form)",
777 "function jcv_retrieveFormName(form)"
778 };
779
780
781 checkScript(htmlSnippet, searchTokens);
782
783
784 }
785
786
787
788 public void testValidateDouble() throws Exception {
789
790
791 UIViewRoot root = facesContext.getViewRoot();
792 assertNotNull(root);
793
794
795 UIComponent form1 = this.createForm("test1", root);
796
797
798 HtmlInputText component1 = createInputText("double", form1);
799
800
801 CommonsValidator validator1 = createValidator(component1, "double");
802 validator1.setClient(Boolean.FALSE);
803
804
805
806 UIComponent form2 = this.createForm("test2", root);
807
808
809 HtmlInputText component2 = createInputText("double", form2);
810
811
812 CommonsValidator validator2 = createValidator(component2, "double");
813 validator2.setClient(Boolean.FALSE);
814
815
816
817 component1.setSubmittedValue("ABCD");
818
819 component1.validate(facesContext);
820
821 ValidatorAction va = CommonsValidator.getValidatorAction("double");
822 assertNotNull("validator action", va);
823 String actualMsg = messages.getMessage(va.getMsg(),
824 facesContext.getViewRoot().getLocale(),
825 new Object[] {component1.getId()});
826
827
828 checkMessage(actualMsg, component1);
829
830 }
831
832
833 public void testValidateLong() throws Exception {
834
835
836 UIViewRoot root = facesContext.getViewRoot();
837 assertNotNull(root);
838
839
840 UIComponent form1 = this.createForm("test1", root);
841
842
843 HtmlInputText component1 = createInputText("long", form1);
844
845
846 CommonsValidator validator1 = createValidator(component1, "long");
847 validator1.setClient(Boolean.FALSE);
848
849
850
851 UIComponent form2 = this.createForm("test2", root);
852
853
854 HtmlInputText component2 = createInputText("long", form2);
855
856
857 CommonsValidator validator2 = createValidator(component2, "long");
858 validator2.setClient(Boolean.FALSE);
859
860
861
862 component1.setSubmittedValue("99999999999999999999999999999999999999999999999999999999");
863
864 component1.validate(facesContext);
865
866 ValidatorAction va = CommonsValidator.getValidatorAction("long");
867 assertNotNull("validator action", va);
868 String actualMsg = messages.getMessage(va.getMsg(),
869 facesContext.getViewRoot().getLocale(),
870 new Object[] {component1.getId()});
871
872
873 checkMessage(actualMsg, component1);
874
875 }
876
877
878
879
880
881 public void testValidateDate() throws Exception {
882
883
884 UIViewRoot root = facesContext.getViewRoot();
885 assertNotNull(root);
886
887
888 UIComponent form1 = this.createForm("test1", root);
889
890
891 HtmlInputText component1 = createInputText("date", form1);
892
893
894 CommonsValidator validator1 = createValidator(component1, "date");
895 validator1.setDatePatternStrict("MM/dd/yyyy");
896
897
898
899 UIComponent form2 = this.createForm("test2", root);
900
901
902 HtmlInputText component2 = createInputText("date", form2);
903
904
905 CommonsValidator validator2 = createValidator(component2, "date");
906 validator2.setDatePatternStrict("MM/dd/yyyy");
907
908
909 ValidatorScript script = createValidatorScript(root);
910
911
912 component1.setSubmittedValue("04/31/2006");
913
914 component1.validate(facesContext);
915
916 ValidatorAction va = CommonsValidator.getValidatorAction("date");
917 assertNotNull("validator action", va);
918 String actualMsg = messages.getMessage(va.getMsg(),
919 facesContext.getViewRoot().getLocale(),
920 new Object[] {component1.getId()});
921
922
923 checkMessage(actualMsg, component1);
924
925
926 StringBuffer htmlSnippet = encode(script);
927
928
929 String[] searchTokens = {
930 "test1_DateValidations()",
931 "test2_DateValidations()",
932 "validateDate(form)",
933 "function validateDate(form)",
934 "function jcv_retrieveFormName(form)"
935 };
936
937
938 checkScript(htmlSnippet, searchTokens);
939
940
941 }
942
943
944 public void testValidateIntRange() throws Exception {
945
946
947 UIViewRoot root = facesContext.getViewRoot();
948 assertNotNull(root);
949
950
951 UIComponent form1 = this.createForm("test1", root);
952
953
954 HtmlInputText component1 = createInputText("intRange", form1);
955
956
957 CommonsValidator validator1 = createValidator(component1, "intRange");
958 validator1.setMax(new Integer(5).toString());
959 validator1.setMin(new Integer(1).toString());
960
961
962
963 UIComponent form2 = this.createForm("test2", root);
964
965
966 HtmlInputText component2 = createInputText("intRange", form2);
967
968
969 CommonsValidator validator2 = createValidator(component2, "intRange");
970 validator2.setMax(new Double(5).toString());
971 validator2.setMin(new Double(1).toString());
972
973
974 ValidatorScript script = createValidatorScript(root);
975
976
977 component1.setSubmittedValue("6");
978
979 component1.validate(facesContext);
980
981
982 String min = (String) validator1.getVars().get(CommonsValidator.MIN_VARNAME);
983 String max = (String) validator1.getVars().get(CommonsValidator.MAX_VARNAME);
984
985 ValidatorAction va = CommonsValidator.getValidatorAction("intRange");
986 assertNotNull("validator action", va);
987 String actualMsg = messages.getMessage(va.getMsg(),
988 facesContext.getViewRoot().getLocale(),
989 new Object[] {component1.getId(), min, max});
990
991
992 checkMessage(actualMsg, component1);
993
994
995 StringBuffer htmlSnippet = encode(script);
996
997
998 String[] searchTokens = {
999 "test1_intRange()",
1000 "test2_intRange()",
1001 "validateIntRange(form)",
1002 "function validateIntRange(form)",
1003 "function jcv_retrieveFormName(form)",
1004 "test1_IntegerValidations()",
1005 "test2_IntegerValidations()"
1006
1007 };
1008
1009
1010 checkScript(htmlSnippet, searchTokens);
1011
1012
1013 }
1014
1015
1016
1017 public void testValidateFloatRange() throws Exception {
1018
1019
1020 UIViewRoot root = facesContext.getViewRoot();
1021 assertNotNull(root);
1022
1023
1024 UIComponent form1 = this.createForm("test1", root);
1025
1026
1027 HtmlInputText component1 = createInputText("floatRange", form1);
1028
1029
1030 CommonsValidator validator1 = createValidator(component1, "floatRange");
1031 validator1.setMax(new Double(5).toString());
1032 validator1.setMin(new Double(1).toString());
1033
1034
1035
1036 UIComponent form2 = this.createForm("test2", root);
1037
1038
1039 HtmlInputText component2 = createInputText("floatRange", form2);
1040
1041
1042 CommonsValidator validator2 = createValidator(component2, "floatRange");
1043 validator2.setMax(new Double(5).toString());
1044 validator2.setMin(new Double(1).toString());
1045
1046
1047 ValidatorScript script = createValidatorScript(root);
1048
1049
1050 component1.setSubmittedValue("6");
1051
1052 component1.validate(facesContext);
1053
1054
1055 String min = (String) validator1.getVars().get(CommonsValidator.MIN_VARNAME);
1056 String max = (String) validator1.getVars().get(CommonsValidator.MAX_VARNAME);
1057 ValidatorAction va = CommonsValidator.getValidatorAction("floatRange");
1058 assertNotNull("validator action", va);
1059 String actualMsg = messages.getMessage(va.getMsg(),
1060 facesContext.getViewRoot().getLocale(),
1061 new Object[] {component1.getId(), min, max});
1062
1063
1064 checkMessage(actualMsg, component1);
1065
1066
1067 StringBuffer htmlSnippet = encode(script);
1068
1069
1070 String[] searchTokens = {
1071 "test1_floatRange()",
1072 "test2_floatRange()",
1073 "validateFloatRange(form)",
1074 "function validateFloatRange(form)",
1075 "function jcv_retrieveFormName(form)",
1076 "test1_FloatValidations()",
1077 "test2_FloatValidations()"
1078 };
1079
1080
1081 checkScript(htmlSnippet, searchTokens);
1082
1083
1084 }
1085
1086
1087 public void testValidateDoubleRange() throws Exception {
1088
1089
1090 UIViewRoot root = facesContext.getViewRoot();
1091 assertNotNull(root);
1092
1093
1094 UIComponent form1 = this.createForm("test1", root);
1095
1096
1097 HtmlInputText component1 = createInputText("doubleRange", form1);
1098
1099
1100 CommonsValidator validator1 = createValidator(component1, "doubleRange");
1101 validator1.setMax(new Double(5).toString());
1102 validator1.setMin(new Double(1).toString());
1103
1104
1105
1106 UIComponent form2 = this.createForm("test2", root);
1107
1108
1109 HtmlInputText component2 = createInputText("doubleRange", form2);
1110
1111
1112 CommonsValidator validator2 = createValidator(component2, "doubleRange");
1113 validator2.setMax(new Double(5).toString());
1114 validator2.setMin(new Double(1).toString());
1115
1116
1117 component1.setSubmittedValue("6");
1118
1119 component1.validate(facesContext);
1120
1121
1122
1123 String min = (String) validator1.getVars().get(CommonsValidator.MIN_VARNAME);
1124 String max = (String) validator1.getVars().get(CommonsValidator.MAX_VARNAME);
1125
1126 ValidatorAction va = CommonsValidator.getValidatorAction("doubleRange");
1127 assertNotNull("validator action", va);
1128 String actualMsg = messages.getMessage(va.getMsg(),
1129 facesContext.getViewRoot().getLocale(),
1130 new Object[] {component1.getId(), min, max});
1131
1132
1133 checkMessage(actualMsg, component1);
1134 }
1135
1136
1137
1138
1139 public void testValidateCreditCard() throws Exception {
1140
1141
1142 UIViewRoot root = facesContext.getViewRoot();
1143 assertNotNull(root);
1144
1145
1146 UIComponent form1 = this.createForm("test1", root);
1147
1148
1149 HtmlInputText component1 = createInputText("creditCard", form1);
1150
1151
1152 CommonsValidator validator1 = createValidator(component1, "creditCard");
1153
1154
1155 UIComponent form2 = this.createForm("test2", root);
1156
1157
1158 HtmlInputText component2 = createInputText("creditCard", form2);
1159
1160
1161 CommonsValidator validator2 = createValidator(component2, "creditCard");
1162
1163
1164 ValidatorScript script = createValidatorScript(root);
1165
1166
1167 component1.setSubmittedValue("11111111111111111111");
1168
1169 component1.validate(facesContext);
1170
1171 ValidatorAction va = CommonsValidator.getValidatorAction("creditCard");
1172 assertNotNull("validator action", va);
1173 String actualMsg = messages.getMessage(va.getMsg(),
1174 facesContext.getViewRoot().getLocale(),
1175 new Object[] {component1.getId()});
1176
1177
1178 checkMessage(actualMsg, component1);
1179
1180
1181 StringBuffer htmlSnippet = encode(script);
1182
1183
1184 String[] searchTokens = {
1185 "test1_creditCard()",
1186 "test2_creditCard()",
1187 "validateCreditCard(form)",
1188 "function validateCreditCard(form)",
1189 "function jcv_retrieveFormName(form)"
1190 };
1191
1192
1193 checkScript(htmlSnippet, searchTokens);
1194
1195
1196 }
1197
1198
1199
1200 public void testValidateEmail() throws Exception {
1201
1202
1203 UIViewRoot root = facesContext.getViewRoot();
1204 assertNotNull(root);
1205
1206 UINamingContainer namingContainer = (UINamingContainer) application.createComponent("javax.faces.NamingContainer");
1207
1208 namingContainer.setId(root.createUniqueId());
1209 root.getChildren().add(root.getChildCount(), namingContainer);
1210
1211
1212 UIComponent form1 = this.createForm("test1", namingContainer);
1213
1214
1215
1216 HtmlInputText component1 = createInputText("email", form1);
1217
1218
1219 CommonsValidator validator1 = createValidator(component1, "email");
1220
1221
1222 UIComponent form2 = this.createForm("test2", root);
1223
1224
1225 HtmlInputText component2 = createInputText("email", form2);
1226
1227
1228 CommonsValidator validator2 = createValidator(component2, "email");
1229
1230
1231 ValidatorScript script = createValidatorScript(root);
1232
1233
1234 component1.setSubmittedValue("xyz@abc");
1235
1236 component1.validate(facesContext);
1237
1238 ValidatorAction va = CommonsValidator.getValidatorAction("email");
1239 assertNotNull("validator action", va);
1240 String actualMsg = messages.getMessage(va.getMsg(),
1241 facesContext.getViewRoot().getLocale(),
1242 new Object[] {component1.getId()});
1243
1244
1245 checkMessage(actualMsg, component1);
1246
1247
1248 StringBuffer htmlSnippet = encode(script);
1249
1250
1251 String[] searchTokens = {
1252 "test1_email()",
1253 "test2_email()",
1254 "validateEmail(form)",
1255 "function validateEmail(form)",
1256 "function jcv_retrieveFormName(form)"
1257 };
1258
1259
1260 checkScript(htmlSnippet, searchTokens);
1261
1262
1263 }
1264
1265
1266
1267 public void testValidateUrl() throws Exception {
1268
1269
1270 UIViewRoot root = facesContext.getViewRoot();
1271 assertNotNull(root);
1272
1273
1274 UIComponent form1 = this.createForm("test1", root);
1275
1276
1277 HtmlInputText component1 = createInputText("url", form1);
1278
1279
1280 CommonsValidator validator1 = createValidator(component1, "url");
1281 validator1.setClient(Boolean.FALSE);
1282
1283
1284 UIComponent form2 = this.createForm("test2", root);
1285
1286
1287 HtmlInputText component2 = createInputText("url", form2);
1288
1289
1290 CommonsValidator validator2 = createValidator(component2, "url");
1291 validator2.setClient(Boolean.FALSE);
1292
1293
1294 component1.setSubmittedValue("xyz://www.wyantdata.com");
1295
1296 component1.validate(facesContext);
1297
1298 ValidatorAction va = CommonsValidator.getValidatorAction("url");
1299 assertNotNull("validator action", va);
1300 String actualMsg = messages.getMessage(va.getMsg(),
1301 facesContext.getViewRoot().getLocale(),
1302 new Object[] {component1.getId()});
1303
1304
1305 checkMessage(actualMsg, component1);
1306
1307 }
1308
1309
1310 private void checkScript(StringBuffer htmlSnippet, String[] searchTokens) {
1311 for (int i = 0; i < searchTokens.length; i++) {
1312 Assert.assertTrue("Not Found: " + searchTokens[i], (htmlSnippet.indexOf(searchTokens[i]) > -1));
1313 }
1314 }
1315
1316 private HtmlForm createForm(String id, UIComponent root) {
1317 HtmlForm component = (HtmlForm) facesContext.getApplication().createComponent("javax.faces.HtmlForm");
1318 assertNotNull(component);
1319
1320 component.setId(id);
1321 component.getAttributes().put("onsubmit", "return validateForm(this);");
1322
1323 root.getChildren().add(root.getChildCount(), component);
1324 return component;
1325 }
1326
1327
1328 private HtmlInputText createInputText(String id, UIComponent parent) {
1329 HtmlInputText component = (HtmlInputText) facesContext.getApplication().createComponent("javax.faces.HtmlInputText");
1330 assertNotNull(component);
1331
1332 component.setId(id);
1333 parent.getChildren().add(parent.getChildCount(), component);
1334
1335 return component;
1336 }
1337
1338
1339 private ValidatorScript createValidatorScript(UIViewRoot root) {
1340
1341 ValidatorScript script = (ValidatorScript) facesContext.getApplication().createComponent("org.apache.shale.ValidatorScript");
1342 assertNotNull(script);
1343 script.setId(root.createUniqueId());
1344 script.setFunctionName("validateForm");
1345
1346 root.getChildren().add(root.getChildCount(), script);
1347
1348 return script;
1349 }
1350
1351
1352
1353 private CommonsValidator createValidator(UIInput parent, String type) {
1354
1355 CommonsValidator validator = (CommonsValidator) facesContext.getApplication().createValidator("org.apache.shale.CommonsValidator");
1356 assertNotNull(validator);
1357
1358 validator.setType(type);
1359 validator.setArg(parent.getId());
1360 validator.setServer(Boolean.TRUE);
1361 validator.setClient(Boolean.TRUE);
1362 parent.addValidator(validator);
1363
1364 return validator;
1365 }
1366
1367
1368 private void checkMessages(UIInput component) {
1369 String id = component.getClientId(facesContext);
1370 Iterator mi = facesContext.getMessages(id);
1371 boolean hasMessage = false;
1372 while (mi.hasNext()) {
1373 hasMessage = true;
1374 }
1375
1376 assertFalse(id + " has messages", hasMessage);
1377
1378 }
1379
1380
1381 private void checkMessage(String expected, UIInput component) {
1382 String id = component.getClientId(facesContext);
1383 Iterator mi = facesContext.getMessages(id);
1384 boolean hasMessage = false;
1385 while (mi.hasNext()) {
1386 FacesMessage message = (FacesMessage) mi.next();
1387 assertEquals(expected, message.getDetail());
1388 hasMessage = true;
1389 }
1390
1391 assertTrue(id + " has messages", hasMessage);
1392 }
1393
1394
1395
1396
1397 private StringBuffer encode(UIComponent component) throws Exception {
1398
1399 StringWriter writer = new StringWriter();
1400
1401 ResponseWriter buffResponsewriter = facesContext.getRenderKit()
1402 .createResponseWriter(writer, null, response.getCharacterEncoding());
1403
1404 facesContext.setResponseWriter(buffResponsewriter);
1405
1406 buffResponsewriter.startDocument();
1407
1408
1409 component.encodeBegin(facesContext);
1410 component.encodeChildren(facesContext);
1411 component.encodeEnd(facesContext);
1412
1413
1414 buffResponsewriter.endDocument();
1415
1416 return writer.getBuffer();
1417 }
1418
1419
1420 }