KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > WebTestCase


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import javax.servlet.ServletContext JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import org.apache.log4j.Logger;
17
18 import com.inversoft.junit.internal.Mediator;
19 import com.inversoft.junit.internal.MediatorFactory;
20 import com.inversoft.junit.internal.http.MockHttpServletRequest;
21 import com.inversoft.junit.internal.http.MockHttpServletResponse;
22 import com.inversoft.junit.internal.http.MockServletConfig;
23 import com.inversoft.junit.internal.http.MockServletContext;
24
25
26 /**
27  * <p>
28  * This class is the base class for all XiriTech Junit
29  * framework tests. This class has full control over how
30  * the tests are executed with respect to local tests or
31  * remote tests. This can also be controlled via the junit
32  * properties file.
33  * </p>
34  *
35  * <p>
36  * The default properties file location is junit.properties
37  * and must be placed in the class path. However, this can
38  * be changed using the junit.config.file command line property.
39  * </p>
40  *
41  * <p>
42  * There are two important items to note in this file. The
43  * first is the <code>test.method</code> property which
44  * determines where the test is executed, locally or locally
45  * and remotely. The second is the test.location which sets
46  * up the URL to contact if the test is a remote test.
47  * </p>
48  *
49  * <p>
50  * Local tests are the same as all other JUnit tests. The
51  * only difference is that they are testing server side
52  * classes and therefore must simulate a container environment.
53  * This is done using Mock classes which mimic the server
54  * classes such as HttpServletRequest.
55  * </p>
56  *
57  * <p>
58  * Remote tests are executed inside a container. This is
59  * accomplished using a Servlet that handles the transfer
60  * of information from the client to the server and vice
61  * versa. For example, the client will tell the servlet
62  * what test method to execute and the servlet will tell
63  * the client what the result was.
64  * </p>
65  *
66  * <p>
67  * Here is some information about the way the framework calls
68  * your test methods.
69  * </p>
70  *
71  * <h1>The client side</h1>
72  * <p>
73  * The client side is generally considered the gray box side.
74  * This is the location where the test is initially setup. This
75  * is also the side where the results of the test are verified.
76  * This side has a very constricted view of the code being tested.
77  * </p>
78  *
79  * <p>
80  * The client side generally is aware of items such as Cookies
81  * and result codes. The client side can also be extended to
82  * verify HTML content.
83  * </p>
84  *
85  * <p>
86  * In general, the client side has two methods where
87  * initialization and verification code may be placed.
88  * These use the same naming convention as standard JUnit
89  * test methods, except that the string <code>test</code> is
90  * replaced with <code>begin</code> and <code>end</code>.
91  * </p>
92  *
93  * <p>
94  * For example:
95  * </p>
96  *
97  * <p>
98  * <ul>
99  * <li>testFoo() - the name of the test method using standard
100  * JUnit naming</li>
101  * <li>beginFoo() - the name of the begin method for the Inversoft
102  * framework</li>
103  * <li>endFoo() - the name of the end method for the Inversoft
104  * framework</li>
105  * </p>
106  *
107  * <h1>The test location side</h1>
108  * <p>
109  * Original this was called the server side because all tests
110  * were run inside the WebLogic container. This has been
111  * modified so that the framework can be used to run tests
112  * inside and outside the container. Therefore, it was
113  * renamed to test location.
114  * </p>
115  *
116  * <p>
117  * The test location side is the white box testing side.
118  * This is the location that code to be tested is actually
119  * tested. This side is identical to JUnit tests. The test
120  * method must be named <code>testX</code> where X is a
121  * unique identifier. The TestCase class can also make use
122  * of the tearDown and setUp methods of the JUnit standard.
123  * </p>
124  *
125  * <h1>Execution order</h1>
126  * <p>
127  * The excution order of the XiriTech framework tests is as
128  * follows. This example uses a test called Foo. This also
129  * divides the method calls into locations for clarity. It
130  * is important to remember that the methods are executed
131  * linearly.
132  * </p>
133  *
134  * <h1>The client side</h1>
135  * <ul>
136  * <li>void beginFoo()</li>
137  * </ul>
138  *
139  * <h1>The test location side</h1>
140  * <ul>
141  * <li>void setUp()</li>
142  * <li>void testFoo()</li>
143  * <li>void tearDown()</li>
144  * </ul>
145  *
146  * <h1>The client side</h1>
147  * <ul>
148  * <li>void endFoo()</li>
149  * </ul>
150  *
151  * @author Brian Pontarelli
152  * @since 1.0
153  * @version 1.0
154  */

