1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.application;
20
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TimeZone;
24
25 import javax.el.ValueExpression;
26 import javax.faces.FactoryFinder;
27 import javax.faces.application.ResourceDependency;
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIComponentBase;
30 import javax.faces.component.UIOutput;
31 import javax.faces.component.UIPanel;
32 import javax.faces.convert.Converter;
33 import javax.faces.convert.DateTimeConverter;
34 import javax.faces.event.ListenerFor;
35 import javax.faces.event.PostAddToViewEvent;
36 import javax.faces.render.RenderKitFactory;
37 import javax.faces.render.Renderer;
38
39 import org.apache.myfaces.component.ComponentResourceContainer;
40 import org.apache.myfaces.config.RuntimeConfig;
41 import org.apache.myfaces.renderkit.html.HtmlScriptRenderer;
42 import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMockTestCase;
43 import org.apache.myfaces.test.el.MockExpressionFactory;
44 import org.apache.myfaces.test.mock.MockPropertyResolver;
45 import org.apache.myfaces.test.mock.MockRenderKit;
46 import org.apache.myfaces.test.mock.MockRenderKitFactory;
47 import org.apache.myfaces.test.mock.MockServletContext;
48 import org.apache.myfaces.test.mock.MockVariableResolver;
49 import org.junit.Assert;
50 import org.junit.Test;
51
52 public class ApplicationImplAnnotationTest extends AbstractJsfConfigurableMockTestCase
53 {
54
55
56
57 public ApplicationImplAnnotationTest()
58 {
59 }
60
61 @Override
62 protected void setFactories() throws Exception
63 {
64 ((MockServletContext)servletContext).addInitParameter(
65 "javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE", "true");
66 super.setFactories();
67 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
68 ApplicationFactoryImpl.class.getName());
69 FactoryFinder.setFactory(
70 FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY,
71 "org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl");
72 }
73
74 @Override
75 protected void setUpExternalContext() throws Exception
76 {
77 super.setUpExternalContext();
78
79 RuntimeConfig.getCurrentInstance(externalContext).setPropertyResolver(
80 new MockPropertyResolver());
81 RuntimeConfig.getCurrentInstance(externalContext).setVariableResolver(
82 new MockVariableResolver());
83 RuntimeConfig.getCurrentInstance(externalContext).setExpressionFactory(
84 new MockExpressionFactory());
85 }
86
87 @Override
88 protected void setUpApplication() throws Exception
89 {
90 super.setUpApplication();
91
92 application.addComponent(UIOutput.COMPONENT_TYPE, UIOutput.class
93 .getName());
94 application.addComponent(UIPanel.COMPONENT_TYPE, UIPanel.class
95 .getName());
96 application.addComponent(ComponentResourceContainer.COMPONENT_TYPE,
97 ComponentResourceContainer.class.getName());
98 }
99
100 @Override
101 protected void setUpRenderKit() throws Exception
102 {
103 RenderKitFactory renderKitFactory = (RenderKitFactory)
104 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
105 renderKit = new MockRenderKit();
106 renderKit.addRenderer("javax.faces.Output", "javax.faces.resource.Script", new HtmlScriptRenderer());
107 renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit);
108 }
109
110 @Override
111 public void tearDown() throws Exception
112 {
113 RuntimeConfig.getCurrentInstance(externalContext).purge();
114 super.tearDown();
115 }
116
117
118
119
120
121 public static class UITestComponentA extends UIComponentBase
122 {
123 public static final String COMPONENT_TYPE = "javax.faces.TestComponentA";
124 public static final String COMPONENT_FAMILY = "javax.faces.TestComponentA";
125 public static final String DEFAULT_RENDERER_TYPE = "javax.faces.TestComponentA";
126
127 public UITestComponentA()
128 {
129 setRendererType(DEFAULT_RENDERER_TYPE);
130 }
131
132 @Override
133 public String getFamily()
134 {
135 return COMPONENT_FAMILY;
136 }
137 }
138
139 @ListenerFor(systemEventClass=PostAddToViewEvent.class,
140 sourceClass=UIComponentBase.class)
141 @ResourceDependency(library = "testLib", name = "testResource.js")
142 public static class TestRendererA extends Renderer
143 {
144
145 }
146
147 @Test
148 public void testCreateComponentRenderer() throws Exception
149 {
150 facesContext.getViewRoot().setRenderKitId(
151 MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
152 facesContext.getRenderKit().addRenderer(
153 UITestComponentA.COMPONENT_FAMILY,
154 UITestComponentA.DEFAULT_RENDERER_TYPE, new TestRendererA());
155
156 application.addComponent(UITestComponentA.COMPONENT_TYPE,
157 UITestComponentA.class.getName());
158
159 UITestComponentA comp = (UITestComponentA) application.createComponent(facesContext,
160 UITestComponentA.COMPONENT_TYPE,
161 UITestComponentA.DEFAULT_RENDERER_TYPE);
162
163 List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
164 Assert.assertEquals(1,componentResources.size());
165 Map<String,Object> attrMap = componentResources.get(0).getAttributes();
166 Assert.assertEquals("testResource.js",attrMap.get("name"));
167 Assert.assertEquals("testLib",attrMap.get("library"));
168 }
169
170 @ResourceDependency(name = "testResource.js")
171 public static class UITestComponentB extends UIComponentBase
172 {
173 public static final String COMPONENT_TYPE = "javax.faces.TestComponentB";
174 public static final String COMPONENT_FAMILY = "javax.faces.TestComponentB";
175 public static final String DEFAULT_RENDERER_TYPE = "javax.faces.TestComponentB";
176
177 public UITestComponentB()
178 {
179 setRendererType(DEFAULT_RENDERER_TYPE);
180 }
181
182 @Override
183 public String getFamily()
184 {
185 return COMPONENT_FAMILY;
186 }
187 }
188
189 @Test
190 public void testCreateComponentAnnotation() throws Exception
191 {
192 application.addComponent(UITestComponentB.COMPONENT_TYPE,
193 UITestComponentB.class.getName());
194
195 UITestComponentB comp = (UITestComponentB) application.createComponent(UITestComponentB.COMPONENT_TYPE);
196
197 List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
198 Assert.assertEquals(1,componentResources.size());
199 Map<String,Object> attrMap = componentResources.get(0).getAttributes();
200 Assert.assertEquals("testResource.js",attrMap.get("name"));
201 }
202
203 @Test
204 public void testCreateComponentRendererAnnotation() throws Exception
205 {
206 facesContext.getViewRoot().setRenderKitId(
207 MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
208 facesContext.getRenderKit().addRenderer(
209 UITestComponentB.COMPONENT_FAMILY,
210 UITestComponentB.DEFAULT_RENDERER_TYPE, new TestRendererA());
211
212 application.addComponent(UITestComponentB.COMPONENT_TYPE,
213 UITestComponentB.class.getName());
214
215 UITestComponentB comp = (UITestComponentB) application.createComponent(facesContext,
216 UITestComponentB.COMPONENT_TYPE,
217 UITestComponentB.DEFAULT_RENDERER_TYPE);
218
219 List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
220 Assert.assertEquals(2,componentResources.size());
221 for (UIComponent component : componentResources)
222 {
223 if ("testResource.js".equals(component.getAttributes().get("name")))
224 {
225
226 }
227 else
228 {
229 Assert.fail("Not expected resource found"+component.getAttributes().get("name"));
230 }
231 }
232 }
233
234 @Test
235 public void testCreateComponentValueExpression1() throws Exception
236 {
237
238 ValueExpression expr = application.getExpressionFactory().createValueExpression(
239 facesContext.getELContext(), "#{testComponent}", Object.class);
240
241 application.addComponent(UITestComponentB.COMPONENT_TYPE,
242 UITestComponentB.class.getName());
243
244 UITestComponentB comp = (UITestComponentB) application.createComponent(
245 expr, facesContext, UITestComponentB.COMPONENT_TYPE);
246
247 List<UIComponent> componentResources =
248 facesContext.getViewRoot().getComponentResources(facesContext, "head");
249 Assert.assertEquals(1,componentResources.size());
250 Map<String,Object> attrMap = componentResources.get(0).getAttributes();
251 Assert.assertEquals("testResource.js",attrMap.get("name"));
252 }
253
254 @Test
255 public void testCreateComponentValueExpression2() throws Exception
256 {
257
258 ValueExpression expr = application.getExpressionFactory().createValueExpression(
259 facesContext.getELContext(), "#{testComponent}", Object.class);
260
261 expr.setValue(facesContext.getELContext(), new UITestComponentB());
262 application.addComponent(UITestComponentB.COMPONENT_TYPE,
263 UITestComponentB.class.getName());
264
265 UITestComponentB comp = (UITestComponentB) application.createComponent(
266 expr, facesContext, UITestComponentB.COMPONENT_TYPE);
267
268 List<UIComponent> componentResources =
269 facesContext.getViewRoot().getComponentResources(facesContext, "head");
270 Assert.assertEquals(1,componentResources.size());
271 Map<String,Object> attrMap = componentResources.get(0).getAttributes();
272 Assert.assertEquals("testResource.js",attrMap.get("name"));
273 }
274
275 @Test
276 public void testCreateComponentValueExpression3() throws Exception
277 {
278
279 ValueExpression expr = application.getExpressionFactory().createValueExpression(
280 facesContext.getELContext(), "#{testComponent}", Object.class);
281
282 facesContext.getViewRoot().setRenderKitId(
283 MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
284 facesContext.getRenderKit().addRenderer(
285 UITestComponentB.COMPONENT_FAMILY,
286 UITestComponentB.DEFAULT_RENDERER_TYPE, new TestRendererA());
287
288 application.addComponent(UITestComponentB.COMPONENT_TYPE,
289 UITestComponentB.class.getName());
290
291 UITestComponentB comp = (UITestComponentB) application.createComponent(expr, facesContext,
292 UITestComponentB.COMPONENT_TYPE,
293 UITestComponentB.DEFAULT_RENDERER_TYPE);
294
295 List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
296 Assert.assertEquals(2,componentResources.size());
297 for (UIComponent component : componentResources)
298 {
299 if ("testResource.js".equals(component.getAttributes().get("name")))
300 {
301
302 }
303 else
304 {
305 Assert.fail("Not expected resource found"+component.getAttributes().get("name"));
306 }
307 }
308 }
309
310 @Test
311 public void testCreateComponentValueExpression4() throws Exception
312 {
313
314 ValueExpression expr = application.getExpressionFactory().createValueExpression(
315 facesContext.getELContext(), "#{testComponent}", Object.class);
316
317 expr.setValue(facesContext.getELContext(), new UITestComponentB());
318
319 facesContext.getViewRoot().setRenderKitId(
320 MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
321 facesContext.getRenderKit().addRenderer(
322 UITestComponentB.COMPONENT_FAMILY,
323 UITestComponentB.DEFAULT_RENDERER_TYPE, new TestRendererA());
324
325 application.addComponent(UITestComponentB.COMPONENT_TYPE,
326 UITestComponentB.class.getName());
327
328 UITestComponentB comp = (UITestComponentB) application.createComponent(expr, facesContext,
329 UITestComponentB.COMPONENT_TYPE,
330 UITestComponentB.DEFAULT_RENDERER_TYPE);
331
332 List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
333 Assert.assertEquals(2,componentResources.size());
334 for (UIComponent component : componentResources)
335 {
336 if ("testResource.js".equals(component.getAttributes().get("name")))
337 {
338
339 }
340 else
341 {
342 Assert.fail("Not expected resource found"+component.getAttributes().get("name"));
343 }
344 }
345 }
346
347 @Test
348 public void testDatetimeconverterDefaultTimezoneIsSystemTimezoneInitParameter()
349 {
350 application.addConverter(java.util.Date.class, "javax.faces.convert.DateTimeConverter");
351 Converter converter = application.createConverter(java.util.Date.class);
352 Assert.assertEquals(((DateTimeConverter) converter).getTimeZone(), TimeZone.getDefault());
353 }
354
355 }