KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > TestSampleServlet


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

20 package org.apache.cactus.sample.servlet;
21
22 import java.io.IOException JavaDoc;
23
24 import java.net.URLDecoder JavaDoc;
25
26 import java.util.Hashtable JavaDoc;
27
28 import org.apache.cactus.Cookie;
29 import org.apache.cactus.ServletTestCase;
30 import org.apache.cactus.WebRequest;
31 import org.apache.cactus.WebResponse;
32
33 /**
34  * Tests of the <code>SampleServlet</code> servlet class.
35  *
36  * @version $Id: TestSampleServlet.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
37  */

38 public class TestSampleServlet extends ServletTestCase
39 {
40     /**
41      * Verify that we can assert the servlet output stream.
42      *
43      * @exception IOException on test failure
44      */

45     public void testReadServletOutputStream() throws IOException JavaDoc
46     {
47         SampleServlet servlet = new SampleServlet();
48
49         servlet.doGet(request, response);
50     }
51
52     /**
53      * Verify that we can assert the servlet output stream.
54      *
55      * @param theResponse the response from the server side.
56      *
57      * @exception IOException on test failure
58      */

59     public void endReadServletOutputStream(WebResponse theResponse)
60         throws IOException JavaDoc
61     {
62         String JavaDoc expected = "<html><head/><body>A GET request</body></html>";
63         String JavaDoc result = theResponse.getText();
64
65         assertEquals(expected, result);
66     }
67
68     //-------------------------------------------------------------------------
69

70     /**
71      * Verify that we can simulate a POST request to a servlet. Note that
72      * we send a parameter to force a POST.
73      *
74      * @param theRequest the request object that serves to initialize the
75      * HTTP connection to the server redirector.
76      */

77     public void beginPostMethod(WebRequest theRequest)
78     {
79         theRequest.addParameter("param", "value", WebRequest.POST_METHOD);
80     }
81
82     /**
83      * Verify that we can simulate a POST request to a servlet. Note that
84      * we send a parameter to force a POST. Otherwise Cactus will do a GET
85      * by default.
86      */

87     public void testPostMethod()
88     {
89         SampleServlet servlet = new SampleServlet();
90
91         assertEquals("POST", servlet.checkMethod(request));
92         assertEquals("value", request.getParameter("param"));
93     }
94
95     //-------------------------------------------------------------------------
96

97     /**
98      * Verify that we can simulate a GET request to a servlet. Note: Cactus
99      * does a GET by default.
100      */

101     public void testGetMethod()
102     {
103         SampleServlet servlet = new SampleServlet();
104
105         assertEquals("GET", servlet.checkMethod(request));
106     }
107
108     //-------------------------------------------------------------------------
109

110     /**
111      * Verify that by default the session implicit object is available and can
112      * be used.
113      */

114     public void testSetAttribute()
115     {
116         SampleServlet servlet = new SampleServlet();
117
118         servlet.setSessionVariable(request);
119
120         assertNotNull(session);
121         assertEquals("value_setSessionVariable",
122             session.getAttribute("name_setSessionVariable"));
123     }
124
125     /**
126      * Verify that we can set an attribute in the request.
127      */

128     public void testSetRequestAttribute()
129     {
130         SampleServlet servlet = new SampleServlet();
131
132         servlet.setRequestAttribute(request);
133
134         assertEquals("value_setRequestAttribute",
135             request.getAttribute("name_setRequestAttribute"));
136     }
137
138     //-------------------------------------------------------------------------
139

140     /**
141      * Verify that we can simulate HTTP parameters in the HTTP request.
142      *
143      * @param theRequest the request object that serves to initialize the
144      * HTTP connection to the server redirector.
145      */

146     public void beginSendParams(WebRequest theRequest)
147     {
148         theRequest.addParameter("param1", "value1");
149         theRequest.addParameter("param2", "value2");
150     }
151
152     /**
153      * Verify that we can send several parameters in the HTTP request.
154      */

155     public void testSendParams()
156     {
157         SampleServlet servlet = new SampleServlet();
158         Hashtable JavaDoc params = servlet.getRequestParameters(request);
159
160         assertNotNull(params.get("param1"));
161         assertNotNull(params.get("param2"));
162         assertEquals("value1", params.get("param1"));
163         assertEquals("value2", params.get("param2"));
164     }
165
166     //-------------------------------------------------------------------------
167

168     /**
169      * Verify that we can simulate HTTP headers in the HTTP request.
170      *
171      * @param theRequest the request object that serves to initialize the
172      * HTTP connection to the server redirector.
173      */

174     public void beginSendHeader(WebRequest theRequest)
175     {
176         theRequest.addHeader("testheader", "this is a header test");
177     }
178
179     /**
180      * Verify that we can simulate HTTP headers in the HTTP request.
181      */

182     public void testSendHeader()
183     {
184         SampleServlet servlet = new SampleServlet();
185         String JavaDoc headerValue = servlet.getRequestHeader(request);
186
187         assertEquals("this is a header test", headerValue);
188     }
189
190     //-------------------------------------------------------------------------
191

192     /**
193      * Verify that we can simulate a single cookie sent in the HTTP request.
194      *
195      * @param theRequest the request object that serves to initialize the
196      * HTTP connection to the server redirector.
197      */

198     public void beginSendCookie(WebRequest theRequest)
199     {
200         // Note: The cookie value that was chosen is a string without spaces
201
// because there is a problem with Resin 1.2.1 which does not support
202
// quoted cookies. It has been fixed since the 15/12/2000 release of
203
// Resin.
204
theRequest.addCookie("testcookie", "thisisacookie");
205     }
206
207     /**
208      * Verify that we can simulate a single cookie sent in the HTTP request.
209      */

210     public void testSendCookie()
211     {
212         SampleServlet servlet = new SampleServlet();
213         Hashtable JavaDoc cookies = servlet.getRequestCookies(request);
214
215         assertNotNull("Cannot find [testcookie] cookie in request",
216                       cookies.get("testcookie"));
217         assertEquals("thisisacookie", cookies.get("testcookie"));
218     }
219
220     /**
221      * Verify that we can simulate multiple cookies sent in the HTTP request.
222      *
223      * @param theRequest the request object that serves to initialize the
224      * HTTP connection to the server redirector.
225      */

226     public void beginSendMultipleCookies(WebRequest theRequest)
227     {
228         theRequest.addCookie("testcookie1", "cookie1");
229         theRequest.addCookie("testcookie2", "cookie2");
230     }
231
232     /**
233      * Verify that we can simulate multiple cookies sent in the HTTP request.
234      */

235     public void testSendMultipleCookies()
236     {
237         SampleServlet servlet = new SampleServlet();
238         Hashtable JavaDoc cookies = servlet.getRequestCookies(request);
239
240         assertNotNull(cookies.get("testcookie1"));
241         assertEquals("cookie1", cookies.get("testcookie1"));
242
243         assertNotNull(cookies.get("testcookie2"));
244         assertEquals("cookie2", cookies.get("testcookie2"));
245     }
246
247     //-------------------------------------------------------------------------
248

249     /**
250      * Verify that it is possible to send back a header and verify it on the
251      * client side.
252      */

253     public void testReceiveHeader()
254     {
255         SampleServlet servlet = new SampleServlet();
256
257         servlet.setResponseHeader(response);
258     }
259
260     /**
261      * Verify that it is possible to send back a header and verify it on the
262      * client side.
263      *
264      * @param theResponse the response from the server side.
265      */

266     public void endReceiveHeader(WebResponse theResponse)
267     {
268         assertEquals("this is a response header",
269             theResponse.getConnection().getHeaderField("responseheader"));
270     }
271
272     //-------------------------------------------------------------------------
273

274     /**
275      * Test that it is possible to send back a Cookie and verify it on the
276      * client side.
277      *
278      * @param theRequest the request object that serves to initialize the
279      * HTTP connection to the server redirector.
280      */

281     public void beginReceiveCookie(WebRequest theRequest)
282     {
283         // Why do we need to have a begin method here ? Good question !
284
// The answer is that in this test, the SampleServlet's
285
// setResponseCookie() method sets the domain name of the cookie
286
// to return to jakarta.apache.org. It means that for this cookie
287
// to be valid the domain of the request (i.e. the host) must be
288
// jakarta.apache.org (this is according to the cookies RFCs). Thus
289
// we need to simulate the URL and act as if we had sent a request
290
// to jakarta.apache.org ! Logical, no ?
291
theRequest.setURL("jakarta.apache.org", null, null, null, null);
292     }
293
294     /**
295      * Test that it is possible to send back a Cookie and verify it on the
296      * client side.
297      */

298     public void testReceiveCookie()
299     {
300         SampleServlet servlet = new SampleServlet();
301
302         servlet.setResponseCookie(response);
303     }
304
305     /**
306      * Test that it is possible to send back a Cookie and verify it on the
307      * client side.
308      *
309      * @param theResponse the response from the server side.
310      *
311      * @exception Exception for backward compatibility with JDK 1.2.2 (not
312      * needed for JDK 1.3+, but needed for URLDecoder.decode())
313      */

314     public void endReceiveCookie(WebResponse theResponse) throws Exception JavaDoc
315     {
316         Cookie cookie = theResponse.getCookie("responsecookie");
317
318         assertNotNull("Cannot find [responsecookie]", cookie);
319         assertEquals("responsecookie", cookie.getName());
320
321
322         // Some servers may encode the cookie value (ex: the latest
323
// version of Tomcat 4.0). In order for this test to succeed on
324
// all servlet engine, we URL decode the cookie value before
325
// comparing it.
326
assertEquals("this is a response cookie",
327             URLDecoder.decode(cookie.getValue()));
328
329         assertEquals("jakarta.apache.org", cookie.getDomain());
330     }
331
332     //-------------------------------------------------------------------------
333

334     /**
335      * Verify that we can use a <code>RequestDispatcher</code> in the class to
336      * test to forward to another page and compare the result sent to the
337      * output stream on the client side.
338      *
339      * @exception Exception on test failure
340      */

341     public void testRequestDispatcherForward() throws Exception JavaDoc
342     {
343         SampleServlet servlet = new SampleServlet();
344
345         servlet.doForward(request, response, config);
346     }
347
348     /**
349      * Verify that we can use a <code>RequestDispatcher</code> in the class to
350      * test to forward to another page and compare the result sent to the
351      * output stream on the client side.
352      *
353      * @param theResponse the response from the server side.
354      *
355      * @exception IOException on test failure
356      */

357     public void endRequestDispatcherForward(WebResponse theResponse)
358         throws IOException JavaDoc
359     {
360         // We cannot test what is exactly returned by the called JSP between
361
// different Servlet engine return different text ! For example some
362
// return the JSP comment, other do not, ...
363
// Thus, we only test for a match of "Hello !"
364
assertTrue("Text missing 'Hello !' : [" + theResponse.getText() + "]",
365             theResponse.getText().indexOf("Hello !") > 0);
366     }
367
368     //-------------------------------------------------------------------------
369

370     /**
371      * Verify that we can use a <code>RequestDispatcher</code> in the class to
372      * test to include another page and compare the result sent to the
373      * output stream on the client side.
374      *
375      * @exception Exception on test failure
376      */

377     public void testRequestDispatcherInclude() throws Exception JavaDoc
378     {
379         SampleServlet servlet = new SampleServlet();
380
381         servlet.doInclude(request, response, config);
382     }
383
384     /**
385      * Verify that we can use a <code>RequestDispatcher</code> in the class to
386      * test to include another page and compare the result sent to the
387      * output stream on the client side.
388      *
389      * @param theResponse the response from the server side.
390      *
391      * @exception IOException on test failure
392      */

393     public void endRequestDispatcherInclude(WebResponse theResponse)
394         throws IOException JavaDoc
395     {
396         // We cannot test what is exactly returned by the included JSP between
397
// different Servlet engine return different text ! For example some
398
// return the JSP comment, other do not, ...
399
// Thus, we only test for a match of "Hello !"
400
assertTrue("Text missing 'Hello !' : [" + theResponse.getText() + "]",
401             theResponse.getText().indexOf("Hello !") > 0);
402     }
403 }
404
Popular Tags