View Javadoc

1   /*
2    * $Id: ServletActionRedirectResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.dispatcher;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.ServletActionContext;
28  import org.apache.struts2.StrutsTestCase;
29  import org.apache.struts2.dispatcher.mapper.ActionMapper;
30  import org.easymock.EasyMock;
31  import org.easymock.IMocksControl;
32  import org.springframework.mock.web.MockHttpServletRequest;
33  import org.springframework.mock.web.MockHttpServletResponse;
34  
35  import com.opensymphony.xwork2.ActionContext;
36  import com.opensymphony.xwork2.ActionInvocation;
37  import com.opensymphony.xwork2.ActionProxy;
38  import com.opensymphony.xwork2.ObjectFactory;
39  import com.opensymphony.xwork2.config.entities.ActionConfig;
40  import com.opensymphony.xwork2.config.entities.ResultConfig;
41  import com.opensymphony.xwork2.util.ValueStack;
42  
43  
44  /***
45   * @version $Date: 2008-04-27 08:41:38 -0500 (Sun, 27 Apr 2008) $ $Id: ServletActionRedirectResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
46   */
47  public class ServletActionRedirectResultTest extends StrutsTestCase {
48  
49      public void testIncludeParameterInResultWithConditionParseOn() throws Exception {
50  
51          ResultConfig resultConfig = new ResultConfig.Builder("", "")
52              .addParam("actionName", "someActionName")
53              .addParam("namespace", "someNamespace")
54              .addParam("encode", "true")
55              .addParam("parse", "true")
56              .addParam("location", "someLocation")
57              .addParam("prependServletContext", "true")
58              .addParam("method", "someMethod")
59              .addParam("param1", "${#value1}")
60              .addParam("param2", "${#value2}")
61              .addParam("param3", "${#value3}")
62              .build();
63  
64  
65  
66          ActionContext context = ActionContext.getContext();
67          ValueStack stack = context.getValueStack();
68          context.getContextMap().put("value1", "value 1");
69          context.getContextMap().put("value2", "value 2");
70          context.getContextMap().put("value3", "value 3");
71          MockHttpServletRequest req = new MockHttpServletRequest();
72          MockHttpServletResponse res = new MockHttpServletResponse();
73          context.put(ServletActionContext.HTTP_REQUEST, req);
74          context.put(ServletActionContext.HTTP_RESPONSE, res);
75  
76  
77          Map<String, ResultConfig> results=  new HashMap<String, ResultConfig>();
78          results.put("myResult", resultConfig);
79  
80          ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
81                  .addResultConfigs(results).build();
82  
83          ServletActionRedirectResult result = new ServletActionRedirectResult();
84          result.setActionName("myAction");
85          result.setNamespace("/myNamespace");
86          result.setParse(true);
87          result.setEncode(false);
88          result.setPrependServletContext(false);
89  
90          IMocksControl control = EasyMock.createControl();
91          ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
92          ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
93          mockInvocation.getProxy();
94          control.andReturn(mockActionProxy);
95          mockInvocation.getResultCode();
96          control.andReturn("myResult");
97          mockActionProxy.getConfig();
98          control.andReturn(actionConfig);
99          mockInvocation.getInvocationContext();
100         control.andReturn(context);
101         mockInvocation.getStack();
102         control.andReturn(stack);
103         control.anyTimes();
104 
105         control.replay();
106         result.setActionMapper(container.getInstance(ActionMapper.class));
107         result.execute(mockInvocation);
108         assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3", res.getRedirectedUrl());
109 
110         control.verify();
111     }
112 
113     public void testIncludeParameterInResult() throws Exception {
114 
115         ResultConfig resultConfig = new ResultConfig.Builder("", "")
116             .addParam("actionName", "someActionName")
117             .addParam("namespace", "someNamespace")
118             .addParam("encode", "true")
119             .addParam("parse", "true")
120             .addParam("location", "someLocation")
121             .addParam("prependServletContext", "true")
122             .addParam("method", "someMethod")
123             .addParam("param1", "value 1")
124             .addParam("param2", "value 2")
125             .addParam("param3", "value 3")
126             .build();
127 
128         ActionContext context = ActionContext.getContext();
129         MockHttpServletRequest req = new MockHttpServletRequest();
130         MockHttpServletResponse res = new MockHttpServletResponse();
131         context.put(ServletActionContext.HTTP_REQUEST, req);
132         context.put(ServletActionContext.HTTP_RESPONSE, res);
133 
134 
135         Map<String, ResultConfig> results=  new HashMap<String, ResultConfig>();
136         results.put("myResult", resultConfig);
137 
138         ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
139                 .addResultConfigs(results).build();
140 
141         ServletActionRedirectResult result = new ServletActionRedirectResult();
142         result.setActionName("myAction");
143         result.setNamespace("/myNamespace");
144         result.setParse(false);
145         result.setEncode(false);
146         result.setPrependServletContext(false);
147 
148         IMocksControl control = EasyMock.createControl();
149         ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
150         ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
151         mockInvocation.getProxy();
152         control.andReturn(mockActionProxy);
153         mockInvocation.getResultCode();
154         control.andReturn("myResult");
155         mockActionProxy.getConfig();
156         control.andReturn(actionConfig);
157         mockInvocation.getInvocationContext();
158         control.andReturn(context);
159 
160         control.replay();
161         result.setActionMapper(container.getInstance(ActionMapper.class));
162         result.execute(mockInvocation);
163         assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3", res.getRedirectedUrl());
164 
165         control.verify();
166     }
167 
168     public void testBuildResultWithParameter() throws Exception {
169 
170         ResultConfig resultConfig = new ResultConfig.Builder("", ServletActionRedirectResult.class.getName())
171             .addParam("actionName", "someActionName")
172             .addParam("namespace", "someNamespace")
173             .addParam("encode", "true")
174             .addParam("parse", "true")
175             .addParam("location", "someLocation")
176             .addParam("prependServletContext", "true")
177             .addParam("method", "someMethod")
178             .addParam("param1", "value 1")
179             .addParam("param2", "value 2")
180             .addParam("param3", "value 3")
181             .build();
182 
183         ObjectFactory factory = container.getInstance(ObjectFactory.class);
184         ServletActionRedirectResult result = (ServletActionRedirectResult) factory.buildResult(resultConfig, new HashMap());
185         assertNotNull(result);
186     }
187 }