001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.webwork.TestAction;
009: import com.opensymphony.webwork.TestActionTagResult;
010: import com.opensymphony.webwork.TestConfigurationProvider;
011: import com.opensymphony.webwork.components.ActionComponent;
012: import com.opensymphony.xwork.Action;
013: import com.opensymphony.xwork.ActionContext;
014: import com.opensymphony.xwork.ActionProxy;
015: import com.opensymphony.xwork.ActionInvocation;
016: import com.opensymphony.xwork.config.ConfigurationManager;
017: import com.opensymphony.xwork.util.OgnlValueStack;
018:
019: import javax.servlet.jsp.JspException;
020: import javax.servlet.jsp.PageContext;
021: import java.util.Map;
022:
023: /**
024: * Unit test for {@link ActionTag}.
025: *
026: * @author Jason Carreira
027: * @author tmjee ( tm_jee(at)yahoo.co.uk )
028: */
029: public class ActionTagTest extends AbstractTagTest {
030:
031: public void testActionTagWithNamespace() {
032: request
033: .setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE
034: + "/" + "foo.action");
035:
036: ActionTag tag = new ActionTag();
037: tag.setPageContext(pageContext);
038: tag.setName(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
039: tag.setId(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
040:
041: try {
042: tag.doStartTag();
043: ActionComponent ac = ((ActionComponent) tag.component);
044: tag.doEndTag();
045: ActionProxy proxy = ac.getProxy();
046:
047: Object o = pageContext
048: .findAttribute(TestConfigurationProvider.TEST_NAMESPACE_ACTION);
049: assertTrue(o instanceof TestAction);
050:
051: assertEquals(TestConfigurationProvider.TEST_NAMESPACE,
052: proxy.getNamespace());
053: } catch (JspException ex) {
054: ex.printStackTrace();
055: fail();
056: }
057: }
058:
059: public void testSimple() {
060: request.setupGetServletPath("/foo.action");
061:
062: ActionTag tag = new ActionTag();
063: tag.setPageContext(pageContext);
064: tag.setName("testAction");
065: tag.setId("testAction");
066:
067: int stackSize = stack.size();
068:
069: try {
070: tag.doStartTag();
071: tag.addParameter("foo", "myFoo");
072: tag.doEndTag();
073:
074: assertEquals(stack.size(), ActionContext.getContext()
075: .getValueStack().size());
076: assertEquals("myFoo", stack.findValue("#testAction.foo"));
077: assertEquals(stackSize, stack.size());
078:
079: Object o = pageContext.findAttribute("testAction");
080: assertTrue(o instanceof TestAction);
081: assertEquals("myFoo", ((TestAction) o).getFoo());
082: assertEquals(Action.SUCCESS, ((TestAction) o).getResult());
083: } catch (JspException ex) {
084: ex.printStackTrace();
085: fail();
086: }
087: }
088:
089: public void testSimpleWithoutServletActionContext() {
090: ServletActionContext.setRequest(null);
091: ServletActionContext.setResponse(null);
092: this .testSimple();
093: }
094:
095: public void testActionWithExecuteResult() throws Exception {
096: ActionTag tag = new ActionTag();
097: tag.setPageContext(pageContext);
098: tag.setNamespace("");
099: tag.setName("testActionTagAction");
100: tag.setExecuteResult(true);
101:
102: tag.doStartTag();
103:
104: // tag clear components on doEndTag
105: ActionComponent component = (ActionComponent) tag
106: .getComponent();
107:
108: tag.doEndTag();
109:
110: TestActionTagResult result = (TestActionTagResult) component
111: .getProxy().getInvocation().getResult();
112:
113: assertTrue(stack.getContext().containsKey(
114: ServletActionContext.PAGE_CONTEXT));
115: assertTrue(stack.getContext().get(
116: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
117: assertTrue(result.isExecuted());
118: }
119:
120: public void testActionWithoutExecuteResult() throws Exception {
121: ActionTag tag = new ActionTag();
122: tag.setPageContext(pageContext);
123: tag.setNamespace("");
124: tag.setName("testActionTagAction");
125: tag.setExecuteResult(false);
126:
127: tag.doStartTag();
128:
129: // tag clear components on doEndTag, so we need to get it here
130: ActionComponent component = (ActionComponent) tag
131: .getComponent();
132:
133: tag.doEndTag();
134:
135: TestActionTagResult result = (TestActionTagResult) component
136: .getProxy().getInvocation().getResult();
137:
138: assertTrue(stack.getContext().containsKey(
139: ServletActionContext.PAGE_CONTEXT));
140: assertTrue(stack.getContext().get(
141: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
142: assertNull(result); // result is never executed, hence never set into invocation
143: }
144:
145: public void testIngoreContextParamsFalse() throws Exception {
146: ActionTag tag = new ActionTag();
147: tag.setPageContext(pageContext);
148: tag.setNamespace("");
149: tag.setName("testActionTagAction");
150: tag.setExecuteResult(false);
151: tag.setIgnoreContextParams(false);
152: ActionContext.getContext().getParameters().put("user",
153: "Santa Claus");
154:
155: tag.doStartTag();
156:
157: // tag clear components on doEndTag, so we need to get it here
158: ActionComponent component = (ActionComponent) tag
159: .getComponent();
160:
161: tag.doEndTag();
162:
163: // check parameters, there should be one
164: ActionInvocation ai = component.getProxy().getInvocation();
165: ActionContext ac = ai.getInvocationContext();
166: assertEquals(1, ac.getParameters().size());
167: }
168:
169: public void testIngoreContextParamsTrue() throws Exception {
170: ActionTag tag = new ActionTag();
171: tag.setPageContext(pageContext);
172: tag.setNamespace("");
173: tag.setName("testActionTagAction");
174: tag.setExecuteResult(false);
175: tag.setIgnoreContextParams(true);
176: ActionContext.getContext().getParameters().put("user",
177: "Santa Claus");
178:
179: tag.doStartTag();
180:
181: // tag clear components on doEndTag, so we need to get it here
182: ActionComponent component = (ActionComponent) tag
183: .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 (RuntimeException e) {
205: assertEquals(
206: "tag 'actioncomponent', field 'name': Action name is required. Example: updatePerson",
207: e.getMessage());
208: }
209: }
210:
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 th 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
234: .getComponent();
235:
236: tag.doEndTag();
237:
238: TestActionTagResult result = (TestActionTagResult) component
239: .getProxy().getInvocation().getResult();
240:
241: assertTrue(stack.getContext().containsKey(
242: ServletActionContext.PAGE_CONTEXT));
243: assertTrue(stack.getContext().get(
244: ServletActionContext.PAGE_CONTEXT) instanceof PageContext);
245: assertTrue(result.isExecuted());
246: }
247:
248: protected void setUp() throws Exception {
249: super .setUp();
250:
251: ConfigurationManager.clearConfigurationProviders();
252: ConfigurationManager
253: .addConfigurationProvider(new TestConfigurationProvider());
254: ConfigurationManager.getConfiguration().reload();
255:
256: ActionContext actionContext = new ActionContext(context);
257: actionContext.setValueStack(stack);
258: ActionContext.setContext(actionContext);
259: }
260:
261: protected void tearDown() throws Exception {
262: ConfigurationManager.destroyConfiguration();
263:
264: OgnlValueStack stack = new OgnlValueStack();
265: ActionContext.setContext(new ActionContext(stack.getContext()));
266: super.tearDown();
267: }
268: }
|