1 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 ; 36 import java.util.Enumeration ; 37 import java.util.Properties ; 38 import java.util.Vector ; 39 import java.util.Hashtable ; 40 41 public class BasicUnitTestManager implements UnitTestManager, ModuleControl 42 { 43 private Vector vectorOfTests; 44 private Hashtable 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 59 60 public BasicUnitTestManager() { 61 } 62 63 66 public void boot(boolean create, Properties startParams) 67 throws StandardException 68 { 69 boolean testStatus = true; 70 71 75 output = Monitor.getStream(); 76 77 contextService = ContextService.getFactory(); 78 79 this.currentOutput = output; 80 81 vectorOfTests = new Vector (); 82 namesOfTests = new Hashtable (); 83 84 findTests(startParams, startParams); 85 try { 86 findTests(System.getProperties(), startParams); 87 } catch (SecurityException 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 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 testName){ 112 113 if ( !namesOfTests.containsKey( testName ) ) 116 { 117 vectorOfTests.addElement(objectToTest); 118 namesOfTests.put( testName, testName ); 119 } 120 } 121 122 private void findTests(Properties testList, Properties startParams) { 123 124 if (testList == null) 125 return; 126 127 for (Enumeration e = testList.propertyNames(); e.hasMoreElements(); ) { 128 129 String key = (String ) e.nextElement(); 130 if (key.startsWith("derby.module.test.")) { 131 String unitTestClass = testList.getProperty(key); 132 133 try { 134 Object 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 160 private void emitAMessage(String message){ 161 162 currentOutput.printlnWithHeader(message); 163 } 164 165 private boolean runATest(UnitTest aTest){ 166 167 boolean result; 168 169 String thisTestName = aTest.getClass().getName(); 170 Date startTime = null, endTime; 171 172 ContextManager cm = null; 174 if (contextService != null) { 175 cm = contextService.newContextManager(); 176 contextService.setCurrentContextManager(cm); 177 } 178 179 if (performanceReportOn) 180 startTime = new Date (); 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 t) { 191 if (t instanceof ThreadDeath ) 192 { 193 t.printStackTrace(output.getPrintWriter()); 194 Runtime.getRuntime().exit(1); 195 } 196 197 result = false; 198 String 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 contextService.resetCurrentContextManager(cm); 210 } 211 } 212 213 if (performanceReportOn){ 214 endTime = new Date (); 215 emitAMessage("Test '" + thisTestName + "' took " + new Long (endTime.getTime() - startTime.getTime()) + " milliseconds."); 216 } 217 218 return result; 219 } 220 221 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 this.testType = testType; 269 this.testDuration = testDuration; 270 return runTests(); 271 } 272 273 274 public void setTestDuration(int testDuration){ 275 this.testDuration = testDuration; 277 return; 278 } 279 280 281 public void setTestType(int testType){ 282 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 |