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