1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.base; |
19 | |
|
20 | |
import java.net.URL; |
21 | |
import java.net.URLClassLoader; |
22 | |
|
23 | |
import javax.faces.FactoryFinder; |
24 | |
import javax.faces.application.ApplicationFactory; |
25 | |
import javax.faces.component.UIViewRoot; |
26 | |
import javax.faces.lifecycle.LifecycleFactory; |
27 | |
import javax.faces.render.RenderKitFactory; |
28 | |
|
29 | |
import junit.framework.TestCase; |
30 | |
|
31 | |
import org.apache.shale.test.mock.MockApplication; |
32 | |
import org.apache.shale.test.mock.MockExternalContext; |
33 | |
import org.apache.shale.test.mock.MockFacesContext; |
34 | |
import org.apache.shale.test.mock.MockFacesContextFactory; |
35 | |
import org.apache.shale.test.mock.MockHttpServletRequest; |
36 | |
import org.apache.shale.test.mock.MockHttpServletResponse; |
37 | |
import org.apache.shale.test.mock.MockHttpSession; |
38 | |
import org.apache.shale.test.mock.MockLifecycle; |
39 | |
import org.apache.shale.test.mock.MockLifecycleFactory; |
40 | |
import org.apache.shale.test.mock.MockRenderKit; |
41 | |
import org.apache.shale.test.mock.MockServletConfig; |
42 | |
import org.apache.shale.test.mock.MockServletContext; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public abstract class AbstractJsfTestCase extends TestCase { |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public AbstractJsfTestCase(String name) { |
86 | 36 | super(name); |
87 | 36 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected void setUp() throws Exception { |
97 | |
|
98 | |
|
99 | 18 | threadContextClassLoader = Thread.currentThread().getContextClassLoader(); |
100 | 18 | Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0], |
101 | |
this.getClass().getClassLoader())); |
102 | |
|
103 | |
|
104 | 18 | servletContext = new MockServletContext(); |
105 | 18 | config = new MockServletConfig(servletContext); |
106 | 18 | session = new MockHttpSession(); |
107 | 18 | session.setServletContext(servletContext); |
108 | 18 | request = new MockHttpServletRequest(session); |
109 | 18 | request.setServletContext(servletContext); |
110 | 18 | response = new MockHttpServletResponse(); |
111 | |
|
112 | |
|
113 | 18 | FactoryFinder.releaseFactories(); |
114 | 18 | FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, |
115 | |
"org.apache.shale.test.mock.MockApplicationFactory"); |
116 | 18 | FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY, |
117 | |
"org.apache.shale.test.mock.MockFacesContextFactory"); |
118 | 18 | FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, |
119 | |
"org.apache.shale.test.mock.MockLifecycleFactory"); |
120 | 18 | FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY, |
121 | |
"org.apache.shale.test.mock.MockRenderKitFactory"); |
122 | |
|
123 | 18 | externalContext = |
124 | |
new MockExternalContext(servletContext, request, response); |
125 | 18 | lifecycleFactory = (MockLifecycleFactory) |
126 | |
FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); |
127 | 18 | lifecycle = (MockLifecycle) |
128 | |
lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); |
129 | 18 | facesContextFactory = (MockFacesContextFactory) |
130 | |
FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); |
131 | 18 | facesContext = (MockFacesContext) |
132 | |
facesContextFactory.getFacesContext(servletContext, |
133 | |
request, |
134 | |
response, |
135 | |
lifecycle); |
136 | 18 | externalContext = (MockExternalContext) facesContext.getExternalContext(); |
137 | 18 | UIViewRoot root = new UIViewRoot(); |
138 | 18 | root.setViewId("/viewId"); |
139 | 18 | root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); |
140 | 18 | facesContext.setViewRoot(root); |
141 | 18 | ApplicationFactory applicationFactory = (ApplicationFactory) |
142 | |
FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); |
143 | 18 | application = (MockApplication) applicationFactory.getApplication(); |
144 | 18 | facesContext.setApplication(application); |
145 | 18 | RenderKitFactory renderKitFactory = (RenderKitFactory) |
146 | |
FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
147 | 18 | renderKit = new MockRenderKit(); |
148 | 18 | renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, renderKit); |
149 | |
|
150 | 18 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
protected void tearDown() throws Exception { |
157 | |
|
158 | 18 | application = null; |
159 | 18 | config = null; |
160 | 18 | externalContext = null; |
161 | 18 | facesContext.release(); |
162 | 18 | facesContext = null; |
163 | 18 | lifecycle = null; |
164 | 18 | lifecycleFactory = null; |
165 | 18 | renderKit = null; |
166 | 18 | request = null; |
167 | 18 | response = null; |
168 | 18 | servletContext = null; |
169 | 18 | session = null; |
170 | 18 | FactoryFinder.releaseFactories(); |
171 | |
|
172 | 18 | Thread.currentThread().setContextClassLoader(threadContextClassLoader); |
173 | 18 | threadContextClassLoader = null; |
174 | |
|
175 | 18 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | 36 | protected MockApplication application = null; |
183 | 36 | protected MockServletConfig config = null; |
184 | 36 | protected MockExternalContext externalContext = null; |
185 | 36 | protected MockFacesContext facesContext = null; |
186 | 36 | protected MockFacesContextFactory facesContextFactory = null; |
187 | 36 | protected MockLifecycle lifecycle = null; |
188 | 36 | protected MockLifecycleFactory lifecycleFactory = null; |
189 | 36 | protected MockRenderKit renderKit = null; |
190 | 36 | protected MockHttpServletRequest request = null; |
191 | 36 | protected MockHttpServletResponse response = null; |
192 | 36 | protected MockServletContext servletContext = null; |
193 | 36 | protected MockHttpSession session = null; |
194 | |
|
195 | |
|
196 | 36 | private ClassLoader threadContextClassLoader = null; |
197 | |
|
198 | |
} |