View Javadoc

1   /*
2    * $Id: PortletUrlTagTest.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.views.jsp;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletMode;
29  import javax.portlet.PortletModeException;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletURL;
32  import javax.portlet.WindowState;
33  import javax.portlet.WindowStateException;
34  
35  import org.apache.struts2.ServletActionContext;
36  import org.apache.struts2.StrutsTestCase;
37  import org.apache.struts2.portlet.PortletActionConstants;
38  import org.apache.struts2.portlet.servlet.PortletServletRequest;
39  import org.apache.struts2.portlet.util.PortletUrlHelper;
40  import org.springframework.mock.web.portlet.MockPortalContext;
41  import org.springframework.mock.web.portlet.MockPortletURL;
42  import org.springframework.mock.web.portlet.MockRenderRequest;
43  import org.springframework.mock.web.portlet.MockRenderResponse;
44  
45  import com.mockobjects.servlet.MockJspWriter;
46  import com.mockobjects.servlet.MockPageContext;
47  import com.opensymphony.xwork2.ActionContext;
48  import com.opensymphony.xwork2.mock.MockActionInvocation;
49  import com.opensymphony.xwork2.mock.MockActionProxy;
50  import com.opensymphony.xwork2.util.ValueStack;
51  
52  /***
53   */
54  @SuppressWarnings("unchecked")
55  public class PortletUrlTagTest extends StrutsTestCase {
56  
57  	private URLTag tag = new URLTag();
58  
59  	private ValueStack stack = null;
60  
61  	private ActionContext context = null;
62  
63  	private MockRenderRequest renderRequest;
64  
65  	private MockPortletUrl renderUrl;
66  
67  	private MockPortletUrl actionUrl;
68  
69  	private MockRenderResponse renderResponse;
70  
71  	private MockPageContext pageContext;
72  
73  	private MockActionInvocation actionInvocation;
74  
75  	private MockActionProxy actionProxy;
76  
77  	private MockJspWriter jspWriter;
78  
79  	public void setUp() throws Exception {
80  		super.setUp();
81  
82  		context = ActionContext.getContext();
83  		stack = context.getValueStack();
84  
85  		renderRequest = new MockRenderRequest();
86  		renderRequest.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
87  		renderUrl = new MockPortletUrl("render");
88  		actionUrl = new MockPortletUrl("action");
89  		renderResponse = new MockRenderResponse() {
90  			@Override
91  			public PortletURL createRenderURL() {
92  				return renderUrl;
93  			}
94  
95  			@Override
96  			public PortletURL createActionURL() {
97  				return actionUrl;
98  			}
99  		};
100 
101 		Map modeMap = new HashMap();
102 		modeMap.put(PortletMode.VIEW, "/view");
103 		modeMap.put(PortletMode.HELP, "/help");
104 		modeMap.put(PortletMode.EDIT, "/edit");
105 
106 		context.put(PortletActionConstants.REQUEST, renderRequest);
107 		context.put(PortletActionConstants.RESPONSE, renderResponse);
108 		context.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
109 		context.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
110 
111 		actionInvocation = new MockActionInvocation();
112 		actionProxy = new MockActionProxy();
113 
114 		actionInvocation.setAction(new Object());
115 		actionInvocation.setProxy(actionProxy);
116 		actionInvocation.setStack(stack);
117 
118 		context.setActionInvocation(actionInvocation);
119 
120 		pageContext = new MockPageContext();
121 		pageContext.setRequest(new PortletServletRequest(renderRequest, null));
122 		jspWriter = new MockJspWriter();
123 		pageContext.setJspWriter(jspWriter);
124 
125 		tag.setPageContext(pageContext);
126 
127 	}
128 
129 	public void testEnsureParamsAreStringArrays() {
130 		Map params = new HashMap();
131 		params.put("param1", "Test1");
132 		params.put("param2", new String[] { "Test2" });
133 
134 		Map result = PortletUrlHelper.ensureParamsAreStringArrays(params);
135 		assertEquals(2, result.size());
136 		assertTrue(result.get("param1") instanceof String[]);
137 	}
138 
139 	public void testSetWindowState() throws Exception {
140 
141 		tag.setAction("testAction");
142 		tag.setWindowState("maximized");
143 		tag.doStartTag();
144 		tag.doEndTag();
145 
146 		assertEquals("/view/testAction", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
147 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
148 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
149 		assertEquals(WindowState.MAXIMIZED, renderUrl.getWindowState());
150 
151 	}
152 
153 	public void testSetPortletMode() throws Exception {
154 
155 		tag.setAction("testAction");
156 		tag.setPortletMode("help");
157 		tag.doStartTag();
158 		tag.doEndTag();
159 
160 		assertEquals("/help/testAction", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
161 		assertEquals(PortletMode.HELP.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
162 		assertEquals(PortletMode.HELP, renderUrl.getPortletMode());
163 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
164 	}
165 
166 	public void testUrlWithQueryParams() throws Exception {
167 
168 		tag.setAction("testAction?testParam1=testValue1");
169 		tag.doStartTag();
170 		tag.doEndTag();
171 
172 		assertEquals("/view/testAction", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
173 		assertEquals("testValue1", renderUrl.getParameter("testParam1"));
174 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
175 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
176 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
177 	}
178 
179 	public void testActionUrl() throws Exception {
180 
181 		tag.setAction("testAction");
182 		tag.setPortletUrlType("action");
183 		tag.doStartTag();
184 		tag.doEndTag();
185 
186 		assertEquals("/view/testAction", actionUrl.getParameter(PortletActionConstants.ACTION_PARAM));
187 		assertEquals(PortletMode.VIEW, actionUrl.getPortletMode());
188 		assertEquals(WindowState.NORMAL, actionUrl.getWindowState());
189 	}
190 
191 	public void testResourceUrl() throws Exception {
192 		renderRequest.setContextPath("/myPortlet");
193 		jspWriter.setExpectedData("/myPortlet/image.gif");
194 		tag.setValue("image.gif");
195 		tag.doStartTag();
196 		tag.doEndTag();
197 		jspWriter.verify();
198 	}
199 
200 	public void testResourceUrlWithNestedParam() throws Exception {
201 		renderRequest.setContextPath("/myPortlet");
202 		jspWriter.setExpectedData("/myPortlet/image.gif?testParam1=testValue1");
203 
204 		ParamTag paramTag = new ParamTag();
205 		paramTag.setPageContext(pageContext);
206 		paramTag.setParent(tag);
207 		paramTag.setName("testParam1");
208 		paramTag.setValue("'testValue1'");
209 		tag.setValue("image.gif");
210 		tag.doStartTag();
211 		paramTag.doStartTag();
212 		paramTag.doEndTag();
213 		tag.doEndTag();
214 		jspWriter.verify();
215 	}
216 
217 	public void testResourceUrlWithTwoNestedParam() throws Exception {
218 		renderRequest.setContextPath("/myPortlet");
219 		jspWriter.setExpectedData("/myPortlet/image.gif?testParam1=testValue1&testParam2=testValue2");
220 
221 		ParamTag paramTag = new ParamTag();
222 		paramTag.setPageContext(pageContext);
223 		paramTag.setParent(tag);
224 		paramTag.setName("testParam1");
225 		paramTag.setValue("'testValue1'");
226 		ParamTag paramTag2 = new ParamTag();
227 		paramTag2.setPageContext(pageContext);
228 		paramTag2.setParent(tag);
229 		paramTag2.setName("testParam2");
230 		paramTag2.setValue("'testValue2'");
231 		tag.setValue("image.gif");
232 		tag.doStartTag();
233 		paramTag.doStartTag();
234 		paramTag.doEndTag();
235 		paramTag2.doStartTag();
236 		paramTag2.doEndTag();
237 		tag.doEndTag();
238 		jspWriter.verify();
239 	}
240 
241 	public void testUrlWithMethod() throws Exception {
242 		tag.setAction("testAction");
243 		tag.setMethod("input");
244 		tag.doStartTag();
245 		tag.doEndTag();
246 
247 		assertEquals("/view/testAction!input", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
248 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
249 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
250 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
251 	}
252 
253 	public void testUrlWithNoActionOrMethod() throws Exception {
254 		actionProxy.setActionName("currentExecutingAction");
255 		actionProxy.setNamespace("/currentNamespace");
256 		tag.doStartTag();
257 		tag.doEndTag();
258 
259 		assertEquals("/view/currentNamespace/currentExecutingAction", renderUrl
260 				.getParameter(PortletActionConstants.ACTION_PARAM));
261 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
262 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
263 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
264 	}
265 
266 	public void testUrlShouldNotIncludeParamsFromHttpQueryString() throws Exception {
267 
268 		PortletServletRequestWithQueryString req = new PortletServletRequestWithQueryString(renderRequest, null);
269 		req.setQueryString("thisParamShouldNotBeIncluded=thisValueShouldNotBeIncluded");
270 		pageContext.setRequest(req);
271 		tag.setAction("testAction?testParam1=testValue1");
272 		tag.doStartTag();
273 		tag.doEndTag();
274 
275 		assertEquals("/view/testAction", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
276 		assertEquals("testValue1", renderUrl.getParameter("testParam1"));
277 		assertNull(renderUrl.getParameter("thisParamShouldNotBeIncluded"));
278 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
279 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
280 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
281 	}
282 
283 	public void testUrlShouldIgnoreIncludeParams() throws Exception {
284 		PortletServletRequestWithQueryString req = new PortletServletRequestWithQueryString(renderRequest, null);
285 		req.setQueryString("thisParamShouldNotBeIncluded=thisValueShouldNotBeIncluded");
286 		pageContext.setRequest(req);
287 		tag.setAction("testAction?testParam1=testValue1");
288 		tag.setIncludeParams("GET");
289 		tag.doStartTag();
290 		tag.doEndTag();
291 
292 		assertEquals("/view/testAction", renderUrl.getParameter(PortletActionConstants.ACTION_PARAM));
293 		assertEquals("testValue1", renderUrl.getParameter("testParam1"));
294 		assertNull(renderUrl.getParameter("thisParamShouldNotBeIncluded"));
295 		assertEquals(PortletMode.VIEW.toString(), renderUrl.getParameter(PortletActionConstants.MODE_PARAM));
296 		assertEquals(PortletMode.VIEW, renderUrl.getPortletMode());
297 		assertEquals(WindowState.NORMAL, renderUrl.getWindowState());
298 	}
299 
300 	private static class PortletServletRequestWithQueryString extends PortletServletRequest {
301 
302 		private String queryString;
303 
304 		public PortletServletRequestWithQueryString(PortletRequest portletRequest, PortletContext portletContext) {
305 			super(portletRequest, portletContext);
306 		}
307 
308 		public void setQueryString(String queryString) {
309 			this.queryString = queryString;
310 		}
311 
312 		@Override
313 		public String getQueryString() {
314 			return queryString;
315 		}
316 
317 	}
318 
319 	private static class MockPortletUrl extends MockPortletURL {
320 
321 		private PortletMode portletMode;
322 
323 		private WindowState windowState;
324 
325 		public MockPortletUrl(String urlType) {
326 			super(new MockPortalContext(), urlType);
327 		}
328 
329 		@Override
330 		public void setPortletMode(PortletMode portletMode) throws PortletModeException {
331 			super.setPortletMode(portletMode);
332 			this.portletMode = portletMode;
333 		}
334 
335 		public PortletMode getPortletMode() {
336 			return portletMode;
337 		}
338 
339 		@Override
340 		public void setWindowState(WindowState windowState) throws WindowStateException {
341 			super.setWindowState(windowState);
342 			this.windowState = windowState;
343 		}
344 
345 		public WindowState getWindowState() {
346 			return windowState;
347 		}
348 
349 	}
350 }