KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > portlet > PortletHrefTest


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.portlet;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.portlet.PortletMode;
18 import javax.portlet.WindowState;
19
20 import junit.framework.TestCase;
21
22 import org.springframework.mock.web.portlet.MockPortletRequest;
23 import org.springframework.mock.web.portlet.MockRenderResponse;
24
25
26 /**
27  * @author Eric Dalquist <a HREF="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
28  * @version $Id: PortletHrefTest.java 943 2005-12-26 22:36:54Z fgiust $
29  */

30 public class PortletHrefTest extends TestCase
31 {
32
33     /**
34      * @see junit.framework.TestCase#getName()
35      */

36     public String JavaDoc getName()
37     {
38         return "PortletHref Test";
39     }
40
41     public void testConstructor()
42     {
43         try
44         {
45             new PortletHref(null, null);
46             fail("IllegalArgumentException should have been thrown");
47         }
48         catch (IllegalArgumentException JavaDoc iae)
49         {
50             // expected
51
}
52
53         try
54         {
55             new PortletHref(new MockPortletRequest(), null);
56             fail("IllegalArgumentException should have been thrown");
57         }
58         catch (IllegalArgumentException JavaDoc iae)
59         {
60             // expected
61
}
62
63         try
64         {
65             new PortletHref(null, new MockRenderResponse());
66             fail("IllegalArgumentException should have been thrown");
67         }
68         catch (IllegalArgumentException JavaDoc iae)
69         {
70             // expected
71
}
72
73         new PortletHref(new MockPortletRequest(), new MockRenderResponse());
74     }
75
76     public void testUrlTypeParameters()
77     {
78         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
79
80         assertFalse(href.isAction());
81
82         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
83         assertTrue(href.isAction());
84
85         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_RENDER);
86         assertFalse(href.isAction());
87
88         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
89         assertTrue(href.isAction());
90
91         try
92         {
93             href.addParameter(PortletHref.PARAM_TYPE, null);
94             fail("IllegalArgumentException should have been thrown");
95         }
96         catch (IllegalArgumentException JavaDoc iae)
97         {
98             // expected
99
}
100
101         try
102         {
103             href.addParameter(PortletHref.PARAM_TYPE, "");
104             fail("IllegalArgumentException should have been thrown");
105         }
106         catch (IllegalArgumentException JavaDoc iae)
107         {
108             // expected
109
}
110
111         try
112         {
113             href.addParameter(PortletHref.PARAM_TYPE, "InvalidUrlType");
114             fail("IllegalArgumentException should have been thrown");
115         }
116         catch (IllegalArgumentException JavaDoc iae)
117         {
118             // expected
119
}
120     }
121
122     public void testSecureParameters()
123     {
124         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
125
126         assertFalse(href.isRequestedSecure());
127
128         href.addParameter(PortletHref.PARAM_SECURE, Boolean.TRUE.toString());
129         assertTrue(href.isRequestedSecure());
130
131         href.addParameter(PortletHref.PARAM_SECURE, "true");
132         assertTrue(href.isRequestedSecure());
133
134         href.addParameter(PortletHref.PARAM_SECURE, "TRUE");
135         assertTrue(href.isRequestedSecure());
136
137         href.addParameter(PortletHref.PARAM_SECURE, "yes");
138         assertFalse(href.isRequestedSecure());
139
140         href.addParameter(PortletHref.PARAM_SECURE, null);
141         assertFalse(href.isRequestedSecure());
142
143         href.addParameter(PortletHref.PARAM_SECURE, "false");
144         assertFalse(href.isRequestedSecure());
145     }
146
147     public void testPortletModeParameters()
148     {
149         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
150
151         assertNull(href.getRequestedMode());
152
153         href.addParameter(PortletHref.PARAM_MODE, "view");
154         assertEquals(new PortletMode("view"), href.getRequestedMode());
155
156         href.addParameter(PortletHref.PARAM_MODE, "help");
157         assertEquals(new PortletMode("help"), href.getRequestedMode());
158
159         href.addParameter(PortletHref.PARAM_MODE, "edit");
160         assertEquals(new PortletMode("edit"), href.getRequestedMode());
161
162         href.addParameter(PortletHref.PARAM_MODE, null);
163         assertNull(href.getRequestedMode());
164
165         try
166         {
167             href.addParameter(PortletHref.PARAM_MODE, "info");
168             fail("IllegalArgumentException should have been thrown");
169         }
170         catch (IllegalArgumentException JavaDoc iae)
171         {
172             // expected
173
}
174     }
175
176     public void testWindowStateParameters()
177     {
178         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
179
180         assertNull(href.getRequestedState());
181
182         href.addParameter(PortletHref.PARAM_STATE, "normal");
183         assertEquals(new WindowState("normal"), href.getRequestedState());
184
185         href.addParameter(PortletHref.PARAM_STATE, "minimized");
186         assertEquals(new WindowState("minimized"), href.getRequestedState());
187
188         href.addParameter(PortletHref.PARAM_STATE, "maximized");
189         assertEquals(new WindowState("maximized"), href.getRequestedState());
190
191         href.addParameter(PortletHref.PARAM_STATE, null);
192         assertNull(href.getRequestedState());
193
194         try
195         {
196             href.addParameter(PortletHref.PARAM_STATE, "exclusive");
197             fail("IllegalArgumentException should have been thrown");
198         }
199         catch (IllegalArgumentException JavaDoc iae)
200         {
201             // expected
202
}
203     }
204
205     public void testInvalidPrefixUseParameters()
206     {
207         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
208
209         try
210         {
211             href.addParameter("portlet:WindowState", "exclusive");
212             fail("IllegalArgumentException should have been thrown");
213         }
214         catch (IllegalArgumentException JavaDoc iae)
215         {
216             // expected
217
}
218     }
219
220     public void testAddParameters()
221     {
222         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
223
224         href.addParameter(PortletHref.PARAM_MODE, "help");
225         href.addParameter(PortletHref.PARAM_STATE, "maximized");
226         href.addParameter(PortletHref.PARAM_SECURE, "true");
227         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
228         href.addParameter("SINGLE_PARAM", "VAL1");
229         href.addParameter("INT_PARAM", 31337);
230
231         assertTrue(href.isRequestedSecure());
232         assertEquals(new PortletMode("help"), href.getRequestedMode());
233         assertEquals(new WindowState("maximized"), href.getRequestedState());
234         assertTrue(href.isAction());
235
236         final Map JavaDoc actualParams = href.getParameterMap();
237         assertEquals(2, actualParams.size());
238         assertEquals(actualParams.get("SINGLE_PARAM"), "VAL1");
239         assertEquals(actualParams.get("INT_PARAM"), "31337");
240     }
241
242     public void testAddParameterMap()
243     {
244         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
245
246         final Map JavaDoc params = new HashMap JavaDoc();
247         params.put(PortletHref.PARAM_MODE, "help");
248         params.put(PortletHref.PARAM_STATE, "maximized");
249         params.put(PortletHref.PARAM_SECURE, "true");
250         params.put(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
251         params.put("SINGLE_PARAM", "VAL1");
252         final String JavaDoc[] multiParam = new String JavaDoc[]{"VAL2", "VAL3"};
253         params.put("MULTI_PARAM", multiParam);
254         params.put("INT_PARAM", new Integer JavaDoc("31337"));
255
256         href.addParameterMap(params);
257
258         assertTrue(href.isRequestedSecure());
259         assertEquals(new PortletMode("help"), href.getRequestedMode());
260         assertEquals(new WindowState("maximized"), href.getRequestedState());
261         assertTrue(href.isAction());
262
263         final Map JavaDoc actualParams = href.getParameterMap();
264         assertEquals(3, actualParams.size());
265         assertEquals(actualParams.get("SINGLE_PARAM"), "VAL1");
266         assertEquals(actualParams.get("INT_PARAM"), "31337");
267
268         final String JavaDoc[] actualMultiParam = (String JavaDoc[]) actualParams.get("MULTI_PARAM");
269         assertEquals(multiParam.length, actualMultiParam.length);
270         assertEquals(multiParam[0], actualMultiParam[0]);
271         assertEquals(multiParam[1], actualMultiParam[1]);
272     }
273
274     public void testSetParameterMap()
275     {
276         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
277
278         href.addParameter("ORIGINAL_PARAM", "ORIGNAL_VALUE");
279
280         final Map JavaDoc params = new HashMap JavaDoc();
281         params.put(PortletHref.PARAM_MODE, "help");
282         params.put(PortletHref.PARAM_STATE, "maximized");
283         params.put(PortletHref.PARAM_SECURE, "true");
284         params.put(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
285         params.put("SINGLE_PARAM", "VAL1");
286         final String JavaDoc[] multiParam = new String JavaDoc[]{"VAL2", "VAL3"};
287         params.put("MULTI_PARAM", multiParam);
288         params.put("INT_PARAM", new Integer JavaDoc("31337"));
289
290         href.setParameterMap(params);
291
292         assertTrue(href.isRequestedSecure());
293         assertEquals(new PortletMode("help"), href.getRequestedMode());
294         assertEquals(new WindowState("maximized"), href.getRequestedState());
295         assertTrue(href.isAction());
296
297         final Map JavaDoc actualParams = href.getParameterMap();
298         assertEquals(3, actualParams.size());
299         assertEquals(actualParams.get("SINGLE_PARAM"), "VAL1");
300         assertEquals(actualParams.get("INT_PARAM"), "31337");
301         assertNull(actualParams.get("ORIGINAL_PARAM"));
302
303         final String JavaDoc[] actualMultiParam = (String JavaDoc[]) actualParams.get("MULTI_PARAM");
304         assertEquals(multiParam.length, actualMultiParam.length);
305         assertEquals(multiParam[0], actualMultiParam[0]);
306         assertEquals(multiParam[1], actualMultiParam[1]);
307     }
308
309     public void testRemoveParameter()
310     {
311         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
312
313         href.addParameter("SINGLE_PARAM", "VAL1");
314
315         final Map JavaDoc actualParams = href.getParameterMap();
316         assertEquals(1, actualParams.size());
317         assertEquals(actualParams.get("SINGLE_PARAM"), "VAL1");
318
319         href.removeParameter("SINGLE_PARAM");
320
321         final Map JavaDoc actualParams2 = href.getParameterMap();
322         assertEquals(0, actualParams2.size());
323         assertNull(actualParams2.get("SINGLE_PARAM"));
324     }
325
326     public void testClone()
327     {
328         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
329
330         href.addParameter(PortletHref.PARAM_MODE, "help");
331         href.addParameter(PortletHref.PARAM_STATE, "maximized");
332         href.addParameter(PortletHref.PARAM_SECURE, "true");
333         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
334         href.addParameter("SINGLE_PARAM", "VAL1");
335         href.addParameter("INT_PARAM", 31337);
336
337         assertTrue(href.isRequestedSecure());
338         assertEquals(new PortletMode("help"), href.getRequestedMode());
339         assertEquals(new WindowState("maximized"), href.getRequestedState());
340         assertTrue(href.isAction());
341
342         final Map JavaDoc actualParams = href.getParameterMap();
343         assertEquals(2, actualParams.size());
344         assertEquals(actualParams.get("SINGLE_PARAM"), "VAL1");
345         assertEquals(actualParams.get("INT_PARAM"), "31337");
346
347         final PortletHref href2 = (PortletHref) href.clone();
348         assertTrue(href != href2);
349         assertEquals(href, href2);
350         assertEquals(href.hashCode(), href2.hashCode());
351     }
352
353     public void testBaseUrl()
354     {
355         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
356
357         href.addParameter(PortletHref.PARAM_MODE, "help");
358         href.addParameter(PortletHref.PARAM_STATE, "maximized");
359         href.addParameter(PortletHref.PARAM_SECURE, "true");
360         href.addParameter("SINGLE_PARAM", "VAL1");
361
362         final String JavaDoc baseRenderUrl = href.getBaseUrl();
363         assertEquals("http://localhost/mockportlet?urlType=render", baseRenderUrl);
364
365         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
366
367         final String JavaDoc baseActionUrl = href.getBaseUrl();
368         assertEquals("http://localhost/mockportlet?urlType=action", baseActionUrl);
369     }
370
371     public void testFullUrl()
372     {
373         final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse());
374
375         final String JavaDoc urlString1 = href.toString();
376         assertEquals("http://localhost/mockportlet?urlType=render", urlString1);
377
378         href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION);
379         final String JavaDoc urlString2 = href.toString();
380         assertEquals("http://localhost/mockportlet?urlType=action", urlString2);
381
382         href.addParameter(PortletHref.PARAM_SECURE, Boolean.TRUE.toString());
383         final String JavaDoc urlString3 = href.toString();
384         assertEquals("https://localhost/mockportlet?urlType=action", urlString3);
385
386         // This code should work but the MockPortletURL provided by spring doesn't follow the
387
// interface docs on invalid modes/states
388
// href.setRequestedState(new WindowState("exclusive"));
389
// try {
390
// href.toString();
391
// fail("IllegalStateException should have been thrown.");
392
// }
393
// catch (IllegalStateException ise) {
394
// //expected
395
// }
396

397         href.addParameter(PortletHref.PARAM_STATE, "normal");
398         final String JavaDoc urlString7 = href.toString();
399         assertEquals("https://localhost/mockportlet?urlType=action;windowState=normal", urlString7);
400
401         href.addParameter(PortletHref.PARAM_STATE, "minimized");
402         final String JavaDoc urlString8 = href.toString();
403         assertEquals("https://localhost/mockportlet?urlType=action;windowState=minimized", urlString8);
404
405         href.addParameter(PortletHref.PARAM_STATE, "maximized");
406         final String JavaDoc urlString9 = href.toString();
407         assertEquals("https://localhost/mockportlet?urlType=action;windowState=maximized", urlString9);
408
409         // This code should work but the MockPortletURL provided by spring doesn't follow the
410
// interface docs on invalid modes/states
411
// href.setRequestedMode(new PortletMode("info"));
412
// try {
413
// href.toString();
414
// fail("IllegalStateException should have been thrown.");
415
// }
416
// catch (IllegalStateException ise) {
417
// //expected
418
// }
419

420         href.addParameter(PortletHref.PARAM_MODE, "view");
421         final String JavaDoc urlString4 = href.toString();
422         assertEquals("https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=view", urlString4);
423
424         href.addParameter(PortletHref.PARAM_MODE, "help");
425         final String JavaDoc urlString5 = href.toString();
426         assertEquals("https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=help", urlString5);
427
428         href.addParameter(PortletHref.PARAM_MODE, "edit");
429         final String JavaDoc urlString6 = href.toString();
430         assertEquals("https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=edit", urlString6);
431
432         href.addParameter("SINGLE_PARAM", "VAL");
433         final String JavaDoc urlString10 = href.toString();
434         assertEquals(
435             "https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=edit;param_SINGLE_PARAM=VAL",
436             urlString10);
437
438         final Map JavaDoc paramMap = new HashMap JavaDoc();
439         paramMap.put("MULTI_PARAM", new String JavaDoc[]{"VAL1", "VAL2"});
440         href.addParameterMap(paramMap);
441         final String JavaDoc urlString11 = href.toString();
442         assertEquals(
443             "https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=edit;param_SINGLE_PARAM=VAL;param_MULTI_PARAM=VAL1;param_MULTI_PARAM=VAL2",
444             urlString11);
445
446         href.setAnchor("ANCHOR");
447         final String JavaDoc urlString12 = href.toString();
448         assertEquals(
449             "https://localhost/mockportlet?urlType=action;windowState=maximized;portletMode=edit;param_SINGLE_PARAM=VAL;param_MULTI_PARAM=VAL1;param_MULTI_PARAM=VAL2#ANCHOR",
450             urlString12);
451     }
452 }
Popular Tags