View Javadoc

1   /*
2    * $Id: ServletRedirectResultTest.java 539819 2007-05-20 03:06:34Z mrdon $
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  package org.apache.struts2.dispatcher;
22  
23  import java.util.HashMap;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import ognl.Ognl;
29  
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.StrutsStatics;
32  import org.apache.struts2.StrutsTestCase;
33  import org.apache.struts2.config.StrutsXmlConfigurationProvider;
34  import org.springframework.mock.web.MockServletContext;
35  
36  import com.mockobjects.dynamic.C;
37  import com.mockobjects.dynamic.Mock;
38  import com.opensymphony.xwork2.ActionContext;
39  import com.opensymphony.xwork2.ActionInvocation;
40  import com.opensymphony.xwork2.config.ConfigurationManager;
41  import com.opensymphony.xwork2.config.entities.PackageConfig;
42  import com.opensymphony.xwork2.mock.MockActionInvocation;
43  import com.opensymphony.xwork2.util.ValueStackFactory;
44  
45  
46  /***
47   */
48  public class ServletRedirectResultTest extends StrutsTestCase implements StrutsStatics {
49  
50      protected ServletRedirectResult view;
51      private Mock requestMock;
52      private Mock responseMock;
53      protected ActionInvocation ai;
54  
55  
56      public void testAbsoluteRedirect() {
57          view.setLocation("/bar/foo.jsp");
58          responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
59          responseMock.expect("sendRedirect", C.args(C.eq("/context/bar/foo.jsp")));
60  
61          try {
62              view.execute(ai);
63              requestMock.verify();
64              responseMock.verify();
65          } catch (Exception e) {
66              e.printStackTrace();
67              fail();
68          }
69      }
70  
71      public void testPrependServletContextFalse() {
72          view.setLocation("/bar/foo.jsp");
73          view.setPrependServletContext(false);
74          responseMock.expectAndReturn("encodeRedirectURL", "/bar/foo.jsp", "/bar/foo.jsp");
75          responseMock.expect("sendRedirect", C.args(C.eq("/bar/foo.jsp")));
76  
77          try {
78              view.execute(ai);
79              requestMock.verify();
80              responseMock.verify();
81          } catch (Exception e) {
82              e.printStackTrace();
83              fail();
84          }
85      }
86  
87      public void testRelativeRedirect() throws Exception {
88          view.setLocation("foo.jsp");
89          requestMock.expectAndReturn("getParameterMap", new HashMap());
90          requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
91          requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
92          responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp", "/context/namespace/foo.jsp");
93          responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp")));
94  
95          view.execute(ai);
96  
97          requestMock.verify();
98          responseMock.verify();
99      }
100     
101     public void testMultipleParametersRedirect() throws Exception {
102         view.setLocation("foo.jsp?foo=bar&baz=jim");
103         requestMock.expectAndReturn("getParameterMap", new HashMap());
104         requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
105         requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
106         responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp?foo=bar&baz=jim", "/context/namespace/foo.jsp?foo=bar&baz=jim");
107         responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp?foo=bar&baz=jim")));
108 
109         view.execute(ai);
110 
111         requestMock.verify();
112         responseMock.verify();
113     }
114 
115     protected void setUp() throws Exception {
116         super.setUp();
117         configurationManager.getConfiguration().
118             addPackageConfig("foo", new PackageConfig("foo", "/namespace", false, null));
119 
120 
121         view = new ServletRedirectResult();
122         container.inject(view);
123 
124         responseMock = new Mock(HttpServletResponse.class);
125 
126         requestMock = new Mock(HttpServletRequest.class);
127         requestMock.matchAndReturn("getContextPath", "/context");
128 
129         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
130         ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
131         ac.put(ServletActionContext.HTTP_RESPONSE, responseMock.proxy());
132         MockActionInvocation ai = new MockActionInvocation();
133         ai.setInvocationContext(ac);
134         this.ai = ai;
135         ai.setStack(ValueStackFactory.getFactory().createValueStack());
136     }
137 }