View Javadoc

1   /*
2    * $Id: ServletRedirectResultTest.java 474191 2006-11-13 08:30:40Z 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     protected void setUp() throws Exception {
102         super.setUp();
103         configurationManager.getConfiguration().
104             addPackageConfig("foo", new PackageConfig("foo", "/namespace", false, null));
105 
106 
107         view = new ServletRedirectResult();
108         container.inject(view);
109 
110         responseMock = new Mock(HttpServletResponse.class);
111 
112         requestMock = new Mock(HttpServletRequest.class);
113         requestMock.matchAndReturn("getContextPath", "/context");
114 
115         ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
116         ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
117         ac.put(ServletActionContext.HTTP_RESPONSE, responseMock.proxy());
118         MockActionInvocation ai = new MockActionInvocation();
119         ai.setInvocationContext(ac);
120         this.ai = ai;
121         ai.setStack(ValueStackFactory.getFactory().createValueStack());
122     }
123 }