KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > harness > T_Generic


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.harness.T_Generic
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.harness;
23
24 import org.apache.derby.iapi.services.monitor.ModuleControl;
25 import org.apache.derby.iapi.services.monitor.Monitor;
26
27 import org.apache.derbyTesting.unitTests.harness.UnitTest;
28 import org.apache.derbyTesting.unitTests.harness.UnitTestConstants;
29 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.services.sanity.SanityManager;
33
34 import java.util.Properties JavaDoc;
35
36 /**
37     Abstract class which executes a unit test.
38
39     <P>To write a test, extend this class with a class which implements the two
40     abstract methods:
41 <UL>
42     <LI>runTests
43     <LI>setUp
44 </UL>
45     @see UnitTest
46     @see ModuleControl
47 */

48 public abstract class T_Generic implements UnitTest, ModuleControl
49 {
50     /**
51       The unqualified name for the module to test. This is set by the generic
52       code.
53       */

54     protected String JavaDoc shortModuleToTestName;
55
56     /**
57       The start parameters for your test. This is set by generic code.
58       */

59     protected Properties JavaDoc startParams;
60
61     /**
62       The HeaderPrintWriter for test output. This is set by the
63       generic code.
64       */

65     protected HeaderPrintWriter out;
66
67     protected T_Generic()
68     {
69     }
70
71     /*
72     ** Public methods of ModuleControl
73     */

74
75     /**
76       ModuleControl.start
77       
78       @see ModuleControl#boot
79       @exception StandardException Module cannot be started.
80       */

81     public void boot(boolean create, Properties JavaDoc startParams)
82          throws StandardException
83     {
84         shortModuleToTestName =
85             getModuleToTestProtocolName()
86             .substring(getModuleToTestProtocolName().lastIndexOf('.')+1);
87
88         this.startParams = startParams;
89     }
90
91     /**
92       ModuleControl.stop
93       
94       @see ModuleControl#stop
95       */

96     public void stop() {
97     }
98
99     /*
100     ** Public methods of UnitTest
101     */

102     /**
103       UnitTest.Execute
104       
105       @see UnitTest#Execute
106       */

107     public boolean Execute(HeaderPrintWriter out)
108     {
109         this.out = out;
110
111         String JavaDoc myClass = this.getClass().getName();
112         String JavaDoc testName = myClass.substring(myClass.lastIndexOf('.') + 1);
113
114         System.out.println("-- Unit Test " + testName + " starting");
115
116         try
117         {
118             runTests();
119         }
120         
121         catch (Throwable JavaDoc t)
122         {
123             
124             while (t != null) {
125                 FAIL(t.toString());
126                 t.printStackTrace(out.getPrintWriter());
127                 if (t instanceof StandardException) {
128                     t = ((StandardException) t).getNestedException();
129                     continue;
130                 }
131                 break;
132             }
133             return false;
134         }
135
136         System.out.println("-- Unit Test " + testName + " finished");
137
138         return true;
139     }
140
141     /**
142       UnitTest.UnitTestDuration
143       
144       @return UnitTestConstants.DURATION_MICRO
145       @see UnitTest#UnitTestDuration
146       @see UnitTestConstants
147       */

148     public int UnitTestDuration() {
149         return UnitTestConstants.DURATION_MICRO;
150     }
151
152     /**
153       UnitTest.UnitTestType
154       
155       @return UnitTestConstants.TYPE_COMMON
156       @see UnitTest#UnitTestType
157       @see UnitTestConstants
158       */

159     public int UnitTestType() {
160         return UnitTestConstants.TYPE_COMMON;
161     }
162
163     /**
164       Emit a message indicating why the test failed.
165
166       RESOLVE: Should this be localized?
167
168       @param msg the message.
169       @return false
170     */

171     protected boolean FAIL(String JavaDoc msg) {
172         out.println("[" + Thread.currentThread().getName() + "] FAIL - " + msg);
173         return false;
174     }
175
176     /**
177       Emit a message saying the test passed.
178       You may use this to emit messages indicating individual test cases
179       within a unit test passed.
180
181       <P>RESOLVE:Localize this.
182       @param test the test which passed.
183       @return true
184       */

185     protected boolean PASS(String JavaDoc testName) {
186         out.println("[" + Thread.currentThread().getName() + "] Pass - "+shortModuleToTestName +" " + testName);
187         return true;
188     }
189
190     /**
191         Emit a message during a unit test run, indent the message
192         to allow the PASS/FAIL messages to stand out.
193     */

194     public void REPORT(String JavaDoc msg) {
195         out.println("[" + Thread.currentThread().getName() + "] " + msg);
196     }
197
198     
199     /**
200       Abstract methods to implement for your test.
201       */

202     
203     /**
204       Run the test. The test should raise an exception if it
205       fails. runTests should return if the tests pass.
206
207       @exception Exception Test code throws these
208       */

209     protected abstract void runTests() throws Exception JavaDoc;
210
211     /**
212       Get the name of the protocol for the module to test.
213       This is the 'factory.MODULE' variable.
214       
215       'moduleName' to the name of the module to test.
216
217       @param testConfiguration the configuration for this test.
218       */

219     protected abstract String JavaDoc getModuleToTestProtocolName();
220 }
221
Popular Tags