View Javadoc

1   /*
2    * $Id: ServletRedirectResultTest.java 755605 2009-03-18 14:36:33Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dispatcher;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.io.StringWriter;
27  import java.io.PrintWriter;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import static javax.servlet.http.HttpServletResponse.*;
32  
33  import ognl.Ognl;
34  
35  import org.apache.struts2.ServletActionContext;
36  import org.apache.struts2.StrutsStatics;
37  import org.apache.struts2.StrutsTestCase;
38  import org.apache.struts2.dispatcher.mapper.ActionMapper;
39  import org.apache.struts2.config.StrutsXmlConfigurationProvider;
40  import org.springframework.mock.web.MockServletContext;
41  import org.springframework.mock.web.MockHttpServletRequest;
42  import org.springframework.mock.web.MockHttpServletResponse;
43  import org.easymock.EasyMock;
44  import org.easymock.IMocksControl;
45  
46  import com.mockobjects.dynamic.C;
47  import com.mockobjects.dynamic.Mock;
48  import com.opensymphony.xwork2.ActionContext;
49  import com.opensymphony.xwork2.ActionInvocation;
50  import com.opensymphony.xwork2.ActionProxy;
51  import com.opensymphony.xwork2.config.ConfigurationManager;
52  import com.opensymphony.xwork2.config.entities.PackageConfig;
53  import com.opensymphony.xwork2.config.entities.ResultConfig;
54  import com.opensymphony.xwork2.config.entities.ActionConfig;
55  import com.opensymphony.xwork2.mock.MockActionInvocation;
56  import com.opensymphony.xwork2.util.ValueStackFactory;
57  
58  
59  /***
60   */
61  public class ServletRedirectResultTest extends StrutsTestCase implements StrutsStatics {
62  
63      protected ServletRedirectResult view;
64      private Mock requestMock;
65      private Mock responseMock;
66      protected ActionInvocation ai;
67  
68  
69      public void testAbsoluteRedirect() {
70          view.setLocation("/bar/foo.jsp");
71          responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
72          responseMock.expect("sendRedirect", C.args(C.eq("/context/bar/foo.jsp")));
73  
74          try {
75              view.execute(ai);
76              requestMock.verify();
77              responseMock.verify();
78          } catch (Exception e) {
79              e.printStackTrace();
80              fail();
81          }
82      }
83  
84      public void testAbsoluteRedirect303() {
85          view.setLocation("/bar/foo.jsp");
86          view.setStatusCode(303);
87          responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
88          responseMock.expect("setStatus", C.args(C.eq(SC_SEE_OTHER)));
89          responseMock.expect("setHeader", C.args(C.eq("Location"), C.eq("/context/bar/foo.jsp")));
90          StringWriter writer = new StringWriter();
91          responseMock.matchAndReturn("getWriter", new PrintWriter(writer));
92  
93          try {
94              view.execute(ai);
95              requestMock.verify();
96              responseMock.verify();
97          } catch (Exception e) {
98              e.printStackTrace();
99              fail();
100         }
101         assertEquals("/context/bar/foo.jsp", writer.toString());
102     }
103 
104     public void testPrependServletContextFalse() {
105         view.setLocation("/bar/foo.jsp");
106         view.setPrependServletContext(false);
107         responseMock.expectAndReturn("encodeRedirectURL", "/bar/foo.jsp", "/bar/foo.jsp");
108         responseMock.expect("sendRedirect", C.args(C.eq("/bar/foo.jsp")));
109 
110         try {
111             view.execute(ai);
112             requestMock.verify();
113             responseMock.verify();
114         } catch (Exception e) {
115             e.printStackTrace();
116             fail();
117         }
118     }
119 
120     public void testRelativeRedirect() throws Exception {
121         view.setLocation("foo.jsp");
122         requestMock.expectAndReturn("getParameterMap", new HashMap());
123         requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
124         requestMock.expectAndReturn("getRequestURI", "/namespace/some.action");
125         requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
126         responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp", "/context/namespace/foo.jsp");
127         responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp")));
128 
129         view.execute(ai);
130 
131         requestMock.verify();
132         responseMock.verify();
133     }
134     
135     public void testMultipleParametersRedirect() throws Exception {
136         view.setLocation("foo.jsp?foo=bar&baz=jim");
137         requestMock.expectAndReturn("getParameterMap", new HashMap());
138         requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
139         requestMock.expectAndReturn("getRequestURI", "/namespace/some.action");
140         requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
141         responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp?foo=bar&baz=jim", "/context/namespace/foo.jsp?foo=bar&baz=jim");
142         responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp?foo=bar&baz=jim")));
143 
144         view.execute(ai);
145 
146         requestMock.verify();
147         responseMock.verify();
148     }
149 
150     public void testIncludeParameterInResult() throws Exception {
151 
152         ResultConfig resultConfig = new ResultConfig.Builder("", "")
153             .addParam("namespace", "someNamespace")
154             .addParam("encode", "true")
155             .addParam("parse", "true")
156             .addParam("location", "someLocation")
157             .addParam("prependServletContext", "true")
158             .addParam("method", "someMethod")
159             .addParam("param1", "value 1")
160             .addParam("param2", "value 2")
161             .addParam("param3", "value 3")
162             .build();
163 
164         ActionContext context = ActionContext.getContext();
165         MockHttpServletRequest req = new MockHttpServletRequest();
166         MockHttpServletResponse res = new MockHttpServletResponse();
167         context.put(ServletActionContext.HTTP_REQUEST, req);
168         context.put(ServletActionContext.HTTP_RESPONSE, res);
169 
170 
171         Map<String, ResultConfig> results=  new HashMap<String, ResultConfig>();
172         results.put("myResult", resultConfig);
173 
174         ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
175                 .addResultConfigs(results).build();
176 
177         ServletRedirectResult result = new ServletRedirectResult();
178         result.setLocation("/myNamespace/myAction.action");
179         result.setParse(false);
180         result.setEncode(false);
181         result.setPrependServletContext(false);
182 
183         IMocksControl control = EasyMock.createControl();
184         ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
185         ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
186         mockInvocation.getProxy();
187         control.andReturn(mockActionProxy);
188         mockInvocation.getResultCode();
189         control.andReturn("myResult");
190         mockActionProxy.getConfig();
191         control.andReturn(actionConfig);
192         mockInvocation.getInvocationContext();
193         control.andReturn(context);
194 
195         control.replay();
196         result.setActionMapper(container.getInstance(ActionMapper.class));
197         result.execute(mockInvocation);
198         assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3", res.getRedirectedUrl());
199 
200         control.verify();
201     }
202 
203     protected void setUp() throws Exception {
204         super.setUp();
205         configurationManager.getConfiguration().
206             addPackageConfig("foo", new PackageConfig.Builder("foo").namespace("/namespace").build());
207 
208         view = new ServletRedirectResult();
209         container.inject(view);
210 
211         responseMock = new Mock(HttpServletResponse.class);
212 
213         requestMock = new Mock(HttpServletRequest.class);
214         requestMock.matchAndReturn("getContextPath", "/context");
215 
216          ResultConfig resultConfig = new ResultConfig.Builder("", "").build();
217 
218         Map<String, ResultConfig> results=  new HashMap<String, ResultConfig>();
219         results.put("myResult", resultConfig);
220 
221         ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
222                 .addResultConfigs(results).build();
223 
224         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
225         ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
226         ac.put(ServletActionContext.HTTP_RESPONSE, responseMock.proxy());
227         MockActionInvocation ai = new MockActionInvocation();
228         ai.setInvocationContext(ac);
229         ai.setResultCode("myResult");
230         ActionProxy mockActionProxy = EasyMock.createNiceMock(ActionProxy.class);
231         ai.setProxy(mockActionProxy);
232         EasyMock.expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
233         EasyMock.replay(mockActionProxy);
234         this.ai = ai;
235         ai.setStack(ActionContext.getContext().getValueStack());
236     }
237 }