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