KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > server > ServletTestCaller


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 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.internal.server;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24
25 import java.lang.reflect.Field JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.apache.cactus.ServletTestCase;
33 import org.apache.cactus.ServletURL;
34 import org.apache.cactus.server.HttpServletRequestWrapper;
35 import org.apache.cactus.server.ServletConfigWrapper;
36
37 /**
38  * Responsible for instanciating the <code>TestCase</code> class on the server
39  * side, set up the implicit objects and call the test method.
40  *
41  * @version $Id: ServletTestCaller.java,v 1.1 2004/05/22 11:34:45 vmassol Exp $
42  */

43 public class ServletTestCaller extends AbstractWebTestCaller
44 {
45     /**
46      * @param theObjects the implicit objects coming from the redirector
47      */

48     public ServletTestCaller(ServletImplicitObjects theObjects)
49     {
50         super(theObjects);
51     }
52
53     /**
54      * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
55      */

56     protected void setTestCaseFields(TestCase theTestInstance)
57         throws Exception JavaDoc
58     {
59         if (!(theTestInstance instanceof ServletTestCase))
60         {
61             return;
62         }
63         
64         ServletTestCase servletInstance = (ServletTestCase) theTestInstance;
65         ServletImplicitObjects servletImplicitObjects =
66             (ServletImplicitObjects) this.webImplicitObjects;
67
68         // Sets the request field of the test case class
69
// ---------------------------------------------
70
// Extract from the HTTP request the URL to simulate (if any)
71
HttpServletRequest JavaDoc request =
72             servletImplicitObjects.getHttpServletRequest();
73
74         ServletURL url = ServletURL.loadFromRequest(request);
75
76         Field JavaDoc requestField = servletInstance.getClass().getField("request");
77
78         requestField.set(servletInstance,
79             new HttpServletRequestWrapper(request, url));
80
81         // Set the response field of the test case class
82
// ---------------------------------------------
83
Field JavaDoc responseField = servletInstance.getClass().getField("response");
84
85         responseField.set(servletInstance,
86             servletImplicitObjects.getHttpServletResponse());
87
88         // Set the config field of the test case class
89
// -------------------------------------------
90
Field JavaDoc configField = servletInstance.getClass().getField("config");
91
92         configField.set(servletInstance, new ServletConfigWrapper(
93             servletImplicitObjects.getServletConfig()));
94
95         // Set the session field of the test case class
96
// --------------------------------------------
97
// Create a Session object if the auto session flag is on
98
if (isAutoSession())
99         {
100             HttpSession JavaDoc session = servletImplicitObjects.getHttpServletRequest()
101                 .getSession(true);
102
103             Field JavaDoc sessionField = servletInstance.getClass().getField("session");
104
105             sessionField.set(servletInstance, session);
106         }
107     }
108
109     /**
110      * @see AbstractWebTestCaller#getResponseWriter()
111      */

112     protected Writer JavaDoc getResponseWriter() throws IOException JavaDoc
113     {
114         return this.webImplicitObjects.getHttpServletResponse().getWriter();
115     }
116 }
117
Popular Tags