001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-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 junit.framework.Test;
023: import junit.framework.TestCase;
024:
025: import org.apache.cactus.internal.client.ClientTestCaseCaller;
026: import org.apache.cactus.internal.configuration.ConfigurationInitializer;
027: import org.apache.cactus.internal.server.ServerTestCaseCaller;
028: import org.apache.cactus.internal.util.TestCaseImplementChecker;
029: import org.apache.cactus.spi.client.connector.ProtocolHandler;
030:
031: /**
032: * Base class for all Cactus test case extensions.
033: *
034: * Note: We must not add any method that can be called by the end user to this
035: * class as users will those methods and it will create a runtime dependency to
036: * this class. We will then have to break binary compatibility if we wish to
037: * move this class around or change its implementation.
038: *
039: * @version $Id: AbstractCactusTestCase.java 238991 2004-05-22 11:34:50Z vmassol $
040: * @since 1.6
041: */
042: public abstract class AbstractCactusTestCase extends TestCase {
043: /**
044: * As this class is the first one loaded on the client side, we ensure
045: * that the Cactus configuration has been initialized. In the future,
046: * this block will be removed as all initialization will be done in Cactus
047: * test suites. However, as we still support using Cactus TestCase classes
048: * we don't have a proper initialization hook and thus we need this hack.
049: */
050: static {
051: ConfigurationInitializer.initialize();
052: }
053:
054: /**
055: * Provides all client side Cactus calling logic.
056: * Note that we are using a delegate class instead of inheritance in order
057: * to hide non public API to the users and thus to be able to easily change
058: * the implementation.
059: */
060: private ClientTestCaseCaller clientCaller;
061:
062: /**
063: * Provides all server side Cactus calling logic.
064: * Note that we are using a delegate class instead of inheritance in order
065: * to hide non public API to the users and thus to be able to easily change
066: * the implementation.
067: */
068: private ServerTestCaseCaller serverCaller;
069:
070: // Abstract methods -----------------------------------------------------
071:
072: /**
073: * Create a protocol handler instance that will be used to connect to the
074: * server side.
075: *
076: * @return the protocol handler instance
077: */
078: protected abstract ProtocolHandler createProtocolHandler();
079:
080: // Constructors ---------------------------------------------------------
081:
082: /**
083: * Default constructor defined in order to allow creating Test Case
084: * without needing to define constructor (new feature in JUnit 3.8.1).
085: * Should only be used with JUnit 3.8.1 or greater.
086: */
087: public AbstractCactusTestCase() {
088: init(null);
089: }
090:
091: /**
092: * Constructs a JUnit test case with the given name.
093: *
094: * @param theName the name of the test case
095: */
096: public AbstractCactusTestCase(String theName) {
097: super (theName);
098: init(null);
099: }
100:
101: /**
102: * Wraps a pure JUnit Test Case in a Cactus Test Case.
103: *
104: * @param theName the name of the test
105: * @param theTest the Test Case class to wrap
106: */
107: public AbstractCactusTestCase(String theName, Test theTest) {
108: super (theName);
109: init(theTest);
110: }
111:
112: // Public methods -------------------------------------------------------
113:
114: /**
115: * JUnit method that is used to run the tests. However, we're intercepting
116: * it so that we can call the server side of Cactus where the tests will
117: * be run (instead of on the client side).
118: *
119: * @exception Throwable if any exception is thrown during the test. Any
120: * exception will be displayed by the JUnit Test Runner
121: */
122: public void runBare() throws Throwable {
123: TestCaseImplementChecker.checkTestName(this );
124: TestCaseImplementChecker.checkTestName(getServerCaller()
125: .getWrappedTest());
126:
127: runBareClient();
128: }
129:
130: /**
131: * @see CactusTestCase#runBareServer()
132: */
133: public void runBareServer() throws Throwable {
134: getServerCaller().runBareInit();
135:
136: // Note: We cannot delegate this piece of code in the
137: // ServerTestCaseDelegate class as it requires to call
138: // super.runBare()
139:
140: if (getServerCaller().getWrappedTest() != null) {
141: ((TestCase) getServerCaller().getWrappedTest()).runBare();
142: } else {
143: super .runBare();
144: }
145: }
146:
147: // Private methods ------------------------------------------------------
148:
149: /**
150: * @param theCaller the client test case calling class
151: */
152: private void setClientCaller(ClientTestCaseCaller theCaller) {
153: this .clientCaller = theCaller;
154: }
155:
156: /**
157: * @param theCaller the server test case calling class
158: */
159: private void setServerCaller(ServerTestCaseCaller theCaller) {
160: this .serverCaller = theCaller;
161: }
162:
163: /**
164: * @return the client test case caller
165: */
166: private ClientTestCaseCaller getClientCaller() {
167: return this .clientCaller;
168: }
169:
170: /**
171: * @return the server test case caller
172: */
173: private ServerTestCaseCaller getServerCaller() {
174: return this .serverCaller;
175: }
176:
177: /**
178: * Initializations common to all constructors.
179: *
180: * @param theTest a pure JUnit Test that Cactus will wrap
181: */
182: private void init(Test theTest) {
183: setClientCaller(new ClientTestCaseCaller(this , theTest,
184: createProtocolHandler()));
185: setServerCaller(new ServerTestCaseCaller(this , theTest));
186: }
187:
188: /**
189: * Introduced for symmetry with {@link #runBareServer()}.
190: *
191: * @see #runBare()
192: */
193: private void runBareClient() throws Throwable {
194: getClientCaller().runBareInit();
195:
196: // Catch the exception just to have a chance to log it
197: try {
198: getClientCaller().runTest();
199: } catch (Throwable t) {
200: // TODO: Move getLogger to this class instead
201: getClientCaller().getLogger().debug("Exception in test", t);
202: throw t;
203: }
204: }
205: }
|