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