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  package org.apache.myfaces.view.facelets.tag.jstl.core;
20  
21  import javax.el.ExpressionFactory;
22  import javax.faces.application.ViewHandler;
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.context.ResponseWriter;
25  
26  import org.apache.myfaces.view.facelets.FaceletTestCase;
27  import org.apache.myfaces.view.facelets.util.FastWriter;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  public class CsetTestCase extends FaceletTestCase {
32  
33      
34      @Override
35      protected void setUpServletObjects() throws Exception
36      {
37          super.setUpServletObjects();
38          servletContext.addInitParameter(ViewHandler.FACELETS_SKIP_COMMENTS_PARAM_NAME, "true");
39          servletContext.addInitParameter(ViewHandler.FACELETS_LIBRARIES_PARAM_NAME, "/user.taglib.xml");
40      }
41      
42      @Override
43      protected ExpressionFactory createExpressionFactory()
44      {
45          // For this test we need the a real one, because the Mock does not
46          // handle VariableMapper stuff properly and ui:param logic will not work
47          return new org.apache.el.ExpressionFactoryImpl();
48      }
49      
50      /**
51       * c:set should not pass to pages referenced by ui:decorate
52       * 
53       * @throws Exception
54       */
55      @Test
56      public void testCsetPageScope1() throws Exception
57      {
58          UIViewRoot root = facesContext.getViewRoot();
59          vdl.buildView(facesContext, root, "csetpagescope1.xhtml");
60          
61          FastWriter fw = new FastWriter();
62          ResponseWriter rw = facesContext.getResponseWriter();
63          rw = rw.cloneWithWriter(fw);
64          facesContext.setResponseWriter(rw);
65          root.encodeAll(facesContext);
66          rw.flush();
67          
68          String result = fw.toString();
69          Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
70      }
71      
72      /**
73       * c:set should not pass to pages referenced by ui:composition
74       * 
75       * @throws Exception
76       */
77      @Test
78      public void testCsetPageScope2() throws Exception
79      {
80          UIViewRoot root = facesContext.getViewRoot();
81          vdl.buildView(facesContext, root, "csetpagescope2.xhtml");
82          
83          FastWriter fw = new FastWriter();
84          ResponseWriter rw = facesContext.getResponseWriter();
85          rw = rw.cloneWithWriter(fw);
86          facesContext.setResponseWriter(rw);
87          root.encodeAll(facesContext);
88          rw.flush();
89          
90          String result = fw.toString();
91          Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
92      }
93  
94      /**
95       * c:set should not pass to pages referenced by ui:include
96       * 
97       * - user tags
98       * 
99       * @throws Exception
100      */
101     @Test
102     public void testCsetPageScope3() throws Exception
103     {
104         UIViewRoot root = facesContext.getViewRoot();
105         vdl.buildView(facesContext, root, "csetpagescope3.xhtml");
106         
107         FastWriter fw = new FastWriter();
108         ResponseWriter rw = facesContext.getResponseWriter();
109         rw = rw.cloneWithWriter(fw);
110         facesContext.setResponseWriter(rw);
111         root.encodeAll(facesContext);
112         rw.flush();
113         
114         String result = fw.toString();
115         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
116     }
117 
118     /**
119      * c:set should not pass to pages referenced by user tags
120      * 
121      * @throws Exception
122      */
123     @Test
124     public void testCsetPageScope4() throws Exception
125     {
126         UIViewRoot root = facesContext.getViewRoot();
127         vdl.buildView(facesContext, root, "csetpagescope4.xhtml");
128         
129         FastWriter fw = new FastWriter();
130         ResponseWriter rw = facesContext.getResponseWriter();
131         rw = rw.cloneWithWriter(fw);
132         facesContext.setResponseWriter(rw);
133         root.encodeAll(facesContext);
134         rw.flush();
135         
136         String result = fw.toString();
137         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
138     }
139     
140     /**
141      * c:set should not pass to pages referenced by composite components
142      * 
143      * @throws Exception
144      */
145     @Test
146     public void testCsetPageScope5() throws Exception
147     {
148         UIViewRoot root = facesContext.getViewRoot();
149         vdl.buildView(facesContext, root, "csetpagescope5.xhtml");
150         
151         FastWriter fw = new FastWriter();
152         ResponseWriter rw = facesContext.getResponseWriter();
153         rw = rw.cloneWithWriter(fw);
154         facesContext.setResponseWriter(rw);
155         root.encodeAll(facesContext);
156         rw.flush();
157         
158         String result = fw.toString();
159         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
160     }
161 
162     /**
163      * c:set tags called outside ui:decorate should apply to definitions inside it,
164      * because c:set apply values to page scope.
165      * 
166      * @throws Exception
167      */
168     @Test
169     public void testCsetPageScope6() throws Exception
170     {
171         UIViewRoot root = facesContext.getViewRoot();
172         vdl.buildView(facesContext, root, "csetpagescope6.xhtml");
173         
174         FastWriter fw = new FastWriter();
175         ResponseWriter rw = facesContext.getResponseWriter();
176         rw = rw.cloneWithWriter(fw);
177         facesContext.setResponseWriter(rw);
178         root.encodeAll(facesContext);
179         rw.flush();
180         
181         String result = fw.toString();
182         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
183         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
184     }
185     
186     /**
187      * c:set tags defined before ui:decorate applies for expressions after that tag 
188      * 
189      * @throws Exception
190      */
191     @Test
192     public void testCsetPageScope7() throws Exception
193     {
194         UIViewRoot root = facesContext.getViewRoot();
195         vdl.buildView(facesContext, root, "csetpagescope7.xhtml");
196         
197         FastWriter fw = new FastWriter();
198         ResponseWriter rw = facesContext.getResponseWriter();
199         rw = rw.cloneWithWriter(fw);
200         facesContext.setResponseWriter(rw);
201         root.encodeAll(facesContext);
202         rw.flush();
203         
204         String result = fw.toString();
205         Assert.assertTrue("Output should contain 'rightValue'", result.contains("rightValue"));
206         Assert.assertFalse("Output should not contain 'doNotPrintValue'", result.contains("doNotPrintValue"));
207     }
208 }