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 java.util.Map;
22  
23  import javax.faces.application.StateManager;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.event.PhaseId;
27  
28  import org.apache.myfaces.shared.config.MyfacesConfig;
29  import org.apache.myfaces.view.facelets.FaceletTestCase;
30  import org.apache.myfaces.view.facelets.bean.Employee;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  public class CifTestCase extends FaceletTestCase
35  {
36  
37      protected void setUpServletObjects() throws Exception
38      {
39          super.setUpServletObjects();
40          
41          servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME,
42                  "true");
43          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS,"true");
44      }
45      
46      @Test
47      public void testIf1() throws Exception
48      {
49          facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
50          
51          Map session = facesContext.getExternalContext().getSessionMap();
52          Employee e = new Employee();
53          session.put("employee", e);
54  
55          UIViewRoot root = facesContext.getViewRoot();
56  
57          // make sure the form is there
58          e.setManagement(true);
59          vdl.buildView(facesContext, root,"if2.xhtml");
60          UIComponent c = root.findComponent("form");
61          Assert.assertNotNull("form is null", c);
62          c = root.findComponent("start");
63          Assert.assertNotNull("start is null", c);
64          c = root.findComponent("end");
65          Assert.assertNotNull("end is null", c);
66                 
67          
68          // now make sure it isn't
69          e.setManagement(false);
70          
71          //facesContext.setViewRoot(facesContext.getApplication().getViewHandler()
72          //        .createView(facesContext, "/test"));
73          root = facesContext.getViewRoot();
74          vdl.buildView(facesContext, root,"if2.xhtml");
75          c = root.findComponent("form");
76          Assert.assertNull("form is not null", c);
77          // start and end are from if2.xhtml, so they shouldn't be here!
78          c = root.findComponent("start");
79          Assert.assertNotNull("start is null", c);
80          c = root.findComponent("end");
81          Assert.assertNotNull("end is null", c);
82          facesContext.getAttributes().remove(root);
83  
84      }
85      
86      @Test
87      public void testIf2() throws Exception
88      {
89          facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
90          
91          Map session = facesContext.getExternalContext().getSessionMap();
92          Employee e = new Employee();
93          session.put("employee", e);
94  
95          UIViewRoot root = facesContext.getViewRoot();
96  
97          // make sure the form is there
98          e.setManagement(false);
99          vdl.buildView(facesContext, root,"if2.xhtml");
100         UIComponent c = root.findComponent("form");
101         Assert.assertNull("form is not null", c);
102         c = root.findComponent("start");
103         Assert.assertNotNull("start is null", c);
104         c = root.findComponent("end");
105         Assert.assertNotNull("end is null", c);
106         //facesContext.getAttributes().remove(root);
107         
108         //rebuild if.xml but with form present now
109         e.setManagement(true);
110         root = facesContext.getViewRoot();
111         vdl.buildView(facesContext, root,"if2.xhtml");
112         c = root.findComponent("form");
113         Assert.assertNotNull("form is null", c);
114         // start and end shouldn't be in the component tree
115         c = root.findComponent("start");
116         Assert.assertNotNull("start is null", c);
117         c = root.findComponent("end");
118         Assert.assertNotNull("start is null", c);
119     }    
120     
121     @Test
122     public void testIf3() throws Exception
123     {
124         facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
125         
126         Map session = facesContext.getExternalContext().getSessionMap();
127         Employee e = new Employee();
128         session.put("employee", e);
129 
130         UIViewRoot root = facesContext.getViewRoot();
131 
132         // make sure the form is there
133         e.setManagement(true);
134         vdl.buildView(facesContext, root,"if3.xhtml");
135         UIComponent c = root.findComponent("form");
136         Assert.assertNotNull("form is null", c);
137         Assert.assertNotNull(c.getFacet("header"));
138         c = root.findComponent("start");
139         Assert.assertNotNull("start is null", c);
140         c = root.findComponent("end");
141         Assert.assertNotNull("end is null", c);
142         //facesContext.getAttributes().remove(root);
143         
144         //rebuild if.xml but with form present now
145         e.setManagement(false);
146         root = facesContext.getViewRoot();
147         vdl.buildView(facesContext, root,"if3.xhtml");
148         c = root.findComponent("form");
149         Assert.assertNotNull("form is null", c);
150         Assert.assertNull(c.getFacet("header"));
151         // start and end shouldn't be in the component tree
152         c = root.findComponent("start");
153         Assert.assertNotNull("start is null", c);
154         c = root.findComponent("end");
155         Assert.assertNotNull("start is null", c);
156     }
157 }