001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet.unit;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.RequestDispatcher;
025: import javax.servlet.ServletException;
026:
027: import org.apache.cactus.ServletTestCase;
028: import org.apache.cactus.WebRequest;
029: import org.apache.cactus.WebResponse;
030:
031: /**
032: * Tests manipulating The Request Dispatcher.
033: *
034: * @version $Id: TestRequestDispatcher.java 238816 2004-02-29 16:36:46Z vmassol $
035: */
036: public class TestRequestDispatcher extends ServletTestCase {
037: /**
038: * Verify that getNamedDispatcher() can be used to get a dispatcher.
039: *
040: * @exception IOException on test failure
041: * @exception ServletException on test failure
042: */
043: public void testGetRequestDispatcherFromNamedDispatcherOK()
044: throws ServletException, IOException {
045: RequestDispatcher rd = config.getServletContext()
046: .getNamedDispatcher("TestJsp");
047:
048: assertNotNull(
049: "Missing configuration for \"TestJsp\" in web.xml", rd);
050: rd.forward(request, response);
051: }
052:
053: /**
054: * Verify that getNamedDispatcher() can be used to get a dispatcher.
055: *
056: * @param theResponse the response from the server side.
057: *
058: * @exception IOException on test failure
059: */
060: public void endGetRequestDispatcherFromNamedDispatcherOK(
061: WebResponse theResponse) throws IOException {
062: String result = theResponse.getText();
063:
064: assertTrue("Page not found, got [" + result + "]", result
065: .indexOf("Hello !") > 0);
066: }
067:
068: //-------------------------------------------------------------------------
069:
070: /**
071: * Verify that getNamedDispatcher() returns null when passed an invalid
072: * name.
073: *
074: * @exception IOException on test failure
075: * @exception ServletException on test failure
076: */
077: public void testGetRequestDispatcherFromNamedDispatcherInvalid()
078: throws ServletException, IOException {
079: RequestDispatcher rd = config.getServletContext()
080: .getNamedDispatcher("invalid name");
081:
082: assertNull(rd);
083: }
084:
085: //-------------------------------------------------------------------------
086:
087: /**
088: * Verify that request.getRequestDispatcher() works properly with an
089: * absolute path
090: *
091: * @exception IOException on test failure
092: * @exception ServletException on test failure
093: */
094: public void testGetRequestDispatcherFromRequest1()
095: throws ServletException, IOException {
096: RequestDispatcher rd = request
097: .getRequestDispatcher("/test/test.jsp");
098:
099: rd.include(request, response);
100: }
101:
102: /**
103: * Verify that request.getRequestDispatcher() works properly with an
104: * absolute path
105: *
106: * @param theResponse the response from the server side.
107: *
108: * @exception IOException on test failure
109: */
110: public void endGetRequestDispatcherFromRequest1(
111: WebResponse theResponse) throws IOException {
112: String result = theResponse.getText();
113:
114: assertTrue("Page not found, got [" + result + "]", result
115: .indexOf("Hello !") > 0);
116: }
117:
118: //-------------------------------------------------------------------------
119:
120: /**
121: * Verify that request.getRequestDispatcher() works properly with a
122: * relative path.
123: *
124: * @param theRequest the request object that serves to initialize the
125: * HTTP connection to the server redirector.
126: */
127: public void beginGetRequestDispatcherFromRequest2(
128: WebRequest theRequest) {
129: theRequest.setURL(null, "/test", "/anything.jsp", null, null);
130: }
131:
132: /**
133: * Verify that request.getRequestDispatcher() works properly with a
134: * relative path.
135: *
136: * @exception IOException on test failure
137: * @exception ServletException on test failure
138: */
139: public void testGetRequestDispatcherFromRequest2()
140: throws ServletException, IOException {
141: RequestDispatcher rd = request
142: .getRequestDispatcher("test/test.jsp");
143:
144: rd.include(request, response);
145: }
146:
147: /**
148: * Verify that request.getRequestDispatcher() works properly with a
149: * relative path.
150: *
151: * @param theResponse the response from the server side.
152: *
153: * @exception IOException on test failure
154: */
155: public void endGetRequestDispatcherFromRequest2(
156: WebResponse theResponse) throws IOException {
157: String result = theResponse.getText();
158:
159: assertTrue("Page not found, got [" + result + "]", result
160: .indexOf("Hello !") > 0);
161: }
162:
163: }
|