1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidadbuild.test;
20
21 import java.io.IOException;
22
23 import java.util.List;
24
25 import javax.faces.FactoryFinder;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.render.RenderKit;
29 import javax.faces.render.RenderKitFactory;
30
31 import org.apache.shale.test.jmock.AbstractJmockJsfTestCase;
32 import org.apache.shale.test.mock.MockFacesContext;
33 import org.apache.shale.test.mock.MockRenderKitFactory;
34 import org.jmock.Mock;
35
36 /**
37 * Base class for JavaServer Faces unit tests.
38 * Acts as a wrapper class to <code>AbstractJsfTestCase</code>
39 *
40 */
41 public class FacesTestCase extends AbstractJmockJsfTestCase
42 {
43 /**
44 * Creates a new FacesTestCase.
45 *
46 * @param testName the unit test name
47 */
48 public FacesTestCase(
49 String testName)
50 {
51 super(testName);
52 }
53
54 @Override
55 protected void setUp() throws Exception
56 {
57 super.setUp();
58 facesContext.getViewRoot().setRenderKitId("org.apache.myfaces.trinidad.core");
59 RenderKitFactory renderKitFactory = (RenderKitFactory)
60 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
61 Mock mockRenderKitty = mock(RenderKit.class);
62 RenderKit renderKit = (RenderKit) mockRenderKitty.proxy();
63 _mockRenderKit = new MockRenderKitWrapper(mockRenderKitty, renderKit);
64 renderKitFactory.addRenderKit("org.apache.myfaces.trinidad.core", renderKit);
65 }
66
67 @Override
68 protected void tearDown() throws Exception
69 {
70 super.tearDown();
71 }
72
73 /**
74 * Configures the FactoryFinder RenderKitFactory implementation class.
75 *
76 * @param renderkitFactoryClass the render kit factory class
77 */
78 protected RenderKitFactory setupRenderKitFactory(
79 Class<? extends RenderKitFactory> renderkitFactoryClass)
80 {
81
82 FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
83 renderkitFactoryClass.getName());
84 return (RenderKitFactory)
85 FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
86 }
87
88 /**
89 * Configures the FactoryFinder MockRenderKitFactory implementation class.
90 * @TODO rename the class.
91 * @TODO move to Shale.
92 */
93 protected MockRenderKitFactory setupMockRenderKitFactory()
94 {
95 return (MockRenderKitFactory)
96 setupRenderKitFactory(MockRenderKitFactory.class);
97 }
98
99
100 /**
101 * Renders the component tree.
102 *
103 * @param context the faces context
104 * @param component the component tree to render
105 *
106 * @throws IOException when the render fails
107 */
108 @SuppressWarnings("unchecked")
109 protected void doRenderResponse(
110 FacesContext context,
111 UIComponent component) throws IOException
112 {
113 component.encodeBegin(context);
114 if (component.getRendersChildren())
115 {
116 component.encodeChildren(context);
117 }
118 else
119 {
120 for(UIComponent child : (List<UIComponent>)component.getChildren())
121 {
122 doRenderResponse(context, child);
123 }
124 }
125 component.encodeEnd(context);
126 }
127
128 public void setCurrentContext(
129 FacesContext context)
130 {
131 TestFacesContext.setCurrentInstance(context);
132 }
133
134 private MockRenderKitWrapper _mockRenderKit = null;
135
136 public MockRenderKitWrapper getMockRenderKitWrapper()
137 {
138 return _mockRenderKit;
139 }
140
141 /**
142 * @todo add this code to MockFacesContext
143 */
144 public static abstract class TestFacesContext extends MockFacesContext
145 {
146 public static void setCurrentInstance(
147 FacesContext context)
148 {
149 FacesContext.setCurrentInstance(context);
150 }
151 }
152 }