View Javadoc

1   /*
2    * $Id: ActionTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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         // tag clear components on doEndTag
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         // tag clear components on doEndTag, so we need to get it here
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); // result is never executed, hence never set into invocation
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         // tag clear components on doEndTag, so we need to get it here
162         ActionComponent component = (ActionComponent) tag.getComponent();
163 
164         tag.doEndTag();
165 
166         // check parameters, there should be one
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         // tag clear components on doEndTag, so we need to get it here
184         ActionComponent component = (ActionComponent) tag.getComponent();
185 
186         tag.doEndTag();
187 
188         // check parameters, there should be one
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     // FIXME: Logging the error seems to cause the standard Maven build to fail
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         // will just log it to ERROR but we run the code to test that it works somehow
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         // tag clear components on doEndTag
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 }