|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ServletTestCase.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.http.HttpServletResponse;
|
|
60 |
import javax.servlet.http.HttpSession;
|
|
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.ServletConfiguration;
|
|
67 |
import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
|
|
68 |
import org.apache.cactus.internal.server.ServerTestCaseDelegate;
|
|
69 |
import org.apache.cactus.server.ServletConfigWrapper;
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Test classes that need access to valid Servlet implicit objects (such as the
|
|
73 |
* the HTTP request, the HTTP response, the servlet config, ...) must subclass
|
|
74 |
* this class.
|
|
75 |
*
|
|
76 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
77 |
*
|
|
78 |
* @version $Id: ServletTestCase.java,v 1.15.2.2 2003/10/23 15:10:08 vmassol Exp $
|
|
79 |
*/
|
|
80 |
public class ServletTestCase 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 |
/**
|
|
95 |
* Valid <code>HttpServletRequest</code> object that you can access from
|
|
96 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
97 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
98 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
99 |
* have the <code>null</code> value.
|
|
100 |
*/
|
|
101 |
public org.apache.cactus.server.HttpServletRequestWrapper request;
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Valid <code>HttpServletResponse</code> object that you can access from
|
|
105 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
106 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
107 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
108 |
* have the <code>null</code> value.
|
|
109 |
*/
|
|
110 |
public HttpServletResponse response;
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Valid <code>HttpSession</code> object that you can access from
|
|
114 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
115 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
116 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
117 |
* have the <code>null</code> value.
|
|
118 |
*/
|
|
119 |
public HttpSession session;
|
|
120 |
|
|
121 |
/**
|
|
122 |
* Valid <code>ServletConfig</code> object that you can access from
|
|
123 |
* the <code>testXXX()</code>, <code>setUp</code> and
|
|
124 |
* <code>tearDown()</code> methods. If you try to access it from either the
|
|
125 |
* <code>beginXXX()</code> or <code>endXXX()</code> methods it will
|
|
126 |
* have the <code>null</code> value.
|
|
127 |
*/
|
|
128 |
public ServletConfigWrapper config;
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Delegate that provides all client side Cactus related test case logic.
|
|
132 |
* We are using a delegate in order to hide non public API to the users
|
|
133 |
* and thus to be able to easily change the implementation.
|
|
134 |
*/
|
|
135 |
private WebClientTestCaseDelegate clientDelegate;
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Delegate that provides all server side Cactus related test case logic.
|
|
139 |
* We are using a delegate in order to hide non public API to the users
|
|
140 |
* and thus to be able to easily change the implementation.
|
|
141 |
*/
|
|
142 |
private ServerTestCaseDelegate serverDelegate;
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Default constructor defined in order to allow creating Test Case
|
|
146 |
* without needing to define constructor (new feature in JUnit 3.8.1).
|
|
147 |
* Should only be used with JUnit 3.8.1 or greater.
|
|
148 |
*
|
|
149 |
* @since 1.5
|
|
150 |
*/
|
|
151 | 0 |
public ServletTestCase()
|
152 |
{ |
|
153 | 0 |
init(null);
|
154 |
} |
|
155 |
|
|
156 |
/**
|
|
157 |
* Constructs a JUnit test case with the given name.
|
|
158 |
*
|
|
159 |
* @param theName the name of the test case
|
|
160 |
*/
|
|
161 | 0 |
public ServletTestCase(String theName)
|
162 |
{ |
|
163 | 0 |
super(theName);
|
164 | 0 |
init(null);
|
165 |
} |
|
166 |
|
|
167 |
/**
|
|
168 |
* Wraps a pure JUnit Test Case in a Cactus Test Case.
|
|
169 |
*
|
|
170 |
* @param theName the name of the test
|
|
171 |
* @param theTest the Test Case class to wrap
|
|
172 |
* @since 1.5
|
|
173 |
*/
|
|
174 | 0 |
public ServletTestCase(String theName, Test theTest)
|
175 |
{ |
|
176 | 0 |
super(theName);
|
177 | 0 |
init(theTest); |
178 |
} |
|
179 |
|
|
180 |
/**
|
|
181 |
* Initializations common to all constructors.
|
|
182 |
*
|
|
183 |
* @param theTest a pure JUnit Test that Cactus will wrap
|
|
184 |
*/
|
|
185 | 0 |
void init(Test theTest)
|
186 |
{ |
|
187 | 0 |
setClientDelegate(new WebClientTestCaseDelegate(
|
188 |
this, theTest, new ServletConfiguration())); |
|
189 | 0 |
setServerDelegate(new ServerTestCaseDelegate(this, theTest)); |
190 |
} |
|
191 |
|
|
192 |
/**
|
|
193 |
* @param theDelegate the client test case delegate
|
|
194 |
*/
|
|
195 | 0 |
void setClientDelegate(WebClientTestCaseDelegate theDelegate)
|
196 |
{ |
|
197 | 0 |
this.clientDelegate = theDelegate;
|
198 |
} |
|
199 |
|
|
200 |
/**
|
|
201 |
* @param theDelegate the client test case delegate
|
|
202 |
*/
|
|
203 | 0 |
void setServerDelegate(ServerTestCaseDelegate theDelegate)
|
204 |
{ |
|
205 | 0 |
this.serverDelegate = theDelegate;
|
206 |
} |
|
207 |
|
|
208 |
/**
|
|
209 |
* @return the client test case delegate
|
|
210 |
*/
|
|
211 | 0 |
WebClientTestCaseDelegate getClientDelegate() |
212 |
{ |
|
213 | 0 |
return this.clientDelegate; |
214 |
} |
|
215 |
|
|
216 |
/**
|
|
217 |
* @return the server test case delegate
|
|
218 |
*/
|
|
219 | 0 |
private ServerTestCaseDelegate getServerDelegate()
|
220 |
{ |
|
221 | 0 |
return this.serverDelegate; |
222 |
} |
|
223 |
|
|
224 |
/**
|
|
225 |
* @return true if this test class has been instanciated on the server
|
|
226 |
* side or false otherwise
|
|
227 |
*/
|
|
228 | 0 |
private boolean isServerSide() |
229 |
{ |
|
230 | 0 |
boolean result = false; |
231 |
|
|
232 | 0 |
if (this.request != null) |
233 |
{ |
|
234 | 0 |
result = true;
|
235 |
} |
|
236 | 0 |
return result;
|
237 |
} |
|
238 |
|
|
239 |
/**
|
|
240 |
* Runs the bare test (either on the client side or on the server side).
|
|
241 |
* This method is overridden from the JUnit
|
|
242 |
* {@link TestCase} class in order to prevent the latter to immediatly
|
|
243 |
* call the <code>setUp()</code> and <code>tearDown()</code> methods
|
|
244 |
* which, in our case, need to be executed on the server side.
|
|
245 |
*
|
|
246 |
* @exception Throwable if any exception is thrown during the test. Any
|
|
247 |
* exception will be displayed by the JUnit Test Runner
|
|
248 |
*/
|
|
249 | 0 |
public void runBare() throws Throwable |
250 |
{ |
|
251 | 0 |
if (isServerSide())
|
252 |
{ |
|
253 | 0 |
getServerDelegate().runBareInit(); |
254 |
} |
|
255 |
else
|
|
256 |
{ |
|
257 | 0 |
getClientDelegate().runBareInit(); |
258 |
} |
|
259 |
|
|
260 |
// Catch the exception just to have a chance to log it
|
|
261 | 0 |
try
|
262 |
{ |
|
263 | 0 |
runCactusTest(); |
264 |
} |
|
265 |
catch (Throwable t)
|
|
266 |
{ |
|
267 | 0 |
if (!isServerSide())
|
268 |
{ |
|
269 | 0 |
getClientDelegate().getLogger().debug("Exception in test", t);
|
270 |
} |
|
271 | 0 |
throw t;
|
272 |
} |
|
273 |
} |
|
274 |
|
|
275 |
/**
|
|
276 |
* Runs a Cactus test case.
|
|
277 |
*
|
|
278 |
* @exception Throwable if any error happens during the execution of
|
|
279 |
* the test
|
|
280 |
*/
|
|
281 | 0 |
protected void runCactusTest() throws Throwable |
282 |
{ |
|
283 | 0 |
if (isServerSide())
|
284 |
{ |
|
285 |
// Note: We cannot delegate this piece of code in the
|
|
286 |
// ServerTestCaseDelegate class as it requires to call
|
|
287 |
// super.runBare()
|
|
288 |
|
|
289 | 0 |
if (getServerDelegate().getWrappedTest() != null) |
290 |
{ |
|
291 | 0 |
((TestCase) getServerDelegate().getWrappedTest()).runBare(); |
292 |
} |
|
293 |
else
|
|
294 |
{ |
|
295 | 0 |
super.runBare();
|
296 |
} |
|
297 |
} |
|
298 |
else
|
|
299 |
{ |
|
300 | 0 |
getClientDelegate().runTest(); |
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
|
|