KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import junit.framework.TestCase;
30
31 import org.apache.cactus.FilterTestCase;
32 import org.apache.cactus.ServletURL;
33 import org.apache.cactus.server.FilterConfigWrapper;
34 import org.apache.cactus.server.HttpServletRequestWrapper;
35
36 /**
37  * Responsible for instanciating the <code>TestCase</code> class on the server
38  * side, set up the implicit objects and call the test method.
39  *
40  * @version $Id: FilterTestCaller.java,v 1.1 2004/05/22 11:34:49 vmassol Exp $
41  */

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

47     public FilterTestCaller(FilterImplicitObjects theObjects)
48     {
49         super(theObjects);
50     }
51
52     /**
53      * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
54      */

55     protected void setTestCaseFields(TestCase theTestInstance)
56         throws Exception JavaDoc
57     {
58         if (!(theTestInstance instanceof FilterTestCase))
59         {
60             return;
61         }
62
63         FilterTestCase filterInstance = (FilterTestCase) theTestInstance;
64         FilterImplicitObjects filterImplicitObjects =
65             (FilterImplicitObjects) this.webImplicitObjects;
66
67         // Sets the request field of the test case class
68
// ---------------------------------------------
69
// Extract from the HTTP request the URL to simulate (if any)
70
HttpServletRequest JavaDoc request =
71             filterImplicitObjects.getHttpServletRequest();
72
73         ServletURL url = ServletURL.loadFromRequest(request);
74
75         Field JavaDoc requestField = filterInstance.getClass().getField("request");
76
77         requestField.set(filterInstance,
78             new HttpServletRequestWrapper(request, url));
79
80         // Set the response field of the test case class
81
// ---------------------------------------------
82
Field JavaDoc responseField = filterInstance.getClass().getField("response");
83
84         responseField.set(filterInstance,
85             filterImplicitObjects.getHttpServletResponse());
86
87         // Set the config field of the test case class
88
// -------------------------------------------
89
Field JavaDoc configField = filterInstance.getClass().getField("config");
90
91         configField.set(filterInstance,
92             new FilterConfigWrapper(filterImplicitObjects.getFilterConfig()));
93
94         // Set the filter chain of the test case class
95
// -------------------------------------------
96
Field JavaDoc chainField = filterInstance.getClass().getField("filterChain");
97
98         chainField.set(filterInstance, filterImplicitObjects.getFilterChain());
99     }
100
101     /**
102      * @see AbstractWebTestCaller#getResponseWriter()
103      */

104     protected Writer JavaDoc getResponseWriter() throws IOException JavaDoc
105     {
106         return this.webImplicitObjects.getHttpServletResponse().getWriter();
107     }
108 }
109
Popular Tags