KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletWebRequest


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.portlet;
16
17 import java.util.List JavaDoc;
18
19 import javax.portlet.PortletRequest;
20 import javax.portlet.PortletResponse;
21 import javax.portlet.PortletSession;
22
23 import org.apache.tapestry.web.WebRequest;
24 import org.apache.tapestry.web.WebSession;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.web.PortletWebRequest}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestPortletWebRequest extends BasePortletWebTestCase
34 {
35     private PortletRequest newRequest()
36     {
37         return (PortletRequest) newMock(PortletRequest.class);
38     }
39
40     private PortletResponse newResponse()
41     {
42         return (PortletResponse) newMock(PortletResponse.class);
43     }
44
45     public void testGetParameterNames()
46     {
47         MockControl control = newControl(PortletRequest.class);
48         PortletRequest request = (PortletRequest) control.getMock();
49
50         request.getParameterNames();
51         control.setReturnValue(newEnumeration());
52
53         replayControls();
54
55         WebRequest wr = new PortletWebRequest(request);
56
57         List JavaDoc l = wr.getParameterNames();
58
59         checkList(l);
60
61         verifyControls();
62     }
63
64     public void testGetParameterValue()
65     {
66         String JavaDoc value = "William Orbit";
67
68         MockControl control = newControl(PortletRequest.class);
69         PortletRequest request = (PortletRequest) control.getMock();
70
71         request.getParameter("artist");
72         control.setReturnValue(value);
73
74         replayControls();
75
76         WebRequest wr = new PortletWebRequest(request);
77
78         assertSame(value, wr.getParameterValue("artist"));
79
80         verifyControls();
81     }
82
83     public void testGetParameterValues()
84     {
85         String JavaDoc[] values =
86         { "William Orbit", "Steely Dan" };
87         MockControl control = newControl(PortletRequest.class);
88         PortletRequest request = (PortletRequest) control.getMock();
89
90         request.getParameterValues("artist");
91         control.setReturnValue(values);
92
93         replayControls();
94
95         WebRequest wr = new PortletWebRequest(request);
96
97         assertSame(values, wr.getParameterValues("artist"));
98
99         verifyControls();
100     }
101
102     public void testGetContextPath()
103     {
104         MockControl control = newControl(PortletRequest.class);
105         PortletRequest request = (PortletRequest) control.getMock();
106
107         request.getContextPath();
108         control.setReturnValue("/foo");
109
110         replayControls();
111
112         WebRequest wr = new PortletWebRequest(request);
113
114         assertEquals("/foo", wr.getContextPath());
115
116         verifyControls();
117     }
118
119     public void testGetAttributeNames()
120     {
121         MockControl control = newControl(PortletRequest.class);
122         PortletRequest request = (PortletRequest) control.getMock();
123
124         request.getAttributeNames();
125         control.setReturnValue(newEnumeration());
126
127         replayControls();
128
129         WebRequest wr = new PortletWebRequest(request);
130
131         List JavaDoc l = wr.getAttributeNames();
132
133         checkList(l);
134
135         verifyControls();
136     }
137
138     public void testGetAttribute()
139     {
140         Object JavaDoc attribute = new Object JavaDoc();
141         MockControl control = newControl(PortletRequest.class);
142         PortletRequest request = (PortletRequest) control.getMock();
143
144         request.getAttribute("attr");
145         control.setReturnValue(attribute);
146
147         replayControls();
148
149         WebRequest wr = new PortletWebRequest(request);
150
151         assertSame(attribute, wr.getAttribute("attr"));
152
153         verifyControls();
154     }
155
156     public void testSetAttribute()
157     {
158         Object JavaDoc attribute = new Object JavaDoc();
159
160         MockControl control = newControl(PortletRequest.class);
161         PortletRequest request = (PortletRequest) control.getMock();
162
163         request.setAttribute("name", attribute);
164
165         replayControls();
166
167         WebRequest wr = new PortletWebRequest(request);
168
169         wr.setAttribute("name", attribute);
170
171         verifyControls();
172     }
173
174     public void testSetAttributeToNull()
175     {
176         MockControl control = newControl(PortletRequest.class);
177         PortletRequest request = (PortletRequest) control.getMock();
178
179         request.removeAttribute("tonull");
180
181         replayControls();
182
183         WebRequest wr = new PortletWebRequest(request);
184
185         wr.setAttribute("tonull", null);
186
187         verifyControls();
188     }
189
190     public void testGetSession()
191     {
192         MockControl control = newControl(PortletRequest.class);
193         PortletRequest request = (PortletRequest) control.getMock();
194
195         PortletSession session = (PortletSession) newMock(PortletSession.class);
196
197         request.getPortletSession(false);
198         control.setReturnValue(null);
199
200         // Get it, doesn't exist, wreate false
201

202         replayControls();
203
204         WebRequest wr = new PortletWebRequest(request);
205
206         assertNull(wr.getSession(false));
207
208         verifyControls();
209
210         request.getPortletSession(true);
211         control.setReturnValue(session);
212
213         // #2: Get it, wreate is true, it is wreated.
214

215         replayControls();
216
217         WebSession cs = wr.getSession(true);
218
219         verifyControls();
220
221         // #3: Cached in local variable, make sure same
222
// think returned.
223

224         replayControls();
225
226         assertSame(cs, wr.getSession(false));
227
228         verifyControls();
229     }
230
231     public void testGetScheme()
232     {
233         MockControl control = newControl(PortletRequest.class);
234         PortletRequest request = (PortletRequest) control.getMock();
235
236         request.getScheme();
237         control.setReturnValue("http");
238
239         replayControls();
240
241         WebRequest wr = new PortletWebRequest(request);
242
243         assertEquals("http", wr.getScheme());
244
245         verifyControls();
246     }
247
248     public void testGetServerName()
249     {
250         MockControl control = newControl(PortletRequest.class);
251         PortletRequest request = (PortletRequest) control.getMock();
252
253         request.getServerName();
254         control.setReturnValue("www.myhost.com");
255
256         replayControls();
257
258         WebRequest wr = new PortletWebRequest(request);
259
260         assertEquals("www.myhost.com", wr.getServerName());
261
262         verifyControls();
263     }
264
265     public void testGetServerPort()
266     {
267         MockControl control = newControl(PortletRequest.class);
268         PortletRequest request = (PortletRequest) control.getMock();
269
270         request.getServerPort();
271         control.setReturnValue(80);
272
273         replayControls();
274
275         WebRequest wr = new PortletWebRequest(request);
276
277         assertEquals(80, wr.getServerPort());
278
279         verifyControls();
280     }
281
282     public void testGetRequestURIUnsupported()
283     {
284         PortletRequest request = newRequest();
285
286         replayControls();
287
288         WebRequest wr = new PortletWebRequest(request);
289
290         assertEquals("<PortletRequest>", wr.getRequestURI());
291
292         verifyControls();
293     }
294
295     public void testForwardUnsupported()
296     {
297         PortletRequest request = newRequest();
298
299         replayControls();
300
301         WebRequest wr = new PortletWebRequest(request);
302
303         try
304         {
305             wr.forward(null);
306             unreachable();
307         }
308         catch (UnsupportedOperationException JavaDoc ex)
309         {
310             // Expected.
311
}
312
313         verifyControls();
314     }
315
316 }
Popular Tags