KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 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
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,v 1.1 2004/05/22 11:34:45 vmassol Exp $
40  */

41 public class JspTestCaller extends ServletTestCaller
42 {
43     /**
44      * @param theObjects the implicit objects coming from the redirector
45      */

46     public JspTestCaller(JspImplicitObjects theObjects)
47     {
48         super(theObjects);
49     }
50
51     /**
52      * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
53      */

54     protected void setTestCaseFields(TestCase theTestInstance)
55         throws Exception JavaDoc
56     {
57         if (!(theTestInstance instanceof JspTestCase))
58         {
59             return;
60         }
61
62         JspTestCase jspInstance = (JspTestCase) theTestInstance;
63         JspImplicitObjects jspImplicitObjects =
64             (JspImplicitObjects) this.webImplicitObjects;
65
66         // Sets the Servlet-related implicit objects
67
// -----------------------------------------
68
super.setTestCaseFields(jspInstance);
69
70         // Set the page context field of the test case class
71
// -------------------------------------------------
72
// Extract from the HTTP request the URL to simulate (if any)
73
HttpServletRequest JavaDoc request = jspImplicitObjects.getHttpServletRequest();
74
75         ServletURL url = ServletURL.loadFromRequest(request);
76
77         Field JavaDoc pageContextField = jspInstance.getClass().getField("pageContext");
78
79         pageContextField.set(jspInstance,
80             new PageContextWrapper(jspImplicitObjects.getPageContext(), url));
81
82         // Set the JSP writer field of the test case class
83
// -----------------------------------------------
84
Field JavaDoc outField = jspInstance.getClass().getField("out");
85
86         outField.set(jspInstance, jspImplicitObjects.getJspWriter());
87     }
88
89     /**
90      * @see AbstractWebTestCaller#getResponseWriter()
91      */

92     protected Writer JavaDoc getResponseWriter() throws IOException JavaDoc
93     {
94         JspImplicitObjects jspImplicitObjects =
95             (JspImplicitObjects) this.webImplicitObjects;
96
97         return jspImplicitObjects.getJspWriter();
98     }
99 }
100
Popular Tags