1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class MockApplication extends Application { |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 18 | public MockApplication() { |
58 | |
|
59 | 18 | setActionListener(new MockActionListener()); |
60 | 18 | components = new HashMap(); |
61 | 18 | converters = new HashMap(); |
62 | 18 | converters1 = new HashMap(); |
63 | 18 | setDefaultLocale(Locale.getDefault()); |
64 | 18 | setDefaultRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); |
65 | 18 | setNavigationHandler(new MockNavigationHandler()); |
66 | 18 | setPropertyResolver(new MockPropertyResolver()); |
67 | 18 | setStateManager(new MockStateManager()); |
68 | 18 | setSupportedLocales(new ArrayList()); |
69 | 18 | validators = new HashMap(); |
70 | 18 | setVariableResolver(new MockVariableResolver()); |
71 | 18 | setViewHandler(new MockViewHandler()); |
72 | |
|
73 | |
|
74 | 18 | addConverter("javax.faces.BigDecimal", "javax.faces.convert.BigDecimalConverter"); |
75 | 18 | addConverter("javax.faces.BigInteger", "javax.faces.convert.BigIntegerConverter"); |
76 | 18 | addConverter("javax.faces.Boolean", "javax.faces.convert.BooleanConverter"); |
77 | 18 | addConverter("javax.faces.Byte", "javax.faces.convert.ByteConverter"); |
78 | 18 | addConverter("javax.faces.Character", "javax.faces.convert.CharacterConverter"); |
79 | 18 | addConverter("javax.faces.DateTime", "javax.faces.convert.DateTimeConverter"); |
80 | 18 | addConverter("javax.faces.Double", "javax.faces.convert.DoubleConverter"); |
81 | 18 | addConverter("javax.faces.Float", "javax.faces.convert.FloatConverter"); |
82 | 18 | addConverter("javax.faces.Integer", "javax.faces.Convert.IntegerConverter"); |
83 | 18 | addConverter("javax.faces.Long", "javax.faces.convert.LongConverter"); |
84 | 18 | addConverter("javax.faces.Number", "javax.faces.convert.NumberConverter"); |
85 | 18 | addConverter("javax.faces.Short", "javax.faces.convert.ShortConverter"); |
86 | |
|
87 | |
|
88 | 26 | addConverter(Boolean.class, "javax.faces.convert.BooleanConverter"); |
89 | 18 | addConverter(Boolean.TYPE, "javax.faces.convert.BooleanConverter"); |
90 | 18 | addConverter(Byte.class, "javax.faces.convert.ByteConverter"); |
91 | 18 | addConverter(Byte.TYPE, "javax.faces.convert.ByteConverter"); |
92 | 18 | addConverter(Character.class, "javax.faces.convert.CharacterConverter"); |
93 | 18 | addConverter(Character.TYPE, "javax.faces.convert.CharacterConverter"); |
94 | 18 | addConverter(Double.class, "javax.faces.convert.DoubleConverter"); |
95 | 18 | addConverter(Double.TYPE, "javax.faces.convert.DoubleConverter"); |
96 | 18 | addConverter(Float.class, "javax.faces.convert.FloatConverter"); |
97 | 18 | addConverter(Float.TYPE, "javax.faces.convert.FloatConverter"); |
98 | 18 | addConverter(Integer.class, "javax.faces.convert.IntegerConverter"); |
99 | 18 | addConverter(Integer.TYPE, "javax.faces.convert.IntegerConverter"); |
100 | 18 | addConverter(Long.class, "javax.faces.convert.LongConverter"); |
101 | 18 | addConverter(Long.TYPE, "javax.faces.convert.LongConverter"); |
102 | 18 | addConverter(Short.class, "javax.faces.convert.ShortConverter"); |
103 | 18 | addConverter(Short.TYPE, "javax.faces.convert.ShortConverter"); |
104 | |
|
105 | 18 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | 18 | private ActionListener actionListener = null; |
115 | 18 | private Map components = null; |
116 | 18 | private Map converters = null; |
117 | 18 | private Map converters1 = null; |
118 | 18 | private Locale defaultLocale = null; |
119 | 18 | private String defaultRenderKitId = null; |
120 | 18 | private String messageBundle = null; |
121 | 18 | private NavigationHandler navigationHandler = null; |
122 | 18 | private PropertyResolver propertyResolver = null; |
123 | 18 | private StateManager stateManager = null; |
124 | 18 | private Collection supportedLocales = null; |
125 | 18 | private Map validators = null; |
126 | 18 | private VariableResolver variableResolver = null; |
127 | 18 | private ViewHandler viewHandler = null; |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public ActionListener getActionListener() { |
135 | |
|
136 | |
return this.actionListener; |
137 | |
|
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
public void setActionListener(ActionListener actionListener) { |
143 | 18 | this.actionListener = actionListener; |
144 | 18 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
public Locale getDefaultLocale() { |
149 | |
|
150 | |
return this.defaultLocale; |
151 | |
|
152 | |
} |
153 | |
|
154 | |
|
155 | |
public void setDefaultLocale(Locale defaultLocale) { |
156 | |
|
157 | 18 | this.defaultLocale = defaultLocale; |
158 | |
|
159 | 18 | } |
160 | |
|
161 | |
|
162 | |
public String getDefaultRenderKitId() { |
163 | |
|
164 | |
return this.defaultRenderKitId; |
165 | |
|
166 | |
} |
167 | |
|
168 | |
|
169 | |
public void setDefaultRenderKitId(String defaultRenderKitId) { |
170 | |
|
171 | 18 | this.defaultRenderKitId = defaultRenderKitId; |
172 | |
|
173 | 18 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
public String getMessageBundle() { |
178 | |
|
179 | |
return this.messageBundle; |
180 | |
|
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
public void setMessageBundle(String messageBundle) { |
186 | |
|
187 | |
this.messageBundle = messageBundle; |
188 | |
|
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
public NavigationHandler getNavigationHandler() { |
194 | |
|
195 | |
return this.navigationHandler; |
196 | |
|
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
public void setNavigationHandler(NavigationHandler navigationHandler) { |
202 | |
|
203 | 18 | this.navigationHandler = navigationHandler; |
204 | |
|
205 | 18 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
public PropertyResolver getPropertyResolver() { |
210 | |
|
211 | 13 | return this.propertyResolver; |
212 | |
|
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
public void setPropertyResolver(PropertyResolver propertyResolver) { |
218 | |
|
219 | 18 | this.propertyResolver = propertyResolver; |
220 | |
|
221 | 18 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
public StateManager getStateManager() { |
226 | |
|
227 | |
return this.stateManager; |
228 | |
|
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
public void setStateManager(StateManager stateManager) { |
234 | |
|
235 | 18 | this.stateManager = stateManager; |
236 | |
|
237 | 18 | } |
238 | |
|
239 | |
|
240 | |
|
241 | |
public Iterator getSupportedLocales() { |
242 | |
|
243 | |
return this.supportedLocales.iterator(); |
244 | |
|
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
public void setSupportedLocales(Collection supportedLocales) { |
250 | |
|
251 | 18 | this.supportedLocales = supportedLocales; |
252 | |
|
253 | 18 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
public VariableResolver getVariableResolver() { |
258 | |
|
259 | 30 | return this.variableResolver; |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
public void setVariableResolver(VariableResolver variableResolver) { |
265 | |
|
266 | 18 | this.variableResolver = variableResolver; |
267 | |
|
268 | 18 | } |
269 | |
|
270 | |
|
271 | |
|
272 | |
public ViewHandler getViewHandler() { |
273 | |
|
274 | |
return this.viewHandler; |
275 | |
|
276 | |
} |
277 | |
|
278 | |
|
279 | |
|
280 | |
public void setViewHandler(ViewHandler viewHandler) { |
281 | |
|
282 | 18 | this.viewHandler = viewHandler; |
283 | |
|
284 | 18 | } |
285 | |
|
286 | |
|
287 | |
|
288 | |
public void addComponent(String componentType, String componentClass) { |
289 | |
|
290 | 45 | components.put(componentType, componentClass); |
291 | |
|
292 | 45 | } |
293 | |
|
294 | |
|
295 | |
|
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 | |
|
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 | |
|
338 | |
public Iterator getComponentTypes() { |
339 | |
|
340 | 1 | return (components.keySet().iterator()); |
341 | |
|
342 | |
} |
343 | |
|
344 | |
|
345 | |
|
346 | |
public void addConverter(String converterId, String converterClass) { |
347 | |
|
348 | 230 | converters.put(converterId, converterClass); |
349 | |
|
350 | 230 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
public void addConverter(Class targetClass, String converterClass) { |
355 | |
|
356 | 300 | converters1.put(targetClass, converterClass); |
357 | |
|
358 | 300 | } |
359 | |
|
360 | |
|
361 | |
|
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 | |
|
379 | |
public Converter createConverter(Class targetClass) { |
380 | |
|
381 | 1 | String converterClass = (String) converters1.get(targetClass); |
382 | 1 | if (converterClass == null) { |
383 | |
return null; |
384 | |
} |
385 | |
try { |
386 | 1 | Class clazz = Class.forName(converterClass); |
387 | 1 | return ((Converter) clazz.newInstance()); |
388 | |
} catch (Exception e) { |
389 | |
throw new FacesException(e); |
390 | |
} |
391 | |
|
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
public Iterator getConverterIds() { |
397 | |
|
398 | 1 | return (converters.keySet().iterator()); |
399 | |
|
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
public Iterator getConverterTypes() { |
405 | |
|
406 | |
return (converters1.keySet().iterator()); |
407 | |
|
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
public MethodBinding createMethodBinding(String ref, Class[] params) { |
413 | |
|
414 | 11 | if (ref == null) { |
415 | |
throw new NullPointerException(); |
416 | |
} else { |
417 | 11 | return (new MockMethodBinding(this, ref, params)); |
418 | |
} |
419 | |
|
420 | |
} |
421 | |
|
422 | |
|
423 | |
|
424 | |
public ValueBinding createValueBinding(String ref) { |
425 | |
|
426 | 30 | if (ref == null) { |
427 | |
throw new NullPointerException(); |
428 | |
} else { |
429 | 30 | return (new MockValueBinding(this, ref)); |
430 | |
} |
431 | |
|
432 | |
} |
433 | |
|
434 | |
|
435 | |
|
436 | |
public void addValidator(String validatorId, String validatorClass) { |
437 | |
|
438 | 5 | validators.put(validatorId, validatorClass); |
439 | |
|
440 | 5 | } |
441 | |
|
442 | |
|
443 | |
|
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 | |
|
458 | |
public Iterator getValidatorIds() { |
459 | 1 | return (validators.keySet().iterator()); |
460 | |
} |
461 | |
|
462 | |
|
463 | |
} |