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.config;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.faces.FacesException;
25  import javax.faces.application.ProjectStage;
26  
27  import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
28  import org.apache.myfaces.config.impl.digester.elements.ManagedProperty;
29  import org.apache.myfaces.el.unified.resolver.ManagedBeanResolver;
30  import org.apache.myfaces.test.mock.MockApplication20;
31  import org.apache.myfaces.test.base.AbstractJsfTestCase;
32  import org.apache.myfaces.test.el.MockValueExpression;
33  
34  /**
35   * Class used to test ManagedBeanBuilder
36   * @author Jakob Korherr (latest modification by $Author: bommel $)
37   */
38  public class ManagedBeanBuilderTest extends AbstractJsfTestCase
39  {
40  
41      /**
42       * A managed bean used in the test cases
43       * @author Jakob Korherr
44       */
45      public static class TestBean {
46          
47          private Map<Object, Object> scope;
48          private TestBean anotherBean;
49  
50          public Map<Object, Object> getScope()
51          {
52              if (scope == null)
53              {
54                  scope = new HashMap<Object, Object>();
55              }
56              return scope;
57          }
58  
59          public void setScope(Map<Object, Object> scope)
60          {
61              this.scope = scope;
62          }
63  
64          public TestBean getAnotherBean()
65          {
66              return anotherBean;
67          }
68  
69          public void setAnotherBean(TestBean anotherBean)
70          {
71              this.anotherBean = anotherBean;
72          }
73          
74      }
75      
76      private RuntimeConfig runtimeConfig;
77      
78      public ManagedBeanBuilderTest(String name)
79      {
80          super(name);
81      }
82      
83      @Override
84      protected void setUp() throws Exception
85      {
86          super.setUp();
87          
88          // override MockApplication20 to get a ProjectStage
89          application = new MockApplication20() {
90  
91              @Override
92              public ProjectStage getProjectStage()
93              {
94                  return ProjectStage.Development;
95              }
96              
97          };
98          // add the ManagedBeanResolver as a ELResolver
99          ManagedBeanResolver resolver = new ManagedBeanResolver();
100         application.addELResolver(resolver);
101         facesContext.setApplication(application);
102         
103         runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
104     }
105     
106     @Override
107     protected void tearDown() throws Exception
108     {
109         runtimeConfig = null;
110         
111         super.tearDown();
112     }
113 
114     
115     /**
116      * Tests, if the ManagedBeanBuilder checks that no property of the managed bean
117      * references to a scope with a potentially shorter lifetime.
118      * E.g. a managed bean of scope session is only allowed to reference an object in
119      * the session, the application and the none scope.
120      */
121     public void testIsInValidScope()
122     {
123         // create sessionBean referencing requestBean
124         ManagedBean sessionBean = new ManagedBean();
125         sessionBean.setBeanClass(TestBean.class.getName());
126         sessionBean.setName("sessionBean");
127         sessionBean.setScope("session");
128         ManagedProperty anotherBeanProperty = new ManagedProperty();
129         anotherBeanProperty.setPropertyName("anotherBean");
130         anotherBeanProperty.setValue("#{requestBean}");
131         sessionBean.addProperty(anotherBeanProperty);
132         runtimeConfig.addManagedBean("sessionBean", sessionBean);
133         
134         // create requestBean
135         ManagedBean requestBean = new ManagedBean();
136         requestBean.setBeanClass(TestBean.class.getName());
137         requestBean.setName("requestBean");
138         requestBean.setScope("request");
139         runtimeConfig.addManagedBean("requestBean", requestBean);
140         
141         try
142         {
143             new MockValueExpression("#{sessionBean}", TestBean.class).getValue(facesContext.getELContext());
144         }
145         catch (FacesException e)
146         {
147             // success --> the ManagedBeanBuilder discovered the reference to a shorter lifetime
148             return;
149         }
150         fail();
151     }
152     
153     /**
154      * Tests the same as testIsInValidScope, but this time the managed bean
155      * has a custom scope.
156      * The spec says, that if a managed bean has a custom scope, the runtime 
157      * is not required to check the references. However, MyFaces checks the
158      * references if the ProjectStage is not Production. 
159      */
160     public void testIsInValidScopeWithCustomScopes()
161     {
162         // create scopeBean
163         ManagedBean scopeBean = new ManagedBean();
164         scopeBean.setBeanClass(TestBean.class.getName());
165         scopeBean.setName("scopeBean");
166         scopeBean.setScope("session");
167         runtimeConfig.addManagedBean("scopeBean", scopeBean);
168         
169         // create sessionBean referencing requestBean
170         ManagedBean sessionBean = new ManagedBean();
171         sessionBean.setBeanClass(TestBean.class.getName());
172         sessionBean.setName("sessionBean");
173         sessionBean.setScope("#{scopeBean.scope}");
174         ManagedProperty anotherBeanProperty = new ManagedProperty();
175         anotherBeanProperty.setPropertyName("anotherBean");
176         anotherBeanProperty.setValue("#{requestBean}");
177         sessionBean.addProperty(anotherBeanProperty);
178         runtimeConfig.addManagedBean("sessionBean", sessionBean);
179         
180         // create requestBean
181         ManagedBean requestBean = new ManagedBean();
182         requestBean.setBeanClass(TestBean.class.getName());
183         requestBean.setName("requestBean");
184         requestBean.setScope("request");
185         runtimeConfig.addManagedBean("requestBean", requestBean);
186         
187         try
188         {
189             new MockValueExpression("#{sessionBean}", TestBean.class).getValue(facesContext.getELContext());
190         }
191         catch (FacesException e)
192         {
193             // success --> the ManagedBeanBuilder discovered the reference to a shorter lifetime
194             return;
195         }
196         fail();
197     }
198     
199     /**
200      * Tests, if the ManagedBeanBuilder checks that no property of the managed bean
201      * references to a scope with a potentially shorter lifetime.
202      * E.g. a managed bean of scope session is only allowed to reference an object in
203      * the session, the application and the none scope.
204      * This test is to test the view scope, introduced in jsf 2.0.
205      */
206     public void testIsInValidScopeViewScope()
207     {
208         // create viewBean referencing requestBean
209         ManagedBean viewBean = new ManagedBean();
210         viewBean.setBeanClass(TestBean.class.getName());
211         viewBean.setName("viewBean");
212         viewBean.setScope("view");
213         ManagedProperty anotherBeanProperty = new ManagedProperty();
214         anotherBeanProperty.setPropertyName("anotherBean");
215         anotherBeanProperty.setValue("#{requestBean}");
216         viewBean.addProperty(anotherBeanProperty);
217         runtimeConfig.addManagedBean("viewBean", viewBean);
218         
219         // create requestBean
220         ManagedBean requestBean = new ManagedBean();
221         requestBean.setBeanClass(TestBean.class.getName());
222         requestBean.setName("requestBean");
223         requestBean.setScope("request");
224         runtimeConfig.addManagedBean("requestBean", requestBean);
225         
226         try
227         {
228             new MockValueExpression("#{viewBean}", TestBean.class).getValue(facesContext.getELContext());
229         }
230         catch (FacesException e)
231         {
232             // success --> the ManagedBeanBuilder discovered the reference to a shorter lifetime
233             return;
234         }
235         fail();
236     }
237 
238 }