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.lifecycle;
20  
21  import static org.easymock.EasyMock.expect;
22  
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.faces.FacesException;
28  
29  import org.apache.myfaces.Assert;
30  import org.apache.myfaces.FacesTestCase;
31  import org.apache.myfaces.TestRunner;
32  
33  /**
34   * @author Mathias Broekelmann (latest modification by $Author: struberg $)
35   * @version $Revision: 1188694 $ $Date: 2011-10-25 10:07:44 -0500 (Tue, 25 Oct 2011) $
36   */
37  public class DefaultRestoreViewSupportTest extends FacesTestCase
38  {
39  
40      private DefaultRestoreViewSupport _testimpl;
41  
42      protected void setUp() throws Exception
43      {
44          super.setUp();
45          _testimpl = new DefaultRestoreViewSupport();
46      }
47  
48      /**
49       * Test method for
50       * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#processComponentBinding(javax.faces.context.FacesContext, javax.faces.component.UIComponent)}.
51       */
52      /*
53      public void testProcessComponentBinding()
54      {
55          UIComponent root = _mocksControl.createMock(UIComponent.class);
56          UIComponent testcomponent = _mocksControl.createMock(UIComponent.class);
57          ValueExpression rootExpression = _mocksControl.createMock(ValueExpression.class);
58          ValueExpression testExpression = _mocksControl.createMock(ValueExpression.class);
59          
60          _mocksControl.checkOrder(true);
61          expect(root.getValueExpression(eq("binding"))).andReturn(rootExpression);
62          expect(_facesContext.getELContext()).andReturn(_elContext);
63          rootExpression.setValue(same(_elContext), same(root));
64          expect(root.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[] { testcomponent }).iterator());
65          expect(testcomponent.getValueExpression(eq("binding"))).andReturn(testExpression);
66          expect(_facesContext.getELContext()).andReturn(_elContext);
67          testExpression.setValue(same(_elContext), same(testcomponent));
68          
69          List<UIComponent> emptyList = Collections.emptyList();
70          expect(testcomponent.getFacetsAndChildren()).andReturn(emptyList.iterator());
71  
72          _mocksControl.replay();
73          _testimpl.processComponentBinding(_facesContext, root);
74          _mocksControl.verify();
75      }
76      */
77  
78      /**
79       * Test method for
80       * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#calculateViewId(javax.faces.context.FacesContext)}.
81       */
82      public void testCalculateViewIdFromRequestAttributeIncludePathInfo()
83      {
84          _mocksControl.checkOrder(true);
85          expect(_facesContext.getExternalContext()).andReturn(_externalContext);
86          Map<String, Object> map = new HashMap<String, Object>();
87          String expectedValue = "javax.servlet.include.path_info_VIEWID";
88          map.put("javax.servlet.include.path_info", expectedValue);
89          expect(_externalContext.getRequestMap()).andReturn(map);
90          //expect(_facesContext.getApplication()).andReturn(_application);
91          //ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
92          //expect(_application.getViewHandler()).andReturn(viewHandler);
93          //expect(viewHandler.deriveViewId(
94          //        same(_facesContext), eq(expectedValue))).andReturn(expectedValue);
95          _mocksControl.replay();
96          assertEquals(expectedValue, _testimpl.calculateViewId(_facesContext));
97          _mocksControl.verify();
98      }
99  
100     /**
101      * Test method for
102      * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#calculateViewId(javax.faces.context.FacesContext)}.
103      */
104     public void testCalculateViewIdFromRequestPathInfo()
105     {
106         _mocksControl.checkOrder(true);
107         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
108         
109         Map<String, Object> emptyMap = Collections.emptyMap();
110         expect(_externalContext.getRequestMap()).andReturn(emptyMap);
111         String expectedValue = "requestPathInfo_VIEWID";
112         expect(_externalContext.getRequestPathInfo()).andReturn(expectedValue);
113         //expect(_facesContext.getApplication()).andReturn(_application);
114         //ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
115         //expect(_application.getViewHandler()).andReturn(viewHandler);
116         //expect(viewHandler.deriveViewId(
117         //        same(_facesContext), eq(expectedValue))).andReturn(expectedValue);
118 
119         _mocksControl.replay();
120         assertEquals(expectedValue, _testimpl.calculateViewId(_facesContext));
121         _mocksControl.verify();
122     }
123 
124     /**
125      * Test method for
126      * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#calculateViewId(javax.faces.context.FacesContext)}.
127      */
128     public void testCalculateViewIdFromRequestAttributeIncludeServletPath()
129     {
130         _mocksControl.checkOrder(true);
131         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
132         Map<String, Object> map = new HashMap<String, Object>();
133         String expectedValue = "javax.servlet.include.servlet_path_VIEWID";
134         map.put("javax.servlet.include.servlet_path", expectedValue);
135         expect(_externalContext.getRequestMap()).andReturn(map);
136         expect(_externalContext.getRequestPathInfo()).andReturn(null);
137         //expect(_facesContext.getApplication()).andReturn(_application);
138         //ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
139         //expect(_application.getViewHandler()).andReturn(viewHandler);
140         //expect(viewHandler.deriveViewId(
141         //        same(_facesContext), eq(expectedValue))).andReturn(expectedValue);
142 
143         _mocksControl.replay();
144         assertEquals(expectedValue, _testimpl.calculateViewId(_facesContext));
145         _mocksControl.verify();
146     }
147 
148     /**
149      * Test method for
150      * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#calculateViewId(javax.faces.context.FacesContext)}.
151      */
152     public void testCalculateViewIdFromRequestServletPath()
153     {
154         _mocksControl.checkOrder(true);
155         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
156         
157         Map<String, Object> emptyMap = Collections.emptyMap();
158         
159         expect(_externalContext.getRequestMap()).andReturn(emptyMap);
160         expect(_externalContext.getRequestPathInfo()).andReturn(null);
161         String expectedValue = "RequestServletPath_VIEWID";
162         expect(_externalContext.getRequestServletPath()).andReturn(expectedValue);
163         //expect(_facesContext.getApplication()).andReturn(_application);
164         //ViewHandler viewHandler = _mocksControl.createMock(ViewHandler.class);
165         //expect(_application.getViewHandler()).andReturn(viewHandler);
166         //expect(viewHandler.deriveViewId(
167         //        same(_facesContext), eq(expectedValue))).andReturn(expectedValue);
168 
169         _mocksControl.replay();
170         assertEquals(expectedValue, _testimpl.calculateViewId(_facesContext));
171         _mocksControl.verify();
172     }
173 
174     /**
175      * Test method for
176      * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#calculateViewId(javax.faces.context.FacesContext)}.
177      */
178     public void testCalculateViewIdFacesException()
179     {
180         _mocksControl.checkOrder(true);
181         expect(_facesContext.getExternalContext()).andReturn(_externalContext);
182         
183         Map<String, Object> emptyMap = Collections.emptyMap();
184         
185         expect(_externalContext.getRequestMap()).andReturn(emptyMap);
186         expect(_externalContext.getRequestPathInfo()).andReturn(null);
187         expect(_externalContext.getRequestServletPath()).andReturn(null);
188 
189         _mocksControl.replay();
190         Assert.assertException(FacesException.class, new TestRunner()
191         {
192             public void run() throws Throwable
193             {
194                 _testimpl.calculateViewId(_facesContext);
195             }
196         });
197         _mocksControl.verify();
198     }
199 
200     /**
201      * Test method for
202      * {@link org.apache.myfaces.lifecycle.DefaultRestoreViewSupport#isPostback(javax.faces.context.FacesContext)}.
203      */
204     public void testIsPostback()
205     {
206         // TODO: not testable unless static call to RendererUtils is removed
207     }
208 
209 }