View Javadoc

1   /*
2    * $Id: ActionTagTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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         // tag clear components on doEndTag
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         // tag clear components on doEndTag, so we need to get it here
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); // result is never executed, hence never set into invocation
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         // tag clear components on doEndTag, so we need to get it here
156         ActionComponent component = (ActionComponent) tag.getComponent();
157 
158         tag.doEndTag();
159 
160         // check parameters, there should be one
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         // tag clear components on doEndTag, so we need to get it here
178         ActionComponent component = (ActionComponent) tag.getComponent();
179 
180         tag.doEndTag();
181 
182         // check parameters, there should be one
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     // FIXME: Logging the error seems to cause the standard Maven build to fail
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         // will just log it to ERROR but we run the code to test that it works somehow
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         // tag clear components on doEndTag
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 }