KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > TestServletWebRequest


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.web;
16
17 import java.io.IOException JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.RequestDispatcher JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25
26 import org.apache.hivemind.ApplicationRuntimeException;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.web.ServletWebRequest}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

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

227         replayControls();
228
229         WebRequest wr = new ServletWebRequest(request, response);
230
231         assertNull(wr.getSession(false));
232
233         verifyControls();
234
235         request.getSession(true);
236         control.setReturnValue(session);
237
238         // #2: Get it, wreate is true, it is wreated.
239

240         replayControls();
241
242         WebSession cs = wr.getSession(true);
243
244         verifyControls();
245
246         // #3: Cached in local variable, make sure same
247
// think returned.
248

249         replayControls();
250
251         assertSame(cs, wr.getSession(false));
252
253         verifyControls();
254     }
255
256     public void testGetScheme()
257     {
258         HttpServletResponse JavaDoc response = newResponse();
259
260         MockControl control = newControl(HttpServletRequest JavaDoc.class);
261         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
262
263         request.getScheme();
264         control.setReturnValue("http");
265
266         replayControls();
267
268         WebRequest wr = new ServletWebRequest(request, response);
269
270         assertEquals("http", wr.getScheme());
271
272         verifyControls();
273     }
274
275     public void testGetServerName()
276     {
277         HttpServletResponse JavaDoc response = newResponse();
278
279         MockControl control = newControl(HttpServletRequest JavaDoc.class);
280         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
281
282         request.getServerName();
283         control.setReturnValue("www.myhost.com");
284
285         replayControls();
286
287         WebRequest wr = new ServletWebRequest(request, response);
288
289         assertEquals("www.myhost.com", wr.getServerName());
290
291         verifyControls();
292     }
293
294     public void testGetServerPort()
295     {
296         HttpServletResponse JavaDoc response = newResponse();
297
298         MockControl control = newControl(HttpServletRequest JavaDoc.class);
299         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
300
301         request.getServerPort();
302         control.setReturnValue(80);
303
304         replayControls();
305
306         WebRequest wr = new ServletWebRequest(request, response);
307
308         assertEquals(80, wr.getServerPort());
309
310         verifyControls();
311     }
312
313     public void testGetRequestURI()
314     {
315         HttpServletResponse JavaDoc response = newResponse();
316
317         MockControl control = newControl(HttpServletRequest JavaDoc.class);
318         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
319
320         request.getRequestURI();
321         control.setReturnValue("/foo/bar");
322
323         replayControls();
324
325         WebRequest wr = new ServletWebRequest(request, response);
326
327         assertEquals("/foo/bar", wr.getRequestURI());
328
329         verifyControls();
330     }
331
332     public void testForwardInternal() throws Exception JavaDoc
333     {
334         MockControl control = newControl(HttpServletRequest JavaDoc.class);
335         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
336
337         HttpServletResponse JavaDoc response = newResponse();
338
339         RequestDispatcher JavaDoc dispatcher = newDispatcher();
340
341         request.getRequestDispatcher("/local.html");
342         control.setReturnValue(dispatcher);
343
344         dispatcher.forward(request, response);
345
346         replayControls();
347
348         WebRequest wr = new ServletWebRequest(request, response);
349
350         wr.forward("local.html");
351
352         verifyControls();
353     }
354
355     public void testForwardNull() throws Exception JavaDoc
356     {
357         MockControl control = newControl(HttpServletRequest JavaDoc.class);
358         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
359
360         HttpServletResponse JavaDoc response = newResponse();
361
362         RequestDispatcher JavaDoc dispatcher = newDispatcher();
363
364         request.getRequestDispatcher("/");
365         control.setReturnValue(dispatcher);
366
367         dispatcher.forward(request, response);
368
369         replayControls();
370
371         WebRequest wr = new ServletWebRequest(request, response);
372
373         wr.forward(null);
374
375         verifyControls();
376     }
377
378     public void testForwardInternalNoDispatcher() throws Exception JavaDoc
379     {
380         MockControl control = newControl(HttpServletRequest JavaDoc.class);
381         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
382
383         HttpServletResponse JavaDoc response = newResponse();
384         request.getRequestDispatcher("/local.html");
385         control.setReturnValue(null);
386
387         replayControls();
388
389         WebRequest wr = new ServletWebRequest(request, response);
390
391         try
392         {
393             wr.forward("local.html");
394             unreachable();
395         }
396         catch (ApplicationRuntimeException ex)
397         {
398             assertEquals(
399                     "Unable to find a request dispatcher for local resource '/local.html'.",
400                     ex.getMessage());
401         }
402
403         verifyControls();
404     }
405
406     public void testForwardInternalFailure() throws Exception JavaDoc
407     {
408         MockControl control = newControl(HttpServletRequest JavaDoc.class);
409         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
410
411         HttpServletResponse JavaDoc response = newResponse();
412
413         MockControl dispatcherc = newControl(RequestDispatcher JavaDoc.class);
414         RequestDispatcher JavaDoc dispatcher = (RequestDispatcher JavaDoc) dispatcherc.getMock();
415
416         Throwable JavaDoc t1 = new ServletException JavaDoc("Mock Servlet Exception");
417
418         request.getRequestDispatcher("/servlet-exception.html");
419         control.setReturnValue(dispatcher);
420
421         dispatcher.forward(request, response);
422         dispatcherc.setThrowable(t1);
423
424         replayControls();
425
426         WebRequest wr = new ServletWebRequest(request, response);
427
428         try
429         {
430             wr.forward("servlet-exception.html");
431             unreachable();
432         }
433         catch (ApplicationRuntimeException ex)
434         {
435             assertEquals(
436                     "Unable to forward to local resource '/servlet-exception.html': Mock Servlet Exception",
437                     ex.getMessage());
438             assertSame(t1, ex.getRootCause());
439         }
440
441         verifyControls();
442
443         Throwable JavaDoc t2 = new IOException JavaDoc("Mock IO Exception");
444
445         request.getRequestDispatcher("/io-exception.html");
446         control.setReturnValue(dispatcher);
447
448         dispatcher.forward(request, response);
449         dispatcherc.setThrowable(t2);
450
451         replayControls();
452
453         try
454         {
455             wr.forward("io-exception.html");
456             unreachable();
457         }
458         catch (ApplicationRuntimeException ex)
459         {
460             assertEquals(
461                     "Unable to forward to local resource '/io-exception.html': Mock IO Exception",
462                     ex.getMessage());
463             assertSame(t2, ex.getRootCause());
464         }
465
466         verifyControls();
467     }
468
469     public void testForwardExternal() throws Exception JavaDoc
470     {
471         HttpServletRequest JavaDoc request = newRequest();
472         MockControl responsec = newControl(HttpServletResponse JavaDoc.class);
473         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) responsec.getMock();
474
475         response.encodeRedirectURL("http://foo.bar");
476         responsec.setReturnValue("<encoded: http://foo.bar>");
477
478         response.sendRedirect("<encoded: http://foo.bar>");
479
480         replayControls();
481
482         WebRequest wr = new ServletWebRequest(request, response);
483
484         wr.forward("http://foo.bar");
485
486         verifyControls();
487     }
488
489     public void testForwardExternalFailure() throws Exception JavaDoc
490     {
491         HttpServletRequest JavaDoc request = newRequest();
492         MockControl responsec = newControl(HttpServletResponse JavaDoc.class);
493         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) responsec.getMock();
494
495         Throwable JavaDoc t = new IOException JavaDoc("Mock IO Exception");
496
497         response.encodeRedirectURL("/");
498         responsec.setReturnValue("<encoded: http://foo.bar>");
499
500         response.sendRedirect("<encoded: http://foo.bar>");
501         responsec.setThrowable(t);
502
503         replayControls();
504
505         WebRequest wr = new ServletWebRequest(request, response);
506
507         try
508         {
509             wr.forward("/");
510             unreachable();
511         }
512         catch (ApplicationRuntimeException ex)
513         {
514             assertEquals("Unable to redirect to /: Mock IO Exception", ex.getMessage());
515             assertSame(t, ex.getRootCause());
516         }
517
518         verifyControls();
519     }
520     
521     public void testGetActivationPathNoPathInfo()
522     {
523         MockControl control = newControl(HttpServletRequest JavaDoc.class);
524         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
525
526         HttpServletResponse JavaDoc response = newResponse();
527         
528         request.getServletPath();
529         control.setReturnValue("/foo");
530         
531         request.getPathInfo();
532         control.setReturnValue(null);
533         
534         replayControls();
535         
536         WebRequest wr = new ServletWebRequest(request, response);
537         
538         assertEquals("/foo", wr.getActivationPath());
539         
540         verifyControls();
541     }
542     
543     public void testGetActvationPathFull()
544     {
545         MockControl control = newControl(HttpServletRequest JavaDoc.class);
546         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
547
548         HttpServletResponse JavaDoc response = newResponse();
549         
550         request.getServletPath();
551         control.setReturnValue("/foo.direct");
552         
553         request.getPathInfo();
554         control.setReturnValue("/bar/baz");
555         
556         replayControls();
557         
558         WebRequest wr = new ServletWebRequest(request, response);
559         
560         assertEquals("/foo.direct/bar/baz", wr.getActivationPath());
561         
562         verifyControls();
563     }
564 }
Popular Tags