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.el;
20  
21  import java.io.StringWriter;
22  import javax.el.ExpressionFactory;
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIViewRoot;
25  import org.apache.myfaces.test.mock.MockResponseWriter;
26  import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
27  import org.apache.myfaces.view.facelets.FaceletTestCase;
28  import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  /**
33   *
34   * @author lu4242
35   */
36  public class CacheELExpressionsTestCase extends FaceletTestCase
37  {
38      @Override
39      protected ExpressionFactory createExpressionFactory()
40      {
41          return new org.apache.el.ExpressionFactoryImpl();
42      }
43      
44      @Test
45      public void testUIParamCaching1() throws Exception
46      {
47          // Works in "none", "allowCset", and "strict" but it does not in always.
48          servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
49                  ELExpressionCacheMode.allowCset.toString());
50          
51          UIViewRoot root = facesContext.getViewRoot();
52          vdl.buildView(facesContext, root, "uiparamcache1.xhtml");
53  
54          StringWriter sw = new StringWriter();
55          MockResponseWriter mrw = new MockResponseWriter(sw);
56          facesContext.setResponseWriter(mrw);
57          
58          root.encodeAll(facesContext);
59          sw.flush();
60          Assert.assertTrue(sw.toString().contains("ALFA"));
61          Assert.assertTrue(sw.toString().contains("BETA"));
62          Assert.assertTrue(sw.toString().contains("GAMMA"));
63          Assert.assertTrue(sw.toString().contains("OMEGA"));
64      }
65      
66      @Test
67      public void testUIParamCaching2() throws Exception
68      {
69          // Works in "none", "allowCset", and "strict" but it does not in always.
70          servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS, 
71                  ELExpressionCacheMode.strict.toString());
72          
73          UIViewRoot root = facesContext.getViewRoot();
74          vdl.buildView(facesContext, root, "uiparamcache1.xhtml");
75  
76          StringWriter sw = new StringWriter();
77          MockResponseWriter mrw = new MockResponseWriter(sw);
78          facesContext.setResponseWriter(mrw);
79          
80          root.encodeAll(facesContext);
81          sw.flush();
82          Assert.assertTrue(sw.toString().contains("ALFA"));
83          Assert.assertTrue(sw.toString().contains("BETA"));
84          Assert.assertTrue(sw.toString().contains("GAMMA"));
85          Assert.assertTrue(sw.toString().contains("OMEGA"));
86      }
87  
88  }