View Javadoc

1   /*
2    * $Id: StrutsResultSupportTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import org.apache.struts2.StrutsTestCase;
21  import org.easymock.EasyMock;
22  
23  import com.opensymphony.xwork2.ActionInvocation;
24  import com.opensymphony.xwork2.ActionSupport;
25  import com.opensymphony.xwork2.util.ValueStack;
26  import com.opensymphony.xwork2.util.ValueStackFactory;
27  
28  /***
29   * Test case for StrutsResultSupport.
30   */
31  public class StrutsResultSupportTest extends StrutsTestCase {
32  
33  	
34  	public void testParse() throws Exception {
35  		ValueStack stack = ValueStackFactory.getFactory().createValueStack();
36  		stack.push(new ActionSupport() {
37  			public String getMyLocation() {
38  				return "ThisIsMyLocation";
39  			}
40  		});
41  		
42  		ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
43  		mockActionInvocation.getStack();
44  		EasyMock.expectLastCall().andReturn(stack);
45  		EasyMock.replay(mockActionInvocation);
46  		
47  		InternalStrutsResultSupport result = new InternalStrutsResultSupport();
48  		result.setParse(true);
49  		result.setEncode(false);
50  		result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
51  		
52  		result.execute(mockActionInvocation);
53  		
54  		assertNotNull(result.getInternalLocation());
55  		assertEquals("/pages/myJsp.jsp?location=ThisIsMyLocation", result.getInternalLocation());
56  		EasyMock.verify(mockActionInvocation);
57  	}
58  	
59  	public void testParseAndEncode() throws Exception {
60  		ValueStack stack = ValueStackFactory.getFactory().createValueStack();
61  		stack.push(new ActionSupport() {
62  			public String getMyLocation() {
63  				return "/myPage?param=value&param1=value1";
64  			}
65  		});
66  		
67  		ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
68  		mockActionInvocation.getStack();
69  		EasyMock.expectLastCall().andReturn(stack);
70  		EasyMock.replay(mockActionInvocation);
71  		
72  		InternalStrutsResultSupport result = new InternalStrutsResultSupport();
73  		result.setParse(true);
74  		result.setEncode(true);
75  		result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
76  		
77  		result.execute(mockActionInvocation);
78  		
79  		assertNotNull(result.getInternalLocation());
80  		assertEquals("/pages/myJsp.jsp?location=%2FmyPage%3Fparam%3Dvalue%26param1%3Dvalue1", result.getInternalLocation());
81  		EasyMock.verify(mockActionInvocation);
82  	}
83  	
84  	
85  	public void testNoParseAndEncode() throws Exception {
86  		ValueStack stack = ValueStackFactory.getFactory().createValueStack();
87  		stack.push(new ActionSupport() {
88  			public String getMyLocation() {
89  				return "myLocation.jsp";
90  			}
91  		});
92  		
93  		ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
94  		EasyMock.replay(mockActionInvocation);
95  		
96  		InternalStrutsResultSupport result = new InternalStrutsResultSupport();
97  		result.setParse(false);
98  		result.setEncode(false); // don't really need this, as encode is only valid when parse is true.
99  		result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
100 		
101 		result.execute(mockActionInvocation);
102 		
103 		assertNotNull(result.getInternalLocation());
104 		assertEquals("/pages/myJsp.jsp?location=${myLocation}", result.getInternalLocation());
105 		EasyMock.verify(mockActionInvocation);
106 	}
107 	
108 	
109 	public static class InternalStrutsResultSupport extends StrutsResultSupport {
110 		private String _internalLocation = null;
111 		
112 		protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
113 			_internalLocation = finalLocation;
114 		}
115 		
116 		public String getInternalLocation() {
117 			return _internalLocation;
118 		}
119 	}
120 }