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