001: /*
002: * $Id: ServletActionRedirectResultTest.java 541522 2007-05-25 03:33:45Z 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.dispatcher;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.struts2.ServletActionContext;
027: import org.apache.struts2.StrutsTestCase;
028: import org.apache.struts2.dispatcher.mapper.ActionMapper;
029: import org.easymock.EasyMock;
030: import org.easymock.IMocksControl;
031: import org.springframework.mock.web.MockHttpServletRequest;
032: import org.springframework.mock.web.MockHttpServletResponse;
033:
034: import com.opensymphony.xwork2.ActionContext;
035: import com.opensymphony.xwork2.ActionInvocation;
036: import com.opensymphony.xwork2.ActionProxy;
037: import com.opensymphony.xwork2.config.entities.ActionConfig;
038: import com.opensymphony.xwork2.config.entities.ResultConfig;
039: import com.opensymphony.xwork2.util.ValueStack;
040:
041: /**
042: * @version $Date: 2007-05-24 23:33:45 -0400 (Thu, 24 May 2007) $ $Id: ServletActionRedirectResultTest.java 541522 2007-05-25 03:33:45Z mrdon $
043: */
044: public class ServletActionRedirectResultTest extends StrutsTestCase {
045:
046: public void testIncludeParameterInResultWithConditionParseOn()
047: throws Exception {
048:
049: ResultConfig resultConfig = new ResultConfig();
050: resultConfig.addParam("actionName", "someActionName");
051: resultConfig.addParam("namespace", "someNamespace");
052: resultConfig.addParam("encode", "true");
053: resultConfig.addParam("parse", "true");
054: resultConfig.addParam("location", "someLocation");
055: resultConfig.addParam("prependServletContext", "true");
056: resultConfig.addParam("method", "someMethod");
057: resultConfig.addParam("param1", "${#value1}");
058: resultConfig.addParam("param2", "${#value2}");
059: resultConfig.addParam("param3", "${#value3}");
060:
061: ActionContext context = ActionContext.getContext();
062: ValueStack stack = context.getValueStack();
063: context.getContextMap().put("value1", "value 1");
064: context.getContextMap().put("value2", "value 2");
065: context.getContextMap().put("value3", "value 3");
066: MockHttpServletRequest req = new MockHttpServletRequest();
067: MockHttpServletResponse res = new MockHttpServletResponse();
068: context.put(ServletActionContext.HTTP_REQUEST, req);
069: context.put(ServletActionContext.HTTP_RESPONSE, res);
070:
071: Map<String, ResultConfig> results = new HashMap<String, ResultConfig>();
072: results.put("myResult", resultConfig);
073:
074: ActionConfig actionConfig = new ActionConfig();
075: actionConfig.setResults(results);
076:
077: ServletActionRedirectResult result = new ServletActionRedirectResult();
078: result.setActionName("myAction");
079: result.setNamespace("/myNamespace");
080: result.setParse(true);
081: result.setEncode(false);
082: result.setPrependServletContext(false);
083:
084: IMocksControl control = EasyMock.createControl();
085: ActionProxy mockActionProxy = control
086: .createMock(ActionProxy.class);
087: ActionInvocation mockInvocation = control
088: .createMock(ActionInvocation.class);
089: mockInvocation.getProxy();
090: control.andReturn(mockActionProxy);
091: mockInvocation.getResultCode();
092: control.andReturn("myResult");
093: mockActionProxy.getConfig();
094: control.andReturn(actionConfig);
095: mockInvocation.getInvocationContext();
096: control.andReturn(context);
097: mockInvocation.getStack();
098: control.andReturn(stack);
099: control.anyTimes();
100:
101: control.replay();
102: result.setActionMapper(container
103: .getInstance(ActionMapper.class));
104: result.execute(mockInvocation);
105: assertEquals(
106: "/myNamespace/myAction.action?param1=value+1¶m2=value+2¶m3=value+3",
107: res.getRedirectedUrl());
108:
109: control.verify();
110: }
111:
112: public void testIncludeParameterInResult() throws Exception {
113:
114: ResultConfig resultConfig = new ResultConfig();
115: resultConfig.addParam("actionName", "someActionName");
116: resultConfig.addParam("namespace", "someNamespace");
117: resultConfig.addParam("encode", "true");
118: resultConfig.addParam("parse", "true");
119: resultConfig.addParam("location", "someLocation");
120: resultConfig.addParam("prependServletContext", "true");
121: resultConfig.addParam("method", "someMethod");
122: resultConfig.addParam("param1", "value 1");
123: resultConfig.addParam("param2", "value 2");
124: resultConfig.addParam("param3", "value 3");
125:
126: ActionContext context = ActionContext.getContext();
127: MockHttpServletRequest req = new MockHttpServletRequest();
128: MockHttpServletResponse res = new MockHttpServletResponse();
129: context.put(ServletActionContext.HTTP_REQUEST, req);
130: context.put(ServletActionContext.HTTP_RESPONSE, res);
131:
132: Map<String, ResultConfig> results = new HashMap<String, ResultConfig>();
133: results.put("myResult", resultConfig);
134:
135: ActionConfig actionConfig = new ActionConfig();
136: actionConfig.setResults(results);
137:
138: ServletActionRedirectResult result = new ServletActionRedirectResult();
139: result.setActionName("myAction");
140: result.setNamespace("/myNamespace");
141: result.setParse(false);
142: result.setEncode(false);
143: result.setPrependServletContext(false);
144:
145: IMocksControl control = EasyMock.createControl();
146: ActionProxy mockActionProxy = control
147: .createMock(ActionProxy.class);
148: ActionInvocation mockInvocation = control
149: .createMock(ActionInvocation.class);
150: mockInvocation.getProxy();
151: control.andReturn(mockActionProxy);
152: mockInvocation.getResultCode();
153: control.andReturn("myResult");
154: mockActionProxy.getConfig();
155: control.andReturn(actionConfig);
156: mockInvocation.getInvocationContext();
157: control.andReturn(context);
158:
159: control.replay();
160: result.setActionMapper(container
161: .getInstance(ActionMapper.class));
162: result.execute(mockInvocation);
163: assertEquals(
164: "/myNamespace/myAction.action?param1=value+1¶m2=value+2¶m3=value+3",
165: res.getRedirectedUrl());
166:
167: control.verify();
168: }
169: }
|