View Javadoc

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