1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }