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.opensymphony.xwork2.Action;
37 import com.opensymphony.xwork2.ActionContext;
38 import com.opensymphony.xwork2.ActionInvocation;
39 import com.opensymphony.xwork2.ActionProxy;
40 import com.opensymphony.xwork2.util.ValueStack;
41 import com.opensymphony.xwork2.util.ValueStackFactory;
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 ActionTag tag = new ActionTag();
77 tag.setPageContext(pageContext);
78 tag.setName("testAction");
79 tag.setId("testAction");
80
81 int stackSize = stack.size();
82
83 try {
84 tag.doStartTag();
85 tag.addParameter("foo", "myFoo");
86 tag.doEndTag();
87
88 assertEquals(stack.size(), ActionContext.getContext().getValueStack().size());
89 assertEquals("myFoo", stack.findValue("#testAction.foo"));
90 assertEquals(stackSize, stack.size());
91
92 Object o = pageContext.findAttribute("testAction");
93 assertTrue(o instanceof TestAction);
94 assertEquals("myFoo", ((TestAction) o).getFoo());
95 assertEquals(Action.SUCCESS, ((TestAction) o).getResult());
96 } catch (JspException ex) {
97 ex.printStackTrace();
98 fail();
99 }
100 }
101
102 public void testSimpleWithoutServletActionContext() {
103 ServletActionContext.setRequest(null);
104 ServletActionContext.setResponse(null);
105 this.testSimple();
106 }
107
108 public void testActionWithExecuteResult() throws Exception {
109 ActionTag tag = new ActionTag();
110 tag.setPageContext(pageContext);
111 tag.setNamespace("");
112 tag.setName("testActionTagAction");
113 tag.setExecuteResult(true);
114
115 tag.doStartTag();
116
117
118 ActionComponent component = (ActionComponent) tag.getComponent();
119
120 tag.doEndTag();
121
122 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
123
124 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
125 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
126 assertTrue(result.isExecuted());
127 }
128
129 public void testActionWithoutExecuteResult() throws Exception {
130 ActionTag tag = new ActionTag();
131 tag.setPageContext(pageContext);
132 tag.setNamespace("");
133 tag.setName("testActionTagAction");
134 tag.setExecuteResult(false);
135
136 tag.doStartTag();
137
138
139 ActionComponent component = (ActionComponent) tag.getComponent();
140
141 tag.doEndTag();
142
143 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
144
145 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
146 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
147 assertNull(result);
148 }
149
150 public void testIngoreContextParamsFalse() throws Exception {
151 ActionTag tag = new ActionTag();
152 tag.setPageContext(pageContext);
153 tag.setNamespace("");
154 tag.setName("testActionTagAction");
155 tag.setExecuteResult(false);
156 tag.setIgnoreContextParams(false);
157 ActionContext.getContext().getParameters().put("user", "Santa Claus");
158
159 tag.doStartTag();
160
161
162 ActionComponent component = (ActionComponent) tag.getComponent();
163
164 tag.doEndTag();
165
166
167 ActionInvocation ai = component.getProxy().getInvocation();
168 ActionContext ac = ai.getInvocationContext();
169 assertEquals(1, ac.getParameters().size());
170 }
171
172 public void testIngoreContextParamsTrue() throws Exception {
173 ActionTag tag = new ActionTag();
174 tag.setPageContext(pageContext);
175 tag.setNamespace("");
176 tag.setName("testActionTagAction");
177 tag.setExecuteResult(false);
178 tag.setIgnoreContextParams(true);
179 ActionContext.getContext().getParameters().put("user", "Santa Claus");
180
181 tag.doStartTag();
182
183
184 ActionComponent component = (ActionComponent) tag.getComponent();
185
186 tag.doEndTag();
187
188
189 ActionInvocation ai = component.getProxy().getInvocation();
190 ActionContext ac = ai.getInvocationContext();
191 assertEquals(0, ac.getParameters().size());
192 }
193
194 public void testNoNameDefined() throws Exception {
195 ActionTag tag = new ActionTag();
196 tag.setPageContext(pageContext);
197 tag.setNamespace("");
198 tag.setName(null);
199 tag.setExecuteResult(false);
200
201 try {
202 tag.doStartTag();
203 tag.doEndTag();
204 fail("Should have thrown RuntimeException");
205 } catch (StrutsException e) {
206 assertEquals("tag 'actioncomponent', field 'name': Action name is required. Example: updatePerson", e.getMessage());
207 }
208 }
209
210
211 public void testUnknownNameDefined() throws Exception {
212 ActionTag tag = new ActionTag();
213 tag.setPageContext(pageContext);
214 tag.setNamespace("");
215 tag.setName("UNKNOWN_NAME");
216 tag.setExecuteResult(false);
217
218 tag.doStartTag();
219 tag.doEndTag();
220
221 }
222
223 public void testActionMethodWithExecuteResult() throws Exception {
224 ActionTag tag = new ActionTag();
225 tag.setPageContext(pageContext);
226 tag.setNamespace("");
227 tag.setName("testActionTagAction!input");
228 tag.setExecuteResult(true);
229
230 tag.doStartTag();
231
232
233 ActionComponent component = (ActionComponent) tag.getComponent();
234
235 tag.doEndTag();
236
237 TestActionTagResult result = (TestActionTagResult) component.getProxy().getInvocation().getResult();
238
239 assertTrue(stack.getContext().containsKey(ServletActionContext.PAGE_CONTEXT));
240 assertTrue(stack.getContext().get(ServletActionContext.PAGE_CONTEXT)instanceof PageContext);
241 assertTrue(result.isExecuted());
242 }
243
244 protected void setUp() throws Exception {
245 super.setUp();
246
247 initDispatcher(new HashMap() {{ put("configProviders", TestConfigurationProvider.class.getName()); }});
248
249 ActionContext actionContext = new ActionContext(context);
250 actionContext.setValueStack(stack);
251 ActionContext.setContext(actionContext);
252 }
253
254 protected void tearDown() throws Exception {
255
256 super.tearDown();
257 }
258 }