01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2003 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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;
23: import java.io.Writer;
24:
25: import java.lang.reflect.Field;
26:
27: import javax.servlet.http.HttpServletRequest;
28:
29: import junit.framework.TestCase;
30:
31: import org.apache.cactus.JspTestCase;
32: import org.apache.cactus.ServletURL;
33: import org.apache.cactus.server.PageContextWrapper;
34:
35: /**
36: * Call the test method on the server side after assigning the JSP implicit
37: * objects using reflection.
38: *
39: * @version $Id: JspTestCaller.java 238991 2004-05-22 11:34:50Z vmassol $
40: */
41: public class JspTestCaller extends ServletTestCaller {
42: /**
43: * @param theObjects the implicit objects coming from the redirector
44: */
45: public JspTestCaller(JspImplicitObjects theObjects) {
46: super (theObjects);
47: }
48:
49: /**
50: * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
51: */
52: protected void setTestCaseFields(TestCase theTestInstance)
53: throws Exception {
54: if (!(theTestInstance instanceof JspTestCase)) {
55: return;
56: }
57:
58: JspTestCase jspInstance = (JspTestCase) theTestInstance;
59: JspImplicitObjects jspImplicitObjects = (JspImplicitObjects) this .webImplicitObjects;
60:
61: // Sets the Servlet-related implicit objects
62: // -----------------------------------------
63: super .setTestCaseFields(jspInstance);
64:
65: // Set the page context field of the test case class
66: // -------------------------------------------------
67: // Extract from the HTTP request the URL to simulate (if any)
68: HttpServletRequest request = jspImplicitObjects
69: .getHttpServletRequest();
70:
71: ServletURL url = ServletURL.loadFromRequest(request);
72:
73: Field pageContextField = jspInstance.getClass().getField(
74: "pageContext");
75:
76: pageContextField.set(jspInstance, new PageContextWrapper(
77: jspImplicitObjects.getPageContext(), url));
78:
79: // Set the JSP writer field of the test case class
80: // -----------------------------------------------
81: Field outField = jspInstance.getClass().getField("out");
82:
83: outField.set(jspInstance, jspImplicitObjects.getJspWriter());
84: }
85:
86: /**
87: * @see AbstractWebTestCaller#getResponseWriter()
88: */
89: protected Writer getResponseWriter() throws IOException {
90: JspImplicitObjects jspImplicitObjects = (JspImplicitObjects) this.webImplicitObjects;
91:
92: return jspImplicitObjects.getJspWriter();
93: }
94: }
|