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 import java.util.Map;
22
23 import org.apache.struts2.ServletActionContext;
24 import org.apache.struts2.StrutsTestCase;
25 import org.easymock.EasyMock;
26 import org.easymock.IMocksControl;
27 import org.springframework.mock.web.MockHttpServletRequest;
28 import org.springframework.mock.web.MockHttpServletResponse;
29
30 import com.opensymphony.xwork2.ActionContext;
31 import com.opensymphony.xwork2.ActionInvocation;
32 import com.opensymphony.xwork2.ActionProxy;
33 import com.opensymphony.xwork2.config.entities.ActionConfig;
34 import com.opensymphony.xwork2.config.entities.ResultConfig;
35 import com.opensymphony.xwork2.util.ValueStack;
36
37
38 /***
39 * @version $Date: 2006-10-09 19:02:56 -0500 (Mon, 09 Oct 2006) $ $Id: ServletActionRedirectResultTest.java 454565 2006-10-10 00:02:56Z jmitchell $
40 */
41 public class ServletActionRedirectResultTest extends StrutsTestCase {
42
43 public void testIncludeParameterInResultWithConditionParseOn() throws Exception {
44
45 ResultConfig resultConfig = new ResultConfig();
46 resultConfig.addParam("actionName", "someActionName");
47 resultConfig.addParam("namespace", "someNamespace");
48 resultConfig.addParam("encode", "true");
49 resultConfig.addParam("parse", "true");
50 resultConfig.addParam("location", "someLocation");
51 resultConfig.addParam("prependServletContext", "true");
52 resultConfig.addParam("method", "someMethod");
53 resultConfig.addParam("param1", "${#value1}");
54 resultConfig.addParam("param2", "${#value2}");
55 resultConfig.addParam("param3", "${#value3}");
56
57
58
59 ActionContext context = ActionContext.getContext();
60 ValueStack stack = context.getValueStack();
61 context.getContextMap().put("value1", "value 1");
62 context.getContextMap().put("value2", "value 2");
63 context.getContextMap().put("value3", "value 3");
64 MockHttpServletRequest req = new MockHttpServletRequest();
65 MockHttpServletResponse res = new MockHttpServletResponse();
66 context.put(ServletActionContext.HTTP_REQUEST, req);
67 context.put(ServletActionContext.HTTP_RESPONSE, res);
68
69
70 Map<String, ResultConfig> results= new HashMap<String, ResultConfig>();
71 results.put("myResult", resultConfig);
72
73 ActionConfig actionConfig = new ActionConfig();
74 actionConfig.setResults(results);
75
76 ServletActionRedirectResult result = new ServletActionRedirectResult();
77 result.setActionName("myAction");
78 result.setNamespace("/myNamespace");
79 result.setParse(true);
80 result.setEncode(false);
81 result.setPrependServletContext(false);
82
83 IMocksControl control = EasyMock.createControl();
84 ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
85 ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
86 mockInvocation.getProxy();
87 control.andReturn(mockActionProxy);
88 mockInvocation.getResultCode();
89 control.andReturn("myResult");
90 mockActionProxy.getConfig();
91 control.andReturn(actionConfig);
92 mockInvocation.getInvocationContext();
93 control.andReturn(context);
94 mockInvocation.getStack();
95 control.andReturn(stack);
96 control.anyTimes();
97
98 control.replay();
99
100 result.execute(mockInvocation);
101 assertEquals("/myNamespace/myAction.action?param2=value+2¶m1=value+1¶m3=value+3", res.getRedirectedUrl());
102
103 control.verify();
104 }
105
106 public void testIncludeParameterInResult() throws Exception {
107
108 ResultConfig resultConfig = new ResultConfig();
109 resultConfig.addParam("actionName", "someActionName");
110 resultConfig.addParam("namespace", "someNamespace");
111 resultConfig.addParam("encode", "true");
112 resultConfig.addParam("parse", "true");
113 resultConfig.addParam("location", "someLocation");
114 resultConfig.addParam("prependServletContext", "true");
115 resultConfig.addParam("method", "someMethod");
116 resultConfig.addParam("param1", "value 1");
117 resultConfig.addParam("param2", "value 2");
118 resultConfig.addParam("param3", "value 3");
119
120 ActionContext context = ActionContext.getContext();
121 MockHttpServletRequest req = new MockHttpServletRequest();
122 MockHttpServletResponse res = new MockHttpServletResponse();
123 context.put(ServletActionContext.HTTP_REQUEST, req);
124 context.put(ServletActionContext.HTTP_RESPONSE, res);
125
126
127 Map<String, ResultConfig> results= new HashMap<String, ResultConfig>();
128 results.put("myResult", resultConfig);
129
130 ActionConfig actionConfig = new ActionConfig();
131 actionConfig.setResults(results);
132
133 ServletActionRedirectResult result = new ServletActionRedirectResult();
134 result.setActionName("myAction");
135 result.setNamespace("/myNamespace");
136 result.setParse(false);
137 result.setEncode(false);
138 result.setPrependServletContext(false);
139
140 IMocksControl control = EasyMock.createControl();
141 ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
142 ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
143 mockInvocation.getProxy();
144 control.andReturn(mockActionProxy);
145 mockInvocation.getResultCode();
146 control.andReturn("myResult");
147 mockActionProxy.getConfig();
148 control.andReturn(actionConfig);
149 mockInvocation.getInvocationContext();
150 control.andReturn(context);
151
152 control.replay();
153
154 result.execute(mockInvocation);
155 assertEquals("/myNamespace/myAction.action?param2=value+2¶m1=value+1¶m3=value+3", res.getRedirectedUrl());
156
157 control.verify();
158 }
159 }