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