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.Collection;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Locale;
25  import java.util.Map;
26  
27  import javax.faces.FacesException;
28  import javax.faces.application.Application;
29  import javax.faces.application.NavigationHandler;
30  import javax.faces.application.StateManager;
31  import javax.faces.application.ViewHandler;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  import javax.faces.convert.Converter;
35  import javax.faces.el.MethodBinding;
36  import javax.faces.el.PropertyResolver;
37  import javax.faces.el.ValueBinding;
38  import javax.faces.el.VariableResolver;
39  import javax.faces.event.ActionListener;
40  import javax.faces.render.RenderKitFactory;
41  import javax.faces.validator.Validator;
42  
43  /***
44   * <p>Mock implementation of <code>Application</code>.</p>
45   *
46   * $Id$
47   */
48  
49  public class MockApplication extends Application {
50  
51  
52      // ------------------------------------------------------------ Constructors
53  
54      /***
55       * <p>Construct a default instance.</p>
56       */
57      public MockApplication() {
58  
59          setActionListener(new MockActionListener());
60          components = new HashMap();
61          converters = new HashMap();
62          converters1 = new HashMap();
63          setDefaultLocale(Locale.getDefault());
64          setDefaultRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
65          setNavigationHandler(new MockNavigationHandler());
66          setPropertyResolver(new MockPropertyResolver());
67          setStateManager(new MockStateManager());
68          setSupportedLocales(new ArrayList());
69          validators = new HashMap();
70          setVariableResolver(new MockVariableResolver());
71          setViewHandler(new MockViewHandler());
72  
73          // Register the standard by-id converters
74          addConverter("javax.faces.BigDecimal", "javax.faces.convert.BigDecimalConverter");
75          addConverter("javax.faces.BigInteger", "javax.faces.convert.BigIntegerConverter");
76          addConverter("javax.faces.Boolean",    "javax.faces.convert.BooleanConverter");
77          addConverter("javax.faces.Byte",       "javax.faces.convert.ByteConverter");
78          addConverter("javax.faces.Character",  "javax.faces.convert.CharacterConverter");
79          addConverter("javax.faces.DateTime",   "javax.faces.convert.DateTimeConverter");
80          addConverter("javax.faces.Double",     "javax.faces.convert.DoubleConverter");
81          addConverter("javax.faces.Float",      "javax.faces.convert.FloatConverter");
82          addConverter("javax.faces.Integer",    "javax.faces.Convert.IntegerConverter");
83          addConverter("javax.faces.Long",       "javax.faces.convert.LongConverter");
84          addConverter("javax.faces.Number",     "javax.faces.convert.NumberConverter");
85          addConverter("javax.faces.Short",      "javax.faces.convert.ShortConverter");
86  
87          // Register the standard by-type converters
88          addConverter(Boolean.class,            "javax.faces.convert.BooleanConverter");
89          addConverter(Boolean.TYPE,             "javax.faces.convert.BooleanConverter");
90          addConverter(Byte.class,               "javax.faces.convert.ByteConverter");
91          addConverter(Byte.TYPE,                "javax.faces.convert.ByteConverter");
92          addConverter(Character.class,          "javax.faces.convert.CharacterConverter");
93          addConverter(Character.TYPE,           "javax.faces.convert.CharacterConverter");
94          addConverter(Double.class,             "javax.faces.convert.DoubleConverter");
95          addConverter(Double.TYPE,              "javax.faces.convert.DoubleConverter");
96          addConverter(Float.class,              "javax.faces.convert.FloatConverter");
97          addConverter(Float.TYPE,               "javax.faces.convert.FloatConverter");
98          addConverter(Integer.class,            "javax.faces.convert.IntegerConverter");
99          addConverter(Integer.TYPE,             "javax.faces.convert.IntegerConverter");
100         addConverter(Long.class,               "javax.faces.convert.LongConverter");
101         addConverter(Long.TYPE,                "javax.faces.convert.LongConverter");
102         addConverter(Short.class,              "javax.faces.convert.ShortConverter");
103         addConverter(Short.TYPE,               "javax.faces.convert.ShortConverter");
104 
105     }
106 
107 
108     // ----------------------------------------------------- Mock Object Methods
109 
110 
111     // ------------------------------------------------------ Instance Variables
112 
113 
114     private ActionListener actionListener = null;
115     private Map components = null;
116     private Map converters = null; // By id
117     private Map converters1 = null; // By type
118     private Locale defaultLocale = null;
119     private String defaultRenderKitId = null;
120     private String messageBundle = null;
121     private NavigationHandler navigationHandler = null;
122     private PropertyResolver propertyResolver = null;
123     private StateManager stateManager = null;
124     private Collection supportedLocales = null;
125     private Map validators = null;
126     private VariableResolver variableResolver = null;
127     private ViewHandler viewHandler = null;
128 
129 
130     // ----------------------------------------------------- Application Methods
131 
132 
133     /*** {@inheritDoc} */
134     public ActionListener getActionListener() {
135 
136         return this.actionListener;
137 
138     }
139 
140 
141     /*** {@inheritDoc} */
142     public void setActionListener(ActionListener actionListener) {
143         this.actionListener = actionListener;
144     }
145 
146 
147     /*** {@inheritDoc} */
148     public Locale getDefaultLocale() {
149 
150         return this.defaultLocale;
151 
152     }
153 
154     /*** {@inheritDoc} */
155     public void setDefaultLocale(Locale defaultLocale) {
156 
157         this.defaultLocale = defaultLocale;
158 
159     }
160 
161     /*** {@inheritDoc} */
162     public String getDefaultRenderKitId() {
163 
164         return this.defaultRenderKitId;
165 
166     }
167 
168     /*** {@inheritDoc} */
169     public void setDefaultRenderKitId(String defaultRenderKitId) {
170 
171         this.defaultRenderKitId = defaultRenderKitId;
172 
173     }
174 
175 
176     /*** {@inheritDoc} */
177     public String getMessageBundle() {
178 
179         return this.messageBundle;
180 
181     }
182 
183 
184     /*** {@inheritDoc} */
185     public void setMessageBundle(String messageBundle) {
186 
187         this.messageBundle = messageBundle;
188 
189     }
190 
191 
192     /*** {@inheritDoc} */
193     public NavigationHandler getNavigationHandler() {
194 
195         return this.navigationHandler;
196 
197     }
198 
199 
200     /*** {@inheritDoc} */
201     public void setNavigationHandler(NavigationHandler navigationHandler) {
202 
203         this.navigationHandler = navigationHandler;
204 
205     }
206 
207 
208     /*** {@inheritDoc} */
209     public PropertyResolver getPropertyResolver() {
210 
211         return this.propertyResolver;
212 
213     }
214 
215 
216     /*** {@inheritDoc} */
217     public void setPropertyResolver(PropertyResolver propertyResolver) {
218 
219         this.propertyResolver = propertyResolver;
220 
221     }
222 
223 
224     /*** {@inheritDoc} */
225     public StateManager getStateManager() {
226 
227         return this.stateManager;
228 
229     }
230 
231 
232     /*** {@inheritDoc} */
233     public void setStateManager(StateManager stateManager) {
234 
235         this.stateManager = stateManager;
236 
237     }
238 
239 
240     /*** {@inheritDoc} */
241     public Iterator getSupportedLocales() {
242 
243         return this.supportedLocales.iterator();
244 
245     }
246 
247 
248     /*** {@inheritDoc} */
249     public void setSupportedLocales(Collection supportedLocales) {
250 
251         this.supportedLocales = supportedLocales;
252 
253     }
254 
255 
256     /*** {@inheritDoc} */
257     public VariableResolver getVariableResolver() {
258 
259         return this.variableResolver;
260     }
261 
262 
263     /*** {@inheritDoc} */
264     public void setVariableResolver(VariableResolver variableResolver) {
265 
266         this.variableResolver = variableResolver;
267 
268     }
269 
270 
271     /*** {@inheritDoc} */
272     public ViewHandler getViewHandler() {
273 
274         return this.viewHandler;
275 
276     }
277 
278 
279     /*** {@inheritDoc} */
280     public void setViewHandler(ViewHandler viewHandler) {
281 
282         this.viewHandler = viewHandler;
283 
284     }
285 
286 
287     /*** {@inheritDoc} */
288     public void addComponent(String componentType, String componentClass) {
289 
290         components.put(componentType, componentClass);
291 
292     }
293 
294 
295     /*** {@inheritDoc} */
296     public UIComponent createComponent(String componentType) {
297 
298         if (componentType == null) {
299             throw new NullPointerException("Requested component type is null");
300         }
301         String componentClass = (String) components.get(componentType);
302         if (componentClass == null) {
303             throw new FacesException("No component class registered for component type '"
304                     + componentType + "'");
305         }
306         try {
307             Class clazz = Class.forName(componentClass);
308             return ((UIComponent) clazz.newInstance());
309         } catch (Exception e) {
310             throw new FacesException(e);
311         }
312 
313     }
314 
315 
316     /*** {@inheritDoc} */
317     public UIComponent createComponent(ValueBinding componentBinding,
318                                        FacesContext context,
319                                        String componentType)
320         throws FacesException {
321 
322         UIComponent component = null;
323         try {
324             component = (UIComponent) componentBinding.getValue(context);
325             if (component == null) {
326                 component = createComponent(componentType);
327                 componentBinding.setValue(context, component);
328             }
329 
330         } catch (Exception e) {
331             throw new FacesException(e);
332         }
333         return component;
334     }
335 
336 
337     /*** {@inheritDoc} */
338     public Iterator getComponentTypes() {
339 
340         return (components.keySet().iterator());
341 
342     }
343 
344 
345     /*** {@inheritDoc} */
346     public void addConverter(String converterId, String converterClass) {
347 
348         converters.put(converterId, converterClass);
349 
350     }
351 
352 
353     /*** {@inheritDoc} */
354     public void addConverter(Class targetClass, String converterClass) {
355 
356         converters1.put(targetClass, converterClass);
357 
358     }
359 
360 
361     /*** {@inheritDoc} */
362     public Converter createConverter(String converterId) {
363 
364         String converterClass = (String) converters.get(converterId);
365         if (converterClass == null) {
366             return null;
367         }
368         try {
369             Class clazz = Class.forName(converterClass);
370             return ((Converter) clazz.newInstance());
371         } catch (Exception e) {
372             throw new FacesException(e);
373         }
374 
375     }
376 
377 
378     /*** {@inheritDoc} */
379     public Converter createConverter(Class targetClass) {
380 
381         String converterClass = (String) converters1.get(targetClass);
382         if (converterClass == null) {
383             return null;
384         }
385         try {
386             Class clazz = Class.forName(converterClass);
387             return ((Converter) clazz.newInstance());
388         } catch (Exception e) {
389             throw new FacesException(e);
390         }
391 
392     }
393 
394 
395     /*** {@inheritDoc} */
396     public Iterator getConverterIds() {
397 
398         return (converters.keySet().iterator());
399 
400     }
401 
402 
403     /*** {@inheritDoc} */
404     public Iterator getConverterTypes() {
405 
406         return (converters1.keySet().iterator());
407 
408     }
409 
410 
411     /*** {@inheritDoc} */
412     public MethodBinding createMethodBinding(String ref, Class[] params) {
413 
414         if (ref == null) {
415             throw new NullPointerException();
416         } else {
417             return (new MockMethodBinding(this, ref, params));
418         }
419 
420     }
421 
422 
423     /*** {@inheritDoc} */
424     public ValueBinding createValueBinding(String ref) {
425 
426         if (ref == null) {
427             throw new NullPointerException();
428         } else {
429             return (new MockValueBinding(this, ref));
430         }
431 
432     }
433 
434 
435     /*** {@inheritDoc} */
436     public void addValidator(String validatorId, String validatorClass) {
437 
438         validators.put(validatorId, validatorClass);
439 
440     }
441 
442 
443     /*** {@inheritDoc} */
444     public Validator createValidator(String validatorId) {
445 
446         String validatorClass = (String) validators.get(validatorId);
447         try {
448             Class clazz = Class.forName(validatorClass);
449             return ((Validator) clazz.newInstance());
450         } catch (Exception e) {
451             throw new FacesException(e);
452         }
453 
454     }
455 
456 
457     /*** {@inheritDoc} */
458     public Iterator getValidatorIds() {
459         return (validators.keySet().iterator());
460     }
461 
462 
463 }