001: /*
002: * $Id: ActionTagTest.java 474191 2006-11-13 08:30:40Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp;
022:
023: import java.util.HashMap;
024:
025: import javax.servlet.jsp.JspException;
026: import javax.servlet.jsp.PageContext;
027:
028: import org.apache.struts2.ServletActionContext;
029: import org.apache.struts2.StrutsException;
030: import org.apache.struts2.TestAction;
031: import org.apache.struts2.TestActionTagResult;
032: import org.apache.struts2.TestConfigurationProvider;
033: import org.apache.struts2.components.ActionComponent;
034:
035: import com.opensymphony.xwork2.Action;
036: import com.opensymphony.xwork2.ActionContext;
037: import com.opensymphony.xwork2.ActionInvocation;
038: import com.opensymphony.xwork2.ActionProxy;
039: import com.opensymphony.xwork2.util.ValueStack;
040: import com.opensymphony.xwork2.util.ValueStackFactory;
041:
042: /**
043: * Unit test for {@link ActionTag}.
044: */
045: public class ActionTagTest extends AbstractTagTest {
046:
047: public void testActionTagWithNamespace() {
048: request
049: .setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE
050: + "/" + "foo.action");
051:
052: ActionTag tag = new ActionTag();
053: tag.setPageContext(pageContext);
054: tag.setName(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
055: tag.setId(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
056:
057: try {
058: tag.doStartTag();
059: ActionComponent ac = ((ActionComponent) tag.component);
060: tag.doEndTag();
061: ActionProxy proxy = ac.getProxy();
062:
063: Object o = pageContext
064: .findAttribute(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
065: assertTrue(o instanceof TestAction);
066:
067: assertEquals(TestConfigurationProvider.TEST_NAMESPACE,
068: proxy.getNamespace());
069: } catch (JspException ex) {
070: ex.printStackTrace();
071: fail();
072: }
073: }
074:
075: public void testSimple() {
076: request.setupGetServletPath("/foo.action");
077:
078: ActionTag tag = new ActionTag();
079: tag.setPageContext(pageContext);
080: tag.setName("testAction");
081: tag.setId("testAction");
082:
083: int stackSize = stack.size();
084:
085: try {
086: tag.doStartTag();
087: tag.addParameter("foo", "myFoo");
088: tag.doEndTag();
089:
090: assertEquals(stack.size(), ActionContext.getContext()
091: .getValueStack().size());
092: assertEquals("myFoo", stack.findValue("#testAction.foo"));
093: assertEquals(stackSize, stack.size());
094:
095: Object o = pageContext.findAttribute("testAction");
096: assertTrue(o instanceof TestAction);
097: assertEquals("myFoo", ((TestAction) o).getFoo());
098: assertEquals(Action.SUCCESS, ((TestAction) o).getResult());
099: } catch (JspException ex) {
100: ex.printStackTrace();
101: fail();
102: }
103: }
104:
105: public void testSimpleWithoutServletActionContext() {
106: ServletActionContext.setRequest(null);
107: ServletActionContext.setResponse(null);
108: this .testSimple();
109: }
110:
111: public void testActionWithExecuteResult() throws Exception {
112: ActionTag tag = new ActionTag();
113: tag.setPageContext(pageContext);
114: tag.setNamespace("");
115: tag.setName("testActionTagAction");
116: tag.setExecuteResult(true);
117:
118: tag.doStartTag();
119:
120: // tag clear components on doEndTag
121: ActionComponent component = (ActionComponent) tag
122: .getComponent();
123:
124: tag.doEndTag();
125:
126: TestActionTagResult result = (TestActionTagResult) component
127: .getProxy().getInvocation().getResult();
128:
129: assertTrue(stack.getContext().containsKey(
130: ServletActionContext.PAGE_CONTEXT));
131: assertTrue(stack.getContext().get(
132: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
133: assertTrue(result.isExecuted());
134: }
135:
136: public void testActionWithoutExecuteResult() throws Exception {
137: ActionTag tag = new ActionTag();
138: tag.setPageContext(pageContext);
139: tag.setNamespace("");
140: tag.setName("testActionTagAction");
141: tag.setExecuteResult(false);
142:
143: tag.doStartTag();
144:
145: // tag clear components on doEndTag, so we need to get it here
146: ActionComponent component = (ActionComponent) tag
147: .getComponent();
148:
149: tag.doEndTag();
150:
151: TestActionTagResult result = (TestActionTagResult) component
152: .getProxy().getInvocation().getResult();
153:
154: assertTrue(stack.getContext().containsKey(
155: ServletActionContext.PAGE_CONTEXT));
156: assertTrue(stack.getContext().get(
157: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
158: assertNull(result); // result is never executed, hence never set into invocation
159: }
160:
161: public void testIngoreContextParamsFalse() throws Exception {
162: ActionTag tag = new ActionTag();
163: tag.setPageContext(pageContext);
164: tag.setNamespace("");
165: tag.setName("testActionTagAction");
166: tag.setExecuteResult(false);
167: tag.setIgnoreContextParams(false);
168: ActionContext.getContext().getParameters().put("user",
169: "Santa Claus");
170:
171: tag.doStartTag();
172:
173: // tag clear components on doEndTag, so we need to get it here
174: ActionComponent component = (ActionComponent) tag
175: .getComponent();
176:
177: tag.doEndTag();
178:
179: // check parameters, there should be one
180: ActionInvocation ai = component.getProxy().getInvocation();
181: ActionContext ac = ai.getInvocationContext();
182: assertEquals(1, ac.getParameters().size());
183: }
184:
185: public void testIngoreContextParamsTrue() throws Exception {
186: ActionTag tag = new ActionTag();
187: tag.setPageContext(pageContext);
188: tag.setNamespace("");
189: tag.setName("testActionTagAction");
190: tag.setExecuteResult(false);
191: tag.setIgnoreContextParams(true);
192: ActionContext.getContext().getParameters().put("user",
193: "Santa Claus");
194:
195: tag.doStartTag();
196:
197: // tag clear components on doEndTag, so we need to get it here
198: ActionComponent component = (ActionComponent) tag
199: .getComponent();
200:
201: tag.doEndTag();
202:
203: // check parameters, there should be one
204: ActionInvocation ai = component.getProxy().getInvocation();
205: ActionContext ac = ai.getInvocationContext();
206: assertEquals(0, ac.getParameters().size());
207: }
208:
209: public void testNoNameDefined() throws Exception {
210: ActionTag tag = new ActionTag();
211: tag.setPageContext(pageContext);
212: tag.setNamespace("");
213: tag.setName(null);
214: tag.setExecuteResult(false);
215:
216: try {
217: tag.doStartTag();
218: tag.doEndTag();
219: fail("Should have thrown RuntimeException");
220: } catch (StrutsException e) {
221: assertEquals(
222: "tag 'actioncomponent', field 'name': Action name is required. Example: updatePerson",
223: e.getMessage());
224: }
225: }
226:
227: // FIXME: Logging the error seems to cause the standard Maven build to fail
228: public void testUnknownNameDefined() throws Exception {
229: ActionTag tag = new ActionTag();
230: tag.setPageContext(pageContext);
231: tag.setNamespace("");
232: tag.setName("UNKNOWN_NAME");
233: tag.setExecuteResult(false);
234:
235: tag.doStartTag();
236: tag.doEndTag();
237: // will just log it to ERROR but we run the code to test that it works somehow
238: }
239:
240: public void testActionMethodWithExecuteResult() throws Exception {
241: ActionTag tag = new ActionTag();
242: tag.setPageContext(pageContext);
243: tag.setNamespace("");
244: tag.setName("testActionTagAction!input");
245: tag.setExecuteResult(true);
246:
247: tag.doStartTag();
248:
249: // tag clear components on doEndTag
250: ActionComponent component = (ActionComponent) tag
251: .getComponent();
252:
253: tag.doEndTag();
254:
255: TestActionTagResult result = (TestActionTagResult) component
256: .getProxy().getInvocation().getResult();
257:
258: assertTrue(stack.getContext().containsKey(
259: ServletActionContext.PAGE_CONTEXT));
260: assertTrue(stack.getContext().get(
261: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
262: assertTrue(result.isExecuted());
263: }
264:
265: protected void setUp() throws Exception {
266: super .setUp();
267:
268: initDispatcher(new HashMap() {
269: {
270: put("configProviders", TestConfigurationProvider.class
271: .getName());
272: }
273: });
274:
275: ActionContext actionContext = new ActionContext(context);
276: actionContext.setValueStack(stack);
277: ActionContext.setContext(actionContext);
278: }
279:
280: protected void tearDown() throws Exception {
281: configurationManager.destroyConfiguration();
282:
283: ValueStack stack = ValueStackFactory.getFactory()
284: .createValueStack();
285: ActionContext.setContext(new ActionContext(stack.getContext()));
286: super.tearDown();
287: }
288: }
|