001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 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.internal.server;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024:
025: import java.lang.reflect.Field;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029:
030: import junit.framework.TestCase;
031:
032: import org.apache.cactus.ServletTestCase;
033: import org.apache.cactus.ServletURL;
034: import org.apache.cactus.server.HttpServletRequestWrapper;
035: import org.apache.cactus.server.ServletConfigWrapper;
036:
037: /**
038: * Responsible for instanciating the <code>TestCase</code> class on the server
039: * side, set up the implicit objects and call the test method.
040: *
041: * @version $Id: ServletTestCaller.java 238991 2004-05-22 11:34:50Z vmassol $
042: */
043: public class ServletTestCaller extends AbstractWebTestCaller {
044: /**
045: * @param theObjects the implicit objects coming from the redirector
046: */
047: public ServletTestCaller(ServletImplicitObjects theObjects) {
048: super (theObjects);
049: }
050:
051: /**
052: * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
053: */
054: protected void setTestCaseFields(TestCase theTestInstance)
055: throws Exception {
056: if (!(theTestInstance instanceof ServletTestCase)) {
057: return;
058: }
059:
060: ServletTestCase servletInstance = (ServletTestCase) theTestInstance;
061: ServletImplicitObjects servletImplicitObjects = (ServletImplicitObjects) this .webImplicitObjects;
062:
063: // Sets the request field of the test case class
064: // ---------------------------------------------
065: // Extract from the HTTP request the URL to simulate (if any)
066: HttpServletRequest request = servletImplicitObjects
067: .getHttpServletRequest();
068:
069: ServletURL url = ServletURL.loadFromRequest(request);
070:
071: Field requestField = servletInstance.getClass().getField(
072: "request");
073:
074: requestField.set(servletInstance,
075: new HttpServletRequestWrapper(request, url));
076:
077: // Set the response field of the test case class
078: // ---------------------------------------------
079: Field responseField = servletInstance.getClass().getField(
080: "response");
081:
082: responseField.set(servletInstance, servletImplicitObjects
083: .getHttpServletResponse());
084:
085: // Set the config field of the test case class
086: // -------------------------------------------
087: Field configField = servletInstance.getClass().getField(
088: "config");
089:
090: configField.set(servletInstance, new ServletConfigWrapper(
091: servletImplicitObjects.getServletConfig()));
092:
093: // Set the session field of the test case class
094: // --------------------------------------------
095: // Create a Session object if the auto session flag is on
096: if (isAutoSession()) {
097: HttpSession session = servletImplicitObjects
098: .getHttpServletRequest().getSession(true);
099:
100: Field sessionField = servletInstance.getClass().getField(
101: "session");
102:
103: sessionField.set(servletInstance, session);
104: }
105: }
106:
107: /**
108: * @see AbstractWebTestCaller#getResponseWriter()
109: */
110: protected Writer getResponseWriter() throws IOException {
111: return this.webImplicitObjects.getHttpServletResponse()
112: .getWriter();
113: }
114: }
|