View Javadoc

1   /*
2    * $Id$
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.components;
23  
24  import java.io.IOException;
25  import java.io.StringWriter;
26  import java.io.Writer;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.portlet.PortletMode;
31  import javax.portlet.PortletURL;
32  
33  import org.apache.struts2.StrutsStatics;
34  import org.apache.struts2.StrutsTestCase;
35  import org.apache.struts2.portlet.PortletActionConstants;
36  import org.apache.struts2.portlet.context.PortletActionContext;
37  import org.apache.struts2.portlet.servlet.PortletServletRequest;
38  import org.apache.struts2.portlet.servlet.PortletServletResponse;
39  import org.easymock.EasyMock;
40  import org.springframework.mock.web.portlet.MockPortalContext;
41  import org.springframework.mock.web.portlet.MockPortletContext;
42  import org.springframework.mock.web.portlet.MockPortletURL;
43  import org.springframework.mock.web.portlet.MockRenderRequest;
44  import org.springframework.mock.web.portlet.MockRenderResponse;
45  
46  import com.opensymphony.xwork2.ActionContext;
47  import com.opensymphony.xwork2.mock.MockActionInvocation;
48  import com.opensymphony.xwork2.mock.MockActionProxy;
49  import com.opensymphony.xwork2.util.ValueStack;
50  
51  public class PortletUrlRendererTest extends StrutsTestCase {
52  
53  	PortletUrlRenderer renderer;
54  	MockPortletURL renderUrl;
55  	MockPortletURL actionUrl;
56  	MockRenderRequest request;
57  	MockRenderResponse response;
58  	MockPortletContext context;
59  	ActionContext ctx;
60  	ValueStack stack;
61  
62  	public void setUp() throws Exception {
63  		super.setUp();
64  		renderer = new PortletUrlRenderer();
65  		context = new MockPortletContext();
66  		renderUrl = new MockPortletURL(
67  				new MockPortalContext(), "render");
68  		actionUrl = new MockPortletURL(
69  				new MockPortalContext(), "action");
70  		request = new MockRenderRequest();
71  		response = new MockRenderResponse() {
72  			@Override
73  			public PortletURL createActionURL() {
74  				return actionUrl;
75  			}
76  			@Override
77  			public PortletURL createRenderURL() {
78  				return renderUrl;
79  			}
80  		};
81  
82  		ctx = ActionContext.getContext();
83  		ctx.put(PortletActionConstants.PHASE,
84  				PortletActionConstants.RENDER_PHASE);
85  		ctx.put(PortletActionConstants.REQUEST, request);
86  		ctx.put(PortletActionConstants.RESPONSE, response);
87  		ctx.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, context);
88  
89  		Map<PortletMode, String> modeMap = new HashMap<PortletMode, String>();
90  		modeMap.put(PortletMode.VIEW, "/view");
91  		ctx.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
92  		stack = ctx.getValueStack();
93  	}
94  
95  	/***
96  	 * Ensure that the namespace of the current executing action is used when no
97  	 * namespace is specified. (WW-1875)
98  	 */
99  	public void testShouldIncludeCurrentNamespaceIfNoNamespaceSpecifiedForRenderUrl()
100 			throws Exception {
101 		
102 		URL url = new URL(stack, new PortletServletRequest(request, null),
103 				new PortletServletResponse(response));
104 
105 		MockActionInvocation ai = new MockActionInvocation();
106 		MockActionProxy ap = new MockActionProxy();
107 		ap.setActionName("testAction");
108 		ap.setNamespace("/current_namespace");
109 		ai.setProxy(ap);
110 		ai.setStack(stack);
111 		ai.setAction(new Object());
112 		ctx.setActionInvocation(ai);
113 
114 		StringWriter renderOutput = new StringWriter();
115 		renderer.renderUrl(renderOutput, url.getUrlProvider());
116 
117 		String action = renderUrl
118 				.getParameter(PortletActionConstants.ACTION_PARAM);
119 		assertEquals("/view/current_namespace/testAction", action);
120 	}
121 
122 	/***
123 	 * Ensure that the namespace of the current executing action is used when no
124 	 * namespace is specified. (WW-1875)
125 	 */
126 	public void testShouldIncludeCurrentNamespaceIfNoNamespaceSpecifiedForRenderFormUrl()
127 			throws Exception {
128 
129 		Form form = new Form(stack, new PortletServletRequest(request, null),
130 				new PortletServletResponse(response));
131 
132 		MockActionInvocation ai = new MockActionInvocation();
133 		MockActionProxy ap = new MockActionProxy();
134 		ap.setActionName("testAction");
135 		ap.setNamespace("/current_namespace");
136 		ai.setProxy(ap);
137 		ai.setStack(stack);
138 		ai.setAction(new Object());
139 		ctx.setActionInvocation(ai);
140 
141 		renderer.renderFormUrl(form);
142 
143 		String action = actionUrl
144 				.getParameter(PortletActionConstants.ACTION_PARAM);
145 		assertEquals("/view/current_namespace/testAction", action);
146 	}
147 	
148 	public void testShouldEvaluateActionAsOGNLExpression() throws Exception {
149 		
150 		TestObject obj = new TestObject();
151 		obj.someProperty = "EvaluatedProperty";
152 		stack.push(obj);
153 		MockActionInvocation ai = new MockActionInvocation();
154 		MockActionProxy ap = new MockActionProxy();
155 		ap.setActionName("testAction");
156 		ap.setNamespace("");
157 		ai.setProxy(ap);
158 		ai.setStack(stack);
159 		ctx.setActionInvocation(ai);
160 		
161 		URL url = new URL(stack, new PortletServletRequest(request, null),
162 				new PortletServletResponse(response));
163 		url.setAction("%{someProperty}");
164 		
165 		StringWriter renderOutput = new StringWriter();
166 		renderer.renderUrl(renderOutput, url.getUrlProvider());
167 
168 		String action = renderUrl
169 				.getParameter(PortletActionConstants.ACTION_PARAM);
170 		assertEquals("/view/EvaluatedProperty", action);
171 		
172 	}
173 	
174 	public void testShouldEvaluateAnchorAsOGNLExpression() throws Exception {
175 		
176 		TestObject obj = new TestObject();
177 		obj.someProperty = "EvaluatedProperty";
178 		stack.push(obj);
179 		MockActionInvocation ai = new MockActionInvocation();
180 		MockActionProxy ap = new MockActionProxy();
181 		ap.setActionName("testAction");
182 		ap.setNamespace("");
183 		ai.setProxy(ap);
184 		ai.setStack(stack);
185 		ctx.setActionInvocation(ai);
186 		
187 		URL url = new URL(stack, new PortletServletRequest(request, null),
188 				new PortletServletResponse(response));
189 		url.setAnchor("%{someProperty}");
190 		
191 		StringWriter renderOutput = new StringWriter();
192 		renderer.renderUrl(renderOutput, url.getUrlProvider());
193 		assertTrue(renderOutput.toString().indexOf("#EvaluatedProperty") != -1);
194 		
195 	}
196 	
197 	public void testShouldPassThroughRenderUrlToServletUrlRendererIfNotPortletRequest() throws Exception {
198 		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
199 		ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, null);
200 		renderer.setServletRenderer(mockRenderer);
201 		URL url = new URL(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
202 		StringWriter renderOutput = new StringWriter();
203 		mockRenderer.renderUrl(renderOutput, url.getUrlProvider());
204 		EasyMock.replay(mockRenderer);
205 		renderer.renderUrl(renderOutput, url.getUrlProvider());
206 		EasyMock.verify(mockRenderer);
207 	}
208 	
209 	public void testShouldPassThroughRenderFormUrlToServletUrlRendererIfNotPortletRequest() throws Exception {
210 		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
211 		ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, null);
212 		renderer.setServletRenderer(mockRenderer);
213 		Form form = new Form(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
214 		mockRenderer.renderFormUrl(form);
215 		EasyMock.replay(mockRenderer);
216 		renderer.renderFormUrl(form);
217 		EasyMock.verify(mockRenderer);
218 	}
219 	
220 	public void testShouldPassThroughRenderUrlToServletUrlRendererWhenPortletUrlTypeIsNone() throws Exception {
221 		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
222 		renderer.setServletRenderer(mockRenderer);
223 		URL url = new URL(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
224 		url.setPortletUrlType("none");
225 		StringWriter renderOutput = new StringWriter();
226 		mockRenderer.renderUrl(renderOutput, url.getUrlProvider());
227 		EasyMock.replay(mockRenderer);
228 		renderer.renderUrl(renderOutput, url.getUrlProvider());
229 		EasyMock.verify(mockRenderer);
230 	}
231 	
232 	private final static class TestObject {
233 		public String someProperty;
234 	}
235 	
236 	
237 }