View Javadoc

1   /*
2    * $Id: StrutsResultSupportTest.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 org.apache.struts2.StrutsTestCase;
25  import org.easymock.EasyMock;
26  
27  import com.opensymphony.xwork2.ActionContext;
28  import com.opensymphony.xwork2.ActionInvocation;
29  import com.opensymphony.xwork2.ActionSupport;
30  import com.opensymphony.xwork2.util.ValueStack;
31  import com.opensymphony.xwork2.util.ValueStackFactory;
32  
33  /***
34   * Test case for StrutsResultSupport.
35   */
36  public class StrutsResultSupportTest extends StrutsTestCase {
37  
38  
39      public void testParse() throws Exception {
40          ValueStack stack = ActionContext.getContext().getValueStack();
41          stack.push(new ActionSupport() {
42              public String getMyLocation() {
43                  return "ThisIsMyLocation";
44              }
45          });
46  
47          ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
48          mockActionInvocation.getStack();
49          EasyMock.expectLastCall().andReturn(stack);
50          EasyMock.replay(mockActionInvocation);
51  
52          InternalStrutsResultSupport result = new InternalStrutsResultSupport();
53          result.setParse(true);
54          result.setEncode(false);
55          result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
56  
57          result.execute(mockActionInvocation);
58  
59          assertNotNull(result.getInternalLocation());
60          assertEquals("/pages/myJsp.jsp?location=ThisIsMyLocation", result.getInternalLocation());
61          EasyMock.verify(mockActionInvocation);
62      }
63  
64      public void testParseAndEncode() throws Exception {
65          ValueStack stack = ActionContext.getContext().getValueStack();
66          stack.push(new ActionSupport() {
67              public String getMyLocation() {
68                  return "/myPage?param=value&param1=value1";
69              }
70          });
71  
72          ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
73          mockActionInvocation.getStack();
74          EasyMock.expectLastCall().andReturn(stack);
75          EasyMock.replay(mockActionInvocation);
76  
77          InternalStrutsResultSupport result = new InternalStrutsResultSupport();
78          result.setParse(true);
79          result.setEncode(true);
80          result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
81  
82          result.execute(mockActionInvocation);
83  
84          assertNotNull(result.getInternalLocation());
85          assertEquals("/pages/myJsp.jsp?location=%2FmyPage%3Fparam%3Dvalue%26param1%3Dvalue1", result.getInternalLocation());
86          EasyMock.verify(mockActionInvocation);
87      }
88  
89  
90      public void testNoParseAndEncode() throws Exception {
91          ValueStack stack = ActionContext.getContext().getValueStack();
92          stack.push(new ActionSupport() {
93              public String getMyLocation() {
94                  return "myLocation.jsp";
95              }
96          });
97  
98          ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
99          EasyMock.replay(mockActionInvocation);
100 
101         InternalStrutsResultSupport result = new InternalStrutsResultSupport();
102         result.setParse(false);
103         result.setEncode(false); // don't really need this, as encode is only valid when parse is true.
104         result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
105 
106         result.execute(mockActionInvocation);
107 
108         assertNotNull(result.getInternalLocation());
109         assertEquals("/pages/myJsp.jsp?location=${myLocation}", result.getInternalLocation());
110         EasyMock.verify(mockActionInvocation);
111     }
112 
113 
114     public static class InternalStrutsResultSupport extends StrutsResultSupport {
115         private String _internalLocation = null;
116 
117         protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
118             _internalLocation = finalLocation;
119         }
120 
121         public String getInternalLocation() {
122             return _internalLocation;
123         }
124     }
125 }