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.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   * @author Mathias Broekelmann (latest modification by $Author: baranda $)
38   * @version $Revision: 544646 $ $Date: 2007-06-05 16:51:27 -0500 (Tue, 05 Jun 2007) $
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       * Test method for
52       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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          // Iterator<Locale> requstLocales = Arrays.asList(new Locale[] { Locale.GERMANY, Locale.US }).iterator();
65          // expect(_externalContext.getRequestLocales()).andReturn(requstLocales);
66      }
67  
68      /**
69       * Test method for
70       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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       * Test method for
84       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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       * Test method for
98       * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
114      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
131      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
153      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
169      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
184      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateLocale(javax.faces.context.FacesContext)}.
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      * Test method for
201      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
215      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#calculateRenderKitId(javax.faces.context.FacesContext)}.
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      * Test method for
235      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
264      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
265      * 
266      * @throws Exception
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      * Test method for
303      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#createView(javax.faces.context.FacesContext, java.lang.String)}.
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      * Test method for
334      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getActionURL(javax.faces.context.FacesContext, java.lang.String)}.
335      */
336     public void testGetActionURL()
337     {
338         // TODO
339     }
340 
341     /**
342      * Test method for
343      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#getResourceURL(javax.faces.context.FacesContext, java.lang.String)}.
344      */
345     public void testGetResourceURL()
346     {
347         // TODO
348     }
349 
350     /**
351      * Test method for
352      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#renderView(javax.faces.context.FacesContext, javax.faces.component.UIViewRoot)}.
353      */
354     public void testRenderView()
355     {
356         // TODO
357     }
358 
359     /**
360      * Test method for
361      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#restoreView(javax.faces.context.FacesContext, java.lang.String)}.
362      */
363     public void testRestoreView()
364     {
365         // TODO
366     }
367 
368     /**
369      * Test method for
370      * {@link org.apache.myfaces.application.jsp.JspViewHandlerImpl#writeState(javax.faces.context.FacesContext)}.
371      */
372     public void testWriteState()
373     {
374         // TODO
375     }
376 }