KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.springframework.mock.web.MockPageContext;
19 import org.springframework.mock.web.portlet.MockPortletRequest;
20 import org.springframework.mock.web.portlet.MockRenderResponse;
21
22
23 /**
24  * @author Eric Dalquist <a HREF="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
25  * @version $Id: PortletRequestHelperTest.java 943 2005-12-26 22:36:54Z fgiust $
26  */

27 public class PortletRequestHelperTest extends TestCase
28 {
29
30     /**
31      * @see junit.framework.TestCase#getName()
32      */

33     public String JavaDoc getName()
34     {
35         return "PortletRequestHelper Test";
36     }
37
38     public void testNullPageContext()
39     {
40         try
41         {
42             new PortletRequestHelper(null);
43             fail("IllegalArgumentException should have been thrown");
44         }
45         catch (IllegalArgumentException JavaDoc iae)
46         {
47             // expected
48
}
49     }
50
51     public void testEmptyPageContext()
52     {
53         final MockPageContext pageContext = new MockPageContext();
54
55         try
56         {
57             new PortletRequestHelper(pageContext);
58             fail("IllegalStateException should have been thrown");
59         }
60         catch (IllegalStateException JavaDoc ise)
61         {
62             // expected
63
}
64
65         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest());
66         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, null);
67         try
68         {
69             new PortletRequestHelper(pageContext);
70             fail("IllegalStateException should have been thrown");
71         }
72         catch (IllegalStateException JavaDoc ise)
73         {
74             // expected
75
}
76
77         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, null);
78         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
79         try
80         {
81             new PortletRequestHelper(pageContext);
82             fail("IllegalStateException should have been thrown");
83         }
84         catch (IllegalStateException JavaDoc ise)
85         {
86             // expected
87
}
88     }
89
90     public void testBasicPageContext()
91     {
92         final MockPageContext pageContext = new MockPageContext();
93
94         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest());
95         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
96
97         new PortletRequestHelper(pageContext);
98     }
99
100     public void testRequestParameters()
101     {
102         final MockPageContext pageContext = new MockPageContext();
103         final MockPortletRequest request = new MockPortletRequest();
104
105         request.setParameter("STRING_PARAM", "STRING_VALUE");
106         request.setParameter("INTEGER_PARAM", "31337");
107
108         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request);
109         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
110
111         final PortletRequestHelper helper = new PortletRequestHelper(pageContext);
112
113         final String JavaDoc strVal = helper.getParameter("STRING_PARAM");
114         assertEquals("STRING_VALUE", strVal);
115
116         final Integer JavaDoc intVal = helper.getIntParameter("INTEGER_PARAM");
117         assertEquals(new Integer JavaDoc(31337), intVal);
118
119         final Integer JavaDoc nullIntVal = helper.getIntParameter("STRING_PARAM");
120         assertNull(nullIntVal);
121
122         final Map JavaDoc params = helper.getParameterMap();
123         assertEquals(2, params.size());
124
125         final String JavaDoc[] expextedStrArryVal = new String JavaDoc[]{"STRING_VALUE"};
126         final String JavaDoc[] strArryVal = (String JavaDoc[]) params.get("STRING_PARAM");
127         assertEquals(expextedStrArryVal.length, strArryVal.length);
128         assertEquals(expextedStrArryVal[0], strArryVal[0]);
129
130         final String JavaDoc[] expextedIntArryVal = new String JavaDoc[]{"31337"};
131         final String JavaDoc[] intArryVal = (String JavaDoc[]) params.get("INTEGER_PARAM");
132         assertEquals(expextedIntArryVal.length, intArryVal.length);
133         assertEquals(expextedIntArryVal[0], intArryVal[0]);
134     }
135
136     public void testCreateEmptyHref()
137     {
138         final MockPageContext pageContext = new MockPageContext();
139
140         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest());
141         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
142
143         final PortletRequestHelper helper = new PortletRequestHelper(pageContext);
144
145         final PortletHref ref = (PortletHref) helper.getHref();
146
147         final Map JavaDoc params = ref.getParameterMap();
148         assertEquals(0, params.size());
149
150         assertNull(ref.getAnchor());
151         assertNull(ref.getRequestedMode());
152         assertNull(ref.getRequestedState());
153
154         assertFalse(ref.isRequestedSecure());
155     }
156
157     public void testCreateSecureHref()
158     {
159         final MockPageContext pageContext = new MockPageContext();
160         final MockPortletRequest request = new MockPortletRequest();
161
162         request.setSecure(true);
163
164         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request);
165         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
166
167         final PortletRequestHelper helper = new PortletRequestHelper(pageContext);
168
169         final PortletHref ref = (PortletHref) helper.getHref();
170
171         final Map JavaDoc params = ref.getParameterMap();
172         assertEquals(0, params.size());
173
174         assertNull(ref.getAnchor());
175         assertNull(ref.getRequestedMode());
176         assertNull(ref.getRequestedState());
177
178         assertTrue(ref.isRequestedSecure());
179     }
180
181     public void testParameterizedHref()
182     {
183         final MockPageContext pageContext = new MockPageContext();
184         final MockPortletRequest request = new MockPortletRequest();
185
186         request.setParameter("STRING_PARAM", "STRING_VALUE");
187         request.setParameter("INTEGER_PARAM", "31337");
188
189         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request);
190         pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse());
191
192         final PortletRequestHelper helper = new PortletRequestHelper(pageContext);
193
194         final PortletHref ref = (PortletHref) helper.getHref();
195
196         final Map JavaDoc params = ref.getParameterMap();
197         assertEquals(2, params.size());
198
199         final String JavaDoc[] expextedStrArryVal = new String JavaDoc[]{"STRING_VALUE"};
200         final String JavaDoc[] strArryVal = (String JavaDoc[]) params.get("STRING_PARAM");
201         assertEquals(expextedStrArryVal.length, strArryVal.length);
202         assertEquals(expextedStrArryVal[0], strArryVal[0]);
203
204         final String JavaDoc[] expextedIntArryVal = new String JavaDoc[]{"31337"};
205         final String JavaDoc[] intArryVal = (String JavaDoc[]) params.get("INTEGER_PARAM");
206         assertEquals(expextedIntArryVal.length, intArryVal.length);
207         assertEquals(expextedIntArryVal[0], intArryVal[0]);
208
209         assertNull(ref.getAnchor());
210         assertNull(ref.getRequestedMode());
211         assertNull(ref.getRequestedState());
212
213         assertFalse(ref.isRequestedSecure());
214     }
215 }
216
Popular Tags