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.ui;
21  
22  import javax.el.ExpressionFactory;
23  import javax.faces.component.UINamingContainer;
24  import javax.faces.component.UIOutput;
25  import javax.faces.component.UIPanel;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.component.html.HtmlOutputText;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.apache.myfaces.config.RuntimeConfig;
31  import org.apache.myfaces.renderkit.html.HtmlCompositeComponentRenderer;
32  import org.apache.myfaces.renderkit.html.HtmlCompositeFacetRenderer;
33  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
34  import org.apache.myfaces.test.mock.MockExternalContext;
35  import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
36  import org.apache.myfaces.view.facelets.FaceletTestCase;
37  import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
38  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
39  import org.apache.myfaces.view.facelets.util.FastWriter;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  public class IncludeParamTestCase extends FaceletTestCase
44  {
45  
46      @Override
47      protected void setupComponents() throws Exception
48      {
49          application.addComponent(UIViewRoot.COMPONENT_TYPE, UIViewRoot.class
50                  .getName());
51          application.addComponent(HtmlOutputText.COMPONENT_TYPE,
52                  HtmlOutputText.class.getName());
53          application.addComponent(UINamingContainer.COMPONENT_TYPE, 
54                  UINamingContainer.class.getName());
55          application.addComponent(UIPanel.COMPONENT_TYPE, 
56                  UIPanel.class.getName());
57      }
58  
59      @Override
60      protected void setupConvertersAndValidators() throws Exception
61      {
62      }
63  
64      @Override
65      protected void setupRenderers() throws Exception
66      {
67          renderKit.addRenderer(UIOutput.COMPONENT_FAMILY, "javax.faces.Text",
68                  new HtmlTextRenderer());
69          renderKit.addRenderer(UINamingContainer.COMPONENT_TYPE,
70                  "javax.faces.Composite", new HtmlCompositeComponentRenderer());
71          renderKit.addRenderer(UIOutput.COMPONENT_TYPE, 
72                  "javax.faces.CompositeFacet", new HtmlCompositeFacetRenderer());
73      }
74  
75      @Override
76      protected ExpressionFactory createExpressionFactory()
77      {
78          // For this test we need the a real one, because the Mock does not
79          // handle VariableMapper stuff properly and ui:param logic will not work
80          return new org.apache.el.ExpressionFactoryImpl();
81      }
82  
83      @Test
84      public void testCaching() throws Exception
85      {
86          UIViewRoot root = facesContext.getViewRoot();
87  
88          request.setAttribute("test", "test2.xml");
89          
90          //https://facelets.dev.java.net/issues/show_bug.cgi?id=117
91          
92          // test1.xml
93          // <ui:composition xmlns="http://www.w3.org/1999/xhtml"
94          //      xmlns:ui="http://java.sun.com/jsf/facelets">
95          //   <ui:include src="#{test}"/>
96          //</ui:composition>
97          
98          // test2.xml
99          //<ui:composition xmlns="http://www.w3.org/1999/xhtml"
100         //    xmlns:ui="http://java.sun.com/jsf/facelets"
101         //    xmlns:h="http://java.sun.com/jsf/html"
102         //    template="test0.xml">
103         //  <ui:param name="testParam" value="page test2" />
104         //</ui:composition>
105         
106         // test0.xml
107         //<ui:composition xmlns="http://www.w3.org/1999/xhtml"
108         //    xmlns:ui="http://java.sun.com/jsf/facelets"
109         //    xmlns:h="http://java.sun.com/jsf/html">
110         //  <p>Component value: <h:outputText value="#{testParam}" /></p>
111         //  <p>Inline EL value: #{testParam}</p> 
112         //</ui:composition>
113         
114         System.out.println("ApplicationImpl:" + facesContext.getApplication().getClass().getName());
115         System.out.println("ExpressionFactory:" + facesContext.getApplication().getExpressionFactory().getClass().getName());
116         
117         vdl.buildView(facesContext, root, "test1.xml");
118 
119         FastWriter fw = new FastWriter();
120         ResponseWriter rw = facesContext.getResponseWriter();
121         rw = rw.cloneWithWriter(fw);
122         facesContext.setResponseWriter(rw);
123         root.encodeAll(facesContext);
124         
125         String result = fw.toString();
126         
127         Assert.assertTrue("Output:" + result, result.contains("<p>Component value: page test2</p>"));
128         Assert.assertTrue("Output:" + result,result.contains("<p>Inline EL value: page test2</p>"));
129         
130         //System.out.println(fw);
131 
132         ComponentSupport.removeTransient(root);
133 
134         request.setAttribute("test", "test3.xml");
135 
136         facesContext.setViewRoot(facesContext.getApplication().getViewHandler()
137                 .createView(facesContext, "/test"));
138         root = facesContext.getViewRoot();
139 
140         vdl.buildView(facesContext, root, "test1.xml");
141 
142         fw = new FastWriter();
143         rw = facesContext.getResponseWriter();
144         rw = rw.cloneWithWriter(fw);
145         facesContext.setResponseWriter(rw);
146         root.encodeAll(facesContext);
147         rw.flush();
148         //System.out.println(fw);
149         
150         result = fw.toString();
151         
152         Assert.assertTrue("Output:" + result, result.contains("<p>Component value: page test3</p>"));
153         Assert.assertTrue("Output:" + result, result.contains("<p>Inline EL value: page test3</p>"));
154 
155     }
156     
157     @Test
158     public void testSimpleCompositionParam() throws Exception
159     {
160         UIViewRoot root = facesContext.getViewRoot();
161         vdl.buildView(facesContext, root, "simpleCompositionParam.xhtml");
162         
163         FastWriter fw = new FastWriter();
164         ResponseWriter rw = facesContext.getResponseWriter();
165         rw = rw.cloneWithWriter(fw);
166         facesContext.setResponseWriter(rw);
167         root.encodeAll(facesContext);
168         rw.flush();
169         
170         String result = fw.toString();
171         Assert.assertTrue("Output:" + result, result.contains("value1"));
172     }
173 
174     /**
175      * ui:param inside ui:decorate applies only to the content of the tag and the
176      * referenced template.
177      * 
178      * @throws Exception
179      */
180     @Test
181     public void testUIParamTemplateScope1() throws Exception
182     {
183         UIViewRoot root = facesContext.getViewRoot();
184         vdl.buildView(facesContext, root, "uiparamtemplatescope1.xhtml");
185         
186         FastWriter fw = new FastWriter();
187         ResponseWriter rw = facesContext.getResponseWriter();
188         rw = rw.cloneWithWriter(fw);
189         facesContext.setResponseWriter(rw);
190         root.encodeAll(facesContext);
191         rw.flush();
192         
193         String result = fw.toString();
194         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
195         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
196     }
197     
198     /**
199      * ui:param defined outside should not pass through ui:include, because it occurs
200      * on another template context.
201      * 
202      * @throws Exception
203      */
204     @Test
205     public void testUIParamTemplateScope2() throws Exception
206     {
207         UIViewRoot root = facesContext.getViewRoot();
208         vdl.buildView(facesContext, root, "uiparamtemplatescope2.xhtml");
209         
210         FastWriter fw = new FastWriter();
211         ResponseWriter rw = facesContext.getResponseWriter();
212         rw = rw.cloneWithWriter(fw);
213         facesContext.setResponseWriter(rw);
214         root.encodeAll(facesContext);
215         rw.flush();
216         
217         String result = fw.toString();
218         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
219     }
220     
221     /**
222      * ui:param inside ui:include applies only to the content of the tag and the
223      * referenced template.
224      * 
225      * @throws Exception
226      */
227     @Test
228     public void testUIParamTemplateScope3() throws Exception
229     {
230         UIViewRoot root = facesContext.getViewRoot();
231         vdl.buildView(facesContext, root, "uiparamtemplatescope3.xhtml");
232         
233         FastWriter fw = new FastWriter();
234         ResponseWriter rw = facesContext.getResponseWriter();
235         rw = rw.cloneWithWriter(fw);
236         facesContext.setResponseWriter(rw);
237         root.encodeAll(facesContext);
238         rw.flush();
239         
240         String result = fw.toString();
241         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
242         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
243     }
244     
245     /**
246      * ui:param inside ui:composition applies only to the content of the tag and the
247      * referenced template.
248      * 
249      * @throws Exception
250      */
251     @Test
252     public void testUIParamTemplateScope4() throws Exception
253     {
254         UIViewRoot root = facesContext.getViewRoot();
255         vdl.buildView(facesContext, root, "uiparamtemplatescope4.xhtml");
256         
257         FastWriter fw = new FastWriter();
258         ResponseWriter rw = facesContext.getResponseWriter();
259         rw = rw.cloneWithWriter(fw);
260         facesContext.setResponseWriter(rw);
261         root.encodeAll(facesContext);
262         rw.flush();
263         
264         String result = fw.toString();
265         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
266         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
267     }
268     
269     /**
270      * ui:param defined outside should only pass through ui:include if the param is declared.
271      * 
272      * @throws Exception
273      */
274     @Test
275     public void testUIParamTemplateScope5() throws Exception
276     {
277         UIViewRoot root = facesContext.getViewRoot();
278         vdl.buildView(facesContext, root, "uiparamtemplatescope5.xhtml");
279         
280         FastWriter fw = new FastWriter();
281         ResponseWriter rw = facesContext.getResponseWriter();
282         rw = rw.cloneWithWriter(fw);
283         facesContext.setResponseWriter(rw);
284         root.encodeAll(facesContext);
285         rw.flush();
286         
287         String result = fw.toString();
288         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
289         Assert.assertTrue("Output should contain 'right2Value'", result.contains("right2Value"));
290         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
291     }
292     
293     /**
294      * ui:param defined outside should not pass through composite components, to do
295      * that the composite component provide a clean attribute interface.
296      * 
297      * @throws Exception
298      */
299     @Test
300     public void testUIParamTemplateScope6() throws Exception
301     {
302         UIViewRoot root = facesContext.getViewRoot();
303         vdl.buildView(facesContext, root, "uiparamtemplatescope6.xhtml");
304         
305         FastWriter fw = new FastWriter();
306         ResponseWriter rw = facesContext.getResponseWriter();
307         rw = rw.cloneWithWriter(fw);
308         facesContext.setResponseWriter(rw);
309         root.encodeAll(facesContext);
310         rw.flush();
311         
312         String result = fw.toString();
313         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
314         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
315     }
316     
317     /**
318      * ui:param should pass through nested ui:decorate or ui:composition constructions, because it is the
319      * same template context. Additionally, ui:param declarations should follow the same ordering rules
320      * for ui:decorate or ui:composition
321      * 
322      * @throws Exception
323      */
324     @Test
325     public void testUIParamTemplateScope7() throws Exception
326     {
327         UIViewRoot root = facesContext.getViewRoot();
328         vdl.buildView(facesContext, root, "uiparamtemplatescope7.xhtml");
329         
330         FastWriter fw = new FastWriter();
331         ResponseWriter rw = facesContext.getResponseWriter();
332         rw = rw.cloneWithWriter(fw);
333         facesContext.setResponseWriter(rw);
334         root.encodeAll(facesContext);
335         rw.flush();
336         
337         String result = fw.toString();
338         Assert.assertTrue("Output should contain 'value1'", result.contains("value1"));
339         Assert.assertTrue("Output should contain 'value2'", result.contains("value2"));
340         Assert.assertTrue("Output should contain 'value3'", result.contains("value3"));
341         Assert.assertTrue("Output should contain 'value4'", result.contains("value4"));
342         Assert.assertTrue("Output should contain 'value5'", result.contains("value5"));
343         Assert.assertTrue("Output should contain 'valu1e5'", result.contains("valu1e5"));
344         Assert.assertTrue("Output should contain 'valu1e6'", result.contains("valu1e6"));
345         Assert.assertFalse("Output should not contain 'value6'", result.contains("value6"));
346     }
347 }