2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Map;
26  import java.util.ResourceBundle;
27  import javax.el.ArrayELResolver;
28  import javax.el.BeanELResolver;
29  import javax.el.CompositeELResolver;
30  import javax.el.ELContextListener;
31  import javax.el.ELException;
32  import javax.el.ELResolver;
33  import javax.el.ExpressionFactory;
34  import javax.el.ListELResolver;
35  import javax.el.MapELResolver;
36  import javax.el.ResourceBundleELResolver;
37  import javax.el.ValueExpression;
38  import javax.faces.FacesException;
39  import javax.faces.component.UIComponent;
40  import javax.faces.component.UIViewRoot;
41  import javax.faces.context.FacesContext;
42  import org.apache.shale.test.el.FacesImplicitObjectELResolver;
43  import org.apache.shale.test.el.FacesPropertyResolverChainWrapper;
44  import org.apache.shale.test.el.FacesResourceBundleELResolver;
45  import org.apache.shale.test.el.FacesScopedAttributeELResolver;
46  import org.apache.shale.test.el.FacesVariableResolverChainWrapper;
47  import org.apache.shale.test.el.MockExpressionFactory;
48  
49  /***
50   * <p>Mock implementation of <code>ExternalContext</code> that includes the semantics
51   * added by JavaServer Faces 1.2.</p>
52   *
53   * $Id: MockApplication12.java 503872 2007-02-05 21:10:06Z rahul $
54   *
55   * @since 1.0.4
56   */
57  public class MockApplication12 extends MockApplication {
58      
59  
60      // ------------------------------------------------------------ Constructors
61  
62  
63      /***
64       * <p>Construct a default instance.</p>
65       */
66      public MockApplication12() {
67  
68          super();
69  
70          // Configure our expression factory and EL resolvers
71          expressionFactory = new MockExpressionFactory();
72  
73      }
74  
75  
76      // ------------------------------------------------------ Instance Variables
77  
78  
79      /***
80       * <p>A list of resource bundles configured for this application.</p>
81       */
82      private Map bundles = new HashMap();
83  
84  
85      /***
86       * <p>The set of configured ELContextListener instances.</p>
87       */
88      private List elContextListeners = new ArrayList();
89  
90  
91      /***
92       * <p>Expression factory for this instance.</p>
93       */
94      private ExpressionFactory expressionFactory = null;
95  
96  
97      /***
98       * <p>The configured composite resolver to be returned by <code>getELResolver()</code>.
99       * This value is lazily instantiated.</p>
100      */
101     private ELResolver resolver = null;
102 
103 
104     /***
105      * <p>The set of ELResolver instances configured on this instance.</p>
106      */
107     private List resolvers = new ArrayList();
108 
109 
110     // ----------------------------------------------------- Mock Object Methods
111 
112 
113     /***
114      * <p>Add the specified resource bundle to those associated with
115      * this application.</p>
116      *
117      * @param name Name under which to add this resource bundle
118      * @param bundle ResourceBundle to add
119      */
120     public void addResourceBundle(String name, ResourceBundle bundle) {
121         bundles.put(name, bundle);
122     }
123 
124 
125     /***
126      * <p>Return a <code>Map</code> of the resource bundles configured
127      * for this application, keyed by name.</p>
128      */
129     public Map getResourceBundles() {
130         return bundles;
131     }
132 
133 
134     // ----------------------------------------------------- Application Methods
135 
136 
137     /*** {@inheritDoc} */
138     public void addELContextListener(ELContextListener listener) {
139 
140         elContextListeners.add(listener);
141 
142     }
143 
144 
145     /*** {@inheritDoc} */
146     public void addELResolver(ELResolver resolver) {
147 
148         // Simulate the restriction that you cannot add resolvers after
149         // the first request has been processed.
150         if (this.resolver != null) {
151             throw new IllegalStateException("Cannot add resolvers now");
152         }
153 
154         resolvers.add(resolver);
155 
156     }
157 
158 
159     /*** {@inheritDoc} */
160     public UIComponent createComponent(ValueExpression expression,
161                                        FacesContext context,
162                                        String componentType) {
163 
164         UIComponent component = null;
165         try {
166             component = (UIComponent) expression.getValue(context.getELContext());
167             if (component == null) {
168                 component = createComponent(componentType);
169                 expression.setValue(context.getELContext(), component);
170             }
171 
172         } catch (Exception e) {
173             throw new FacesException(e);
174         }
175         return component;
176 
177     }
178 
179 
180     /*** {@inheritDoc} */
181     public Object evaluateExpressionGet(FacesContext context,
182                                         String expression,
183                                         Class expectedType) throws ELException {
184 
185         ValueExpression ve = getExpressionFactory().createValueExpression
186           (context.getELContext(), expression, expectedType);
187         return ve.getValue(context.getELContext());
188 
189     }
190 
191 
192     /*** {@inheritDoc} */
193     public ELContextListener[] getELContextListeners() {
194 
195         return (ELContextListener[])
196           elContextListeners.toArray(new ELContextListener[elContextListeners.size()]);
197 
198     }
199 
200 
201     /*** {@inheritDoc} */
202     public ELResolver getELResolver() {
203 
204         if (resolver == null) {
205 
206             // Configure a default ELResolver per Section 5.6.2 of JSF 1.2
207             CompositeELResolver composite = new CompositeELResolver();
208 
209             composite.add(new FacesImplicitObjectELResolver());
210 
211             CompositeELResolver nested = new CompositeELResolver();
212             // FIXME - nested.add() "ELResolvers from application configuration resources"
213             nested.add(new FacesVariableResolverChainWrapper());
214             nested.add(new FacesPropertyResolverChainWrapper());
215             Iterator items = resolvers.iterator();
216             while (items.hasNext()) {
217                 nested.add((ELResolver) items.next());
218             }
219             composite.add(nested);
220 
221             // composite.add(new faces.ManagedBeanELResolver()); // FIXME
222             composite.add(new ResourceBundleELResolver());
223             composite.add(new FacesResourceBundleELResolver());
224             composite.add(new MapELResolver());
225             composite.add(new ListELResolver());
226             composite.add(new ArrayELResolver());
227             composite.add(new BeanELResolver());
228             composite.add(new FacesScopedAttributeELResolver());
229 
230             // Make the resolver we have configured the application wide one
231             resolver = composite;
232 
233         }
234         return resolver;
235 
236     }
237 
238 
239     /*** {@inheritDoc} */
240     public ExpressionFactory getExpressionFactory() {
241 
242         return this.expressionFactory;
243 
244     }
245 
246 
247     /*** {@inheritDoc} */
248     public ResourceBundle getResourceBundle(FacesContext context, String name) {
249 
250         if ((context == null) || (name == null)) {
251             throw new NullPointerException();
252         }
253         Locale locale = null;
254         UIViewRoot viewRoot = context.getViewRoot();
255         if (viewRoot != null) {
256             locale = viewRoot.getLocale();
257         }
258         if (locale == null) {
259             locale = Locale.getDefault();
260         }
261         return ResourceBundle.getBundle(name, locale);
262 
263     }
264 
265 
266     /*** {@inheritDoc} */
267     public void removeELContextListener(ELContextListener listener) {
268 
269         elContextListeners.remove(listener);
270 
271     }
272 
273 
274 }