View Javadoc

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