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.ExpressionFactory;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIViewRoot;
27  
28  import org.apache.myfaces.test.mock.MockResponseWriter;
29  import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
30  import org.apache.myfaces.view.facelets.FaceletTestCase;
31  import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  public class CompositeComponentNestedCCTestCase extends FaceletTestCase
36  {
37  
38      @Override
39      protected void setupComponents() throws Exception
40      {
41          super.setupComponents();
42      }
43      
44      @Override
45      protected ExpressionFactory createExpressionFactory()
46      {
47          return new org.apache.el.ExpressionFactoryImpl();
48      }
49      
50      @Test
51      public void testCompositeNestedCC1() throws Exception
52      {
53          UIViewRoot root = facesContext.getViewRoot();
54          vdl.buildView(facesContext, root, "testCompositeNestedCC1.xhtml");
55  
56          UIComponent panelGroup1 = root.findComponent("testGroup1");
57          Assert.assertNotNull(panelGroup1);
58  
59          StringWriter sw = new StringWriter();
60          MockResponseWriter mrw = new MockResponseWriter(sw);
61          facesContext.setResponseWriter(mrw);
62          
63          root.encodeAll(facesContext);
64          sw.flush();
65          Assert.assertTrue(sw.toString().contains("ALFA"));
66          Assert.assertTrue(sw.toString().contains("BETA"));
67          Assert.assertTrue(sw.toString().contains("GAMMA"));
68      }
69      
70      /**
71       * The same tests, but with caching enabled. In this case, ccLevel changes
72       * all the time, so the caching logic must take that into account.
73       * 
74       * @throws Exception 
75       */
76      @Test
77      public void testCompositeNestedCC1Cache() throws Exception
78      {
79          servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
80                  ELExpressionCacheMode.always.toString());
81          UIViewRoot root = facesContext.getViewRoot();
82          vdl.buildView(facesContext, root, "testCompositeNestedCC1.xhtml");
83  
84          UIComponent panelGroup1 = root.findComponent("testGroup1");
85          Assert.assertNotNull(panelGroup1);
86  
87          StringWriter sw = new StringWriter();
88          MockResponseWriter mrw = new MockResponseWriter(sw);
89          facesContext.setResponseWriter(mrw);
90          
91          root.encodeAll(facesContext);
92          sw.flush();
93          Assert.assertTrue(sw.toString().contains("ALFA"));
94          Assert.assertTrue(sw.toString().contains("BETA"));
95          Assert.assertTrue(sw.toString().contains("GAMMA"));
96      }
97      
98      /**
99       * Try a nested reference inside the same composite component
100      * 
101      * @throws Exception 
102      */
103     @Test
104     public void testCompositeNestedCC2() throws Exception
105     {
106         UIViewRoot root = facesContext.getViewRoot();
107         vdl.buildView(facesContext, root, "testCompositeNestedCC2.xhtml");
108 
109         UIComponent panelGroup1 = root.findComponent("testGroup1");
110         Assert.assertNotNull(panelGroup1);
111 
112         StringWriter sw = new StringWriter();
113         MockResponseWriter mrw = new MockResponseWriter(sw);
114         facesContext.setResponseWriter(mrw);
115         
116         root.encodeAll(facesContext);
117         sw.flush();
118         Assert.assertTrue(sw.toString().contains("ALFA"));
119         Assert.assertTrue(sw.toString().contains("BETA"));
120         Assert.assertTrue(sw.toString().contains("GAMMA"));
121         Assert.assertTrue(sw.toString().contains("OMEGA"));
122     }
123 
124     /**
125      * Try a nested reference inside the same composite component
126      * 
127      * @throws Exception 
128      */
129     @Test
130     public void testCompositeNestedCC2Cache() throws Exception
131     {
132         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
133                 ELExpressionCacheMode.always.toString());
134         UIViewRoot root = facesContext.getViewRoot();
135         vdl.buildView(facesContext, root, "testCompositeNestedCC2.xhtml");
136 
137         UIComponent panelGroup1 = root.findComponent("testGroup1");
138         Assert.assertNotNull(panelGroup1);
139 
140         StringWriter sw = new StringWriter();
141         MockResponseWriter mrw = new MockResponseWriter(sw);
142         facesContext.setResponseWriter(mrw);
143         
144         root.encodeAll(facesContext);
145         sw.flush();
146         Assert.assertTrue(sw.toString().contains("ALFA"));
147         Assert.assertTrue(sw.toString().contains("BETA"));
148         Assert.assertTrue(sw.toString().contains("GAMMA"));
149         Assert.assertTrue(sw.toString().contains("OMEGA"));
150     }
151     
152     /**
153      * Try what happen when a dynamic ui:include is used inside a composite
154      * component and that include contains a reference to the same composite
155      * component. 
156      * 
157      * @throws Exception 
158      */
159     @Test
160     public void testCompositeNestedCC3() throws Exception
161     {
162         UIViewRoot root = facesContext.getViewRoot();
163         vdl.buildView(facesContext, root, "testCompositeNestedCC3.xhtml");
164 
165         UIComponent panelGroup1 = root.findComponent("testGroup1");
166         Assert.assertNotNull(panelGroup1);
167 
168         StringWriter sw = new StringWriter();
169         MockResponseWriter mrw = new MockResponseWriter(sw);
170         facesContext.setResponseWriter(mrw);
171         
172         root.encodeAll(facesContext);
173         sw.flush();
174         Assert.assertTrue(sw.toString().contains("ALFA"));
175         Assert.assertTrue(sw.toString().contains("BETA"));
176         Assert.assertTrue(sw.toString().contains("GAMMA"));
177         Assert.assertTrue(sw.toString().contains("OMEGA"));
178     }
179     
180     /**
181      * Try what happen when a dynamic ui:include is used inside a composite
182      * component and that include contains a reference to the same composite
183      * component. 
184      * 
185      * @throws Exception 
186      */
187     @Test
188     public void testCompositeNestedCC3Cache() throws Exception
189     {
190         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
191                 ELExpressionCacheMode.always.toString()); //allowCset
192         
193         UIViewRoot root = facesContext.getViewRoot();
194         vdl.buildView(facesContext, root, "testCompositeNestedCC3.xhtml");
195 
196         UIComponent panelGroup1 = root.findComponent("testGroup1");
197         Assert.assertNotNull(panelGroup1);
198 
199         StringWriter sw = new StringWriter();
200         MockResponseWriter mrw = new MockResponseWriter(sw);
201         facesContext.setResponseWriter(mrw);
202         
203         root.encodeAll(facesContext);
204         sw.flush();
205         Assert.assertTrue(sw.toString().contains("ALFA"));
206         Assert.assertTrue(sw.toString().contains("BETA"));
207         Assert.assertTrue(sw.toString().contains("GAMMA"));
208         Assert.assertTrue(sw.toString().contains("OMEGA"));
209     }
210     
211     /**
212      * Try a nested insertChildren with some conditional inclusions.
213      * 
214      * @throws Exception 
215      */
216     @Test
217     public void testCompositeNestedCC4() throws Exception
218     {
219         UIViewRoot root = facesContext.getViewRoot();
220         vdl.buildView(facesContext, root, "testCompositeNestedCC4.xhtml");
221 
222         UIComponent panelGroup1 = root.findComponent("testGroup1");
223         Assert.assertNotNull(panelGroup1);
224 
225         StringWriter sw = new StringWriter();
226         MockResponseWriter mrw = new MockResponseWriter(sw);
227         facesContext.setResponseWriter(mrw);
228         
229         root.encodeAll(facesContext);
230         sw.flush();
231         Assert.assertTrue(sw.toString().contains("ALFA"));
232         Assert.assertTrue(sw.toString().contains("BETA"));
233         Assert.assertTrue(sw.toString().contains("GAMMA"));
234         Assert.assertTrue(sw.toString().contains("OMEGA"));
235     }
236     
237     /**
238      * Try a nested insertChildren with some conditional inclusions.
239      * 
240      * @throws Exception 
241      */
242     @Test
243     public void testCompositeNestedCC4Cache() throws Exception
244     {
245         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
246                 ELExpressionCacheMode.always.toString());
247 
248         UIViewRoot root = facesContext.getViewRoot();
249         vdl.buildView(facesContext, root, "testCompositeNestedCC4.xhtml");
250 
251         UIComponent panelGroup1 = root.findComponent("testGroup1");
252         Assert.assertNotNull(panelGroup1);
253 
254         StringWriter sw = new StringWriter();
255         MockResponseWriter mrw = new MockResponseWriter(sw);
256         facesContext.setResponseWriter(mrw);
257         
258         root.encodeAll(facesContext);
259         sw.flush();
260         Assert.assertTrue(sw.toString().contains("ALFA"));
261         Assert.assertTrue(sw.toString().contains("BETA"));
262         Assert.assertTrue(sw.toString().contains("GAMMA"));
263         Assert.assertTrue(sw.toString().contains("OMEGA"));
264     }
265 
266     @Test
267     public void testCompositeNestedCC5() throws Exception
268     {
269         UIViewRoot root = facesContext.getViewRoot();
270         vdl.buildView(facesContext, root, "testCompositeNestedCC5.xhtml");
271 
272         UIComponent panelGroup1 = root.findComponent("testGroup1");
273         Assert.assertNotNull(panelGroup1);
274 
275         StringWriter sw = new StringWriter();
276         MockResponseWriter mrw = new MockResponseWriter(sw);
277         facesContext.setResponseWriter(mrw);
278         
279         root.encodeAll(facesContext);
280         sw.flush();
281         Assert.assertTrue(sw.toString().contains("ALFA"));
282         Assert.assertTrue(sw.toString().contains("BETA"));
283         Assert.assertTrue(sw.toString().contains("GAMMA"));
284     }
285     
286     /**
287      * The same tests, but with caching enabled. In this case, ccLevel changes
288      * all the time, so the caching logic must take that into account.
289      * 
290      * @throws Exception 
291      */
292     @Test
293     public void testCompositeNestedCC5Cache() throws Exception
294     {
295         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
296                 ELExpressionCacheMode.always.toString());
297         UIViewRoot root = facesContext.getViewRoot();
298         vdl.buildView(facesContext, root, "testCompositeNestedCC5.xhtml");
299 
300         UIComponent panelGroup1 = root.findComponent("testGroup1");
301         Assert.assertNotNull(panelGroup1);
302 
303         StringWriter sw = new StringWriter();
304         MockResponseWriter mrw = new MockResponseWriter(sw);
305         facesContext.setResponseWriter(mrw);
306         
307         root.encodeAll(facesContext);
308         sw.flush();
309         Assert.assertTrue(sw.toString().contains("ALFA"));
310         Assert.assertTrue(sw.toString().contains("BETA"));
311         Assert.assertTrue(sw.toString().contains("GAMMA"));
312     }
313     
314     /**
315      * Try a nested reference inside the same composite component
316      * 
317      * @throws Exception 
318      */
319     @Test
320     public void testCompositeNestedCC6() throws Exception
321     {
322         UIViewRoot root = facesContext.getViewRoot();
323         vdl.buildView(facesContext, root, "testCompositeNestedCC6.xhtml");
324 
325         UIComponent panelGroup1 = root.findComponent("testGroup1");
326         Assert.assertNotNull(panelGroup1);
327 
328         StringWriter sw = new StringWriter();
329         MockResponseWriter mrw = new MockResponseWriter(sw);
330         facesContext.setResponseWriter(mrw);
331         
332         root.encodeAll(facesContext);
333         sw.flush();
334         Assert.assertTrue(sw.toString().contains("ALFA"));
335         Assert.assertTrue(sw.toString().contains("BETA"));
336         Assert.assertTrue(sw.toString().contains("GAMMA"));
337         Assert.assertTrue(sw.toString().contains("OMEGA"));
338     }
339 
340     /**
341      * Try a nested reference inside the same composite component
342      * 
343      * @throws Exception 
344      */
345     @Test
346     public void testCompositeNestedCC6Cache() throws Exception
347     {
348         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
349                 ELExpressionCacheMode.always.toString());
350         UIViewRoot root = facesContext.getViewRoot();
351         vdl.buildView(facesContext, root, "testCompositeNestedCC6.xhtml");
352 
353         UIComponent panelGroup1 = root.findComponent("testGroup1");
354         Assert.assertNotNull(panelGroup1);
355 
356         StringWriter sw = new StringWriter();
357         MockResponseWriter mrw = new MockResponseWriter(sw);
358         facesContext.setResponseWriter(mrw);
359         
360         root.encodeAll(facesContext);
361         sw.flush();
362         Assert.assertTrue(sw.toString().contains("ALFA"));
363         Assert.assertTrue(sw.toString().contains("BETA"));
364         Assert.assertTrue(sw.toString().contains("GAMMA"));
365         Assert.assertTrue(sw.toString().contains("OMEGA"));
366     }
367     
368     /**
369      * Try what happen when a dynamic ui:include is used inside a composite
370      * component and that include contains a reference to the same composite
371      * component. 
372      * 
373      * @throws Exception 
374      */
375     @Test
376     public void testCompositeNestedCC7() throws Exception
377     {
378         UIViewRoot root = facesContext.getViewRoot();
379         vdl.buildView(facesContext, root, "testCompositeNestedCC7.xhtml");
380 
381         UIComponent panelGroup1 = root.findComponent("testGroup1");
382         Assert.assertNotNull(panelGroup1);
383 
384         StringWriter sw = new StringWriter();
385         MockResponseWriter mrw = new MockResponseWriter(sw);
386         facesContext.setResponseWriter(mrw);
387         
388         root.encodeAll(facesContext);
389         sw.flush();
390         Assert.assertTrue(sw.toString().contains("ALFA"));
391         Assert.assertTrue(sw.toString().contains("BETA"));
392         Assert.assertTrue(sw.toString().contains("GAMMA"));
393         Assert.assertTrue(sw.toString().contains("OMEGA"));
394     }
395     
396     /**
397      * Try what happen when a dynamic ui:include is used inside a composite
398      * component and that include contains a reference to the same composite
399      * component. 
400      * 
401      * @throws Exception 
402      */
403     @Test
404     public void testCompositeNestedCC7Cache() throws Exception
405     {
406         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
407                 ELExpressionCacheMode.always.toString()); //allowCset
408         
409         UIViewRoot root = facesContext.getViewRoot();
410         vdl.buildView(facesContext, root, "testCompositeNestedCC7.xhtml");
411 
412         UIComponent panelGroup1 = root.findComponent("testGroup1");
413         Assert.assertNotNull(panelGroup1);
414 
415         StringWriter sw = new StringWriter();
416         MockResponseWriter mrw = new MockResponseWriter(sw);
417         facesContext.setResponseWriter(mrw);
418         
419         root.encodeAll(facesContext);
420         sw.flush();
421         Assert.assertTrue(sw.toString().contains("ALFA"));
422         Assert.assertTrue(sw.toString().contains("BETA"));
423         Assert.assertTrue(sw.toString().contains("GAMMA"));
424         Assert.assertTrue(sw.toString().contains("OMEGA"));
425     }
426     
427     /**
428      * Try a nested insertChildren with some conditional inclusions.
429      * 
430      * @throws Exception 
431      */
432     @Test
433     public void testCompositeNestedCC8() throws Exception
434     {
435         UIViewRoot root = facesContext.getViewRoot();
436         vdl.buildView(facesContext, root, "testCompositeNestedCC8.xhtml");
437 
438         UIComponent panelGroup1 = root.findComponent("testGroup1");
439         Assert.assertNotNull(panelGroup1);
440 
441         StringWriter sw = new StringWriter();
442         MockResponseWriter mrw = new MockResponseWriter(sw);
443         facesContext.setResponseWriter(mrw);
444         
445         root.encodeAll(facesContext);
446         sw.flush();
447         Assert.assertTrue(sw.toString().contains("ALFA"));
448         Assert.assertTrue(sw.toString().contains("BETA"));
449         Assert.assertTrue(sw.toString().contains("GAMMA"));
450         Assert.assertTrue(sw.toString().contains("OMEGA"));
451     }
452     
453     /**
454      * Try a nested insertChildren with some conditional inclusions.
455      * 
456      * @throws Exception 
457      */
458     @Test
459     public void testCompositeNestedCC8Cache() throws Exception
460     {
461         servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
462                 ELExpressionCacheMode.always.toString());
463 
464         UIViewRoot root = facesContext.getViewRoot();
465         vdl.buildView(facesContext, root, "testCompositeNestedCC8.xhtml");
466 
467         UIComponent panelGroup1 = root.findComponent("testGroup1");
468         Assert.assertNotNull(panelGroup1);
469 
470         StringWriter sw = new StringWriter();
471         MockResponseWriter mrw = new MockResponseWriter(sw);
472         facesContext.setResponseWriter(mrw);
473         
474         root.encodeAll(facesContext);
475         sw.flush();
476         Assert.assertTrue(sw.toString().contains("ALFA"));
477         Assert.assertTrue(sw.toString().contains("BETA"));
478         Assert.assertTrue(sw.toString().contains("GAMMA"));
479         Assert.assertTrue(sw.toString().contains("OMEGA"));
480     }
481 }