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.pool;
20  
21  import javax.faces.application.ProjectStage;
22  import javax.faces.application.StateManager;
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.event.PhaseId;
26  import javax.faces.render.RenderKitFactory;
27  import junit.framework.Assert;
28  import org.apache.myfaces.shared.config.MyfacesConfig;
29  import org.apache.myfaces.view.facelets.FaceletTestCase;
30  import org.apache.myfaces.view.facelets.ViewPoolProcessor;
31  import org.junit.Test;
32  
33  /**
34   *
35   * @author lu4242
36   */
37  public class ViewPoolFaceletsTestCase extends FaceletTestCase
38  {
39      
40      @Override
41      protected void setUpServletObjects() throws Exception
42      {
43          super.setUpServletObjects();
44          servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, "true");
45          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
46          servletContext.addInitParameter("javax.faces.FACELETS_LIBRARIES", "/test-facelet.taglib.xml");
47          //servletContext.addInitParameter(ViewPoolProcessor.INIT_PARAM_VIEW_POOL_ENABLED, "true");
48          servletContext.addInitParameter("org.apache.myfaces.CACHE_EL_EXPRESSIONS", "alwaysRecompile");
49          servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, "Production");
50      }
51  
52      @Override
53      public void setUp() throws Exception
54      {
55          super.setUp();
56          ViewPoolProcessor.initialize(facesContext);
57      }
58  
59      @Override
60      protected void setupComponents() throws Exception
61      {
62          super.setupComponents();
63          application.addComponent(UISimpleComponentA.COMPONENT_TYPE, UISimpleComponentA.class.getName());
64      }
65  
66      /**
67       * Check remove component resource added using h:outputScript
68       * 
69       * @throws Exception 
70       */
71      @Test
72      public void testDynPageResourceCleanup1() throws Exception
73      {
74          facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
75          ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
76          
77          // Store structure for state checkA=false
78          facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
79          UIViewRoot root = facesContext.getViewRoot();
80          vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
81          processor.storeViewStructureMetadata(facesContext, root);
82  
83          // Store structure for state checkA=true
84          facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.TRUE);
85          root = new UIViewRoot();
86          root.setViewId("/test");
87          root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
88          facesContext.setViewRoot(root);
89          vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
90          
91          processor.storeViewStructureMetadata(facesContext, root);
92          
93          // Try change a view with state checkA=true to checkA=false
94          facesContext.getAttributes().remove(root); //change set filled view
95          facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
96          vdl.buildView(facesContext, root, "dynPageResourceCleanup1.xhtml");
97          
98          UIComponent headerFacet = root.getFacet("head");
99          int oldCount = headerFacet.getChildCount();
100         // the resource should still be there
101         //Assert.assertEquals(1, oldCount);
102         // The code in MYFACES-3659 reset the component after refresh
103         Assert.assertEquals(0, oldCount);
104 
105         // Clear the view and synchronize resources
106         ViewStructureMetadata metadata = processor.retrieveViewStructureMetadata(facesContext, root);
107         Assert.assertNotNull(metadata);
108         processor.clearTransientAndNonFaceletComponentsForDynamicView(facesContext, root, metadata);
109         
110         // the resource added by state checkA=true should not be there
111         Assert.assertEquals(0, headerFacet.getChildCount());
112     }
113     
114     /**
115      * Check remove component resource added using @ResourceDependency
116      * 
117      * @throws Exception 
118      */
119     @Test
120     public void testDynPageResourceCleanup2() throws Exception
121     {
122         facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
123         ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
124         
125         // Store structure for state checkA=false
126         facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
127         UIViewRoot root = facesContext.getViewRoot();
128         vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
129         processor.storeViewStructureMetadata(facesContext, root);
130 
131         // Store structure for state checkA=true
132         facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.TRUE);
133         root = new UIViewRoot();
134         root.setViewId("/test");
135         root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
136         facesContext.setViewRoot(root);
137         vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
138         
139         processor.storeViewStructureMetadata(facesContext, root);
140         
141         // Try change a view with state checkA=true to checkA=false
142         facesContext.getAttributes().remove(root); //change set filled view
143         facesContext.getExternalContext().getRequestMap().put("checkA", Boolean.FALSE);
144         vdl.buildView(facesContext, root, "dynPageResourceCleanup2.xhtml");
145         
146         // the resource should still be there
147         UIComponent headerFacet = root.getFacet("head");
148         int oldCount = headerFacet.getChildCount();
149         Assert.assertEquals(1, oldCount);
150 
151         // Clear the view and synchronize resources
152         ViewStructureMetadata metadata = processor.retrieveViewStructureMetadata(facesContext, root);
153         Assert.assertNotNull(metadata);
154         processor.clearTransientAndNonFaceletComponentsForDynamicView(facesContext, root, metadata);
155         
156         // the resource added by state checkA=true should not be there
157         Assert.assertEquals(0, headerFacet.getChildCount());
158     }
159     
160 }