View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Mathias Broekelmann (latest modification by $Author: struberg $)
49   * @version $Revision: 1188246 $ $Date: 2011-10-24 17:28:22 +0000 (Mon, 24 Oct 2011) $
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       * Test method for
63       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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          // Iterator<Locale> requstLocales = Arrays.asList(new Locale[] { Locale.GERMANY, Locale.US }).iterator();
76          // expect(_externalContext.getRequestLocales()).andReturn(requstLocales);
77      }
78  
79      /**
80       * Test method for
81       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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       * Test method for
97       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
113      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
131      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
148      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
170      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
186      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
201      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
218      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
234      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
256      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
285      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
286      * 
287      * @throws Exception
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      * Test method for
324      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
355      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getActionURL(javax.faces.context.FacesContext, java.lang.String)}.
356      */
357     public void testGetActionURL()
358     {
359         // TODO
360     }
361 
362     /**
363      * Test method for
364      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getResourceURL(javax.faces.context.FacesContext, java.lang.String)}.
365      */
366     public void testGetResourceURL()
367     {
368         // TODO
369     }
370 
371     /**
372      * Test method for
373      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#renderView(javax.faces.context.FacesContext, javax.faces.component.UIViewRoot)}.
374      */
375     public void testRenderView()
376     {
377         // TODO
378     }
379 
380     /**
381      * Test method for
382      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#restoreView(javax.faces.context.FacesContext, java.lang.String)}.
383      */
384     public void testRestoreView()
385     {
386         // TODO
387     }
388 
389     /**
390      * Test method for
391      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#writeState(javax.faces.context.FacesContext)}.
392      */
393     public void testWriteState()
394     {
395         // TODO
396     }
397 }