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.FilterTestCase;
33 import org.apache.cactus.ServletURL;
34 import org.apache.cactus.server.FilterConfigWrapper;
35 import org.apache.cactus.server.AbstractHttpServletRequestWrapper;
36
37 /**
38 * Responsible for instanciating the <code>TestCase</code> class on the server
39 * side, set up the implicit objects and call the test method.
40 *
41 * @version $Id: FilterTestCaller.java 292560 2005-09-29 21:48:10Z kenney $
42 */
43 public class FilterTestCaller extends AbstractWebTestCaller
44 {
45 /**
46 * @param theObjects the implicit objects coming from the redirector
47 */
48 public FilterTestCaller(FilterImplicitObjects theObjects)
49 {
50 super(theObjects);
51 }
52
53 /**
54 * {@inheritDoc}
55 * @see AbstractWebTestCaller#setTestCaseFields(TestCase)
56 */
57 protected void setTestCaseFields(TestCase theTestInstance)
58 throws Exception
59 {
60 if (!(theTestInstance instanceof FilterTestCase))
61 {
62 return;
63 }
64
65 FilterTestCase filterInstance = (FilterTestCase) theTestInstance;
66 FilterImplicitObjects filterImplicitObjects =
67 (FilterImplicitObjects) this.webImplicitObjects;
68
69 // Sets the request field of the test case class
70 // ---------------------------------------------
71 // Extract from the HTTP request the URL to simulate (if any)
72 HttpServletRequest request =
73 filterImplicitObjects.getHttpServletRequest();
74
75 ServletURL url = ServletURL.loadFromRequest(request);
76
77 Field requestField = filterInstance.getClass().getField("request");
78
79 requestField.set(filterInstance,
80 AbstractHttpServletRequestWrapper.newInstance(request, url));
81
82 // Set the response field of the test case class
83 // ---------------------------------------------
84 Field responseField = filterInstance.getClass().getField("response");
85
86 responseField.set(filterInstance,
87 filterImplicitObjects.getHttpServletResponse());
88
89 // Set the config field of the test case class
90 // -------------------------------------------
91 Field configField = filterInstance.getClass().getField("config");
92
93 configField.set(filterInstance,
94 new FilterConfigWrapper(filterImplicitObjects.getFilterConfig()));
95
96 // Set the filter chain of the test case class
97 // -------------------------------------------
98 Field chainField = filterInstance.getClass().getField("filterChain");
99
100 chainField.set(filterInstance, filterImplicitObjects.getFilterChain());
101 }
102
103 /**
104 * {@inheritDoc}
105 * @see AbstractWebTestCaller#getResponseWriter()
106 */
107 protected Writer getResponseWriter() throws IOException
108 {
109 return this.webImplicitObjects.getHttpServletResponse().getWriter();
110 }
111 }