1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.InputStream;
25 import java.io.StringWriter;
26 import java.lang.reflect.Field;
27 import java.net.URI;
28 import java.net.URL;
29
30 import javax.el.ExpressionFactory;
31 import javax.faces.FacesException;
32 import javax.faces.FactoryFinder;
33 import javax.faces.application.ProjectStage;
34 import javax.faces.application.StateManager;
35 import javax.faces.component.UIViewRoot;
36 import javax.faces.context.FacesContext;
37 import javax.faces.context.ResponseWriter;
38 import javax.faces.render.RenderKitFactory;
39
40 import org.apache.myfaces.application.ApplicationFactoryImpl;
41 import org.apache.myfaces.application.ViewHandlerImpl;
42 import org.apache.myfaces.config.FacesConfigDispenser;
43 import org.apache.myfaces.config.FacesConfigUnmarshaller;
44 import org.apache.myfaces.config.RuntimeConfig;
45 import org.apache.myfaces.config.element.Behavior;
46 import org.apache.myfaces.config.element.ClientBehaviorRenderer;
47 import org.apache.myfaces.config.element.FacesConfig;
48 import org.apache.myfaces.config.element.Renderer;
49 import org.apache.myfaces.config.impl.digester.DigesterFacesConfigDispenserImpl;
50 import org.apache.myfaces.config.impl.digester.DigesterFacesConfigUnmarshallerImpl;
51 import org.apache.myfaces.context.PartialViewContextFactoryImpl;
52 import org.apache.myfaces.shared.application.ViewHandlerSupport;
53 import org.apache.myfaces.shared.util.ClassUtils;
54 import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMultipleRequestsTestCase;
55 import org.apache.myfaces.test.el.MockExpressionFactory;
56 import org.apache.myfaces.test.mock.MockExternalContext;
57 import org.apache.myfaces.test.mock.MockFacesContext;
58 import org.apache.myfaces.test.mock.MockFacesContextFactory;
59 import org.apache.myfaces.test.mock.MockPropertyResolver;
60 import org.apache.myfaces.test.mock.MockVariableResolver;
61 import org.apache.myfaces.test.mock.visit.MockVisitContextFactory;
62 import org.apache.myfaces.view.facelets.impl.FaceletCacheFactoryImpl;
63 import org.apache.myfaces.view.facelets.mock.MockViewDeclarationLanguageFactory;
64 import org.apache.myfaces.view.facelets.tag.jsf.TagHandlerDelegateFactoryImpl;
65
66 public abstract class FaceletMultipleRequestsTestCase extends AbstractJsfConfigurableMultipleRequestsTestCase
67 {
68 private final String filePath = this.getDirectory();
69 protected FacesConfigDispenser dispenser = null;
70 protected MockFaceletViewDeclarationLanguage vdl;
71
72
73 @Override
74 protected void setUpServletContextAndSession() throws Exception
75 {
76 super.setUpServletContextAndSession();
77 URI context = this.getContext();
78 servletContext.setDocumentRoot(new File(context));
79
80
81 servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME,
82 StateManager.STATE_SAVING_METHOD_CLIENT);
83 servletContext.addInitParameter("org.apache.myfaces.PRETTY_HTML","true");
84 servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT","true");
85 servletContext.addInitParameter("org.apache.myfaces.RENDER_CLEAR_JAVASCRIPT_FOR_BUTTON","false");
86 servletContext.addInitParameter("org.apache.myfaces.SAVE_FORM_SUBMIT_LINK_IE","false");
87 servletContext.addInitParameter("org.apache.myfaces.READONLY_AS_DISABLED_FOR_SELECTS","true");
88 servletContext.addInitParameter("org.apache.myfaces.RENDER_VIEWSTATE_ID","true");
89 servletContext.addInitParameter("org.apache.myfaces.STRICT_XHTML_LINKS","true");
90 servletContext.addInitParameter("org.apache.myfaces.CONFIG_REFRESH_PERIOD","0");
91 servletContext.addInitParameter("org.apache.myfaces.VIEWSTATE_JAVASCRIPT","false");
92 servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, "UnitTest");
93 }
94
95 @Override
96 protected void setUpServletRequestAndResponse() throws Exception
97 {
98 super.setUpServletRequestAndResponse();
99 URI context = this.getContext();
100 request.setPathElements(context.getPath(), null, context.getPath(), context.getQuery());
101 }
102
103 protected URI getContext()
104 {
105 try
106 {
107 ClassLoader cl = Thread.currentThread().getContextClassLoader();
108 URL url = cl.getResource(this.filePath);
109 if (url == null)
110 {
111 throw new FileNotFoundException(cl.getResource("").getFile()
112 + this.filePath + " was not found");
113 }
114 else
115 {
116 return new URI(url.toString());
117 }
118 }
119 catch (Exception e)
120 {
121 throw new RuntimeException("Error Initializing Context", e);
122 }
123 }
124
125 protected URL getLocalFile(String name) throws FileNotFoundException
126 {
127 ClassLoader cl = Thread.currentThread().getContextClassLoader();
128 URL url = cl.getResource(this.filePath + "/" + name);
129 if (url == null)
130 {
131 throw new FileNotFoundException(cl.getResource("").getFile() + name
132 + " was not found");
133 }
134 return url;
135 }
136
137 private String getDirectory()
138 {
139 return this.getClass().getName().substring(0,
140 this.getClass().getName().lastIndexOf('.')).replace('.', '/')
141 + "/";
142 }
143
144 @Override
145 protected void setFactories() throws Exception
146 {
147 super.setFactories();
148
149 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
150 ApplicationFactoryImpl.class.getName());
151 FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
152 "org.apache.myfaces.test.mock.MockFacesContextFactory");
153 FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
154 "org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory");
155 FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
156 "org.apache.myfaces.test.mock.MockRenderKitFactory");
157 FactoryFinder.setFactory(
158 FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY,
159 MockViewDeclarationLanguageFactory.class.getName());
160 FactoryFinder.setFactory(FactoryFinder.TAG_HANDLER_DELEGATE_FACTORY,
161 TagHandlerDelegateFactoryImpl.class.getName());
162 FactoryFinder.setFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY,
163 PartialViewContextFactoryImpl.class.getName());
164 FactoryFinder.setFactory(FactoryFinder.VISIT_CONTEXT_FACTORY,
165 MockVisitContextFactory.class.getName());
166 FactoryFinder.setFactory(FactoryFinder.FACELET_CACHE_FACTORY,
167 FaceletCacheFactoryImpl.class.getName());
168 }
169
170 @Override
171 protected void setUpExternalContext() throws Exception
172 {
173 super.setUpExternalContext();
174
175 RuntimeConfig.getCurrentInstance(externalContext).setPropertyResolver(
176 new MockPropertyResolver());
177 RuntimeConfig.getCurrentInstance(externalContext).setVariableResolver(
178 new MockVariableResolver());
179 RuntimeConfig.getCurrentInstance(externalContext).setExpressionFactory(
180 createExpressionFactory());
181 }
182
183 protected ExpressionFactory createExpressionFactory()
184 {
185 return new MockExpressionFactory();
186 }
187
188 @Override
189 protected void setUpFacesContext() throws Exception
190 {
191 super.setUpFacesContext();
192
193
194 ResponseWriter rw = renderKit.createResponseWriter(
195 new StringWriter(), null, null);
196 facesContext.setResponseWriter(rw);
197 }
198
199 @Override
200 protected void setUpRenderKit() throws Exception
201 {
202 super.setUpRenderKit();
203 setupComponents();
204 setupConvertersAndValidators();
205 setupBehaviors();
206 setupRenderers();
207 }
208
209 @Override
210 protected void setUpView() throws Exception
211 {
212 UIViewRoot root = new UIViewRoot();
213 root.setViewId("/test");
214 root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
215 facesContext.setViewRoot(root);
216 }
217
218 @Override
219 protected void setUpApplication() throws Exception
220 {
221 FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
222 "org.apache.myfaces.test.mock.MockFacesContextFactory");
223 facesContextFactory = (MockFacesContextFactory) FactoryFinder
224 .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
225 facesContext = (MockFacesContext) facesContextFactory.getFacesContext(
226 servletContext, request, response, lifecycle);
227 if (facesContext.getExternalContext() != null)
228 {
229 externalContext = (MockExternalContext) facesContext
230 .getExternalContext();
231 }
232
233 super.setUpApplication();
234
235 ((MockFacesContext)facesContext).setApplication(application);
236
237 ViewHandlerImpl viewHandler = (ViewHandlerImpl) application.getViewHandler();
238 viewHandler.setViewHandlerSupport(new ViewHandlerSupport(){
239
240 public String calculateActionURL(FacesContext facesContext,
241 String viewId)
242 {
243 return viewId;
244 }
245
246 public String calculateViewId(FacesContext context, String viewId)
247 {
248 return viewId;
249 }
250
251 public String calculateAndCheckViewId(FacesContext context, String viewId)
252 {
253 return viewId;
254 }
255
256 });
257
258
259
260
261
262 }
263
264 @Override
265 public void setUp() throws Exception
266 {
267 super.setUp();
268
269
270
271 vdl = (MockFaceletViewDeclarationLanguage) application.getViewHandler().
272 getViewDeclarationLanguage(facesContext,"/test");
273
274 externalContext = null;
275 facesContext = null;
276 }
277
278
279
280
281
282
283
284 protected void loadStandardFacesConfig() throws Exception
285 {
286 if (dispenser == null)
287 {
288 InputStream stream = ClassUtils
289 .getResourceAsStream("META-INF/standard-faces-config.xml");
290 FacesConfigUnmarshaller<? extends FacesConfig> unmarshaller = new DigesterFacesConfigUnmarshallerImpl(
291 externalContext);
292 dispenser = new DigesterFacesConfigDispenserImpl();
293 dispenser.feed(unmarshaller.getFacesConfig(stream,
294 "META-INF/standard-faces-config.xml"));
295 }
296 }
297
298
299
300
301
302 protected void setupComponents() throws Exception
303 {
304 loadStandardFacesConfig();
305 for (String componentType : dispenser.getComponentTypes())
306 {
307 application.addComponent(componentType, dispenser
308 .getComponentClass(componentType));
309 }
310 }
311
312 protected void setupBehaviors() throws Exception
313 {
314 loadStandardFacesConfig();
315 for (Behavior behavior : dispenser.getBehaviors())
316 {
317 application.addBehavior(behavior.getBehaviorId(), behavior.getBehaviorClass());
318 }
319 }
320
321
322
323
324
325 protected void setupRenderers() throws Exception
326 {
327 loadStandardFacesConfig();
328 for (Renderer element : dispenser
329 .getRenderers(RenderKitFactory.HTML_BASIC_RENDER_KIT))
330 {
331 javax.faces.render.Renderer renderer;
332 try
333 {
334 renderer = (javax.faces.render.Renderer) ClassUtils
335 .newInstance(element.getRendererClass());
336 }
337 catch (Throwable e)
338 {
339
340 continue;
341 }
342
343 renderKit.addRenderer(element.getComponentFamily(), element
344 .getRendererType(), renderer);
345 }
346
347 for (ClientBehaviorRenderer element : dispenser.getClientBehaviorRenderers(RenderKitFactory.HTML_BASIC_RENDER_KIT))
348 {
349 javax.faces.render.ClientBehaviorRenderer renderer;
350
351 try
352 {
353 renderer = (javax.faces.render.ClientBehaviorRenderer) ClassUtils
354 .newInstance(element.getRendererClass());
355 }
356 catch (Throwable e)
357 {
358
359 continue;
360 }
361
362 renderKit.addClientBehaviorRenderer(element.getRendererType(), renderer);
363 }
364 }
365
366
367
368
369
370 protected void setupConvertersAndValidators() throws Exception
371 {
372 loadStandardFacesConfig();
373 for (String validatorId : dispenser.getValidatorIds())
374 {
375 application.addValidator(validatorId, dispenser
376 .getValidatorClass(validatorId));
377 }
378 for (String converterId : dispenser.getConverterIds())
379 {
380 application.addConverter(converterId, dispenser
381 .getConverterClassById(converterId));
382 }
383 for (String validatorId : dispenser.getValidatorIds())
384 {
385 application.addValidator(validatorId, dispenser
386 .getValidatorClass(validatorId));
387 }
388 }
389
390 public URL resolveUrl(String path)
391 {
392 try
393 {
394 return new URL(this.getContext().toURL(), path.substring(1));
395 }
396 catch (Exception e)
397 {
398 throw new FacesException(e);
399 }
400 }
401
402
403
404
405
406
407 public void setProjectStage(ProjectStage stage) throws IllegalStateException
408 {
409 try
410 {
411 Field projectStageField = application.getClass().getDeclaredField("_projectStage");
412 projectStageField.setAccessible(true);
413 projectStageField.set(application, stage);
414 }
415 catch (Exception e)
416 {
417 throw new IllegalStateException("Could not configure ProjectStage for test case", e);
418 }
419 }
420
421 }