KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.harness.BasicUnitTestManager
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.context.ContextManager;
25 import org.apache.derby.iapi.services.context.ContextService;
26 import org.apache.derby.iapi.services.monitor.ModuleControl;
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.services.monitor.Monitor;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
31 import org.apache.derbyTesting.unitTests.harness.UnitTest;
32 import org.apache.derbyTesting.unitTests.harness.UnitTestConstants;
33 import org.apache.derbyTesting.unitTests.harness.UnitTestManager;
34 import org.apache.derbyTesting.unitTests.harness.T_Bomb;
35 import java.util.Date JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.Vector JavaDoc;
39 import java.util.Hashtable JavaDoc;
40
41 public class BasicUnitTestManager implements UnitTestManager, ModuleControl
42 {
43     private Vector JavaDoc vectorOfTests;
44     private Hashtable JavaDoc namesOfTests;
45
46     private static boolean alreadyRun = false;
47     private HeaderPrintWriter output;
48     private HeaderPrintWriter currentOutput;
49     private int testType = UnitTestConstants.TYPE_COMPLETE;
50     private int testDuration = UnitTestConstants.DURATION_FOREVER;
51     private boolean reportOutputOn = true;
52     private boolean performanceReportOn = false;
53     private ContextService contextService;
54     private boolean runForever = false;
55
56     /*
57     ** Constructor
58     */

59
60     public BasicUnitTestManager() {
61     }
62
63     /*
64     ** Methods of ModuleControl
65     */

66     public void boot(boolean create, Properties JavaDoc startParams)
67          throws StandardException
68     {
69         boolean testStatus = true;
70
71         // startParams should define output, for now
72
// use the sytem trace stream. If that doesn't exist
73
// then use a null stream.
74

75         output = Monitor.getStream();
76
77         contextService = ContextService.getFactory();
78
79         this.currentOutput = output;
80
81         vectorOfTests = new Vector JavaDoc();
82         namesOfTests = new Hashtable JavaDoc();
83
84         findTests(startParams, startParams);
85         try {
86             findTests(System.getProperties(), startParams);
87         } catch (SecurityException JavaDoc se) {
88         }
89         findTests(Monitor.getMonitor().getApplicationProperties(), startParams);
90
91         if ( !alreadyRun )
92         {
93             testStatus = runTests();
94             alreadyRun = true;
95         }
96
97         if (!testStatus) {
98
99             // try to print out that the shutdown is occurring.
100
System.out.println("Shutting down due to unit test failure.");
101             output.printlnWithHeader("Shutting down due to unit test failure, see log for more information.");
102
103             Monitor.getMonitor().shutdown();
104         }
105     }
106
107     public void stop(){
108         return;
109     }
110
111     public synchronized void registerTest(UnitTest objectToTest, String JavaDoc testName){
112
113         // only add the new test if it isn't already there.
114
// otherwise you will upset me.
115
if ( !namesOfTests.containsKey( testName ) )
116         {
117             vectorOfTests.addElement(objectToTest);
118             namesOfTests.put( testName, testName );
119         }
120     }
121
122     private void findTests(Properties JavaDoc testList, Properties JavaDoc startParams) {
123
124         if (testList == null)
125             return;
126
127         for (Enumeration JavaDoc e = testList.propertyNames(); e.hasMoreElements(); ) {
128
129             String JavaDoc key = (String JavaDoc) e.nextElement();
130             if (key.startsWith("derby.module.test.")) {
131                 String JavaDoc unitTestClass = testList.getProperty(key);
132
133                 try {
134                     Object JavaDoc unitTest =
135                         Monitor.bootServiceModule(false, this, unitTestClass,
136                                                   startParams);
137                     if (unitTest instanceof UnitTest) {
138                         registerTest((UnitTest) unitTest, unitTestClass);
139                     } else if (unitTest != null) {
140                     System.out.println("class does not implement UnitTest " +
141                                        unitTestClass);
142                     }
143                 } catch (StandardException se) {
144                     System.out.println("exception booting " + unitTestClass);
145                     System.out.println(se.toString());
146                     se.printStackTrace(System.out);
147                 }
148             }
149         }
150     }
151
152     /**
153      * emitAMessage
154      *
155      * Convenience routine to emit messages. This routine only works
156      * for messages provided by this package.
157      *
158      * @see UnitTestConstants for supported durations.
159      **/

160     private void emitAMessage(String JavaDoc message){
161
162        currentOutput.printlnWithHeader(message);
163     }
164
165     private boolean runATest(UnitTest aTest){
166
167         boolean result;
168
169         String JavaDoc thisTestName = aTest.getClass().getName();
170         Date JavaDoc startTime = null, endTime;
171
172         // push a new context manager
173
ContextManager cm = null;
174         if (contextService != null) {
175             cm = contextService.newContextManager();
176             contextService.setCurrentContextManager(cm);
177         }
178
179         if (performanceReportOn)
180             startTime = new Date JavaDoc();
181
182         try{
183             emitAMessage("Starting test '" + thisTestName + "'.");
184             result = aTest.Execute(currentOutput);
185             if (result == true)
186                 emitAMessage("Test '" + thisTestName + "' passed");
187             else
188                 emitAMessage("Test '" + thisTestName + "' failed");
189
190         } catch (Throwable JavaDoc t) {
191             if (t instanceof ThreadDeath JavaDoc)
192             {
193                 t.printStackTrace(output.getPrintWriter());
194                 Runtime.getRuntime().exit(1);
195             }
196
197             result = false;
198             String JavaDoc msg = t.getMessage();
199             if (msg == null) msg = t.getClass().getName();
200             emitAMessage("Test '" + thisTestName + "' failed with exception '" + msg +"'.");
201             t.printStackTrace(output.getPrintWriter());
202         } finally {
203
204             if (contextService != null) {
205                 //
206
//Assure the completed test does not stick around
207
//cm.cleanupOnError
208
// (BasicUnitTestDatabaseException.cleanUp());
209
contextService.resetCurrentContextManager(cm);
210             }
211         }
212
213         if (performanceReportOn){
214             endTime = new Date JavaDoc();
215             emitAMessage("Test '" + thisTestName + "' took " + new Long JavaDoc(endTime.getTime() - startTime.getTime()) + " milliseconds.");
216         }
217
218         return result;
219     }
220
221     // STUB: Verify its ok this is synchronized.
222
public synchronized boolean runTests(){
223
224         boolean result = true;
225         int passCount = 0;
226         int failCount = 0;
227         int skipCount = 0;
228         boolean runTests = true;
229
230         if (SanityManager.DEBUG)
231         {
232             runTests =
233                 !SanityManager.DEBUG_ON(UnitTestManager.SKIP_UNIT_TESTS);
234             runForever =
235                 SanityManager.DEBUG_ON(UnitTestManager.RUN_FOREVER);
236         }
237         if (runTests) {
238
239         if (!runForever) T_Bomb.makeBomb();
240         for(int ix = vectorOfTests.size() - 1; ix >= 0 ; ix--){
241
242             UnitTest thisTest =
243                 ((UnitTest)vectorOfTests.elementAt(ix));
244             if (thisTest.UnitTestDuration() <= this.testDuration &&
245                 thisTest.UnitTestType() <= this.testType){
246                 if (runATest(thisTest))
247                     passCount++;
248                 else
249                     failCount++;
250                 vectorOfTests.removeElementAt(ix);
251             }
252             else{
253                 skipCount++;
254             }
255         }
256         emitAMessage("Test Summary - Run " + (passCount+failCount) +
257             ", Passed " + passCount + ", Failed " + failCount + ", Skipped " + skipCount + ".");
258         }
259         else {
260             emitAMessage("Tests not run.");
261         }
262         return (failCount == 0);
263     }
264
265
266     public boolean runTests(int testType, int testDuration){
267         //STUB: Sanity check for type/duration
268
this.testType = testType;
269         this.testDuration = testDuration;
270         return runTests();
271     }
272
273
274     public void setTestDuration(int testDuration){
275         //STUB: Sanity check for type/duration
276
this.testDuration = testDuration;
277         return;
278     }
279
280
281     public void setTestType(int testType){
282         //STUB: Sanity check for type/duration
283
this.testType = testType;
284         return;
285     }
286
287
288     public void setPerformanceReportOn(boolean performanceReportOn){
289         this.performanceReportOn = performanceReportOn;
290         return;
291     }
292 }
293
294
Popular Tags