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.HashMap; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.faces.FactoryFinder; |
27 | |
import javax.faces.application.Application; |
28 | |
import javax.faces.application.FacesMessage; |
29 | |
import javax.faces.application.FacesMessage.Severity; |
30 | |
import javax.faces.component.UIViewRoot; |
31 | |
import javax.faces.context.ExternalContext; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.context.ResponseStream; |
34 | |
import javax.faces.context.ResponseWriter; |
35 | |
import javax.faces.lifecycle.Lifecycle; |
36 | |
import javax.faces.render.RenderKit; |
37 | |
import javax.faces.render.RenderKitFactory; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class MockFacesContext extends FacesContext { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public MockFacesContext() { |
53 | |
super(); |
54 | |
setCurrentInstance(this); |
55 | |
} |
56 | |
|
57 | |
|
58 | 18 | public MockFacesContext(ExternalContext externalContext) { |
59 | 18 | setExternalContext(externalContext); |
60 | 18 | setCurrentInstance(this); |
61 | 18 | } |
62 | |
|
63 | |
|
64 | |
public MockFacesContext(ExternalContext externalContext, Lifecycle lifecycle) { |
65 | 18 | this(externalContext); |
66 | 18 | this.lifecycle = lifecycle; |
67 | 18 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void setApplication(Application application) { |
79 | |
|
80 | 18 | this.application = application; |
81 | |
|
82 | 18 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void setExternalContext(ExternalContext externalContext) { |
91 | |
|
92 | 18 | this.externalContext = externalContext; |
93 | |
|
94 | 18 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public static void setCurrentInstance(FacesContext facesContext) { |
103 | |
|
104 | 36 | FacesContext.setCurrentInstance(facesContext); |
105 | |
|
106 | 36 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | 18 | private Application application = null; |
113 | 18 | private ExternalContext externalContext = null; |
114 | 18 | private Lifecycle lifecycle = null; |
115 | 18 | private Map messages = new HashMap(); |
116 | 18 | private boolean renderResponse = false; |
117 | 18 | private boolean responseComplete = false; |
118 | 18 | private ResponseStream responseStream = null; |
119 | 18 | private ResponseWriter responseWriter = null; |
120 | 18 | private UIViewRoot viewRoot = null; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public Application getApplication() { |
128 | |
|
129 | 13 | return this.application; |
130 | |
|
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
public Iterator getClientIdsWithMessages() { |
136 | |
|
137 | |
return messages.keySet().iterator(); |
138 | |
|
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
public ExternalContext getExternalContext() { |
144 | |
|
145 | 62 | return this.externalContext; |
146 | |
|
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
public Severity getMaximumSeverity() { |
152 | |
|
153 | |
Severity severity = null; |
154 | |
Iterator messages = getMessages(); |
155 | |
while (messages.hasNext()) { |
156 | |
FacesMessage message = (FacesMessage) messages.next(); |
157 | |
if (severity == null) { |
158 | |
severity = message.getSeverity(); |
159 | |
} else if (message.getSeverity().getOrdinal() > severity.getOrdinal()) { |
160 | |
severity = message.getSeverity(); |
161 | |
} |
162 | |
} |
163 | |
return severity; |
164 | |
|
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
public Iterator getMessages() { |
170 | |
|
171 | |
ArrayList results = new ArrayList(); |
172 | |
Iterator clientIds = messages.keySet().iterator(); |
173 | |
while (clientIds.hasNext()) { |
174 | |
String clientId = (String) clientIds.next(); |
175 | |
results.addAll((List) messages.get(clientId)); |
176 | |
} |
177 | |
return results.iterator(); |
178 | |
|
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
public Iterator getMessages(String clientId) { |
184 | |
|
185 | |
List list = (List) messages.get(clientId); |
186 | |
if (list == null) { |
187 | |
list = new ArrayList(); |
188 | |
} |
189 | |
return list.iterator(); |
190 | |
|
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
public RenderKit getRenderKit() { |
196 | |
|
197 | |
UIViewRoot vr = getViewRoot(); |
198 | |
if (vr == null) { |
199 | |
return null; |
200 | |
} |
201 | |
String renderKitId = vr.getRenderKitId(); |
202 | |
if (renderKitId == null) { |
203 | |
return null; |
204 | |
} |
205 | |
RenderKitFactory rkFactory = (RenderKitFactory) |
206 | |
FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
207 | |
return rkFactory.getRenderKit(this, renderKitId); |
208 | |
|
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
public boolean getRenderResponse() { |
214 | |
|
215 | |
return this.renderResponse; |
216 | |
|
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
public boolean getResponseComplete() { |
222 | |
|
223 | |
return this.responseComplete; |
224 | |
|
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
public ResponseStream getResponseStream() { |
230 | |
|
231 | |
return this.responseStream; |
232 | |
|
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
public void setResponseStream(ResponseStream responseStream) { |
238 | |
|
239 | |
this.responseStream = responseStream; |
240 | |
|
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
public ResponseWriter getResponseWriter() { |
246 | |
|
247 | |
return this.responseWriter; |
248 | |
|
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
public void setResponseWriter(ResponseWriter responseWriter) { |
254 | |
|
255 | |
this.responseWriter = responseWriter; |
256 | |
|
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
public UIViewRoot getViewRoot() { |
262 | |
|
263 | |
return this.viewRoot; |
264 | |
|
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
public void setViewRoot(UIViewRoot viewRoot) { |
270 | |
|
271 | 18 | this.viewRoot = viewRoot; |
272 | |
|
273 | 18 | } |
274 | |
|
275 | |
|
276 | |
|
277 | |
public void addMessage(String clientId, FacesMessage message) { |
278 | |
|
279 | |
if (message == null) { |
280 | |
throw new NullPointerException(); |
281 | |
} |
282 | |
List list = (List) messages.get(clientId); |
283 | |
if (list == null) { |
284 | |
list = new ArrayList(); |
285 | |
messages.put(clientId, list); |
286 | |
} |
287 | |
list.add(message); |
288 | |
|
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
public void release() { |
294 | |
|
295 | 18 | application = null; |
296 | 18 | externalContext = null; |
297 | 18 | messages.clear(); |
298 | 18 | renderResponse = false; |
299 | 18 | responseComplete = false; |
300 | 18 | responseStream = null; |
301 | 18 | responseWriter = null; |
302 | 18 | viewRoot = null; |
303 | 18 | setCurrentInstance(null); |
304 | |
|
305 | 18 | } |
306 | |
|
307 | |
|
308 | |
|
309 | |
public void renderResponse() { |
310 | |
|
311 | |
this.renderResponse = true; |
312 | |
|
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
public void responseComplete() { |
318 | |
|
319 | |
this.responseComplete = true; |
320 | |
|
321 | |
} |
322 | |
|
323 | |
|
324 | |
} |