1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp;
23
24 import java.util.HashMap;
25
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.PageContext;
28
29 import org.apache.struts2.ServletActionContext;
30 import org.apache.struts2.StrutsException;
31 import org.apache.struts2.TestAction;
32 import org.apache.struts2.TestActionTagResult;
33 import org.apache.struts2.TestConfigurationProvider;
34 import org.apache.struts2.components.ActionComponent;
35
36 import com.mockobjects.dynamic.Mock;
37 import com.opensymphony.xwork2.Action;
38 import com.opensymphony.xwork2.ActionContext;
39 import com.opensymphony.xwork2.ActionInvocation;
40 import com.opensymphony.xwork2.ActionProxy;
41 import com.opensymphony.xwork2.config.entities.ActionConfig;
42
43
44 /***
45 * Unit test for {@link ActionTag}.
46 */
47 public class ActionTagTest extends AbstractTagTest {
48
49 public void testActionTagWithNamespace() {
50 request.setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE + "/" + "foo.action");
51
52 ActionTag tag = new ActionTag();
53 tag.setPageContext(pageContext);
54 tag.setName(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
55 tag.setId(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
56
57 try {
58 tag.doStartTag();
59 ActionComponent ac = ((ActionComponent) tag.component);
60 tag.doEndTag();
61 ActionProxy proxy = ac.getProxy();
62
63 Object o = pageContext.findAttribute(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
64 assertTrue(o instanceof TestAction);
65
66 assertEquals(TestConfigurationProvider.TEST_NAMESPACE, proxy.getNamespace());
67 } catch (JspException ex) {
68 ex.printStackTrace();
69 fail();
70 }
71 }
72
73 public void testSimple() {
74 request.setupGetServletPath("/foo.action");
75
76 ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", "testAction");
77 container.inject(config.getInterceptors().get(0).getInterceptor());
78
79 ActionTag tag = new ActionTag();
80 tag.setPageContext(pageContext);
81 tag.setName("testAction");
82 tag.setId("testAction");
83
84 int stackSize = stack.size();
85
86 try {
87 tag.doStartTag();
88 tag.addParameter("foo", "myFoo");
89 tag.doEndTag();
90
91 assertEquals(stack.size(), ActionContext.getContext().getValueStack().size());
92 assertEquals("myFoo", stack.findValue("#testAction.foo"));
93 assertEquals(stackSize, stack.size());
94
95 Object o = pageContext.findAttribute("testAction");
96 assertTrue(o instanceof TestAction);
97 assertEquals("myFoo", ((TestAction) o).getFoo());
98 assertEquals(Action.SUCCESS, ((TestAction) o).getResult());
99 } catch (JspException ex) {
100 ex.printStackTrace();
101 fail();
102 }
103 }
104
105 public void testSimpleWithoutServletActionContext() {
106 ServletActionContext.setRequest(null);
107 ServletActionContext.setResponse(null);
108 this.testSimple();
109 }
110
111 public void testSimpleWithctionMethodInOriginalURI() {
112 request.setupGetServletPath("/foo!foo.action");
113
114 ActionConfig config = configuration.getRuntimeConfiguration().getActionConfig("", "testAction");
115 container.inject(config.getInterceptors().get(0).getInterceptor());
116
117 ActionTag tag = new ActionTag();
118 tag.setPageContext(pageContext);
119 tag.setName("testAction");
120 tag.setId("testAction");
121
122 int stackSize = stack.size();
123
124 try {
125 tag.doStartTag();
126 tag.addParameter("foo", "myFoo");
127 tag.doEndTag();
128
129 assertEquals(stack.size(), ActionContext.getContext().getValueStack().size());
130 assertEquals("myFoo", stack.findValue("#testAction.foo"));
131 assertEquals(stackSize, stack.size());
132
133 Object o = pageContext.findAttribute("testAction");
134 assertTrue(o instanceof TestAction);
135 assertEquals("myFoo", ((TestAction) o).getFoo());
136 assertEquals(Action.SUCCESS, ((TestAction) o).getResult());
137 } catch (JspException ex) {
138 ex.printStackTrace();
139 fail();
140 }
141 }
142
143 public void testActionWithExecuteResult() throws Exception {
144 ActionTag tag = new ActionTag();
145 tag.setPageContext(pageContext);
146 tag.setNamespace("");
147 tag.setName("testActionTagAction");
148 tag.setExecuteResult(true);
149
150 tag.doStartTag();
151
152
153 ActionComponent component = (ActionComponent) tag.getComponent();
154
155 tag.doEndTag();
156
157 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
158
159 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
160 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
161 assertTrue(result.isExecuted());
162 }
163
164 public void testActionWithoutExecuteResult() throws Exception {
165 ActionTag tag = new ActionTag();
166 tag.setPageContext(pageContext);
167 tag.setNamespace("");
168 tag.setName("testActionTagAction");
169 tag.setExecuteResult(false);
170
171 tag.doStartTag();
172
173
174 ActionComponent component = (ActionComponent) tag.getComponent();
175
176 tag.doEndTag();
177
178 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
179
180 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
181 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
182 assertNull(result);
183 }
184
185 public void testExecuteButResetReturnSameInvocation() throws Exception {
186 Mock mockActionInv = new Mock(ActionInvocation.class);
187 ActionTag tag = new ActionTag();
188 tag.setPageContext(pageContext);
189 tag.setNamespace("");
190 tag.setName("testActionTagAction");
191 tag.setExecuteResult(true);
192 ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInv.proxy());
193
194 ActionInvocation oldInvocation = ActionContext.getContext().getActionInvocation();
195 assertNotNull(oldInvocation);
196
197 tag.doStartTag();
198
199
200 ActionComponent component = (ActionComponent) tag.getComponent();
201
202 tag.doEndTag();
203 assertTrue(oldInvocation == ActionContext.getContext().getActionInvocation());
204 }
205
206 public void testIngoreContextParamsFalse() throws Exception {
207 ActionTag tag = new ActionTag();
208 tag.setPageContext(pageContext);
209 tag.setNamespace("");
210 tag.setName("testActionTagAction");
211 tag.setExecuteResult(false);
212 tag.setIgnoreContextParams(false);
213 ActionContext.getContext().getParameters().put("user", "Santa Claus");
214
215 tag.doStartTag();
216
217
218 ActionComponent component = (ActionComponent) tag.getComponent();
219
220 tag.doEndTag();
221
222
223 ActionInvocation ai = component.getProxy().getInvocation();
224 ActionContext ac = ai.getInvocationContext();
225 assertEquals(1, ac.getParameters().size());
226 }
227
228 public void testIngoreContextParamsTrue() throws Exception {
229 ActionTag tag = new ActionTag();
230 tag.setPageContext(pageContext);
231 tag.setNamespace("");
232 tag.setName("testActionTagAction");
233 tag.setExecuteResult(false);
234 tag.setIgnoreContextParams(true);
235 ActionContext.getContext().getParameters().put("user", "Santa Claus");
236
237 tag.doStartTag();
238
239
240 ActionComponent component = (ActionComponent) tag.getComponent();
241
242 tag.doEndTag();
243
244
245 ActionInvocation ai = component.getProxy().getInvocation();
246 ActionContext ac = ai.getInvocationContext();
247 assertEquals(0, ac.getParameters().size());
248 }
249
250 public void testNoNameDefined() throws Exception {
251 ActionTag tag = new ActionTag();
252 tag.setPageContext(pageContext);
253 tag.setNamespace("");
254 tag.setName(null);
255 tag.setExecuteResult(false);
256
257 try {
258 tag.doStartTag();
259 tag.doEndTag();
260 fail("Should have thrown RuntimeException");
261 } catch (StrutsException e) {
262 assertEquals("tag 'actioncomponent', field 'name': Action name is required. Example: updatePerson", e.getMessage());
263 }
264 }
265
266
267 public void testUnknownNameDefined() throws Exception {
268 ActionTag tag = new ActionTag();
269 tag.setPageContext(pageContext);
270 tag.setNamespace("");
271 tag.setName("UNKNOWN_NAME");
272 tag.setExecuteResult(false);
273
274 tag.doStartTag();
275 tag.doEndTag();
276
277 }
278
279 public void testActionMethodWithExecuteResult() throws Exception {
280 ActionTag tag = new ActionTag();
281 tag.setPageContext(pageContext);
282 tag.setNamespace("");
283 tag.setName("testActionTagAction!input");
284 tag.setExecuteResult(true);
285
286 tag.doStartTag();
287
288
289 ActionComponent component = (ActionComponent) tag.getComponent();
290
291 tag.doEndTag();
292
293 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
294
295 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
296 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
297 assertTrue(result.isExecuted());
298 }
299
300 protected void setUp() throws Exception {
301 super.setUp();
302
303 initDispatcher(new HashMap() {{ put("configProviders", TestConfigurationProvider.class.getName()); }});
304
305 ActionContext actionContext = new ActionContext(context);
306 actionContext.setValueStack(stack);
307 ActionContext.setContext(actionContext);
308 }
309
310 protected void tearDown() throws Exception {
311
312 super.tearDown();
313 }
314 }