2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

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  
30  import junit.framework.TestCase;
31  
32  import org.apache.cactus.JspTestCase;
33  import org.apache.cactus.ServletURL;
34  import org.apache.cactus.server.AbstractPageContextWrapper;
35  
36  /**
37   * Call the test method on the server side after assigning the JSP implicit
38   * objects using reflection.
39   *
40   * @version $Id: JspTestCaller.java 238991 2004-05-22 11:34:50Z vmassol $
41   */
42  public class JspTestCaller extends ServletTestCaller
43  {
44      /**
45       * @param theObjects the implicit objects coming from the redirector
46       */
47      public JspTestCaller(JspImplicitObjects theObjects)
48      {
49          super(theObjects);
50      }
51  
52      /**
53       * {@inheritDoc}
54       * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
55       */
56      protected void setTestCaseFields(TestCase theTestInstance)
57          throws Exception
58      {
59          if (!(theTestInstance instanceof JspTestCase))
60          {
61              return; 
62          }
63  
64          JspTestCase jspInstance = (JspTestCase) theTestInstance;
65          JspImplicitObjects jspImplicitObjects = 
66              (JspImplicitObjects) this.webImplicitObjects;
67  
68          // Sets the Servlet-related implicit objects
69          // -----------------------------------------
70          super.setTestCaseFields(jspInstance);
71  
72          // Set the page context field of the test case class
73          // -------------------------------------------------
74          // Extract from the HTTP request the URL to simulate (if any)
75          HttpServletRequest request = jspImplicitObjects.getHttpServletRequest();
76  
77          ServletURL url = ServletURL.loadFromRequest(request);
78  
79          Field pageContextField = jspInstance.getClass().getField("pageContext");
80  
81          pageContextField.set(jspInstance, 
82              AbstractPageContextWrapper.newInstance(
83                  jspImplicitObjects.getPageContext(), url));
84  
85          // Set the JSP writer field of the test case class
86          // -----------------------------------------------
87          Field outField = jspInstance.getClass().getField("out");
88  
89          outField.set(jspInstance, jspImplicitObjects.getJspWriter());
90      }
91  
92      /**
93       * {@inheritDoc}
94       * @see AbstractWebTestCaller#getResponseWriter()
95       */
96      protected Writer getResponseWriter() throws IOException
97      {
98          JspImplicitObjects jspImplicitObjects = 
99              (JspImplicitObjects) this.webImplicitObjects;
100 
101         return jspImplicitObjects.getJspWriter();
102     }
103 }