KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > AbstractTestAbstractCactusTestCase


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;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23
24 import java.net.URL JavaDoc;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.TestCase;
28
29 import org.apache.cactus.WebRequest;
30 import org.apache.cactus.internal.client.ClientException;
31 import org.apache.cactus.internal.client.ClientTestCaseCaller;
32 import org.apache.cactus.internal.client.WebResponseObjectFactory;
33 import org.apache.cactus.internal.client.connector.http.HttpProtocolHandler;
34 import org.apache.cactus.internal.configuration.DefaultServletConfiguration;
35 import org.apache.cactus.internal.util.JUnitVersionHelper;
36 import org.apache.cactus.mock.MockHttpURLConnection;
37
38 /**
39  * Test class that intercepts all exceptions (and assert them) coming from
40  * test case classes that inherits from it. This class is used to unit test
41  * the {@link AbstractCactusTestCase} class.
42  *
43  * @version $Id: AbstractTestAbstractCactusTestCase.java,v 1.1 2004/05/22 11:34:49 vmassol Exp $
44  */

45 public abstract class AbstractTestAbstractCactusTestCase extends TestCase
46 {
47     /**
48      * Override default method so that configuration checks are not run during
49      * these unit tests.
50      *
51      * @exception Throwable if any exception is thrown during the test. Any
52      * exception will be displayed by the JUnit Test Runner
53      * @see AbstractTestCase#runBare()
54      */

55     public void runBare() throws Throwable JavaDoc
56     {
57         runTest();
58     }
59
60     /**
61      * Intercepts running test cases to check for normal exceptions.
62      *
63      * @exception Throwable any error that occurred when calling the test method
64      * for the current test case.
65      * @see AbstractTestCase#runTest()
66      */

67     protected void runTest() throws Throwable JavaDoc
68     {
69         ClientTestCaseCaller delegator = new ClientTestCaseCaller(
70             this, this,
71             new HttpProtocolHandler(new DefaultServletConfiguration()));
72
73         try
74         {
75             // Call the begin method
76
WebRequest request =
77                 new WebRequestImpl(new DefaultServletConfiguration());
78
79             delegator.callBeginMethod(request);
80
81             // Create a mock HttpURLConnection as it is needed by HttpUnit
82
// for creating a WebResponse
83
MockHttpURLConnection connection = new MockHttpURLConnection(
84                 new URL JavaDoc("http://something"));
85
86             // Set the values expected by Http Unit. Note: only the test
87
// cases that have an end method with an HttpUnit WebReponse
88
// will use the HttpURLConnection.
89
connection.setExpectedGetHeaderField("HTTP/1.1 200 OK");
90             connection.setExpectedGetInputStream(
91                 new ByteArrayInputStream JavaDoc("".getBytes()));
92
93             // Create a WebResponse object and call the end method
94
delegator.callEndMethod(request,
95                 new WebResponseObjectFactory(connection));
96         }
97         catch (AssertionFailedError e)
98         {
99             // Perform asserts
100
if (!verifyBeginMethodsOk(e.getMessage())
101                 && !verifyEndMethodsOk(e.getMessage()))
102             {
103                 throw e;
104             }
105         }
106         catch (ClientException e)
107         {
108             // Perform asserts
109
if (!verifyBeginMethodsOk(e.getMessage())
110                 && !verifyEndMethodsOk(e.getMessage()))
111             {
112                 throw e;
113             }
114         }
115     }
116
117     /**
118      * @param theTestName the test name to verify
119      * @return true if the test name to verify corresponds to the currently
120      * executing test
121      */

122     private boolean checkName(String JavaDoc theTestName)
123     {
124         return JUnitVersionHelper.getTestCaseName(this).equals(
125             theTestName);
126     }
127     
128     /**
129      * Assert begin method tests.
130      *
131      * @param theMessage the error message from the exception
132      * @return false is no test matches
133      */

134     private boolean verifyBeginMethodsOk(String JavaDoc theMessage)
135     {
136         // Test that when a begin method for a given test does not have the
137
// correct return type (i.e. void), a
138
// <code>AssertionFailedError</code> exception is returned.
139
if (checkName("testBeginMethodBadReturnType"))
140         {
141             assertEquals("The method "
142                 + "[beginBeginMethodBadReturnType] should return void and "
143                 + "not [java.lang.String]", theMessage);
144
145             return true;
146         }
147
148         // Test that when a begin method for a given test is not declared
149
// public a <code>AssertionFailedError</code> exception is returned.
150
if (checkName("testBeginMethodNotPublic"))
151         {
152             assertEquals("Method [beginBeginMethodNotPublic] should be "
153                 + "declared public", theMessage);
154
155             return true;
156         }
157
158         // Test that when a begin method for a given test has the wrong
159
// type of parameters, a <code>AssertionFailedError</code> exception
160
// is returned.
161
if (checkName("testBeginMethodBadParamType"))
162         {
163             assertEquals("The method "
164                 + "[beginBeginMethodBadParamType] must accept "
165                 + "[org.apache.cactus.Request] as 1st parameter, but "
166                 + "found a [java.lang.String] parameter instead", theMessage);
167
168             return true;
169         }
170
171         // Test that when a begin method for a given test has the wrong
172
// number of parameters, a <code>AssertionFailedError</code>
173
// exception is returned.
174
if (checkName("testBeginMethodBadParamNumber"))
175         {
176             assertEquals("The method "
177                 + "[beginBeginMethodBadParamNumber] must have "
178                 + "1 parameter(s), but 2 parameter(s) were found",
179                 theMessage);
180
181             return true;
182         }
183
184         // Verify that the begin method with a
185
// <code>WebRequest</code> parameter is called correctly.
186
if (checkName("testBeginMethodOK"))
187         {
188             assertEquals("beginBeginMethodOK", theMessage);
189
190             return true;
191         }
192
193         return false;
194     }
195
196     /**
197      * Assert end method tests.
198      *
199      * @param theMessage the error message from the exception
200      * @return false is no test matches
201      */

202     private boolean verifyEndMethodsOk(String JavaDoc theMessage)
203     {
204         // Test that when an end method for a given test does not have the
205
// correct return type (i.e. void), a
206
// <code>AssertionFailedError</code> exception is returned.
207
if (checkName("testEndMethodBadReturnType"))
208         {
209             assertEquals("The method "
210                 + "[endEndMethodBadReturnType] should return void and "
211                 + "not [java.lang.String]", theMessage);
212
213             return true;
214         }
215
216         // Test that when an end method for a given test is not declared
217
// public a <code>AssertionFailedError</code> exception is returned.
218
if (checkName("testEndMethodNotPublic"))
219         {
220             assertEquals("Method [endEndMethodNotPublic] should be "
221                 + "declared public", theMessage);
222
223             return true;
224         }
225
226         // Test that when an end method for a given test has the wrong
227
// type of parameters, a <code>AssertionFailedError</code> exception
228
// is returned.
229
if (checkName("testEndMethodBadParamType"))
230         {
231             assertEquals("The method [endEndMethodBadParamType] "
232                 + "has a bad parameter of type [java.lang.String]",
233                 theMessage);
234
235             return true;
236         }
237
238         // Test that when an end method for a given test has the wrong
239
// number of parameters, a <code>AssertionFailedError</code>
240
// exception is returned.
241
if (checkName("testEndMethodBadParamNumber"))
242         {
243             assertEquals("The method [endEndMethodBadParamNumber] "
244                 + "must have 1 parameter(s), but 2 parameter(s) were found",
245                 theMessage);
246
247             return true;
248         }
249
250         // Test that the end method is called correctly when it's signature
251
// contains a <code>org.apache.cactus.WebResponse</code>
252
// parameter.
253
if (checkName("testEndMethodOK1"))
254         {
255             assertEquals("endEndMethodOK1", theMessage);
256
257             return true;
258         }
259
260         // Test that the end method is called correctly when it's signature
261
// contains a <code>com.meterware.httpunit.WebResponse</code>
262
// parameter.
263
if (checkName("testEndMethodOK2"))
264         {
265             assertEquals("endEndMethodOK2", theMessage);
266
267             return true;
268         }
269
270         // Test that the deprecated end method with the
271
// <code>HttpURLConnection</code> parameter can still be called
272
// correctly.
273
if (checkName("testEndMethodOK3"))
274         {
275             assertEquals("endEndMethodOK3", theMessage);
276
277             return true;
278         }
279
280         return false;
281     }
282
283 }
284
Popular Tags