1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.views.jsp;
22
23 import java.lang.reflect.Field;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import javax.portlet.PortletMode;
30 import javax.portlet.PortletRequest;
31 import javax.portlet.PortletResponse;
32 import javax.portlet.PortletURL;
33 import javax.portlet.RenderRequest;
34 import javax.portlet.RenderResponse;
35 import javax.portlet.WindowState;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.jsp.PageContext;
39
40 import junit.textui.TestRunner;
41
42 import org.apache.struts2.dispatcher.Dispatcher;
43 import org.apache.struts2.portlet.PortletActionConstants;
44 import org.apache.struts2.portlet.util.PortletUrlHelper;
45 import org.jmock.Mock;
46 import org.jmock.cglib.MockObjectTestCase;
47 import org.jmock.core.Constraint;
48
49 import com.mockobjects.servlet.MockJspWriter;
50 import com.opensymphony.xwork2.ActionContext;
51 import com.opensymphony.xwork2.util.ValueStack;
52 import com.opensymphony.xwork2.util.ValueStackFactory;
53
54 /***
55 */
56 public class PortletUrlTagTest extends MockObjectTestCase {
57
58 URLTag tag = new URLTag();
59
60 Mock mockHttpReq = null;
61
62 Mock mockHttpRes = null;
63
64 Mock mockPortletReq = null;
65
66 Mock mockPortletRes = null;
67
68 Mock mockPageCtx = null;
69
70 Mock mockPortletUrl = null;
71
72 MockJspWriter mockJspWriter = null;
73
74 ValueStack stack = null;
75
76 public static void main(String[] args) {
77 TestRunner.run(PortletUrlTagTest.class);
78 }
79
80 public void setUp() throws Exception {
81 super.setUp();
82
83 Dispatcher du = new Dispatcher(null, new HashMap());
84 du.init();
85 Dispatcher.setInstance(du);
86
87 mockPortletApiAvailable();
88
89 stack = ValueStackFactory.getFactory().createValueStack();
90
91
92 mockHttpReq = mock(HttpServletRequest.class);
93 mockHttpRes = mock(HttpServletResponse.class);
94 mockPortletReq = mock(RenderRequest.class);
95 mockPortletRes = mock(RenderResponse.class);
96 mockPageCtx = mock(PageContext.class);
97 mockPortletUrl = mock(PortletURL.class);
98 mockJspWriter = new MockJspWriter();
99
100 mockPageCtx.stubs().method("getRequest").will(
101 returnValue((HttpServletRequest) mockHttpReq.proxy()));
102 mockPageCtx.stubs().method("getResponse").will(
103 returnValue((HttpServletResponse) mockHttpRes.proxy()));
104 mockPageCtx.stubs().method("getOut").will(returnValue(mockJspWriter));
105
106 mockHttpReq.stubs().method("getScheme").will(returnValue("http"));
107 mockHttpReq.stubs().method("getAttribute").with(
108 eq("struts.valueStack")).will(returnValue(stack));
109 mockHttpReq.stubs().method("getAttribute").with(
110 eq("javax.portlet.response")).will(
111 returnValue((PortletResponse) mockPortletRes.proxy()));
112 mockHttpReq.stubs().method("getAttribute").with(
113 eq("javax.portlet.request")).will(
114 returnValue((PortletRequest) mockPortletReq.proxy()));
115
116 mockPortletReq.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
117 mockPortletReq.stubs().method("getWindowState").will(returnValue(WindowState.NORMAL));
118 mockPortletReq.stubs().method("getContextPath").will(returnValue("/contextPath"));
119
120 tag.setPageContext((PageContext) mockPageCtx.proxy());
121
122 Map modeMap = new HashMap();
123 modeMap.put(PortletMode.VIEW, "/view");
124 modeMap.put(PortletMode.HELP, "/help");
125 modeMap.put(PortletMode.EDIT, "/edit");
126 Map sessionMap = new HashMap();
127 Map contextMap = new HashMap();
128 contextMap.put(ActionContext.SESSION, sessionMap);
129 contextMap.put(PortletActionConstants.REQUEST, mockPortletReq.proxy());
130 contextMap.put(PortletActionConstants.RESPONSE, mockPortletRes.proxy());
131 contextMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
132 contextMap.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
133 ActionContext ctx = new ActionContext(contextMap);
134 ctx.setValueStack(stack);
135 ActionContext.setContext(ctx);
136 }
137
138 /***
139 *
140 */
141 private void mockPortletApiAvailable() {
142 try {
143 Field field = Dispatcher.class.getDeclaredField("portletSupportActive");
144 field.setAccessible(true);
145 field.set(null, Boolean.TRUE);
146 }
147 catch(Exception e) {
148
149 }
150
151 }
152
153 public void testEnsureParamsAreStringArrays() {
154 Map params = new HashMap();
155 params.put("param1", "Test1");
156 params.put("param2", new String[] { "Test2" });
157
158 Map result = PortletUrlHelper.ensureParamsAreStringArrays(params);
159 assertEquals(2, result.size());
160 assertTrue(result.get("param1") instanceof String[]);
161 }
162
163 public void testSetWindowState() throws Exception {
164
165 PortletMode mode = PortletMode.VIEW;
166
167 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
168
169 mockPortletRes.expects(once()).method("createRenderURL").will(
170 returnValue((PortletURL) mockPortletUrl.proxy()));
171
172 Map paramMap = new HashMap();
173 paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
174 paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
175
176 mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
177 mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.MAXIMIZED));
178 mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
179
180 tag.setAction("testAction");
181 tag.setWindowState("maximized");
182 tag.doStartTag();
183 tag.doEndTag();
184
185 }
186
187 public void testSetPortletMode() throws Exception {
188
189 PortletMode mode = PortletMode.HELP;
190
191 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
192
193 mockPortletRes.expects(once()).method("createRenderURL").will(
194 returnValue((PortletURL) mockPortletUrl.proxy()));
195
196 Map paramMap = new HashMap();
197 paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/help/testAction"});
198 paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
199
200 mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
201 mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.HELP));
202 mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
203
204 tag.setAction("testAction");
205 tag.setPortletMode("help");
206 tag.doStartTag();
207 tag.doEndTag();
208 }
209
210 public void testUrlWithQueryParams() throws Exception {
211
212 PortletMode mode = PortletMode.VIEW;
213
214 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
215
216 mockPortletRes.expects(once()).method("createRenderURL").will(
217 returnValue((PortletURL) mockPortletUrl.proxy()));
218
219 Map paramMap = new HashMap();
220 paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
221 paramMap.put("testParam1", new String[]{"testValue1"});
222 paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
223
224 mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
225 mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
226 mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
227
228 tag.setAction("testAction?testParam1=testValue1");
229 tag.doStartTag();
230 tag.doEndTag();
231 }
232
233 public void testActionUrl() throws Exception {
234
235 PortletMode mode = PortletMode.VIEW;
236
237 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
238
239 mockPortletRes.expects(once()).method("createActionURL").will(
240 returnValue((PortletURL) mockPortletUrl.proxy()));
241
242 Map paramMap = new HashMap();
243 paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
244 paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
245
246 mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
247 mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
248 mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
249
250 tag.setAction("testAction");
251 tag.setPortletUrlType("action");
252 tag.doStartTag();
253 tag.doEndTag();
254 }
255
256 public void testResourceUrl() throws Exception {
257 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
258 mockPortletRes.expects(once()).method("encodeURL").will(returnValue("/contextPath/image.gif"));
259 mockJspWriter.setExpectedData("/contextPath/image.gif");
260 tag.setValue("image.gif");
261 tag.doStartTag();
262 tag.doEndTag();
263 mockJspWriter.verify();
264 }
265
266 public void testResourceUrlWithNestedParam() throws Exception {
267 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
268 mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1")).will(returnValue("/contextPath/image.gif?testParam1=testValue1"));
269 mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1");
270
271 ParamTag paramTag = new ParamTag();
272 paramTag.setPageContext((PageContext)mockPageCtx.proxy());
273 paramTag.setParent(tag);
274 paramTag.setName("testParam1");
275 paramTag.setValue("'testValue1'");
276 tag.setValue("image.gif");
277 tag.doStartTag();
278 paramTag.doStartTag();
279 paramTag.doEndTag();
280 tag.doEndTag();
281 mockJspWriter.verify();
282 }
283
284 public void testResourceUrlWithTwoNestedParam() throws Exception {
285 mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
286 mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2")).will(returnValue("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2"));
287 mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2");
288
289 ParamTag paramTag = new ParamTag();
290 paramTag.setPageContext((PageContext)mockPageCtx.proxy());
291 paramTag.setParent(tag);
292 paramTag.setName("testParam1");
293 paramTag.setValue("'testValue1'");
294 ParamTag paramTag2 = new ParamTag();
295 paramTag2.setPageContext((PageContext)mockPageCtx.proxy());
296 paramTag2.setParent(tag);
297 paramTag2.setName("testParam2");
298 paramTag2.setValue("'testValue2'");
299 tag.setValue("image.gif");
300 tag.doStartTag();
301 paramTag.doStartTag();
302 paramTag.doEndTag();
303 paramTag2.doStartTag();
304 paramTag2.doEndTag();
305 tag.doEndTag();
306 mockJspWriter.verify();
307 }
308
309 private static class ParamMapConstraint implements Constraint {
310
311 private Map myExpectedMap = null;
312 private Map myActualMap = null;
313
314 public ParamMapConstraint(Map expectedMap) {
315 if(expectedMap == null) {
316 throw new IllegalArgumentException("Use an isNull constraint instead!");
317 }
318 myExpectedMap = expectedMap;
319 }
320
321
322
323
324 public boolean eval(Object val) {
325 myActualMap = (Map)val;
326 boolean result = false;
327 if(val != null) {
328 if(myExpectedMap.size() == myActualMap.size()) {
329 Iterator keys = myExpectedMap.keySet().iterator();
330 boolean allSame = true;
331 while(keys.hasNext()) {
332 Object key = keys.next();
333 if(!myActualMap.containsKey(key)) {
334 allSame = false;
335 break;
336 }
337 else {
338 String[] expected = (String[])myExpectedMap.get(key);
339 String[] actual = (String[])myActualMap.get(key);
340 if(!Arrays.equals(expected, actual)) {
341 allSame = false;
342 break;
343 }
344 }
345 }
346 result = allSame;
347 }
348 }
349 return result;
350 }
351
352
353
354
355 public StringBuffer describeTo(StringBuffer sb) {
356 return sb.append(myExpectedMap);
357 }
358
359
360
361 }
362
363 }