View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.view.facelets.tag.composite;
21  
22  import java.io.StringWriter;
23  
24  import javax.el.MethodExpression;
25  import javax.el.ValueExpression;
26  import javax.faces.application.Resource;
27  import javax.faces.component.UICommand;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIInput;
30  import javax.faces.component.UINamingContainer;
31  import javax.faces.component.UIOutput;
32  import javax.faces.component.UIViewRoot;
33  import javax.faces.component.html.HtmlCommandLink;
34  import javax.faces.component.html.HtmlGraphicImage;
35  import javax.faces.component.html.HtmlOutputText;
36  import javax.faces.event.PreRenderViewEvent;
37  
38  import org.apache.myfaces.config.NamedEventManager;
39  import org.apache.myfaces.config.RuntimeConfig;
40  import org.apache.myfaces.test.mock.MockResponseWriter;
41  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
42  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
43  import org.apache.myfaces.view.facelets.FaceletTestCase;
44  import org.apache.myfaces.view.facelets.bean.HelloWorld;
45  import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
46  import org.junit.Assert;
47  import org.junit.Test;
48  
49  public class CompositeComponentTestCase extends FaceletTestCase
50  {
51  
52      @Override
53      protected void setupComponents() throws Exception
54      {
55          super.setupComponents();
56          application.addComponent(CompositeTestComponent.class.getName(), 
57                  CompositeTestComponent.class.getName());
58      }
59      
60      @Override
61      protected void setUpServletObjects() throws Exception
62      {
63          super.setUpServletObjects();
64          servletContext.addInitParameter("javax.faces.FACELETS_LIBRARIES", "/test-facelet.taglib.xml");
65      }
66      
67      
68      @Override
69      protected void setUpExternalContext() throws Exception
70      {
71          super.setUpExternalContext();
72          
73          RuntimeConfig.getCurrentInstance(externalContext).setNamedEventManager(new NamedEventManager());
74      }
75  
76      /**
77       * Test if a child component inside composite component template is
78       * rendered.
79       * 
80       * @throws Exception
81       */
82      @Test
83      public void testSimpleCompositeComponent() throws Exception
84      {
85          UIViewRoot root = facesContext.getViewRoot();
86          vdl.buildView(facesContext, root, "testSimpleComposite.xhtml");
87  
88          UIComponent panelGroup = root.findComponent("testGroup");
89          Assert.assertNotNull(panelGroup);
90          UINamingContainer compositeComponent = (UINamingContainer) panelGroup.getChildren().get(0);
91          Assert.assertNotNull(compositeComponent);
92          UIOutput text = (UIOutput) compositeComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME).findComponent("text");
93          Assert.assertNotNull(text);
94          
95          StringWriter sw = new StringWriter();
96          MockResponseWriter mrw = new MockResponseWriter(sw);
97          facesContext.setResponseWriter(mrw);
98          
99          compositeComponent.encodeAll(facesContext);
100         sw.flush();
101         
102         HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
103                 new HtmlRenderedAttr("value")
104         };
105             
106         HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
107     }
108 
109     /**
110      * Test simple attribute resolution (not set, default, normal use case).
111      * 
112      * @throws Exception
113      */
114     @Test
115     public void testSimpleCompositeAttribute() throws Exception
116     {
117         UIViewRoot root = facesContext.getViewRoot();
118         vdl.buildView(facesContext, root, "testSimpleAttribute.xhtml");
119 
120         UIComponent panelGroup1 = root.findComponent("testGroup1");
121         Assert.assertNotNull(panelGroup1);
122         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
123         Assert.assertNotNull(compositeComponent1);
124         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
125         Assert.assertNotNull(facet1);
126         HtmlOutputText text1 = (HtmlOutputText) facet1.findComponent("text");
127         Assert.assertNotNull(text1);
128         
129         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
130         facet1.pushComponentToEL(facesContext, facet1);
131         text1.pushComponentToEL(facesContext, text1);
132         //set on tag
133         Assert.assertEquals("class1", text1.getStyleClass());
134         //set as default
135         Assert.assertEquals("background:red", text1.getStyle());
136         //Check coercion of attribute using type value
137         Assert.assertEquals(5, compositeComponent1.getAttributes().get("index"));
138         //Check default coercion
139         ValueExpression ve = facesContext.getApplication().getExpressionFactory().createValueExpression(
140                 facesContext.getELContext(), "#{cc.attrs.cols}", Object.class);
141         Assert.assertEquals(1, ve.getValue(facesContext.getELContext()));
142         text1.popComponentFromEL(facesContext);
143         facet1.popComponentFromEL(facesContext);
144         compositeComponent1.popComponentFromEL(facesContext);
145         
146         UIComponent panelGroup2 = root.findComponent("testGroup2");
147         Assert.assertNotNull(panelGroup2);
148         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
149         Assert.assertNotNull(compositeComponent2);
150         UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
151         Assert.assertNotNull(facet2);        
152         HtmlOutputText text2 = (HtmlOutputText) facet2.findComponent("text");
153         Assert.assertNotNull(text2);
154         
155         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
156         facet2.pushComponentToEL(facesContext, facet2);
157         text2.pushComponentToEL(facesContext, text2);
158         //set on tag
159         Assert.assertEquals("background:green", text2.getStyle());
160         // not set, should return null, but since there is a ValueExpression indirection,
161         // coercing rules apply here, so null is converted as ""
162         Assert.assertEquals("", text2.getStyleClass());
163         text2.popComponentFromEL(facesContext);
164         facet2.popComponentFromEL(facesContext);
165         compositeComponent2.popComponentFromEL(facesContext);
166 
167         StringWriter sw = new StringWriter();
168         MockResponseWriter mrw = new MockResponseWriter(sw);
169         facesContext.setResponseWriter(mrw);
170         
171         compositeComponent1.encodeAll(facesContext);
172         sw.flush();
173         
174         HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
175                 new HtmlRenderedAttr("style")
176         };
177             
178         HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
179     }
180     
181     @Test
182     public void testSimpleCompositeAttributeMethodExpression() throws Exception
183     {
184         HelloWorld helloWorld = new HelloWorld(); 
185         
186         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
187                 helloWorld);
188         
189         UIViewRoot root = facesContext.getViewRoot();
190         vdl.buildView(facesContext, root, "testSimpleAttributeMethodExpression.xhtml");
191 
192         UIComponent form = root.findComponent("testForm1");
193         Assert.assertNotNull(form);
194         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
195         Assert.assertNotNull(compositeComponent);
196         UICommand button = (UICommand) compositeComponent.findComponent("button");
197         Assert.assertNotNull(button);
198         Assert.assertEquals("#{helloWorldBean.send}", button.getActionExpression().getExpressionString());
199         Assert.assertEquals("#{helloWorldBean.send}", ((MethodExpression)compositeComponent.getAttributes().get("metodo")).getExpressionString());
200         Assert.assertNull(button.getAttributes().get("metodo"));
201         
202         UICommand link = (UICommand) compositeComponent.findComponent("link");
203         Assert.assertNotNull(link);
204         Assert.assertEquals(1, link.getActionListeners().length);
205         UIInput input = (UIInput) compositeComponent.findComponent("input");
206         Assert.assertNotNull(input);
207         Assert.assertEquals(1, input.getValidators().length);
208         Assert.assertEquals(1, input.getValueChangeListeners().length);
209         
210         StringWriter sw = new StringWriter();
211         MockResponseWriter mrw = new MockResponseWriter(sw);
212         facesContext.setResponseWriter(mrw);
213         
214         //root.encodeAll(facesContext);
215         //compositeComponent.encodeAll(facesContext);
216         //sw.flush();
217         //System.out.print(sw.toString());
218     }
219     
220     @Test
221     public void testSimpleActionSource() throws Exception
222     {
223         HelloWorld helloWorld = new HelloWorld(); 
224         
225         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
226                 helloWorld);
227         
228         UIViewRoot root = facesContext.getViewRoot();
229         vdl.buildView(facesContext, root, "testSimpleActionSource.xhtml");
230         
231         UIComponent form = root.findComponent("testForm1");
232         Assert.assertNotNull(form);
233         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
234         Assert.assertNotNull(compositeComponent);
235         UICommand button = (UICommand) compositeComponent.findComponent("button");
236         Assert.assertNotNull(button);
237         Assert.assertEquals(3, button.getActionListeners().length);
238         
239         //StringWriter sw = new StringWriter();
240         //MockResponseWriter mrw = new MockResponseWriter(sw);
241         //facesContext.setResponseWriter(mrw);
242         
243         //root.encodeAll(facesContext);
244         //sw.flush();
245         //System.out.print(sw.toString());
246     }
247     
248     @Test
249     public void testSimpleValueHolder() throws Exception
250     {
251         HelloWorld helloWorld = new HelloWorld(); 
252         
253         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
254                 helloWorld);
255         
256         UIViewRoot root = facesContext.getViewRoot();
257         vdl.buildView(facesContext, root, "testSimpleValueHolder.xhtml");
258         
259         UIComponent form = root.findComponent("testForm1");
260         Assert.assertNotNull(form);
261         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
262         Assert.assertNotNull(compositeComponent);
263         UIOutput text = (UIOutput) compositeComponent.findComponent("text");
264         Assert.assertNotNull(text);
265         Assert.assertNotNull(text.getConverter());
266         //Assert.assertEquals(2, button.getActionListeners().length);
267         
268         //StringWriter sw = new StringWriter();
269         //MockResponseWriter mrw = new MockResponseWriter(sw);
270         //facesContext.setResponseWriter(mrw);
271         
272         //root.encodeAll(facesContext);
273         //sw.flush();
274         //System.out.print(sw.toString());
275     }
276     
277     @Test
278     public void testCompositeActionSource() throws Exception
279     {
280         HelloWorld helloWorld = new HelloWorld(); 
281         
282         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
283                 helloWorld);
284         
285         UIViewRoot root = facesContext.getViewRoot();
286         vdl.buildView(facesContext, root, "testCompositeActionSource.xhtml");
287         
288         UIComponent form = root.findComponent("testForm1");
289         Assert.assertNotNull(form);
290         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
291         Assert.assertNotNull(compositeComponent);
292         UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("button3");
293         Assert.assertNotNull(compositeComponent2);
294         UICommand button = (UICommand) compositeComponent2.findComponent("button");
295         Assert.assertNotNull(button);
296         //One added in testCompositeActionSource, the other one
297         //inside compositeActionSource.xhtml
298         Assert.assertEquals(2, button.getActionListeners().length);
299         
300         //StringWriter sw = new StringWriter();
301         //MockResponseWriter mrw = new MockResponseWriter(sw);
302         //facesContext.setResponseWriter(mrw);
303         
304         //root.encodeAll(facesContext);
305         //sw.flush();
306         //System.out.print(sw.toString());
307     }
308     
309     @Test
310     public void testSimpleInsertChildren() throws Exception
311     {
312         HelloWorld helloWorld = new HelloWorld(); 
313         
314         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
315                 helloWorld);
316         
317         UIViewRoot root = facesContext.getViewRoot();
318         vdl.buildView(facesContext, root, "testSimpleInsertChildren.xhtml");
319         
320         /*
321         UIComponent form = root.findComponent("testForm1");
322         Assert.assertNotNull(form);
323         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
324         Assert.assertNotNull(compositeComponent);
325         UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("button3");
326         Assert.assertNotNull(compositeComponent2);
327         UICommand button = (UICommand) compositeComponent2.findComponent("button");
328         Assert.assertNotNull(button);
329         */
330         StringWriter sw = new StringWriter();
331         MockResponseWriter mrw = new MockResponseWriter(sw);
332         facesContext.setResponseWriter(mrw);
333         
334         root.encodeAll(facesContext);
335         sw.flush();
336 
337         String resp = sw.toString();
338         
339         Assert.assertTrue(resp.contains("Hello"));
340         Assert.assertTrue(resp.contains("Leonardo"));
341         Assert.assertTrue(resp.contains("Alfredo"));
342         Assert.assertTrue(resp.contains("Uribe"));
343         Assert.assertTrue(resp.contains("Sayonara"));
344         //System.out.print(sw.toString());
345     }
346     
347     @Test
348     public void testSimpleInsertChildrenAjax() throws Exception
349     {
350         HelloWorld helloWorld = new HelloWorld(); 
351         
352         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
353                 helloWorld);
354         
355         UIViewRoot root = facesContext.getViewRoot();
356         vdl.buildView(facesContext, root, "testSimpleInsertChildrenAjax.xhtml");
357         
358         UIComponent panelGroup1 = root.findComponent("testGroup1");
359         Assert.assertNotNull(panelGroup1);
360         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
361         Assert.assertNotNull(compositeComponent1);
362         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
363         Assert.assertNotNull(facet1);
364         HtmlCommandLink link = (HtmlCommandLink) facet1.findComponent("link");
365         Assert.assertNotNull(link);
366         Assert.assertEquals(1, link.getClientBehaviors().size());
367         Assert.assertEquals(1, link.getClientBehaviors().get(link.getDefaultEventName()).size());
368         /*
369         StringWriter sw = new StringWriter();
370         MockResponseWriter mrw = new MockResponseWriter(sw);
371         facesContext.setResponseWriter(mrw);
372         
373         root.encodeAll(facesContext);
374         sw.flush();
375 
376         String resp = sw.toString();
377         */
378         //System.out.print(sw.toString());
379     }
380 
381     @Test
382     public void testSimpleInsertChildrenAjax2() throws Exception
383     {
384         HelloWorld helloWorld = new HelloWorld(); 
385         
386         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
387                 helloWorld);
388         
389         UIViewRoot root = facesContext.getViewRoot();
390         vdl.buildView(facesContext, root, "testSimpleInsertChildrenAjax2.xhtml");
391         
392         UIComponent panelGroup1 = root.findComponent("testGroup1");
393         Assert.assertNotNull(panelGroup1);
394         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
395         Assert.assertNotNull(compositeComponent1);
396         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
397         Assert.assertNotNull(facet1);
398         HtmlCommandLink link = (HtmlCommandLink) compositeComponent1.findComponent("link");
399         Assert.assertNotNull(link);
400         Assert.assertEquals(1, link.getClientBehaviors().size());
401         Assert.assertEquals(1, link.getClientBehaviors().get(link.getDefaultEventName()).size());
402         /*
403         StringWriter sw = new StringWriter();
404         MockResponseWriter mrw = new MockResponseWriter(sw);
405         facesContext.setResponseWriter(mrw);
406         
407         root.encodeAll(facesContext);
408         sw.flush();
409 
410         String resp = sw.toString();
411         */
412         //System.out.print(sw.toString());
413     }
414     
415     @Test
416     public void testSimpleInsertChildrenNoAjax() throws Exception
417     {
418         HelloWorld helloWorld = new HelloWorld(); 
419         
420         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
421                 helloWorld);
422         
423         UIViewRoot root = facesContext.getViewRoot();
424         vdl.buildView(facesContext, root, "testSimpleInsertChildrenNoAjax.xhtml");
425         
426         UIComponent panelGroup1 = root.findComponent("testGroup1");
427         Assert.assertNotNull(panelGroup1);
428         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
429         Assert.assertNotNull(compositeComponent1);
430         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
431         Assert.assertNotNull(facet1);
432         HtmlCommandLink link = (HtmlCommandLink) facet1.findComponent("link");
433         Assert.assertNotNull(link);
434         Assert.assertEquals(0, link.getClientBehaviors().size());
435         /*
436         StringWriter sw = new StringWriter();
437         MockResponseWriter mrw = new MockResponseWriter(sw);
438         facesContext.setResponseWriter(mrw);
439         
440         root.encodeAll(facesContext);
441         sw.flush();
442 
443         String resp = sw.toString();
444         */
445         //System.out.print(sw.toString());
446     }
447     
448     @Test
449     public void testCompositeInsertChildren() throws Exception
450     {
451         HelloWorld helloWorld = new HelloWorld(); 
452         
453         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
454                 helloWorld);
455         
456         UIViewRoot root = facesContext.getViewRoot();
457         vdl.buildView(facesContext, root, "testCompositeInsertChildren.xhtml");
458         
459         UIComponent panelGroup1 = root.findComponent("testGroup1");
460         Assert.assertNotNull(panelGroup1);
461         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
462         Assert.assertNotNull(compositeComponent1);
463         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
464         Assert.assertNotNull(facet1);
465         
466         StringWriter sw = new StringWriter();
467         MockResponseWriter mrw = new MockResponseWriter(sw);
468         facesContext.setResponseWriter(mrw);
469 
470         facet1.encodeAll(facesContext);
471 
472         sw.flush();
473         
474         String resp = sw.toString();
475 
476         Assert.assertTrue(resp.contains("ALFA"));
477         Assert.assertTrue(resp.contains("BETA"));
478         Assert.assertTrue(resp.contains("GAMMA"));
479         Assert.assertTrue(resp.contains("OMEGA"));
480     }
481     
482     @Test
483     public void testCompositeInsertChildrenPreserveTemplateSlot() throws Exception
484     {
485         HelloWorld helloWorld = new HelloWorld(); 
486         
487         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
488                 helloWorld);
489         
490         UIViewRoot root = facesContext.getViewRoot();
491         vdl.buildView(facesContext, root, "testCompositeInsertChildren2.xhtml");
492         
493         UIComponent panelGroup1 = root.findComponent("testGroup1");
494         Assert.assertNotNull(panelGroup1);
495         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
496         Assert.assertNotNull(compositeComponent1);
497         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
498         Assert.assertNotNull(facet1);
499         
500         StringWriter sw = new StringWriter();
501         MockResponseWriter mrw = new MockResponseWriter(sw);
502         facesContext.setResponseWriter(mrw);
503 
504         facet1.encodeAll(facesContext);
505 
506         sw.flush();
507         
508         String resp = sw.toString();
509 
510         Assert.assertTrue(resp.contains("ALFA"));
511         Assert.assertTrue(resp.contains("BETA"));
512         Assert.assertTrue(resp.contains("GAMMA"));
513         Assert.assertTrue(resp.contains("OMEGA"));
514     }
515     
516     @Test
517     public void testCompositeInsertChildren3() throws Exception
518     {
519         HelloWorld helloWorld = new HelloWorld(); 
520         
521         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
522                 helloWorld);
523         
524         UIViewRoot root = facesContext.getViewRoot();
525         vdl.buildView(facesContext, root, "testCompositeInsertChildren3.xhtml");
526         
527         UIComponent panelGroup1 = root.findComponent("testGroup1");
528         Assert.assertNotNull(panelGroup1);
529         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
530         Assert.assertNotNull(compositeComponent1);
531         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
532         Assert.assertNotNull(facet1);
533         
534         StringWriter sw = new StringWriter();
535         MockResponseWriter mrw = new MockResponseWriter(sw);
536         facesContext.setResponseWriter(mrw);
537 
538         panelGroup1.encodeAll(facesContext);
539 
540         sw.flush();
541         
542         String resp = sw.toString();
543 
544         Assert.assertTrue(resp.contains("ALFA"));
545         Assert.assertTrue(resp.contains("BETA"));
546         Assert.assertTrue(resp.contains("GAMMA"));
547         Assert.assertTrue(resp.contains("OMEGA"));
548     }
549     
550     @Test
551     public void testCompositeInsertChildren4() throws Exception
552     {
553         HelloWorld helloWorld = new HelloWorld(); 
554         
555         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
556                 helloWorld);
557         
558         UIViewRoot root = facesContext.getViewRoot();
559         vdl.buildView(facesContext, root, "testCompositeInsertChildren4.xhtml");
560         
561         UIComponent panelGroup1 = root.findComponent("testGroup1");
562         Assert.assertNotNull(panelGroup1);
563         //UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
564         //Assert.assertNotNull(compositeComponent1);
565         //UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
566         //Assert.assertNotNull(facet1);
567         
568         StringWriter sw = new StringWriter();
569         MockResponseWriter mrw = new MockResponseWriter(sw);
570         facesContext.setResponseWriter(mrw);
571 
572         panelGroup1.encodeAll(facesContext);
573 
574         sw.flush();
575         
576         String resp = sw.toString();
577 
578         Assert.assertTrue(resp.contains("ALFA"));
579         Assert.assertTrue(resp.contains("BETA"));
580         Assert.assertTrue(resp.contains("GAMMA"));
581         Assert.assertTrue(resp.contains("OMEGA"));
582     }
583     
584         
585     @Test
586     public void testCompositeInsertChildren5() throws Exception
587     {
588         HelloWorld helloWorld = new HelloWorld(); 
589         
590         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
591                 helloWorld);
592         
593         UIViewRoot root = facesContext.getViewRoot();
594         vdl.buildView(facesContext, root, "testCompositeInsertChildren5.xhtml");
595         
596         UIComponent panelGroup1 = root.findComponent("testGroup1");
597         Assert.assertNotNull(panelGroup1);
598         //UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
599         //Assert.assertNotNull(compositeComponent1);
600         //UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
601         //Assert.assertNotNull(facet1);
602         
603         StringWriter sw = new StringWriter();
604         MockResponseWriter mrw = new MockResponseWriter(sw);
605         facesContext.setResponseWriter(mrw);
606 
607         panelGroup1.encodeAll(facesContext);
608 
609         sw.flush();
610         
611         String resp = sw.toString();
612 
613         Assert.assertTrue(resp.contains("ALFA"));
614         Assert.assertTrue(resp.contains("BETA"));
615         Assert.assertTrue(resp.contains("GAMMA"));
616         Assert.assertTrue(resp.contains("OMEGA"));
617     }
618 
619     @Test
620     public void testCompositeInsertChildren6() throws Exception
621     {
622         HelloWorld helloWorld = new HelloWorld(); 
623         
624         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
625                 helloWorld);
626         
627         UIViewRoot root = facesContext.getViewRoot();
628         vdl.buildView(facesContext, root, "testCompositeInsertChildren6.xhtml");
629         
630         UIComponent panelGroup1 = root.findComponent("testGroup1");
631         Assert.assertNotNull(panelGroup1);
632         //UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
633         //Assert.assertNotNull(compositeComponent1);
634         //UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
635         //Assert.assertNotNull(facet1);
636         
637         StringWriter sw = new StringWriter();
638         MockResponseWriter mrw = new MockResponseWriter(sw);
639         facesContext.setResponseWriter(mrw);
640 
641         panelGroup1.encodeAll(facesContext);
642 
643         sw.flush();
644         
645         String resp = sw.toString();
646 
647         Assert.assertTrue(resp.contains("ALFA"));
648         Assert.assertTrue(resp.contains("BETA"));
649         Assert.assertTrue(resp.contains("GAMMA"));
650         Assert.assertTrue(resp.contains("OMEGA"));
651     }
652 
653     @Test
654     public void testCompositeInsertFacet() throws Exception
655     {
656         HelloWorld helloWorld = new HelloWorld(); 
657         
658         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
659                 helloWorld);
660         
661         UIViewRoot root = facesContext.getViewRoot();
662         vdl.buildView(facesContext, root, "testCompositeInsertFacet.xhtml");
663         
664         UIComponent panelGroup1 = root.findComponent("testGroup1");
665         Assert.assertNotNull(panelGroup1);
666         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
667         Assert.assertNotNull(compositeComponent1);
668         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
669         Assert.assertNotNull(facet1);
670         
671         UINamingContainer compositeComponent2 = (UINamingContainer) facet1.getChildren().get(0);
672         Assert.assertNotNull(compositeComponent2);
673         UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
674         Assert.assertNotNull(facet2);
675         Assert.assertEquals(1,facet2.getChildCount());
676         UIOutput targetComp = (UIOutput) facet2.getChildren().get(0);
677         UIComponent insertedFacet = targetComp.getFacet("header");
678         Assert.assertNotNull(insertedFacet);
679     }
680     
681     @Test
682     public void testCompositeInsertFacetChildren() throws Exception
683     {
684         HelloWorld helloWorld = new HelloWorld(); 
685         
686         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
687                 helloWorld);
688         
689         UIViewRoot root = facesContext.getViewRoot();
690         vdl.buildView(facesContext, root, "testCompositeInsertFacetChildren.xhtml");
691         
692         UIComponent panelGroup1 = root.findComponent("testGroup1");
693         Assert.assertNotNull(panelGroup1);
694         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
695         Assert.assertNotNull(compositeComponent1);
696         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
697         Assert.assertNotNull(facet1);
698         
699         UINamingContainer compositeComponent2 = (UINamingContainer) facet1.getChildren().get(0);
700         Assert.assertNotNull(compositeComponent2);
701         UIComponent facet2 = compositeComponent2.getFacet(UIComponent.COMPOSITE_FACET_NAME);
702         Assert.assertNotNull(facet2);
703         Assert.assertEquals(3,facet2.getChildCount());
704         UIComponent insertedFacet = facet2.getChildren().get(1).getFacet("header");
705         Assert.assertNotNull(insertedFacet);
706     }
707 
708     @Test
709     public void testSimpleRenderFacet() throws Exception
710     {
711         HelloWorld helloWorld = new HelloWorld(); 
712         
713         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
714                 helloWorld);
715         
716         UIViewRoot root = facesContext.getViewRoot();
717         vdl.buildView(facesContext, root, "testSimpleRenderFacet.xhtml");
718         
719         UIComponent panelGroup1 = root.findComponent("testGroup1");
720         Assert.assertNotNull(panelGroup1);
721         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
722         Assert.assertNotNull(compositeComponent1);
723         
724         StringWriter sw = new StringWriter();
725         MockResponseWriter mrw = new MockResponseWriter(sw);
726         facesContext.setResponseWriter(mrw);
727 
728         compositeComponent1.encodeAll(facesContext);
729 
730         sw.flush();
731         
732         String resp = sw.toString();
733 
734         Assert.assertTrue(resp.contains("HELLO"));
735         Assert.assertTrue(resp.contains("WORLD"));
736         
737     }
738     
739     @Test
740     public void testSimpleFEvent() throws Exception
741     {
742         HelloWorld helloWorld = new HelloWorld(); 
743         
744         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
745                 helloWorld);
746         
747         UIViewRoot root = facesContext.getViewRoot();
748         vdl.buildView(facesContext, root, "testSimpleFEvent.xhtml");
749         
750         UIComponent panelGroup1 = root.findComponent("testGroup1");
751         Assert.assertNotNull(panelGroup1);
752         CompositeTestComponent compositeComponent1 = (CompositeTestComponent) panelGroup1.getChildren().get(0);
753         Assert.assertNotNull(compositeComponent1);
754         
755         Assert.assertTrue("postAddToViewCallback should be called", (Boolean) compositeComponent1.getAttributes().get("postAddToViewCallback"));
756         
757         /*
758         StringWriter sw = new StringWriter();
759         MockResponseWriter mrw = new MockResponseWriter(sw);
760         facesContext.setResponseWriter(mrw);
761 
762         compositeComponent1.encodeAll(facesContext);
763 
764         sw.flush();
765         
766         String resp = sw.toString();
767 
768         Assert.assertTrue(resp.contains("HELLO"));
769         Assert.assertTrue(resp.contains("WORLD"));
770         */
771         
772     }
773     
774     @Test
775     public void testSimpleFEvent2() throws Exception
776     {
777         HelloWorld helloWorld = new HelloWorld(); 
778         
779         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
780                 helloWorld);
781         
782         UIViewRoot root = facesContext.getViewRoot();
783         vdl.buildView(facesContext, root, "testSimpleFEvent2.xhtml");
784         
785         UIComponent panelGroup1 = root.findComponent("testGroup1");
786         Assert.assertNotNull(panelGroup1);
787         CompositeTestComponent compositeComponent1 = (CompositeTestComponent) panelGroup1.getChildren().get(0);
788         Assert.assertNotNull(compositeComponent1);
789         
790         application.publishEvent(facesContext, PreRenderViewEvent.class, root);
791         
792         Assert.assertTrue("preRenderViewCallback should be called", (Boolean) compositeComponent1.getAttributes().get("preRenderViewCallback"));
793         
794         /*
795         StringWriter sw = new StringWriter();
796         MockResponseWriter mrw = new MockResponseWriter(sw);
797         facesContext.setResponseWriter(mrw);
798 
799         compositeComponent1.encodeAll(facesContext);
800 
801         sw.flush();
802         
803         String resp = sw.toString();
804 
805         Assert.assertTrue(resp.contains("HELLO"));
806         Assert.assertTrue(resp.contains("WORLD"));
807         */
808         
809     }
810     
811     @Test
812     public void testsCompositeRefVE() throws Exception {
813         
814         servletContext.addInitParameter(
815                 FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
816                 "always");
817         
818         UIViewRoot root = facesContext.getViewRoot();
819         vdl.buildView(facesContext, root, "testCompositeRefVE.xhtml");
820 
821         UIComponent panelGroup1 = root.findComponent("testGroup1");
822         Assert.assertNotNull(panelGroup1);
823         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
824         Assert.assertNotNull(compositeComponent1);
825         UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
826         Assert.assertNotNull(facet1);
827 
828         StringWriter sw = new StringWriter();
829         MockResponseWriter mrw = new MockResponseWriter(sw);
830         facesContext.setResponseWriter(mrw);
831         
832         compositeComponent1.encodeAll(facesContext);
833         sw.flush();
834         
835         Assert.assertTrue("Error when rendering" + sw.toString(), sw.toString().contains("success"));
836     }
837 
838     @Test
839     public void testSimpleThisResourceReference() throws Exception
840     {
841         UIViewRoot root = facesContext.getViewRoot();
842         vdl.buildView(facesContext, root, "testSimpleThisResourceReference.xhtml");
843 
844         UINamingContainer compositeComponent = (UINamingContainer) root.findComponent("cc1");
845         Assert.assertNotNull(compositeComponent);
846         HtmlGraphicImage gi = (HtmlGraphicImage) compositeComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME).findComponent("gi");
847         Assert.assertNotNull(gi);
848         
849         StringWriter sw = new StringWriter();
850         MockResponseWriter mrw = new MockResponseWriter(sw);
851         facesContext.setResponseWriter(mrw);
852         
853         compositeComponent.encodeAll(facesContext);
854         sw.flush();
855 
856         String result = sw.toString();
857         
858         Resource resource = facesContext.getApplication().getResourceHandler().createResource("logo_mini.jpg", "testComposite");
859         Assert.assertNotNull(resource);
860         
861         Assert.assertTrue(result.contains(resource.getRequestPath()));
862     }
863 
864 }