1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.dispatcher;
19
20 import java.util.HashMap;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import ognl.Ognl;
26
27 import org.apache.struts2.ServletActionContext;
28 import org.apache.struts2.StrutsStatics;
29 import org.apache.struts2.StrutsTestCase;
30 import org.apache.struts2.config.StrutsXmlConfigurationProvider;
31 import org.springframework.mock.web.MockServletContext;
32
33 import com.mockobjects.dynamic.C;
34 import com.mockobjects.dynamic.Mock;
35 import com.opensymphony.xwork2.ActionContext;
36 import com.opensymphony.xwork2.ActionInvocation;
37 import com.opensymphony.xwork2.config.ConfigurationManager;
38 import com.opensymphony.xwork2.config.entities.PackageConfig;
39 import com.opensymphony.xwork2.mock.MockActionInvocation;
40 import com.opensymphony.xwork2.util.ValueStackFactory;
41
42
43 /***
44 */
45 public class ServletRedirectResultTest extends StrutsTestCase implements StrutsStatics {
46
47 protected ServletRedirectResult view;
48 private Mock requestMock;
49 private Mock responseMock;
50 protected ActionInvocation ai;
51
52
53 public void testAbsoluteRedirect() {
54 view.setLocation("/bar/foo.jsp");
55 responseMock.expectAndReturn("encodeRedirectURL", "/context/bar/foo.jsp", "/context/bar/foo.jsp");
56 responseMock.expect("sendRedirect", C.args(C.eq("/context/bar/foo.jsp")));
57
58 try {
59 view.execute(ai);
60 requestMock.verify();
61 responseMock.verify();
62 } catch (Exception e) {
63 e.printStackTrace();
64 fail();
65 }
66 }
67
68 public void testPrependServletContextFalse() {
69 view.setLocation("/bar/foo.jsp");
70 view.setPrependServletContext(false);
71 responseMock.expectAndReturn("encodeRedirectURL", "/bar/foo.jsp", "/bar/foo.jsp");
72 responseMock.expect("sendRedirect", C.args(C.eq("/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 testRelativeRedirect() throws Exception {
85 view.setLocation("foo.jsp");
86 requestMock.expectAndReturn("getParameterMap", new HashMap());
87 requestMock.expectAndReturn("getServletPath", "/namespace/some.action");
88 requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
89 responseMock.expectAndReturn("encodeRedirectURL", "/context/namespace/foo.jsp", "/context/namespace/foo.jsp");
90 responseMock.expect("sendRedirect", C.args(C.eq("/context/namespace/foo.jsp")));
91
92 view.execute(ai);
93
94 requestMock.verify();
95 responseMock.verify();
96 }
97
98 protected void setUp() throws Exception {
99 super.setUp();
100 Dispatcher du = new Dispatcher(new MockServletContext());
101 Dispatcher.setInstance(du);
102 ConfigurationManager cm = new ConfigurationManager();
103 cm.addConfigurationProvider(new StrutsXmlConfigurationProvider("struts.xml", false));
104 du.setConfigurationManager(cm);
105 du.getConfigurationManager().getConfiguration().
106 addPackageConfig("foo", new PackageConfig("foo", "/namespace", false, null));
107
108
109 view = new ServletRedirectResult();
110
111 responseMock = new Mock(HttpServletResponse.class);
112
113 requestMock = new Mock(HttpServletRequest.class);
114 requestMock.matchAndReturn("getContextPath", "/context");
115
116 ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
117 ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
118 ac.put(ServletActionContext.HTTP_RESPONSE, responseMock.proxy());
119 MockActionInvocation ai = new MockActionInvocation();
120 ai.setInvocationContext(ac);
121 this.ai = ai;
122 ai.setStack(ValueStackFactory.getFactory().createValueStack());
123 }
124 }