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