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.compiler;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import javax.faces.application.StateManager;
25 import javax.faces.application.ViewHandler;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIOutput;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.event.PhaseId;
30 import javax.faces.render.RenderKitFactory;
31 import javax.faces.render.ResponseStateManager;
32
33 import org.apache.myfaces.application.StateManagerImpl;
34 import org.apache.myfaces.renderkit.html.HtmlResponseStateManager;
35 import org.apache.myfaces.shared.config.MyfacesConfig;
36 import org.apache.myfaces.shared.util.StateUtils;
37 import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
38 import org.apache.myfaces.test.mock.MockFacesContext20;
39 import org.apache.myfaces.test.mock.MockRenderKit;
40 import org.apache.myfaces.view.facelets.FaceletMultipleRequestsTestCase;
41 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
42 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
43 import org.junit.Test;
44 import org.testng.Assert;
45
46 public class UniqueComponentIdTestCase extends FaceletMultipleRequestsTestCase
47 {
48
49 @Override
50 protected void setUpApplication() throws Exception
51 {
52 super.setUpApplication();
53
54 application.setStateManager(new StateManagerImpl());
55 }
56
57 @Override
58 protected void setUpServletContextAndSession() throws Exception
59 {
60 super.setUpServletContextAndSession();
61
62 servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, "true");
63 servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
64 servletContext.addInitParameter(ViewHandler.FACELETS_REFRESH_PERIOD_PARAM_NAME, "-1");
65 servletContext.setAttribute(MyfacesConfig.class.getName(), new MyfacesConfig());
66 }
67
68
69
70
71 @Test
72 public void testUniqueComponentIdCif() throws Exception
73 {
74
75
76
77
78
79
80
81
82
83
84
85 String viewStateParam = null;
86 String componentBid = null;
87 String tagBId = null;
88 Set<String> tagUniqueIdSet = new HashSet<String>();
89 Set<String> componentIdSet = new HashSet<String>();
90 try
91 {
92 setupRequest();
93 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
94 request.setAttribute("condition", true);
95 UIViewRoot root = facesContext.getViewRoot();
96 root.setViewId("/testUniqueComponentIdCif.xhtml");
97 vdl.buildView(facesContext, root, "/testUniqueComponentIdCif.xhtml");
98
99
100
101 for (UIComponent child : root.getChildren())
102 {
103 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
104 if (tagUniqueIdSet.contains(id))
105 {
106 Assert.fail();
107 }
108 tagUniqueIdSet.add(id);
109 String componentId = child.getId();
110 if (componentIdSet.contains(componentId))
111 {
112 Assert.fail();
113 }
114 componentIdSet.add(componentId);
115 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
116 {
117 if ("-B-".equals(((UIOutput)child).getValue()))
118 {
119 componentBid = componentId;
120 tagBId = id;
121 }
122 }
123 }
124
125 Assert.assertNotNull(componentBid);
126 Assert.assertNotNull(tagBId);
127
128 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
129
130 viewStateParam = application.getStateManager().getViewState(facesContext);
131 }
132 finally
133 {
134 tearDownRequest();
135 }
136
137 tagUniqueIdSet.remove(tagBId);
138 componentIdSet.remove(componentBid);
139
140 try
141 {
142 setupRequest();
143 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
144 request.setAttribute("condition", false);
145 UIViewRoot root = facesContext.getViewRoot();
146 root.setViewId("/testUniqueComponentIdCif.xhtml");
147 vdl.buildView(facesContext, root, "/testUniqueComponentIdCif.xhtml");
148
149
150
151 for (UIComponent child : root.getChildren())
152 {
153 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
154 if (!tagUniqueIdSet.contains(id))
155 {
156 Assert.fail();
157 }
158 String componentId = child.getId();
159 if (!componentIdSet.contains(componentId))
160 {
161 Assert.fail();
162 }
163 }
164
165 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
166
167 viewStateParam = application.getStateManager().getViewState(facesContext);
168 }
169 finally
170 {
171 tearDownRequest();
172 }
173
174 }
175
176
177
178
179 @Test
180 public void testUniqueComponentIdCifSaveCondition() throws Exception
181 {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 String viewStateParam = null;
197 String componentBid = null;
198 String tagBId = null;
199 Set<String> tagUniqueIdSet = new HashSet<String>();
200 Set<String> componentIdSet = new HashSet<String>();
201
202 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
203 StateUtils.initSecret(servletContext);
204 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
205 try
206 {
207 setupRequest();
208 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
209 request.setAttribute("condition", true);
210 UIViewRoot root = facesContext.getViewRoot();
211 root.setViewId("/testUniqueComponentIdCif.xhtml");
212 vdl.buildView(facesContext, root, "/testUniqueComponentIdCif.xhtml");
213
214
215
216 for (UIComponent child : root.getChildren())
217 {
218 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
219 if (tagUniqueIdSet.contains(id))
220 {
221 Assert.fail();
222 }
223 tagUniqueIdSet.add(id);
224 String componentId = child.getId();
225 if (componentIdSet.contains(componentId))
226 {
227 Assert.fail();
228 }
229 componentIdSet.add(componentId);
230 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
231 {
232 if ("-B-".equals(((UIOutput)child).getValue()))
233 {
234 componentBid = componentId;
235 tagBId = id;
236 }
237 }
238 }
239
240 Assert.assertNotNull(componentBid);
241 Assert.assertNotNull(tagBId);
242
243 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
244
245 viewStateParam = application.getStateManager().getViewState(facesContext);
246 }
247 finally
248 {
249 tearDownRequest();
250 }
251
252 try
253 {
254 setupRequest();
255 facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
256 request.setAttribute("condition", false);
257 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
258 ((MockFacesContext20)facesContext).setPostback(true);
259
260 UIViewRoot root = application.getStateManager().restoreView(facesContext, "/testUniqueComponentIdCif.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
261
262 Assert.assertNotNull(root);
263
264 boolean restoredB = false;
265
266 for (UIComponent child : root.getChildren())
267 {
268 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
269 if (!tagUniqueIdSet.contains(id))
270 {
271 Assert.fail();
272 }
273 String componentId = child.getId();
274 if (!componentIdSet.contains(componentId))
275 {
276 Assert.fail();
277 }
278 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
279 {
280 if ("-B-".equals(((UIOutput)child).getValue()))
281 {
282 restoredB = true;
283 }
284 }
285 }
286
287 Assert.assertTrue(restoredB);
288
289
290 tagUniqueIdSet.remove(tagBId);
291 componentIdSet.remove(componentBid);
292
293
294 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
295 vdl.buildView(facesContext, root, "/testUniqueComponentIdCif.xhtml");
296
297
298 for (UIComponent child : root.getChildren())
299 {
300 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
301 if (!tagUniqueIdSet.contains(id))
302 {
303 Assert.fail();
304 }
305 String componentId = child.getId();
306 if (!componentIdSet.contains(componentId))
307 {
308 Assert.fail();
309 }
310 }
311 }
312 finally
313 {
314 tearDownRequest();
315 }
316 }
317
318
319
320
321 @Test
322 public void testUniqueComponentIdInclude1_1() throws Exception
323 {
324
325
326
327
328
329
330
331
332
333
334
335
336
337 String viewStateParam = null;
338 String componentBid = null;
339 String tagBId = null;
340 Set<String> tagUniqueIdSet = new HashSet<String>();
341 Set<String> componentIdSet = new HashSet<String>();
342 try
343 {
344 setupRequest();
345 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
346 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_1.xhtml");
347 UIViewRoot root = facesContext.getViewRoot();
348 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
349 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
350
351
352
353 for (UIComponent child : root.getChildren())
354 {
355 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
356 if (tagUniqueIdSet.contains(id))
357 {
358 Assert.fail();
359 }
360 tagUniqueIdSet.add(id);
361 String componentId = child.getId();
362 if (componentIdSet.contains(componentId))
363 {
364 Assert.fail();
365 }
366 componentIdSet.add(componentId);
367 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
368 {
369 if ("-B-".equals(((UIOutput)child).getValue()))
370 {
371 componentBid = componentId;
372 tagBId = id;
373 }
374 }
375 }
376
377 Assert.assertNotNull(componentBid);
378 Assert.assertNotNull(tagBId);
379
380 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
381
382 viewStateParam = application.getStateManager().getViewState(facesContext);
383 }
384 finally
385 {
386 tearDownRequest();
387 }
388
389 tagUniqueIdSet.remove(tagBId);
390 componentIdSet.remove(componentBid);
391
392 try
393 {
394 setupRequest();
395 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
396 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_0.xhtml");
397 UIViewRoot root = facesContext.getViewRoot();
398 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
399 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
400
401
402
403 for (UIComponent child : root.getChildren())
404 {
405 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
406 if (!tagUniqueIdSet.contains(id))
407 {
408 Assert.fail();
409 }
410 String componentId = child.getId();
411 if (!componentIdSet.contains(componentId))
412 {
413 Assert.fail();
414 }
415 }
416
417 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
418
419 viewStateParam = application.getStateManager().getViewState(facesContext);
420 }
421 finally
422 {
423 tearDownRequest();
424 }
425
426 }
427
428 @Test
429 public void testUniqueComponentIdInclude1_2() throws Exception
430 {
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 String viewStateParam = null;
454 String componentBid = null;
455 String tagBId = null;
456 Set<String> tagUniqueIdSet = new HashSet<String>();
457 Set<String> componentIdSet = new HashSet<String>();
458 try
459 {
460 setupRequest();
461 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
462 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_1.xhtml");
463 UIViewRoot root = facesContext.getViewRoot();
464 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
465 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
466
467
468
469 for (UIComponent child : root.getChildren())
470 {
471 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
472 if (tagUniqueIdSet.contains(id))
473 {
474 Assert.fail();
475 }
476 tagUniqueIdSet.add(id);
477 String componentId = child.getId();
478 if (componentIdSet.contains(componentId))
479 {
480 Assert.fail();
481 }
482 componentIdSet.add(componentId);
483 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
484 {
485 if ("-B-".equals(((UIOutput)child).getValue()))
486 {
487 componentBid = componentId;
488 tagBId = id;
489 }
490 }
491 }
492
493 Assert.assertNotNull(componentBid);
494 Assert.assertNotNull(tagBId);
495
496 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
497
498 viewStateParam = application.getStateManager().getViewState(facesContext);
499 }
500 finally
501 {
502 tearDownRequest();
503 }
504
505 tagUniqueIdSet.remove(tagBId);
506 componentIdSet.remove(componentBid);
507
508 try
509 {
510 setupRequest();
511 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
512 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_2.xhtml");
513 UIViewRoot root = facesContext.getViewRoot();
514 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
515 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
516
517
518
519 for (UIComponent child : root.getChildren())
520 {
521 String componentId = child.getId();
522
523 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
524 if (componentBid.equals(componentId))
525 {
526 Assert.assertFalse(tagBId.equals(id));
527 }
528 else
529 {
530 if (!tagUniqueIdSet.contains(id))
531 {
532 Assert.fail();
533 }
534 if (!componentIdSet.contains(componentId))
535 {
536 Assert.fail();
537 }
538 }
539 }
540
541 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
542
543 viewStateParam = application.getStateManager().getViewState(facesContext);
544 }
545 finally
546 {
547 tearDownRequest();
548 }
549
550 }
551
552
553
554
555 @Test
556 public void testUniqueComponentIdIncludeSaveCondition() throws Exception
557 {
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577 String viewStateParam = null;
578 String componentBid = null;
579 String tagBId = null;
580 Set<String> tagUniqueIdSet = new HashSet<String>();
581 Set<String> componentIdSet = new HashSet<String>();
582
583 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
584 StateUtils.initSecret(servletContext);
585 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
586 try
587 {
588 setupRequest();
589 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
590 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_1.xhtml");
591 UIViewRoot root = facesContext.getViewRoot();
592 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
593 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
594
595
596
597 for (UIComponent child : root.getChildren())
598 {
599 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
600 if (tagUniqueIdSet.contains(id))
601 {
602 Assert.fail();
603 }
604 tagUniqueIdSet.add(id);
605 String componentId = child.getId();
606 if (componentIdSet.contains(componentId))
607 {
608 Assert.fail();
609 }
610 componentIdSet.add(componentId);
611 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
612 {
613 if ("-B-".equals(((UIOutput)child).getValue()))
614 {
615 componentBid = componentId;
616 tagBId = id;
617 child.getAttributes().put("test", "test");
618 }
619 }
620 }
621
622 Assert.assertNotNull(componentBid);
623 Assert.assertNotNull(tagBId);
624
625 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
626
627 viewStateParam = application.getStateManager().getViewState(facesContext);
628 }
629 finally
630 {
631 tearDownRequest();
632 }
633
634 try
635 {
636 setupRequest();
637 facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
638 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_0.xhtml");
639 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
640 ((MockFacesContext20)facesContext).setPostback(true);
641
642 UIViewRoot root = application.getStateManager().restoreView(facesContext, "/testUniqueComponentIdInclude1.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
643
644 Assert.assertNotNull(root);
645
646 boolean restoredB = false;
647
648 for (UIComponent child : root.getChildren())
649 {
650 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
651 if (!tagUniqueIdSet.contains(id))
652 {
653 Assert.fail();
654 }
655 String componentId = child.getId();
656 if (!componentIdSet.contains(componentId))
657 {
658 Assert.fail();
659 }
660 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
661 {
662 if ("-B-".equals(((UIOutput)child).getValue()))
663 {
664 restoredB = true;
665 Assert.assertNotNull(child.getAttributes().get("test"));
666 }
667 }
668 }
669
670 Assert.assertTrue(restoredB);
671
672
673 tagUniqueIdSet.remove(tagBId);
674 componentIdSet.remove(componentBid);
675
676
677 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
678 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
679
680
681 for (UIComponent child : root.getChildren())
682 {
683 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
684 if (!tagUniqueIdSet.contains(id))
685 {
686 Assert.fail();
687 }
688 String componentId = child.getId();
689 if (!componentIdSet.contains(componentId))
690 {
691 Assert.fail();
692 }
693 }
694 }
695 finally
696 {
697 tearDownRequest();
698 }
699 }
700
701 @Test
702 public void testUniqueComponentIdIncludeSaveCondition2() throws Exception
703 {
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726 String viewStateParam = null;
727 String componentBid = null;
728 String tagBId = null;
729 Set<String> tagUniqueIdSet = new HashSet<String>();
730 Set<String> componentIdSet = new HashSet<String>();
731
732 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
733 StateUtils.initSecret(servletContext);
734 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
735 try
736 {
737 setupRequest();
738 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
739 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_1.xhtml");
740 UIViewRoot root = facesContext.getViewRoot();
741 root.setViewId("/testUniqueComponentIdInclude1.xhtml");
742 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
743
744
745
746 for (UIComponent child : root.getChildren())
747 {
748 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
749 if (tagUniqueIdSet.contains(id))
750 {
751 Assert.fail();
752 }
753 tagUniqueIdSet.add(id);
754 String componentId = child.getId();
755 if (componentIdSet.contains(componentId))
756 {
757 Assert.fail();
758 }
759 componentIdSet.add(componentId);
760 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
761 {
762 if ("-B-".equals(((UIOutput)child).getValue()))
763 {
764 componentBid = componentId;
765 tagBId = id;
766 child.getAttributes().put("test", "test");
767 }
768 }
769 }
770
771 Assert.assertNotNull(componentBid);
772 Assert.assertNotNull(tagBId);
773
774 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
775
776 viewStateParam = application.getStateManager().getViewState(facesContext);
777 }
778 finally
779 {
780 tearDownRequest();
781 }
782
783 try
784 {
785 setupRequest();
786 facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
787 request.setAttribute("pageSelected", "testUniqueComponentIdInclude1_2.xhtml");
788 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
789 ((MockFacesContext20)facesContext).setPostback(true);
790
791 UIViewRoot root = application.getStateManager().restoreView(facesContext, "/testUniqueComponentIdInclude1.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
792
793 Assert.assertNotNull(root);
794
795 boolean restoredB = false;
796
797 for (UIComponent child : root.getChildren())
798 {
799 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
800 if (!tagUniqueIdSet.contains(id))
801 {
802 Assert.fail();
803 }
804 String componentId = child.getId();
805 if (!componentIdSet.contains(componentId))
806 {
807 Assert.fail();
808 }
809 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
810 {
811 if ("-B-".equals(((UIOutput)child).getValue()))
812 {
813 restoredB = true;
814 Assert.assertNotNull(child.getAttributes().get("test"));
815 }
816 }
817 }
818
819 Assert.assertTrue(restoredB);
820
821
822 tagUniqueIdSet.remove(tagBId);
823 componentIdSet.remove(componentBid);
824
825
826 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
827 vdl.buildView(facesContext, root, "/testUniqueComponentIdInclude1.xhtml");
828
829 restoredB = false;
830
831
832 for (UIComponent child : root.getChildren())
833 {
834 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
835 String componentId = child.getId();
836 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
837 {
838 if ("-B-".equals(((UIOutput)child).getValue()))
839 {
840 restoredB = true;
841
842
843 Assert.assertFalse(tagBId.equals(id));
844 Assert.assertNull(child.getAttributes().get("test"));
845 }
846 }
847 else
848 {
849 if (!tagUniqueIdSet.contains(id))
850 {
851 Assert.fail();
852 }
853 if (!componentIdSet.contains(componentId))
854 {
855 Assert.fail();
856 }
857 }
858 }
859
860 Assert.assertTrue(restoredB);
861 }
862 finally
863 {
864 tearDownRequest();
865 }
866 }
867
868
869
870
871 @Test
872 public void testUniqueComponentIdDecorate1_1() throws Exception
873 {
874 String viewStateParam = null;
875 String componentBid = null;
876 String tagBId = null;
877 String componentDid = null;
878 String tagDId = null;
879 Set<String> tagUniqueIdSet = new HashSet<String>();
880 Set<String> componentIdSet = new HashSet<String>();
881 try
882 {
883 setupRequest();
884 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
885 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_1.xhtml");
886 UIViewRoot root = facesContext.getViewRoot();
887 root.setViewId("/testUniqueComponentIdDecorate1.xhtml");
888 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
889
890
891
892 for (UIComponent child : root.getChildren())
893 {
894 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
895 if (tagUniqueIdSet.contains(id))
896 {
897 Assert.fail();
898 }
899 tagUniqueIdSet.add(id);
900 String componentId = child.getId();
901 if (componentIdSet.contains(componentId))
902 {
903 Assert.fail();
904 }
905 componentIdSet.add(componentId);
906 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
907 {
908 if ("-B-".equals(((UIOutput)child).getValue()))
909 {
910 componentBid = componentId;
911 tagBId = id;
912 }
913 if ("-D-".equals(((UIOutput)child).getValue()))
914 {
915 componentDid = componentId;
916 tagDId = id;
917 }
918 }
919 }
920
921 Assert.assertNotNull(componentBid);
922 Assert.assertNotNull(tagBId);
923 Assert.assertNotNull(componentDid);
924 Assert.assertNotNull(tagDId);
925
926 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
927
928 viewStateParam = application.getStateManager().getViewState(facesContext);
929 }
930 finally
931 {
932 tearDownRequest();
933 }
934
935 tagUniqueIdSet.remove(tagBId);
936 componentIdSet.remove(componentBid);
937 tagUniqueIdSet.remove(tagDId);
938 componentIdSet.remove(componentDid);
939
940 try
941 {
942 setupRequest();
943 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
944 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_0.xhtml");
945 UIViewRoot root = facesContext.getViewRoot();
946 root.setViewId("/testUniqueComponentIdDecorate1.xhtml");
947 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
948
949
950
951 for (UIComponent child : root.getChildren())
952 {
953 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
954 String componentId = child.getId();
955 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
956 {
957 if ("-C-".equals(((UIOutput)child).getValue()))
958 {
959
960
961
962 Assert.assertTrue(!tagUniqueIdSet.contains(id));
963 }
964 else
965 {
966 if (!tagUniqueIdSet.contains(id))
967 {
968 Assert.fail();
969 }
970 if (!componentIdSet.contains(componentId))
971 {
972 Assert.fail();
973 }
974 }
975 }
976 else
977 {
978 if (!tagUniqueIdSet.contains(id))
979 {
980 Assert.fail();
981 }
982 if (!componentIdSet.contains(componentId))
983 {
984 Assert.fail();
985 }
986 }
987 }
988
989 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
990
991 viewStateParam = application.getStateManager().getViewState(facesContext);
992 }
993 finally
994 {
995 tearDownRequest();
996 }
997
998 }
999
1000
1001
1002
1003 @Test
1004 public void testUniqueComponentIdDecorate1_2() throws Exception
1005 {
1006 String viewStateParam = null;
1007 String componentBid = null;
1008 String tagBId = null;
1009 String componentDid = null;
1010 String tagDId = null;
1011 Set<String> tagUniqueIdSet = new HashSet<String>();
1012 Set<String> componentIdSet = new HashSet<String>();
1013 try
1014 {
1015 setupRequest();
1016 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1017 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_1.xhtml");
1018 UIViewRoot root = facesContext.getViewRoot();
1019 root.setViewId("/testUniqueComponentIdDecorate1.xhtml");
1020 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
1021
1022
1023
1024 for (UIComponent child : root.getChildren())
1025 {
1026 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1027 if (tagUniqueIdSet.contains(id))
1028 {
1029 Assert.fail();
1030 }
1031 tagUniqueIdSet.add(id);
1032 String componentId = child.getId();
1033 if (componentIdSet.contains(componentId))
1034 {
1035 Assert.fail();
1036 }
1037 componentIdSet.add(componentId);
1038 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1039 {
1040 if ("-B-".equals(((UIOutput)child).getValue()))
1041 {
1042 componentBid = componentId;
1043 tagBId = id;
1044 }
1045 if ("-D-".equals(((UIOutput)child).getValue()))
1046 {
1047 componentDid = componentId;
1048 tagDId = id;
1049 }
1050 }
1051 }
1052
1053 Assert.assertNotNull(componentBid);
1054 Assert.assertNotNull(tagBId);
1055 Assert.assertNotNull(componentDid);
1056 Assert.assertNotNull(tagDId);
1057
1058 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1059
1060 viewStateParam = application.getStateManager().getViewState(facesContext);
1061 }
1062 finally
1063 {
1064 tearDownRequest();
1065 }
1066
1067 tagUniqueIdSet.remove(tagBId);
1068 componentIdSet.remove(componentBid);
1069 tagUniqueIdSet.remove(tagDId);
1070 componentIdSet.remove(componentDid);
1071
1072 try
1073 {
1074 setupRequest();
1075 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1076 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_2.xhtml");
1077 UIViewRoot root = facesContext.getViewRoot();
1078 root.setViewId("/testUniqueComponentIdDecorate1.xhtml");
1079 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
1080
1081
1082
1083 for (UIComponent child : root.getChildren())
1084 {
1085 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1086 String componentId = child.getId();
1087 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1088 {
1089 if ("-C-".equals(((UIOutput)child).getValue()) ||
1090 "-B-".equals(((UIOutput)child).getValue()) ||
1091 "-D-".equals(((UIOutput)child).getValue()))
1092 {
1093
1094
1095
1096 Assert.assertTrue(!tagUniqueIdSet.contains(id));
1097 }
1098 else
1099 {
1100 if (!tagUniqueIdSet.contains(id))
1101 {
1102 Assert.fail();
1103 }
1104 if (!componentIdSet.contains(componentId))
1105 {
1106 Assert.fail();
1107 }
1108 }
1109 }
1110 else
1111 {
1112 if (!tagUniqueIdSet.contains(id))
1113 {
1114 Assert.fail();
1115 }
1116 if (!componentIdSet.contains(componentId))
1117 {
1118 Assert.fail();
1119 }
1120 }
1121 }
1122
1123 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1124
1125 viewStateParam = application.getStateManager().getViewState(facesContext);
1126 }
1127 finally
1128 {
1129 tearDownRequest();
1130 }
1131
1132 }
1133
1134 @Test
1135 public void testUniqueComponentIdDecorateSaveCondition() throws Exception
1136 {
1137 String viewStateParam = null;
1138 String componentBid = null;
1139 String tagBId = null;
1140 Set<String> tagUniqueIdSet = new HashSet<String>();
1141 Set<String> componentIdSet = new HashSet<String>();
1142
1143 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
1144 StateUtils.initSecret(servletContext);
1145 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
1146 try
1147 {
1148 setupRequest();
1149 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1150 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_1.xhtml");
1151 UIViewRoot root = facesContext.getViewRoot();
1152 root.setViewId("/testUniqueComponentIdDecorate1.xhtml");
1153 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
1154
1155
1156
1157 for (UIComponent child : root.getChildren())
1158 {
1159 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1160 if (tagUniqueIdSet.contains(id))
1161 {
1162 Assert.fail();
1163 }
1164 tagUniqueIdSet.add(id);
1165 String componentId = child.getId();
1166 if (componentIdSet.contains(componentId))
1167 {
1168 Assert.fail();
1169 }
1170 componentIdSet.add(componentId);
1171 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1172 {
1173 if ("-B-".equals(((UIOutput)child).getValue()))
1174 {
1175 componentBid = componentId;
1176 tagBId = id;
1177 child.getAttributes().put("test", "test");
1178 }
1179 }
1180 }
1181
1182 Assert.assertNotNull(componentBid);
1183 Assert.assertNotNull(tagBId);
1184
1185 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1186
1187 viewStateParam = application.getStateManager().getViewState(facesContext);
1188 }
1189 finally
1190 {
1191 tearDownRequest();
1192 }
1193
1194 try
1195 {
1196 setupRequest();
1197 facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
1198 request.setAttribute("pageSelected", "testUniqueComponentIdDecorate1_0.xhtml");
1199 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
1200 ((MockFacesContext20)facesContext).setPostback(true);
1201
1202 UIViewRoot root = application.getStateManager().restoreView(facesContext, "/testUniqueComponentIdDecorate1.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
1203
1204 Assert.assertNotNull(root);
1205
1206 boolean restoredB = false;
1207
1208 for (UIComponent child : root.getChildren())
1209 {
1210 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1211 String componentId = child.getId();
1212
1213 if (!tagUniqueIdSet.contains(id))
1214 {
1215 Assert.fail();
1216 }
1217 if (!componentIdSet.contains(componentId))
1218 {
1219 Assert.fail();
1220 }
1221 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1222 {
1223 if ("-B-".equals(((UIOutput)child).getValue()))
1224 {
1225 restoredB = true;
1226 Assert.assertNotNull(child.getAttributes().get("test"));
1227 }
1228 }
1229 }
1230
1231 Assert.assertTrue(restoredB);
1232
1233
1234 tagUniqueIdSet.remove(tagBId);
1235 componentIdSet.remove(componentBid);
1236
1237
1238 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1239 vdl.buildView(facesContext, root, "/testUniqueComponentIdDecorate1.xhtml");
1240
1241
1242 for (UIComponent child : root.getChildren())
1243 {
1244 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1245 String componentId = child.getId();
1246 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput
1247 && "-C-".equals(((UIOutput)child).getValue()))
1248 {
1249
1250
1251
1252 Assert.assertTrue(!tagUniqueIdSet.contains(id));
1253 }
1254 else
1255 {
1256 if (!tagUniqueIdSet.contains(id))
1257 {
1258 Assert.fail();
1259 }
1260 if (!componentIdSet.contains(componentId))
1261 {
1262 Assert.fail();
1263 }
1264 }
1265 }
1266 }
1267 finally
1268 {
1269 tearDownRequest();
1270 }
1271 }
1272
1273 @Test
1274 public void testUniqueComponentIdChoose() throws Exception
1275 {
1276 String viewStateParam = null;
1277 String componentBid = null;
1278 String tagBId = null;
1279 Set<String> tagUniqueIdSet = new HashSet<String>();
1280 Set<String> componentIdSet = new HashSet<String>();
1281 try
1282 {
1283 setupRequest();
1284 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1285 request.setAttribute("selectionC", true);
1286 UIViewRoot root = facesContext.getViewRoot();
1287 root.setViewId("/testUniqueComponentIdChoose.xhtml");
1288 vdl.buildView(facesContext, root, "/testUniqueComponentIdChoose.xhtml");
1289
1290
1291
1292 for (UIComponent child : root.getChildren())
1293 {
1294 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1295 if (tagUniqueIdSet.contains(id))
1296 {
1297 Assert.fail();
1298 }
1299 tagUniqueIdSet.add(id);
1300 String componentId = child.getId();
1301 if (componentIdSet.contains(componentId))
1302 {
1303 Assert.fail();
1304 }
1305 componentIdSet.add(componentId);
1306 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1307 {
1308 if ("-C-".equals(((UIOutput)child).getValue()))
1309 {
1310 componentBid = componentId;
1311 tagBId = id;
1312 }
1313 }
1314 }
1315
1316 Assert.assertNotNull(componentBid);
1317 Assert.assertNotNull(tagBId);
1318
1319 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1320
1321 viewStateParam = application.getStateManager().getViewState(facesContext);
1322 }
1323 finally
1324 {
1325 tearDownRequest();
1326 }
1327
1328 tagUniqueIdSet.remove(tagBId);
1329 componentIdSet.remove(componentBid);
1330
1331 try
1332 {
1333 setupRequest();
1334 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1335 request.setAttribute("selectionX", true);
1336 UIViewRoot root = facesContext.getViewRoot();
1337 root.setViewId("/testUniqueComponentIdChoose.xhtml");
1338 vdl.buildView(facesContext, root, "/testUniqueComponentIdChoose.xhtml");
1339
1340
1341
1342 for (UIComponent child : root.getChildren())
1343 {
1344 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1345 if (!tagUniqueIdSet.contains(id))
1346 {
1347 Assert.fail();
1348 }
1349 String componentId = child.getId();
1350 if (!componentIdSet.contains(componentId))
1351 {
1352 Assert.fail();
1353 }
1354 }
1355
1356 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1357
1358 viewStateParam = application.getStateManager().getViewState(facesContext);
1359 }
1360 finally
1361 {
1362 tearDownRequest();
1363 }
1364
1365 }
1366
1367 @Test
1368 public void testUniqueComponentIdChooseSaveCondition() throws Exception
1369 {
1370 String viewStateParam = null;
1371 String componentBid = null;
1372 String tagBId = null;
1373 Set<String> tagUniqueIdSet = new HashSet<String>();
1374 Set<String> componentIdSet = new HashSet<String>();
1375
1376 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
1377 StateUtils.initSecret(servletContext);
1378 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
1379 try
1380 {
1381 setupRequest();
1382 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1383 request.setAttribute("selectionB", true);
1384 UIViewRoot root = facesContext.getViewRoot();
1385 root.setViewId("/testUniqueComponentIdChoose.xhtml");
1386 vdl.buildView(facesContext, root, "/testUniqueComponentIdChoose.xhtml");
1387
1388
1389
1390 for (UIComponent child : root.getChildren())
1391 {
1392 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1393 if (tagUniqueIdSet.contains(id))
1394 {
1395 Assert.fail();
1396 }
1397 tagUniqueIdSet.add(id);
1398 String componentId = child.getId();
1399 if (componentIdSet.contains(componentId))
1400 {
1401 Assert.fail();
1402 }
1403 componentIdSet.add(componentId);
1404 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1405 {
1406 if ("-B-".equals(((UIOutput)child).getValue()))
1407 {
1408 componentBid = componentId;
1409 tagBId = id;
1410 }
1411 }
1412 }
1413
1414 Assert.assertNotNull(componentBid);
1415 Assert.assertNotNull(tagBId);
1416
1417 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
1418
1419 viewStateParam = application.getStateManager().getViewState(facesContext);
1420 }
1421 finally
1422 {
1423 tearDownRequest();
1424 }
1425
1426 try
1427 {
1428 setupRequest();
1429 facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
1430 request.setAttribute("selectionX", true);
1431 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
1432 ((MockFacesContext20)facesContext).setPostback(true);
1433
1434 UIViewRoot root = application.getStateManager().restoreView(facesContext, "/testUniqueComponentIdChoose.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
1435
1436 Assert.assertNotNull(root);
1437
1438 boolean restoredB = false;
1439
1440 for (UIComponent child : root.getChildren())
1441 {
1442 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1443 if (!tagUniqueIdSet.contains(id))
1444 {
1445 Assert.fail();
1446 }
1447 String componentId = child.getId();
1448 if (!componentIdSet.contains(componentId))
1449 {
1450 Assert.fail();
1451 }
1452 if (componentId.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) && child instanceof UIOutput)
1453 {
1454 if ("-B-".equals(((UIOutput)child).getValue()))
1455 {
1456 restoredB = true;
1457 }
1458 }
1459 }
1460
1461 Assert.assertTrue(restoredB);
1462
1463
1464 tagUniqueIdSet.remove(tagBId);
1465 componentIdSet.remove(componentBid);
1466
1467
1468 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
1469 vdl.buildView(facesContext, root, "/testUniqueComponentIdChoose.xhtml");
1470
1471
1472 for (UIComponent child : root.getChildren())
1473 {
1474 String id = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
1475 if (!tagUniqueIdSet.contains(id))
1476 {
1477 Assert.fail();
1478 }
1479 String componentId = child.getId();
1480 if (!componentIdSet.contains(componentId))
1481 {
1482 Assert.fail();
1483 }
1484 }
1485 }
1486 finally
1487 {
1488 tearDownRequest();
1489 }
1490 }
1491
1492 }