View Javadoc

1   /*
2    * Copyright 2013 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.myfaces.view.facelets.el;
17  
18  import java.io.StringWriter;
19  import javax.el.ExpressionFactory;
20  import javax.faces.component.UIViewRoot;
21  import org.apache.myfaces.test.mock.MockResponseWriter;
22  import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
23  import org.apache.myfaces.view.facelets.FaceletTestCase;
24  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
25  import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   *
31   * @author lu4242
32   */
33  public class CacheELExpressionsAlwaysRecompileTestCase extends FaceletTestCase
34  {
35      @Override
36      protected void setUpServletObjects() throws Exception
37      {
38          super.setUpServletObjects();
39          servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS,
40              ELExpressionCacheMode.alwaysRecompile.toString());
41          servletContext.addInitParameter(FaceletViewDeclarationLanguage.PARAM_SKIP_COMMENTS, "true");
42          servletContext.addInitParameter(FaceletViewDeclarationLanguage.PARAM_LIBRARIES, "/user.taglib.xml");
43      }
44      
45      @Override
46      protected ExpressionFactory createExpressionFactory()
47      {
48          return new org.apache.el.ExpressionFactoryImpl();
49      }
50      
51      @Test
52      public void testUIParamCaching1() throws Exception
53      {
54          UIViewRoot root = facesContext.getViewRoot();
55          vdl.buildView(facesContext, root, "uiparamcache1.xhtml");
56  
57          StringWriter sw = new StringWriter();
58          MockResponseWriter mrw = new MockResponseWriter(sw);
59          facesContext.setResponseWriter(mrw);
60          
61          root.encodeAll(facesContext);
62          sw.flush();
63          Assert.assertTrue(sw.toString().contains("ALFA"));
64          Assert.assertTrue(sw.toString().contains("BETA"));
65          Assert.assertTrue(sw.toString().contains("GAMMA"));
66          Assert.assertTrue(sw.toString().contains("OMEGA"));
67      }
68      
69      @Test
70      public void testUserTagCaching1() throws Exception
71      {
72          UIViewRoot root = facesContext.getViewRoot();
73          vdl.buildView(facesContext, root, "usertagtest1.xhtml");
74  
75          StringWriter sw = new StringWriter();
76          MockResponseWriter mrw = new MockResponseWriter(sw);
77          facesContext.setResponseWriter(mrw);
78          
79          root.encodeAll(facesContext);
80          sw.flush();
81          Assert.assertTrue(sw.toString().contains("ALFA"));
82          Assert.assertTrue(sw.toString().contains("BETA"));
83          Assert.assertTrue(sw.toString().contains("GAMMA"));
84          Assert.assertTrue(sw.toString().contains("OMEGA"));
85      }
86  
87      @Test
88      public void testUIParamIncludeCaching1() throws Exception
89      {
90          UIViewRoot root = facesContext.getViewRoot();
91          vdl.buildView(facesContext, root, "includetagtest1.xhtml");
92  
93          StringWriter sw = new StringWriter();
94          MockResponseWriter mrw = new MockResponseWriter(sw);
95          facesContext.setResponseWriter(mrw);
96          
97          root.encodeAll(facesContext);
98          sw.flush();
99          Assert.assertTrue(sw.toString().contains("ALFA"));
100         Assert.assertTrue(sw.toString().contains("BETA"));
101         Assert.assertTrue(sw.toString().contains("GAMMA"));
102         Assert.assertTrue(sw.toString().contains("OMEGA"));
103     }
104 }