View Javadoc

1   /*
2    * $Id: ServletRedirectResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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.io.StringWriter;
26  import java.io.PrintWriter;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import static javax.servlet.http.HttpServletResponse.*;
31  
32  import ognl.Ognl;
33  
34  import org.apache.struts2.ServletActionContext;
35  import org.apache.struts2.StrutsStatics;
36  import org.apache.struts2.StrutsTestCase;
37  import org.apache.struts2.config.StrutsXmlConfigurationProvider;
38  import org.springframework.mock.web.MockServletContext;
39  
40  import com.mockobjects.dynamic.C;
41  import com.mockobjects.dynamic.Mock;
42  import com.opensymphony.xwork2.ActionContext;
43  import com.opensymphony.xwork2.ActionInvocation;
44  import com.opensymphony.xwork2.config.ConfigurationManager;
45  import com.opensymphony.xwork2.config.entities.PackageConfig;
46  import com.opensymphony.xwork2.mock.MockActionInvocation;
47  import com.opensymphony.xwork2.util.ValueStackFactory;
48  
49  
50  /***
51   */
52  public class ServletRedirectResultTest extends StrutsTestCase implements StrutsStatics {
53  
54      protected ServletRedirectResult view;
55      private Mock requestMock;
56      private Mock responseMock;
57      protected ActionInvocation ai;
58  
59  
60      public void testAbsoluteRedirect() {
61          view.setLocation("/bar/foo.jsp");
62          responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
63          responseMock.expect("sendRedirect", C.args(C.eq("/context/bar/foo.jsp")));
64  
65          try {
66              view.execute(ai);
67              requestMock.verify();
68              responseMock.verify();
69          } catch (Exception e) {
70              e.printStackTrace();
71              fail();
72          }
73      }
74  
75      public void testAbsoluteRedirect303() {
76          view.setLocation("/bar/foo.jsp");
77          view.setStatusCode(303);
78          responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
79          responseMock.expect("setStatus", C.args(C.eq(SC_SEE_OTHER)));
80          responseMock.expect("setHeader", C.args(C.eq("Location"), C.eq("/context/bar/foo.jsp")));
81          StringWriter writer = new StringWriter();
82          responseMock.matchAndReturn("getWriter", new PrintWriter(writer));
83  
84          try {
85              view.execute(ai);
86              requestMock.verify();
87              responseMock.verify();
88          } catch (Exception e) {
89              e.printStackTrace();
90              fail();
91          }
92          assertEquals("/context/bar/foo.jsp", writer.toString());
93      }
94  
95      public void testPrependServletContextFalse() {
96          view.setLocation("/bar/foo.jsp");
97          view.setPrependServletContext(false);
98          responseMock.expectAndReturn("encodeRedirectURL", "/bar/foo.jsp", "/bar/foo.jsp");
99          responseMock.expect("sendRedirect", C.args(C.eq("/bar/foo.jsp")));
100 
101         try {
102             view.execute(ai);
103             requestMock.verify();
104             responseMock.verify();
105         } catch (Exception e) {
106             e.printStackTrace();
107             fail();
108         }
109     }
110 
111     public void testRelativeRedirect() throws Exception {
112         view.setLocation("foo.jsp");
113         requestMock.expectAndReturn("getParameterMap", new HashMap());
114         requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
115         requestMock.expectAndReturn("getRequestURI", "/namespace/some.action");
116         requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
117         responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp", "/context/namespace/foo.jsp");
118         responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp")));
119 
120         view.execute(ai);
121 
122         requestMock.verify();
123         responseMock.verify();
124     }
125     
126     public void testMultipleParametersRedirect() throws Exception {
127         view.setLocation("foo.jsp?foo=bar&baz=jim");
128         requestMock.expectAndReturn("getParameterMap", new HashMap());
129         requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
130         requestMock.expectAndReturn("getRequestURI", "/namespace/some.action");
131         requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
132         responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp?foo=bar&baz=jim", "/context/namespace/foo.jsp?foo=bar&baz=jim");
133         responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp?foo=bar&baz=jim")));
134 
135         view.execute(ai);
136 
137         requestMock.verify();
138         responseMock.verify();
139     }
140 
141     protected void setUp() throws Exception {
142         super.setUp();
143         configurationManager.getConfiguration().
144             addPackageConfig("foo", new PackageConfig.Builder("foo").namespace("/namespace").build());
145 
146         view = new ServletRedirectResult();
147         container.inject(view);
148 
149         responseMock = new Mock(HttpServletResponse.class);
150 
151         requestMock = new Mock(HttpServletRequest.class);
152         requestMock.matchAndReturn("getContextPath", "/context");
153 
154         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
155         ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
156         ac.put(ServletActionContext.HTTP_RESPONSE, responseMock.proxy());
157         MockActionInvocation ai = new MockActionInvocation();
158         ai.setInvocationContext(ac);
159         this.ai = ai;
160         ai.setStack(ActionContext.getContext().getValueStack());
161     }
162 }