KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-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 junit.framework.Test;
23 import junit.framework.TestCase;
24
25 import org.apache.cactus.internal.client.ClientTestCaseCaller;
26 import org.apache.cactus.internal.configuration.ConfigurationInitializer;
27 import org.apache.cactus.internal.server.ServerTestCaseCaller;
28 import org.apache.cactus.internal.util.TestCaseImplementChecker;
29 import org.apache.cactus.spi.client.connector.ProtocolHandler;
30
31 /**
32  * Base class for all Cactus test case extensions.
33  *
34  * Note: We must not add any method that can be called by the end user to this
35  * class as users will those methods and it will create a runtime dependency to
36  * this class. We will then have to break binary compatibility if we wish to
37  * move this class around or change its implementation.
38  *
39  * @version $Id: AbstractCactusTestCase.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
40  * @since 1.6
41  */

42 public abstract class AbstractCactusTestCase extends TestCase
43 {
44     /**
45      * As this class is the first one loaded on the client side, we ensure
46      * that the Cactus configuration has been initialized. In the future,
47      * this block will be removed as all initialization will be done in Cactus
48      * test suites. However, as we still support using Cactus TestCase classes
49      * we don't have a proper initialization hook and thus we need this hack.
50      */

51     static
52     {
53         ConfigurationInitializer.initialize();
54     }
55
56     /**
57      * Provides all client side Cactus calling logic.
58      * Note that we are using a delegate class instead of inheritance in order
59      * to hide non public API to the users and thus to be able to easily change
60      * the implementation.
61      */

62     private ClientTestCaseCaller clientCaller;
63
64     /**
65      * Provides all server side Cactus calling logic.
66      * Note that we are using a delegate class instead of inheritance in order
67      * to hide non public API to the users and thus to be able to easily change
68      * the implementation.
69      */

70     private ServerTestCaseCaller serverCaller;
71
72     // Abstract methods -----------------------------------------------------
73

74     /**
75      * Create a protocol handler instance that will be used to connect to the
76      * server side.
77      *
78      * @return the protocol handler instance
79      */

80     protected abstract ProtocolHandler createProtocolHandler();
81     
82     // Constructors ---------------------------------------------------------
83

84     /**
85      * Default constructor defined in order to allow creating Test Case
86      * without needing to define constructor (new feature in JUnit 3.8.1).
87      * Should only be used with JUnit 3.8.1 or greater.
88      */

89     public AbstractCactusTestCase()
90     {
91         init(null);
92     }
93
94     /**
95      * Constructs a JUnit test case with the given name.
96      *
97      * @param theName the name of the test case
98      */

99     public AbstractCactusTestCase(String JavaDoc theName)
100     {
101         super(theName);
102         init(null);
103     }
104
105     /**
106      * Wraps a pure JUnit Test Case in a Cactus Test Case.
107      *
108      * @param theName the name of the test
109      * @param theTest the Test Case class to wrap
110      */

111     public AbstractCactusTestCase(String JavaDoc theName, Test theTest)
112     {
113         super(theName);
114         init(theTest);
115     }
116     
117     // Public methods -------------------------------------------------------
118

119     /**
120      * JUnit method that is used to run the tests. However, we're intercepting
121      * it so that we can call the server side of Cactus where the tests will
122      * be run (instead of on the client side).
123      *
124      * @exception Throwable if any exception is thrown during the test. Any
125      * exception will be displayed by the JUnit Test Runner
126      */

127     public void runBare() throws Throwable JavaDoc
128     {
129         TestCaseImplementChecker.checkTestName(this);
130         TestCaseImplementChecker.checkTestName(
131             getServerCaller().getWrappedTest());
132
133         runBareClient();
134     }
135
136     /**
137      * @see CactusTestCase#runBareServer()
138      */

139     public void runBareServer() throws Throwable JavaDoc
140     {
141         getServerCaller().runBareInit();
142
143         // Note: We cannot delegate this piece of code in the
144
// ServerTestCaseDelegate class as it requires to call
145
// super.runBare()
146

147         if (getServerCaller().getWrappedTest() != null)
148         {
149             ((TestCase) getServerCaller().getWrappedTest()).runBare();
150         }
151         else
152         {
153             super.runBare();
154         }
155     }
156     
157     // Private methods ------------------------------------------------------
158

159     /**
160      * @param theCaller the client test case calling class
161      */

162     private void setClientCaller(ClientTestCaseCaller theCaller)
163     {
164         this.clientCaller = theCaller;
165     }
166
167     /**
168      * @param theCaller the server test case calling class
169      */

170     private void setServerCaller(ServerTestCaseCaller theCaller)
171     {
172         this.serverCaller = theCaller;
173     }
174
175     /**
176      * @return the client test case caller
177      */

178     private ClientTestCaseCaller getClientCaller()
179     {
180         return this.clientCaller;
181     }
182
183     /**
184      * @return the server test case caller
185      */

186     private ServerTestCaseCaller getServerCaller()
187     {
188         return this.serverCaller;
189     }
190     
191     /**
192      * Initializations common to all constructors.
193      *
194      * @param theTest a pure JUnit Test that Cactus will wrap
195      */

196     private void init(Test theTest)
197     {
198         setClientCaller(new ClientTestCaseCaller(this, theTest,
199             createProtocolHandler()));
200         setServerCaller(new ServerTestCaseCaller(this, theTest));
201     }
202
203     /**
204      * Introduced for symmetry with {@link #runBareServer()}.
205      *
206      * @see #runBare()
207      */

208     private void runBareClient() throws Throwable JavaDoc
209     {
210         getClientCaller().runBareInit();
211
212         // Catch the exception just to have a chance to log it
213
try
214         {
215             getClientCaller().runTest();
216         }
217         catch (Throwable JavaDoc t)
218         {
219             // TODO: Move getLogger to this class instead
220
getClientCaller().getLogger().debug("Exception in test", t);
221             throw t;
222         }
223     }
224 }
225
Popular Tags