1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.application.jsp;
20
21 import static org.apache.myfaces.Assert.assertException;
22 import org.apache.myfaces.FacesTestCase;
23 import org.apache.myfaces.TestRunner;
24 import org.apache.myfaces.application.InvalidViewIdException;
25 import org.apache.myfaces.application.ViewHandlerSupport;
26 import static org.easymock.EasyMock.*;
27 import org.easymock.IAnswer;
28
29 import javax.faces.application.ViewHandler;
30 import javax.faces.component.UIViewRoot;
31 import javax.faces.render.RenderKitFactory;
32 import javax.faces.render.ResponseStateManager;
33 import javax.servlet.http.HttpServletResponse;
34 import java.util.*;
35
36
37
38
39
40 public class JspViewHandlerImplTest extends FacesTestCase
41 {
42 private JspViewHandlerImpl _testimpl;
43
44 protected void setUp() throws Exception
45 {
46 super.setUp();
47 _testimpl = new JspViewHandlerImpl();
48 }
49
50
51
52
53
54 public void testCalculateLocaleNPE()
55 {
56 assertException(NullPointerException.class, new TestRunner()
57 {
58 public void run() throws Throwable
59 {
60 _testimpl.calculateLocale(null);
61 }
62 });
63
64
65
66 }
67
68
69
70
71
72 public void testCalculateLocaleWithNoRequestLocaleAndNoDefaultLocale()
73 {
74 expectApplicationAndExternalContextGet();
75 expect(_externalContext.getRequestLocales()).andReturn(Collections.EMPTY_LIST.iterator());
76 expect(_application.getDefaultLocale()).andReturn(null);
77 _mocksControl.replay();
78 assertEquals(Locale.getDefault(), _testimpl.calculateLocale(_facesContext));
79 _mocksControl.verify();
80 }
81
82
83
84
85
86 public void testCalculateLocaleWithNoRequestLocaleAndDefaultLocale()
87 {
88 expectApplicationAndExternalContextGet();
89 expect(_externalContext.getRequestLocales()).andReturn(Collections.EMPTY_LIST.iterator());
90 expect(_application.getDefaultLocale()).andReturn(Locale.KOREAN);
91 _mocksControl.replay();
92 assertEquals(Locale.KOREAN, _testimpl.calculateLocale(_facesContext));
93 _mocksControl.verify();
94 }
95
96
97
98
99
100 public void testCalculateLocaleWithNoMatchingRequestLocaleAndNoSupportedLocaleAndDefaultLocale()
101 {
102 expectApplicationAndExternalContextGet();
103 Iterator<Locale> requestLocales = Arrays.asList(new Locale[] { Locale.KOREAN }).iterator();
104 expect(_externalContext.getRequestLocales()).andReturn(requestLocales);
105 expect(_application.getSupportedLocales()).andReturn(Collections.EMPTY_LIST.iterator());
106 expect(_application.getDefaultLocale()).andReturn(Locale.GERMAN);
107 _mocksControl.replay();
108 assertEquals(Locale.GERMAN, _testimpl.calculateLocale(_facesContext));
109 _mocksControl.verify();
110 }
111
112
113
114
115
116 public void testCalculateLocaleWithNoMatchingRequestLocaleWithSupportedLocaleAndDefaultLocale()
117 {
118 expectApplicationAndExternalContextGet();
119 Iterator<Locale> requestLocales = Arrays.asList(new Locale[] { Locale.KOREAN }).iterator();
120 expect(_externalContext.getRequestLocales()).andReturn(requestLocales);
121 Iterator<Locale> supportedLocales = Arrays.asList(new Locale[] { Locale.CHINESE }).iterator();
122 expect(_application.getSupportedLocales()).andReturn(supportedLocales);
123 expect(_application.getDefaultLocale()).andReturn(Locale.GERMAN);
124 _mocksControl.replay();
125 assertEquals(Locale.GERMAN, _testimpl.calculateLocale(_facesContext));
126 _mocksControl.verify();
127 }
128
129
130
131
132
133 public void testCalculateLocaleWithMatchingRequestLocaleWithSupportedLocale()
134 {
135 expectApplicationAndExternalContextGet();
136 Iterator<Locale> requestLocales = Arrays.asList(new Locale[] { Locale.KOREAN, Locale.GERMANY }).iterator();
137 expect(_externalContext.getRequestLocales()).andReturn(requestLocales);
138 final Collection<Locale> supportedLocales = Arrays.asList(new Locale[] { Locale.GERMANY });
139 expect(_application.getSupportedLocales()).andAnswer(new IAnswer<Iterator<Locale>>()
140 {
141 public Iterator<Locale> answer() throws Throwable
142 {
143 return supportedLocales.iterator();
144 }
145 }).times(2);
146 _mocksControl.replay();
147 assertEquals(Locale.GERMANY, _testimpl.calculateLocale(_facesContext));
148 _mocksControl.verify();
149 }
150
151
152
153
154
155 public void testCalculateLocaleIfRequestLocaleLanguageMatchesSupportedLocaleWOCountry()
156 {
157 expectApplicationAndExternalContextGet();
158 Iterator<Locale> requestLocales = Arrays.asList(new Locale[] { Locale.GERMANY, Locale.KOREAN }).iterator();
159 expect(_externalContext.getRequestLocales()).andReturn(requestLocales);
160 Iterator<Locale> supportedLocales = Arrays.asList(new Locale[] { Locale.GERMAN, Locale.KOREAN }).iterator();
161 expect(_application.getSupportedLocales()).andReturn(supportedLocales);
162 _mocksControl.replay();
163 assertEquals(Locale.GERMAN, _testimpl.calculateLocale(_facesContext));
164 _mocksControl.verify();
165 }
166
167
168
169
170
171 public void testCalculateRenderKitIdFromRequest()
172 {
173 expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
174 Map<String, Object> map = new HashMap<String, Object>();
175 map.put(ResponseStateManager.RENDER_KIT_ID_PARAM, "xxx");
176 expect(_externalContext.getRequestMap()).andReturn(map);
177 _mocksControl.replay();
178 assertEquals("xxx", _testimpl.calculateRenderKitId(_facesContext));
179 _mocksControl.verify();
180 }
181
182
183
184
185
186 public void testCalculateLocaleWithNoMatchingRequestLocaleWithSupportedLocaleAndNoDefaultLocale()
187 {
188 expectApplicationAndExternalContextGet();
189 Iterator<Locale> requestLocales = Arrays.asList(new Locale[] { Locale.ENGLISH }).iterator();
190 expect(_externalContext.getRequestLocales()).andReturn(requestLocales);
191 expect(_application.getDefaultLocale()).andReturn(null);
192 Iterator<Locale> supportedLocales = Arrays.asList(new Locale[] { Locale.KOREAN }).iterator();
193 expect(_application.getSupportedLocales()).andReturn(supportedLocales);
194 _mocksControl.replay();
195 assertEquals(Locale.getDefault(), _testimpl.calculateLocale(_facesContext));
196 _mocksControl.verify();
197 }
198
199
200
201
202
203 public void testCalculateRenderKitIdFromApplicationDefault()
204 {
205 expectApplicationAndExternalContextGet();
206 expect(_externalContext.getRequestMap()).andReturn(Collections.EMPTY_MAP);
207 expect(_application.getDefaultRenderKitId()).andReturn("xxx");
208 _mocksControl.replay();
209 assertEquals("xxx", _testimpl.calculateRenderKitId(_facesContext));
210 _mocksControl.verify();
211 }
212
213
214
215
216
217 public void testCalculateRenderKitIdFinalFallBack()
218 {
219 expectApplicationAndExternalContextGet();
220 expect(_externalContext.getRequestMap()).andReturn(Collections.EMPTY_MAP);
221 expect(_application.getDefaultRenderKitId()).andReturn(null);
222 _mocksControl.replay();
223 assertEquals(RenderKitFactory.HTML_BASIC_RENDER_KIT, _testimpl.calculateRenderKitId(_facesContext));
224 _mocksControl.verify();
225 }
226
227 private void expectApplicationAndExternalContextGet()
228 {
229 expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
230 expect(_facesContext.getApplication()).andReturn(_application).anyTimes();
231 }
232
233
234
235
236
237 public void testCreateView()
238 {
239 ViewHandlerSupport viewHandlerSupport = _mocksControl.createMock(ViewHandlerSupport.class);
240 _testimpl.setViewHandlerSupport(viewHandlerSupport);
241 expect(viewHandlerSupport.calculateViewId(same(_facesContext), eq("viewidxxx"))).andReturn("calculatedviewId");
242 expect(_facesContext.getApplication()).andReturn(_application);
243 ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
244 expect(_application.getViewHandler()).andReturn(viewHandler);
245 expect(_facesContext.getViewRoot()).andReturn(null);
246 UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
247 expect(_application.createComponent(eq(UIViewRoot.COMPONENT_TYPE))).andReturn(viewRoot);
248
249 expect(viewHandler.calculateRenderKitId(same(_facesContext))).andReturn("renderkitid");
250 Locale locale = new Locale("xxx");
251 expect(viewHandler.calculateLocale(same(_facesContext))).andReturn(locale);
252
253 viewRoot.setLocale(locale);
254 viewRoot.setRenderKitId("renderkitid");
255 viewRoot.setViewId("calculatedviewId");
256
257 _mocksControl.replay();
258 assertEquals(viewRoot, _testimpl.createView(_facesContext, "viewidxxx"));
259 _mocksControl.verify();
260 }
261
262
263
264
265
266
267
268 public void testCreateViewWithInvalidViewId() throws Exception
269 {
270 ViewHandlerSupport viewHandlerSupport = _mocksControl.createMock(ViewHandlerSupport.class);
271 _testimpl.setViewHandlerSupport(viewHandlerSupport);
272 expect(viewHandlerSupport.calculateViewId(same(_facesContext), eq("viewidxxx"))).andThrow(
273 new InvalidViewIdException("xxx"));
274
275 expect(_facesContext.getExternalContext()).andReturn(_externalContext);
276 HttpServletResponse httpServletResponse = _mocksControl.createMock(HttpServletResponse.class);
277 expect(_externalContext.getResponse()).andReturn(httpServletResponse);
278 httpServletResponse.sendError(eq(HttpServletResponse.SC_NOT_FOUND), (String) anyObject());
279 _facesContext.responseComplete();
280
281 expect(_facesContext.getApplication()).andReturn(_application);
282 ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
283 expect(_application.getViewHandler()).andReturn(viewHandler);
284 expect(_facesContext.getViewRoot()).andReturn(null);
285 UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
286 expect(_application.createComponent(eq(UIViewRoot.COMPONENT_TYPE))).andReturn(viewRoot);
287
288 expect(viewHandler.calculateRenderKitId(same(_facesContext))).andReturn("renderkitid");
289 Locale locale = new Locale("xxx");
290 expect(viewHandler.calculateLocale(same(_facesContext))).andReturn(locale);
291
292 viewRoot.setLocale(locale);
293 viewRoot.setRenderKitId("renderkitid");
294 viewRoot.setViewId("viewidxxx");
295
296 _mocksControl.replay();
297 assertEquals(viewRoot, _testimpl.createView(_facesContext, "viewidxxx"));
298 _mocksControl.verify();
299 }
300
301
302
303
304
305 public void testCreateViewWithExistingViewRoot()
306 {
307 ViewHandlerSupport viewHandlerSupport = _mocksControl.createMock(ViewHandlerSupport.class);
308 _testimpl.setViewHandlerSupport(viewHandlerSupport);
309 expect(viewHandlerSupport.calculateViewId(same(_facesContext), eq("viewidxxx"))).andReturn("calculatedviewId");
310 expect(_facesContext.getApplication()).andReturn(_application);
311 ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
312 expect(_application.getViewHandler()).andReturn(viewHandler);
313
314 UIViewRoot existingViewRoot = _mocksControl.createMock(UIViewRoot.class);
315 Locale locale = new Locale("xxx");
316 expect(existingViewRoot.getLocale()).andReturn(locale);
317 expect(existingViewRoot.getRenderKitId()).andReturn("renderkitid");
318
319 expect(_facesContext.getViewRoot()).andReturn(existingViewRoot);
320 UIViewRoot newViewRoot = _mocksControl.createMock(UIViewRoot.class);
321 expect(_application.createComponent(eq(UIViewRoot.COMPONENT_TYPE))).andReturn(newViewRoot);
322
323 newViewRoot.setLocale(locale);
324 newViewRoot.setRenderKitId("renderkitid");
325 newViewRoot.setViewId("calculatedviewId");
326
327 _mocksControl.replay();
328 assertEquals(newViewRoot, _testimpl.createView(_facesContext, "viewidxxx"));
329 _mocksControl.verify();
330 }
331
332
333
334
335
336 public void testGetActionURL()
337 {
338
339 }
340
341
342
343
344
345 public void testGetResourceURL()
346 {
347
348 }
349
350
351
352
353
354 public void testRenderView()
355 {
356
357 }
358
359
360
361
362
363 public void testRestoreView()
364 {
365
366 }
367
368
369
370
371
372 public void testWriteState()
373 {
374
375 }
376 }