155 public class WebTestCase extends TestCase {
156
157     /**
158      * This is the logger for the AbstractTestCase
159      */

160     private static final Logger logger = Logger.getLogger(WebTestCase.class);
161
162     /**
163      * The parameters to the constructor of the WebTestCase. This is a single
164      * string because this is also the same param types required by JUnit
165      */

166     public static final Class JavaDoc[] CONSTRUCTOR_PARAM_TYPES = new Class JavaDoc[] {String JavaDoc.class};
167
168
169     /**
170      * The flag that determines if this test is running locally or remotely
171      */

172     private boolean local;
173
174     /**
175      * The HttpServletRequest for use in the test methods
176      */

177     protected HttpServletRequest JavaDoc request;
178
179     /**
180      * The HttpServletResponse for use in the test methods
181      */

182     protected HttpServletResponse JavaDoc response;
183
184     /**
185      * The HttpServletRequest for use in the test methods
186      */

187     private HttpServletRequest JavaDoc origRequest;
188
189     /**
190      * The HttpServletResponse for use in the test methods
191      */

192     private HttpServletResponse JavaDoc origResponse;
193
194     /**
195      * The ServletContext for use in the test methods
196      */

197     protected ServletContext JavaDoc context;
198
199
200     /**
201      * This is the constructor that mimics the TestCase constructor for
202      * build test cases
203      */

204     public WebTestCase(String JavaDoc name) {
205         super(name);
206         if (logger.isDebugEnabled()) {
207             logger.debug("The name of the test is " + name);
208         }
209     }
210
211
212     /**
213      * Gets the local flag
214      *
215      * @return Returns the local flag
216      */

217     public boolean isLocal() {
218         return local;
219     }
220
221     /**
222      * Sets the local flag
223      *
224      * @param local the local flag
225      */

226     public void setLocal(boolean local) {
227         this.local = local;
228     }
229
230
231     /**
232      * This method always executes on the client side. This is the main control
233      * method and therefore is final so that it can not be overridden, which
234      * would in effect cause the entire framework to cease working.
235      *
236      * @throws Throwable If any test failed
237      */

238     public final void runBare() throws Throwable JavaDoc {
239
240         // Call setUp() on the client, which may or may not be at the same location
241
// as the test method execution (which may happen locally or remotely)
242
setUp();
243
244         try {
245             Mediator mediator = MediatorFactory.buildMediator(local);
246             mediator.mediate(this);
247         } catch (Throwable JavaDoc t) {
248             if (logger.isDebugEnabled()) {
249                 logger.debug("The throwable being thrown from the AbstactTestCase is: " +
250                     t.toString());
251             }
252             throw t;
253         }
254
255         // Call tearDown() on the client, which may or may not be at the same location
256
// as the test method execution (which may happen locally or remotely)
257
tearDown();
258     }
259
260
261     //-------------------------------------------------------------------------
262
// Helper Methods
263
//-------------------------------------------------------------------------
264

265
266     /**
267      * Returns the Request down-cast to a MockHttpServletRequest. This can and will
268      * throw a ClassCastException if the request is not an instance of the
269      * MockHttpServletRequest class
270      */

271     public MockHttpServletRequest getRequest() {
272         if (!isLocal()) {
273             throw new IllegalStateException JavaDoc("You can only call getRequest when" +
274                 " running tests locally");
275         }
276
277         return (MockHttpServletRequest) request;
278     }
279
280     /**
281      * Sets in the request object that can be used for testing.
282      */

283     public void setRequest(HttpServletRequest JavaDoc request) {
284         this.request = request;
285     }
286
287     /**
288      * Returns the Response down-cast to a MockHttpServletResponse. This can and will
289      * throw a ClassCastException if the request is not an instance of the
290      * MockHttpServletResponse class
291      */

292     public MockHttpServletResponse getResponse() {
293         if (!isLocal()) {
294             throw new IllegalStateException JavaDoc("You can only call getRequest when" +
295                 " running tests locally");
296         }
297         return (MockHttpServletResponse) response;
298     }
299
300     /**
301      * Sets in the response object that can be used for testing.
302      */

303     public void setResponse(HttpServletResponse JavaDoc response) {
304         this.response = response;
305     }
306
307     /**
308      * Gets the original request object. This should not be used as it does
309      * not reflect the correct URL of the tests, but is included in case it is
310      * needed.
311      *
312      * @return The original request.
313      */

314     public HttpServletRequest JavaDoc getOrigRequest() {
315         return origRequest;
316     }
317
318     /**
319      * Sets the original request object. This should not be used as it does
320      * not reflect the correct URL of the tests, but is included in case it is
321      * needed.
322      *
323      * @param origRequest The original request.
324      */

325     public void setOrigRequest(HttpServletRequest JavaDoc origRequest) {
326         this.origRequest = origRequest;
327     }
328
329     /**
330      * Gets the original response object. This should not be used as it does
331      * influence the result returned to the client.
332      *
333      * @return The original response.
334      */

335     public HttpServletResponse JavaDoc getOrigResponse() {
336         return origResponse;
337     }
338
339     /**
340      * Sets the original request object. This should not be used as it does
341      * influence the result returned to the client.
342      *
343      * @param origResponse The original response.
344      */

345     public void setOrigResponse(HttpServletResponse JavaDoc origResponse) {
346         this.origResponse = origResponse;
347     }
348
349     /**
350      * Returns the Request down-cast to a MockServletContext. This can and will
351      * throw a ClassCastException if the request is not an instance of the
352      * MockServletContext class
353      */

354     public MockServletContext getContext() {
355         if (!isLocal()) {
356             throw new IllegalStateException JavaDoc("You can only call getRequest when" +
357                 " running tests locally");
358         }
359         return (MockServletContext) context;
360     }
361
362     /**
363      * Sets in the servlet context for testing.
364      */

365     public void setContext(ServletContext JavaDoc context) {
366         this.context = context;
367     }
368
369     /**
370      * Constructs a new MockServletConfig that can be used to test Servlet
371      * initialization.
372      *
373      * @return A new MockServletConfig that is setup correctly with the
374      * ServletContext
375      */

376     public MockServletConfig createConfig() {
377         return new MockServletConfig(context);
378     }
379
380     /**
381      * Constructs a new MockServletConfig that can be used to test Servlet
382      * initialization. This ServletConfig is configured with the servlet name
383      * given
384      *
385      * @param servletName The name of the servlet the config is for
386      * @return A new MockServletConfig that is setup correctly with the
387      * ServletContext
388      */

389     public MockServletConfig createConfig(String JavaDoc servletName) {
390         return new MockServletConfig(servletName, context);
391     }
392 }
Popular Tags