1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.server;
22
23 import java.io.IOException;
24 import java.io.Writer;
25
26 import java.lang.reflect.Field;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30
31 import junit.framework.TestCase;
32
33 import org.apache.cactus.ServletTestCase;
34 import org.apache.cactus.ServletURL;
35 import org.apache.cactus.server.AbstractHttpServletRequestWrapper;
36 import org.apache.cactus.server.AbstractServletConfigWrapper;
37
38 /**
39 * Responsible for instanciating the <code>TestCase</code> class on the server
40 * side, set up the implicit objects and call the test method.
41 *
42 * @version $Id: ServletTestCaller.java 292559 2005-09-29 21:36:43Z kenney $
43 */
44 public class ServletTestCaller extends AbstractWebTestCaller
45 {
46 /**
47 * @param theObjects the implicit objects coming from the redirector
48 */
49 public ServletTestCaller(ServletImplicitObjects theObjects)
50 {
51 super(theObjects);
52 }
53
54 /**
55 * {@inheritDoc}
56 * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
57 */
58 protected void setTestCaseFields(TestCase theTestInstance)
59 throws Exception
60 {
61 if (!(theTestInstance instanceof ServletTestCase))
62 {
63 return;
64 }
65
66 ServletTestCase servletInstance = (ServletTestCase) theTestInstance;
67 ServletImplicitObjects servletImplicitObjects =
68 (ServletImplicitObjects) this.webImplicitObjects;
69
70 // Sets the request field of the test case class
71 // ---------------------------------------------
72 // Extract from the HTTP request the URL to simulate (if any)
73 HttpServletRequest request =
74 servletImplicitObjects.getHttpServletRequest();
75
76 ServletURL url = ServletURL.loadFromRequest(request);
77
78 Field requestField = servletInstance.getClass().getField("request");
79
80 requestField.set(servletInstance,
81 AbstractHttpServletRequestWrapper.newInstance(request, url));
82
83 // Set the response field of the test case class
84 // ---------------------------------------------
85 Field responseField = servletInstance.getClass().getField("response");
86
87 responseField.set(servletInstance,
88 servletImplicitObjects.getHttpServletResponse());
89
90 // Set the config field of the test case class
91 // -------------------------------------------
92 Field configField = servletInstance.getClass().getField("config");
93
94 configField.set(servletInstance, AbstractServletConfigWrapper.
95 newInstance(servletImplicitObjects.getServletConfig()));
96
97 // Set the session field of the test case class
98 // --------------------------------------------
99 // Create a Session object if the auto session flag is on
100 if (isAutoSession())
101 {
102 HttpSession session = servletImplicitObjects.getHttpServletRequest()
103 .getSession(true);
104
105 Field sessionField = servletInstance.getClass().getField("session");
106
107 sessionField.set(servletInstance, session);
108 }
109 }
110
111 /**
112 * {@inheritDoc}
113 * @see AbstractWebTestCaller#getResponseWriter()
114 */
115 protected Writer getResponseWriter() throws IOException
116 {
117 return this.webImplicitObjects.getHttpServletResponse().getWriter();
118 }
119 }