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