001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal;
021:
022: import java.io.ByteArrayInputStream;
023:
024: import java.net.URL;
025:
026: import junit.framework.AssertionFailedError;
027: import junit.framework.TestCase;
028:
029: import org.apache.cactus.WebRequest;
030: import org.apache.cactus.internal.client.ClientException;
031: import org.apache.cactus.internal.client.ClientTestCaseCaller;
032: import org.apache.cactus.internal.client.WebResponseObjectFactory;
033: import org.apache.cactus.internal.client.connector.http.HttpProtocolHandler;
034: import org.apache.cactus.internal.configuration.DefaultServletConfiguration;
035: import org.apache.cactus.internal.util.JUnitVersionHelper;
036: import org.apache.cactus.mock.MockHttpURLConnection;
037:
038: /**
039: * Test class that intercepts all exceptions (and assert them) coming from
040: * test case classes that inherits from it. This class is used to unit test
041: * the {@link AbstractCactusTestCase} class.
042: *
043: * @version $Id: AbstractTestAbstractCactusTestCase.java 238991 2004-05-22 11:34:50Z vmassol $
044: */
045: public abstract class AbstractTestAbstractCactusTestCase extends
046: TestCase {
047: /**
048: * Override default method so that configuration checks are not run during
049: * these unit tests.
050: *
051: * @exception Throwable if any exception is thrown during the test. Any
052: * exception will be displayed by the JUnit Test Runner
053: * @see AbstractTestCase#runBare()
054: */
055: public void runBare() throws Throwable {
056: runTest();
057: }
058:
059: /**
060: * Intercepts running test cases to check for normal exceptions.
061: *
062: * @exception Throwable any error that occurred when calling the test method
063: * for the current test case.
064: * @see AbstractTestCase#runTest()
065: */
066: protected void runTest() throws Throwable {
067: ClientTestCaseCaller delegator = new ClientTestCaseCaller(this ,
068: this , new HttpProtocolHandler(
069: new DefaultServletConfiguration()));
070:
071: try {
072: // Call the begin method
073: WebRequest request = new WebRequestImpl(
074: new DefaultServletConfiguration());
075:
076: delegator.callBeginMethod(request);
077:
078: // Create a mock HttpURLConnection as it is needed by HttpUnit
079: // for creating a WebResponse
080: MockHttpURLConnection connection = new MockHttpURLConnection(
081: new URL("http://something"));
082:
083: // Set the values expected by Http Unit. Note: only the test
084: // cases that have an end method with an HttpUnit WebReponse
085: // will use the HttpURLConnection.
086: connection.setExpectedGetHeaderField("HTTP/1.1 200 OK");
087: connection
088: .setExpectedGetInputStream(new ByteArrayInputStream(
089: "".getBytes()));
090:
091: // Create a WebResponse object and call the end method
092: delegator.callEndMethod(request,
093: new WebResponseObjectFactory(connection));
094: } catch (AssertionFailedError e) {
095: // Perform asserts
096: if (!verifyBeginMethodsOk(e.getMessage())
097: && !verifyEndMethodsOk(e.getMessage())) {
098: throw e;
099: }
100: } catch (ClientException e) {
101: // Perform asserts
102: if (!verifyBeginMethodsOk(e.getMessage())
103: && !verifyEndMethodsOk(e.getMessage())) {
104: throw e;
105: }
106: }
107: }
108:
109: /**
110: * @param theTestName the test name to verify
111: * @return true if the test name to verify corresponds to the currently
112: * executing test
113: */
114: private boolean checkName(String theTestName) {
115: return JUnitVersionHelper.getTestCaseName(this ).equals(
116: theTestName);
117: }
118:
119: /**
120: * Assert begin method tests.
121: *
122: * @param theMessage the error message from the exception
123: * @return false is no test matches
124: */
125: private boolean verifyBeginMethodsOk(String theMessage) {
126: // Test that when a begin method for a given test does not have the
127: // correct return type (i.e. void), a
128: // <code>AssertionFailedError</code> exception is returned.
129: if (checkName("testBeginMethodBadReturnType")) {
130: assertEquals(
131: "The method "
132: + "[beginBeginMethodBadReturnType] should return void and "
133: + "not [java.lang.String]", theMessage);
134:
135: return true;
136: }
137:
138: // Test that when a begin method for a given test is not declared
139: // public a <code>AssertionFailedError</code> exception is returned.
140: if (checkName("testBeginMethodNotPublic")) {
141: assertEquals(
142: "Method [beginBeginMethodNotPublic] should be "
143: + "declared public", theMessage);
144:
145: return true;
146: }
147:
148: // Test that when a begin method for a given test has the wrong
149: // type of parameters, a <code>AssertionFailedError</code> exception
150: // is returned.
151: if (checkName("testBeginMethodBadParamType")) {
152: assertEquals(
153: "The method "
154: + "[beginBeginMethodBadParamType] must accept "
155: + "[org.apache.cactus.Request] as 1st parameter, but "
156: + "found a [java.lang.String] parameter instead",
157: theMessage);
158:
159: return true;
160: }
161:
162: // Test that when a begin method for a given test has the wrong
163: // number of parameters, a <code>AssertionFailedError</code>
164: // exception is returned.
165: if (checkName("testBeginMethodBadParamNumber")) {
166: assertEquals("The method "
167: + "[beginBeginMethodBadParamNumber] must have "
168: + "1 parameter(s), but 2 parameter(s) were found",
169: theMessage);
170:
171: return true;
172: }
173:
174: // Verify that the begin method with a
175: // <code>WebRequest</code> parameter is called correctly.
176: if (checkName("testBeginMethodOK")) {
177: assertEquals("beginBeginMethodOK", theMessage);
178:
179: return true;
180: }
181:
182: return false;
183: }
184:
185: /**
186: * Assert end method tests.
187: *
188: * @param theMessage the error message from the exception
189: * @return false is no test matches
190: */
191: private boolean verifyEndMethodsOk(String theMessage) {
192: // Test that when an end method for a given test does not have the
193: // correct return type (i.e. void), a
194: // <code>AssertionFailedError</code> exception is returned.
195: if (checkName("testEndMethodBadReturnType")) {
196: assertEquals(
197: "The method "
198: + "[endEndMethodBadReturnType] should return void and "
199: + "not [java.lang.String]", theMessage);
200:
201: return true;
202: }
203:
204: // Test that when an end method for a given test is not declared
205: // public a <code>AssertionFailedError</code> exception is returned.
206: if (checkName("testEndMethodNotPublic")) {
207: assertEquals("Method [endEndMethodNotPublic] should be "
208: + "declared public", theMessage);
209:
210: return true;
211: }
212:
213: // Test that when an end method for a given test has the wrong
214: // type of parameters, a <code>AssertionFailedError</code> exception
215: // is returned.
216: if (checkName("testEndMethodBadParamType")) {
217: assertEquals("The method [endEndMethodBadParamType] "
218: + "has a bad parameter of type [java.lang.String]",
219: theMessage);
220:
221: return true;
222: }
223:
224: // Test that when an end method for a given test has the wrong
225: // number of parameters, a <code>AssertionFailedError</code>
226: // exception is returned.
227: if (checkName("testEndMethodBadParamNumber")) {
228: assertEquals(
229: "The method [endEndMethodBadParamNumber] "
230: + "must have 1 parameter(s), but 2 parameter(s) were found",
231: theMessage);
232:
233: return true;
234: }
235:
236: // Test that the end method is called correctly when it's signature
237: // contains a <code>org.apache.cactus.WebResponse</code>
238: // parameter.
239: if (checkName("testEndMethodOK1")) {
240: assertEquals("endEndMethodOK1", theMessage);
241:
242: return true;
243: }
244:
245: // Test that the end method is called correctly when it's signature
246: // contains a <code>com.meterware.httpunit.WebResponse</code>
247: // parameter.
248: if (checkName("testEndMethodOK2")) {
249: assertEquals("endEndMethodOK2", theMessage);
250:
251: return true;
252: }
253:
254: // Test that the deprecated end method with the
255: // <code>HttpURLConnection</code> parameter can still be called
256: // correctly.
257: if (checkName("testEndMethodOK3")) {
258: assertEquals("endEndMethodOK3", theMessage);
259:
260: return true;
261: }
262:
263: return false;
264: }
265:
266: }
|