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;
20  
21  import static org.easymock.EasyMock.*;
22  
23  import java.lang.reflect.Method;
24  
25  import javax.faces.application.ViewHandler;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.myfaces.Assert;
29  import org.apache.myfaces.FacesTestCase;
30  import org.apache.myfaces.TestRunner;
31  import org.apache.myfaces.application.DefaultViewHandlerSupport.FacesServletMapping;
32  
33  /**
34   * @author Mathias Broekelmann (latest modification by $Author: skitching $)
35   * @version $Revision: 673811 $ $Date: 2008-07-03 16:18:36 -0500 (Thu, 03 Jul 2008) $
36   */
37  public class DefaultViewHandlerSupportTest extends FacesTestCase
38  {
39  
40      private static final String DEFAULT_SUFFIX = ".jsp";
41  
42      /**
43       * Test method for
44       * {@link org.apache.myfaces.application.DefaultViewHandlerSupport#calculateViewId(javax.faces.context.FacesContext, java.lang.String)}.
45       * 
46       * @throws Exception
47       */
48      public void testCalculateViewId() throws Exception
49      {
50          // extension mapping
51          assertCalculateViewId("xx.jsp", "/xx.faces", null, ".jsp", "xx.faces");
52          assertCalculateViewId("xx.jspx", "/xx.faces", null, ".jspx", "xx.faces");
53          assertCalculateViewId("xx.jsp", "/xx.jsf", null, ".jsp", "xx.jsf");
54          assertCalculateViewId("xx.jspx", "/xx.jsf", null, ".jspx", "xx.jsf");
55  
56          // path mapping
57          assertCalculateViewId("xx.jsp", "/faces", "/xx.jsp", ".jsp", "xx.jsp");
58          assertCalculateViewId("xx.xyz", "/faces", "/xx.xyz", ".jsp", "xx.xyz");
59      }
60      
61      private void assertCalculateViewId(
62              String expectedViewId, String servletPath, String pathInfo, 
63                  String contextSuffix, String viewId) throws Exception
64      {
65          DefaultViewHandlerSupport support = createdMockedViewHandlerSupport();
66          
67          expect(support.getContextSuffix(same(_facesContext))).andReturn(contextSuffix);
68          expect(support.getFacesServletMapping(same(_facesContext))).andReturn(
69                  DefaultViewHandlerSupport.calculateFacesServletMapping(servletPath, pathInfo));
70          expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
71          
72          _mocksControl.replay();
73          assertEquals(expectedViewId, support.calculateViewId(_facesContext, viewId));
74          _mocksControl.reset();
75      }
76  
77      /**
78       * Test method for
79       * {@link org.apache.myfaces.application.DefaultViewHandlerSupport#applyDefaultSuffix(javax.faces.context.FacesContext, java.lang.String)}.
80       */
81      public void testApplyDefaultSuffix()
82      {
83          // TODO
84      }
85  
86      /**
87       * Test method for
88       * {@link org.apache.myfaces.application.DefaultViewHandlerSupport#calculateActionURL(javax.faces.context.FacesContext, java.lang.String)}.
89       * 
90       * @throws
91       * @throws Exception
92       */
93      public void testCalculateActionURL() throws Exception
94      {
95          Assert.assertException(IllegalArgumentException.class, new TestRunner()
96          {
97              public void run() throws Throwable
98              {
99                  new DefaultViewHandlerSupport().calculateActionURL(_facesContext, "xxx");
100             }
101         });
102 
103         // extension mapping
104         assertActionUrl("/context/faces/testViewId.faces", "/context", "/testViewId.faces", null, "/faces/testViewId.jsp");
105         assertActionUrl("/ctx/faces/testViewId.jsf", "/ctx",  "/faces/testViewId.jsf", null, "/faces/testViewId.jsp");
106 
107         // path mapping
108         assertActionUrl("/context/testViewId.jsx", "/context", "", "/testViewId.jsx", "/testViewId.jsx");
109         assertActionUrl("/context/faces/testViewId.jsp", "/context", "/faces", "/testViewId.jsp", "/testViewId.jsp");
110     }
111     
112     private void assertActionUrl(
113             String expectedActionUrl, String contextPath, String servletPath, 
114                 String pathInfo, String viewId) throws Exception 
115     {
116         DefaultViewHandlerSupport support = createdMockedViewHandlerSupport();
117         
118         expect(support.getContextSuffix(same(_facesContext))).andReturn(DEFAULT_SUFFIX);
119         expect(support.getFacesServletMapping(same(_facesContext))).andReturn(
120                 DefaultViewHandlerSupport.calculateFacesServletMapping(servletPath, pathInfo));
121         expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
122         expect(_externalContext.getRequestContextPath()).andReturn(contextPath);
123         
124         _mocksControl.replay();
125         assertEquals(expectedActionUrl, support.calculateActionURL(_facesContext, viewId));
126         _mocksControl.reset();
127     }
128 
129     private DefaultViewHandlerSupport createdMockedViewHandlerSupport() throws Exception
130     {
131         Class<DefaultViewHandlerSupport> supportClass = DefaultViewHandlerSupport.class;
132         Class<?>[] parameterTypes = new Class[] { FacesContext.class };
133 
134         DefaultViewHandlerSupport support = _mocksControl.createMock(
135             supportClass, new Method[] {
136                 // the methods "getFacesServletMapping" and "getContextSuffix" will be mocked
137                 supportClass.getDeclaredMethod("getFacesServletMapping", parameterTypes),
138                 supportClass.getDeclaredMethod("getContextSuffix", parameterTypes) 
139             }
140         );
141 
142         return support;
143     }
144     
145     /**
146      * Test method for
147      * {@link org.apache.myfaces.application.DefaultViewHandlerSupport#calculateFacesServletMapping(String, String)}.
148      */
149     public void testCalculateFacesServletMapping() throws Exception
150     {
151         assertExtensionMapping(".jsf", "/index.jsf", null);
152         assertExtensionMapping(".jsf", "/secure/login.jsf", null);
153 
154         assertPathBasedMapping("/faces", "/faces", null);
155         assertPathBasedMapping("/faces", "/faces", "/index.jsp");
156         assertPathBasedMapping("/faces", "/faces", "/secure/login.jsp");
157     }
158 
159     /**
160      * Convenience method that tests if the ViewHandlerSupport object knows that
161      * the "given request" has been handled by a FacesServlet being postfix
162      * mapped. Extract the path elements of a Request-URI according to the
163      * Servlet specification and pass the servletPath and pathInfo element.
164      * 
165      * @param extension
166      *            expected extension
167      * @param servletPath
168      *            servletPath of the "current request" (e.g. "/faces")
169      * @param pathInfo
170      *            <code>null</code>
171      */
172     private void assertExtensionMapping(
173             String extension, String servletPath, String pathInfo)
174     {
175         FacesServletMapping mapping = 
176             DefaultViewHandlerSupport.calculateFacesServletMapping(servletPath, pathInfo);
177         assertTrue(mapping.isExtensionMapping());
178         assertEquals(extension, mapping.getExtension());
179     }
180     
181     /**
182      * Convenience method that tests if the ViewHandlerSupport object knows that
183      * the "given request" has been handled by a FacesServlet being prefix
184      * mapped. Extract the path elements of a Request-URI according to the
185      * Servlet specification and pass the servletPath and pathInfo element.
186      * 
187      * @param prefix
188      *            expected prefix
189      * @param servletPath
190      *            servletPath of the "current request" (e.g. "/faces")
191      * @param pathInfo
192      *            infoPath of the "current request" (e.g. "/login.jsp")
193      */
194     private void assertPathBasedMapping(
195             String prefix, String servletPath, String pathInfo)
196     {
197         FacesServletMapping mapping = 
198             DefaultViewHandlerSupport.calculateFacesServletMapping(servletPath, pathInfo);
199         assertFalse(mapping.isExtensionMapping());
200         assertEquals(prefix, mapping.getPrefix());
201     }
202     
203     /**
204      * Test method for
205      * {@link org.apache.myfaces.application.DefaultViewHandlerSupport#getContextSuffix(javax.faces.context.FacesContext)}.
206      */
207     public void testGetContextSuffix()
208     {
209         DefaultViewHandlerSupport testImpl = new DefaultViewHandlerSupport();
210         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
211         expect(_externalContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME)).andReturn(null);
212         _mocksControl.replay();
213         assertEquals(ViewHandler.DEFAULT_SUFFIX, testImpl.getContextSuffix(_facesContext));
214         _mocksControl.reset();
215         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
216         expect(_externalContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME)).andReturn(".xxx");
217         _mocksControl.replay();
218         assertEquals(".xxx", testImpl.getContextSuffix(_facesContext));
219     }
220 }