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 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   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
39   * @version $Revision: 1151677 $ $Date: 2011-07-27 19:03:59 -0500 (Wed, 27 Jul 2011) $
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       * Test method for
53       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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          // Iterator<Locale> requstLocales = Arrays.asList(new Locale[] { Locale.GERMANY, Locale.US }).iterator();
66          // expect(_externalContext.getRequestLocales()).andReturn(requstLocales);
67      }
68  
69      /**
70       * Test method for
71       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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       * Test method for
87       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
103      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
121      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
138      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
160      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
176      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
191      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
208      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
224      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
246      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
275      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
276      * 
277      * @throws Exception
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      * Test method for
314      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
345      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getActionURL(javax.faces.context.FacesContext, java.lang.String)}.
346      */
347     public void testGetActionURL()
348     {
349         // TODO
350     }
351 
352     /**
353      * Test method for
354      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getResourceURL(javax.faces.context.FacesContext, java.lang.String)}.
355      */
356     public void testGetResourceURL()
357     {
358         // TODO
359     }
360 
361     /**
362      * Test method for
363      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#renderView(javax.faces.context.FacesContext, javax.faces.component.UIViewRoot)}.
364      */
365     public void testRenderView()
366     {
367         // TODO
368     }
369 
370     /**
371      * Test method for
372      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#restoreView(javax.faces.context.FacesContext, java.lang.String)}.
373      */
374     public void testRestoreView()
375     {
376         // TODO
377     }
378 
379     /**
380      * Test method for
381      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#writeState(javax.faces.context.FacesContext)}.
382      */
383     public void testWriteState()
384     {
385         // TODO
386     }
387 }