View Javadoc

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