KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestRequestDispatcher


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.unit;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.RequestDispatcher JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26
27 import org.apache.cactus.ServletTestCase;
28 import org.apache.cactus.WebRequest;
29 import org.apache.cactus.WebResponse;
30
31 /**
32  * Tests manipulating The Request Dispatcher.
33  *
34  * @version $Id: TestRequestDispatcher.java,v 1.3 2004/02/29 16:36:44 vmassol Exp $
35  */

36 public class TestRequestDispatcher extends ServletTestCase
37 {
38     /**
39      * Verify that getNamedDispatcher() can be used to get a dispatcher.
40      *
41      * @exception IOException on test failure
42      * @exception ServletException on test failure
43      */

44     public void testGetRequestDispatcherFromNamedDispatcherOK()
45         throws ServletException JavaDoc, IOException JavaDoc
46     {
47         RequestDispatcher JavaDoc rd = config.getServletContext().getNamedDispatcher(
48             "TestJsp");
49
50         assertNotNull("Missing configuration for \"TestJsp\" in web.xml", rd);
51         rd.forward(request, response);
52     }
53
54     /**
55      * Verify that getNamedDispatcher() can be used to get a dispatcher.
56      *
57      * @param theResponse the response from the server side.
58      *
59      * @exception IOException on test failure
60      */

61     public void endGetRequestDispatcherFromNamedDispatcherOK(
62         WebResponse theResponse) throws IOException JavaDoc
63     {
64         String JavaDoc result = theResponse.getText();
65
66         assertTrue("Page not found, got [" + result + "]",
67             result.indexOf("Hello !") > 0);
68     }
69
70     //-------------------------------------------------------------------------
71

72     /**
73      * Verify that getNamedDispatcher() returns null when passed an invalid
74      * name.
75      *
76      * @exception IOException on test failure
77      * @exception ServletException on test failure
78      */

79     public void testGetRequestDispatcherFromNamedDispatcherInvalid()
80         throws ServletException JavaDoc, IOException JavaDoc
81     {
82         RequestDispatcher JavaDoc rd = config.getServletContext().getNamedDispatcher(
83             "invalid name");
84
85         assertNull(rd);
86     }
87
88     //-------------------------------------------------------------------------
89

90     /**
91      * Verify that request.getRequestDispatcher() works properly with an
92      * absolute path
93      *
94      * @exception IOException on test failure
95      * @exception ServletException on test failure
96      */

97     public void testGetRequestDispatcherFromRequest1()
98         throws ServletException JavaDoc, IOException JavaDoc
99     {
100         RequestDispatcher JavaDoc rd = request.getRequestDispatcher("/test/test.jsp");
101
102         rd.include(request, response);
103     }
104
105     /**
106      * Verify that request.getRequestDispatcher() works properly with an
107      * absolute path
108      *
109      * @param theResponse the response from the server side.
110      *
111      * @exception IOException on test failure
112      */

113     public void endGetRequestDispatcherFromRequest1(WebResponse theResponse)
114         throws IOException JavaDoc
115     {
116         String JavaDoc result = theResponse.getText();
117
118         assertTrue("Page not found, got [" + result + "]",
119             result.indexOf("Hello !") > 0);
120     }
121
122     //-------------------------------------------------------------------------
123

124     /**
125      * Verify that request.getRequestDispatcher() works properly with a
126      * relative path.
127      *
128      * @param theRequest the request object that serves to initialize the
129      * HTTP connection to the server redirector.
130      */

131     public void beginGetRequestDispatcherFromRequest2(WebRequest theRequest)
132     {
133         theRequest.setURL(null, "/test", "/anything.jsp", null, null);
134     }
135
136     /**
137      * Verify that request.getRequestDispatcher() works properly with a
138      * relative path.
139      *
140      * @exception IOException on test failure
141      * @exception ServletException on test failure
142      */

143     public void testGetRequestDispatcherFromRequest2()
144         throws ServletException JavaDoc, IOException JavaDoc
145     {
146         RequestDispatcher JavaDoc rd = request.getRequestDispatcher("test/test.jsp");
147
148         rd.include(request, response);
149     }
150
151     /**
152      * Verify that request.getRequestDispatcher() works properly with a
153      * relative path.
154      *
155      * @param theResponse the response from the server side.
156      *
157      * @exception IOException on test failure
158      */

159     public void endGetRequestDispatcherFromRequest2(WebResponse theResponse)
160         throws IOException JavaDoc
161     {
162         String JavaDoc result = theResponse.getText();
163
164         assertTrue("Page not found, got [" + result + "]",
165             result.indexOf("Hello !") > 0);
166     }
167
168 }
169
Popular Tags