|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FilterTestCase.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus;
|
|
58 |
|
|
59 |
import javax.servlet.FilterChain;
|
|
60 |
import javax.servlet.http.HttpServletResponse;
|
|
61 |
|
|
62 |
import junit.framework.Test;
|
|
63 |
import junit.framework.TestCase;
|
|
64 |
|
|
65 |
import org.apache.cactus.configuration.ConfigurationInitializer;
|
|
66 |
import org.apache.cactus.configuration.FilterConfiguration;
|
|
67 |
import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
|
|
68 |
import org.apache.cactus.internal.server.ServerTestCaseDelegate;
|
|
69 |
import org.apache.cactus.server.FilterConfigWrapper;
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Test classes that need access to valid Filter implicit objects (such as the
|
|
73 |
* <code>FilterConfig</code> and <code>FilterChain</code> objects) must
|
|
74 |
* subclass this class.
|
|
75 |
*
|
|
76 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
77 |
*
|
|
78 |
* @version $Id: FilterTestCase.java,v 1.17.2.2 2003/10/23 15:10:09 vmassol Exp $
|
|
79 |
*/
|
|
80 |
public class FilterTestCase extends TestCase |
|
81 |
{ |
|
82 |
/**
|
|
83 |
* As this class is the first one loaded on the client side, we ensure
|
|
84 |
* that the Cactus configuration has been initialized. In the future,
|
|
85 |
* this block will be removed as all initialization will be done in Cactus
|
|
86 |
* test suites. However, as we still support using Cactus TestCase classes
|
|
87 |
* we don't a proper initialization hook and thus we need this hack.
|
|
88 |
*/
|
|
89 |
static
|
|
90 |
{ |
|
91 | 0 |
ConfigurationInitializer.initialize(); |
92 |
} |
|
93 |
|
|
94 |
// TODO: Find a way to factorize FilterTestCase and ServletTestCase
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Valid <code>HttpServletRequest</code> object that you can access from
|
|
98 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
99 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
100 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
101 |
* have the <code>null</code> value.
|
|
102 |
*/
|
|
103 |
public org.apache.cactus.server.HttpServletRequestWrapper request;
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Valid <code>HttpServletResponse</code> object that you can access from
|
|
107 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
108 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
109 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
110 |
* have the <code>null</code> value.
|
|
111 |
*/
|
|
112 |
public HttpServletResponse response;
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Valid <code>FilterConfig</code> object that you can access from
|
|
116 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
117 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
118 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
119 |
* have the <code>null</code> value.
|
|
120 |
*/
|
|
121 |
public FilterConfigWrapper config;
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Valid <code>FilterChain</code> object that you can access from
|
|
125 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
126 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
127 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
128 |
* have the <code>null</code> value.
|
|
129 |
*/
|
|
130 |
public FilterChain filterChain;
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Delegate that provides all client side Cactus related test case logic.
|
|
134 |
* We are using a delegate in order to hide non public API to the users
|
|
135 |
* and thus to be able to easily change the implementation.
|
|
136 |
*/
|
|
137 |
private WebClientTestCaseDelegate clientDelegate;
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Delegate that provides all server side Cactus related test case logic.
|
|
141 |
* We are using a delegate in order to hide non public API to the users
|
|
142 |
* and thus to be able to easily change the implementation.
|
|
143 |
*/
|
|
144 |
private ServerTestCaseDelegate serverDelegate;
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Default constructor defined in order to allow creating Test Case
|
|
148 |
* without needing to define constructor (new feature in JUnit 3.8.1).
|
|
149 |
* Should only be used with JUnit 3.8.1 or greater.
|
|
150 |
*
|
|
151 |
* @since 1.5
|
|
152 |
*/
|
|
153 | 0 |
public FilterTestCase()
|
154 |
{ |
|
155 | 0 |
init(null);
|
156 |
} |
|
157 |
|
|
158 |
/**
|
|
159 |
* Constructs a JUnit test case with the given name.
|
|
160 |
*
|
|
161 |
* @param theName the name of the test case
|
|
162 |
*/
|
|
163 | 0 |
public FilterTestCase(String theName)
|
164 |
{ |
|
165 | 0 |
super(theName);
|
166 | 0 |
init(null);
|
167 |
} |
|
168 |
|
|
169 |
/**
|
|
170 |
* Wraps a pure JUnit Test Case in a Cactus Test Case.
|
|
171 |
*
|
|
172 |
* @param theName the name of the test
|
|
173 |
* @param theTest the Test Case class to wrap
|
|
174 |
* @since 1.5
|
|
175 |
*/
|
|
176 | 0 |
public FilterTestCase(String theName, Test theTest)
|
177 |
{ |
|
178 | 0 |
super(theName);
|
179 | 0 |
init(theTest); |
180 |
} |
|
181 |
|
|
182 |
/**
|
|
183 |
* Initializations common to all constructors.
|
|
184 |
*
|
|
185 |
* @param theTest a pure JUnit Test that Cactus will wrap
|
|
186 |
*/
|
|
187 | 0 |
void init(Test theTest)
|
188 |
{ |
|
189 | 0 |
setClientDelegate(new WebClientTestCaseDelegate(
|
190 |
this, theTest, new FilterConfiguration())); |
|
191 | 0 |
setServerDelegate(new ServerTestCaseDelegate(this, theTest)); |
192 |
} |
|
193 |
|
|
194 |
/**
|
|
195 |
* @param theDelegate the client test case delegate
|
|
196 |
*/
|
|
197 | 0 |
void setClientDelegate(WebClientTestCaseDelegate theDelegate)
|
198 |
{ |
|
199 | 0 |
this.clientDelegate = theDelegate;
|
200 |
} |
|
201 |
|
|
202 |
/**
|
|
203 |
* @param theDelegate the client test case delegate
|
|
204 |
*/
|
|
205 | 0 |
void setServerDelegate(ServerTestCaseDelegate theDelegate)
|
206 |
{ |
|
207 | 0 |
this.serverDelegate = theDelegate;
|
208 |
} |
|
209 |
|
|
210 |
/**
|
|
211 |
* @return the client test case delegate
|
|
212 |
*/
|
|
213 | 0 |
WebClientTestCaseDelegate getClientDelegate() |
214 |
{ |
|
215 | 0 |
return this.clientDelegate; |
216 |
} |
|
217 |
|
|
218 |
/**
|
|
219 |
* @return the server test case delegate
|
|
220 |
*/
|
|
221 | 0 |
private ServerTestCaseDelegate getServerDelegate()
|
222 |
{ |
|
223 | 0 |
return this.serverDelegate; |
224 |
} |
|
225 |
|
|
226 |
/**
|
|
227 |
* @return true if this test class has been instanciated on the server
|
|
228 |
* side or false otherwise
|
|
229 |
*/
|
|
230 | 0 |
private boolean isServerSide() |
231 |
{ |
|
232 | 0 |
boolean result = false; |
233 |
|
|
234 | 0 |
if (this.request != null) |
235 |
{ |
|
236 | 0 |
result = true;
|
237 |
} |
|
238 | 0 |
return result;
|
239 |
} |
|
240 |
|
|
241 |
/**
|
|
242 |
* Runs the bare test (either on the client side or on the server side).
|
|
243 |
* This method is overridden from the JUnit
|
|
244 |
* {@link TestCase} class in order to prevent the latter to immediatly
|
|
245 |
* call the <code>setUp()</code> and <code>tearDown()</code> methods
|
|
246 |
* which, in our case, need to be executed on the server side.
|
|
247 |
*
|
|
248 |
* @exception Throwable if any exception is thrown during the test. Any
|
|
249 |
* exception will be displayed by the JUnit Test Runner
|
|
250 |
*/
|
|
251 | 0 |
public void runBare() throws Throwable |
252 |
{ |
|
253 | 0 |
if (isServerSide())
|
254 |
{ |
|
255 | 0 |
getServerDelegate().runBareInit(); |
256 |
} |
|
257 |
else
|
|
258 |
{ |
|
259 | 0 |
getClientDelegate().runBareInit(); |
260 |
} |
|
261 |
|
|
262 |
// Catch the exception just to have a chance to log it
|
|
263 | 0 |
try
|
264 |
{ |
|
265 | 0 |
runCactusTest(); |
266 |
} |
|
267 |
catch (Throwable t)
|
|
268 |
{ |
|
269 | 0 |
if (!isServerSide())
|
270 |
{ |
|
271 | 0 |
getClientDelegate().getLogger().debug("Exception in test", t);
|
272 |
} |
|
273 | 0 |
throw t;
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
/**
|
|
278 |
* Runs a Cactus test case.
|
|
279 |
*
|
|
280 |
* @exception Throwable if any error happens during the execution of
|
|
281 |
* the test
|
|
282 |
*/
|
|
283 | 0 |
protected void runCactusTest() throws Throwable |
284 |
{ |
|
285 | 0 |
if (isServerSide())
|
286 |
{ |
|
287 |
// Note: We cannot delegate this piece of code in the
|
|
288 |
// ServerTestCaseDelegate class as it requires to call
|
|
289 |
// super.runBare()
|
|
290 |
|
|
291 | 0 |
if (getServerDelegate().getWrappedTest() != null) |
292 |
{ |
|
293 | 0 |
((TestCase) getServerDelegate().getWrappedTest()).runBare(); |
294 |
} |
|
295 |
else
|
|
296 |
{ |
|
297 | 0 |
super.runBare();
|
298 |
} |
|
299 |
} |
|
300 |
else
|
|
301 |
{ |
|
302 | 0 |
getClientDelegate().runTest(); |
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
|
|