1 30 31 package net.sourceforge.groboutils.junit.v1; 32 33 import org.apache.log4j.Logger; 34 import junit.framework.TestCase; 35 import junit.framework.TestResult; 36 import junit.framework.AssertionFailedError; 37 import junit.framework.Assert; 38 39 40 74 public abstract class TestRunnable extends Assert 75 implements Runnable 76 { 77 private static final Class THIS_CLASS = TestRunnable.class; 78 protected static final Logger LOG = Logger.getLogger( THIS_CLASS ); 79 private static int testCount = 0; 80 81 private MultiThreadedTestRunner mttr; 82 private int testIndex; 83 private boolean ignoreStopErrors = false; 84 85 86 public TestRunnable() 87 { 88 synchronized( THIS_CLASS ) 89 { 90 this.testIndex = testCount++; 91 } 92 } 93 94 95 TestRunnable( boolean ignoreStopErrors ) 96 { 97 this(); 98 this.ignoreStopErrors = ignoreStopErrors; 99 } 100 101 102 103 125 public abstract void runTest() throws Throwable ; 126 127 133 public void delay( long millis ) throws InterruptedException 134 { 135 Thread.sleep( millis ); 136 } 137 138 142 public void run() 143 { 144 if (this.mttr == null) 145 { 146 throw new IllegalStateException ( 147 "Owning runner never defined. The runnables should only be "+ 148 "started through the MultiThreadedTestRunner instance." ); 149 } 150 151 LOG.info( "Starting test thread "+this.testIndex ); 152 try 153 { 154 runTest(); 155 } 156 catch (InterruptedException ie) 157 { 158 } 161 catch (MultiThreadedTestRunner.TestDeathException tde) 162 { 163 if (!this.ignoreStopErrors) 167 { 168 LOG.info( "Aborted test thread "+this.testIndex ); 169 throw tde; 170 } 171 } 172 catch (Throwable t) 173 { 174 177 this.mttr.handleException( t ); 180 } 181 LOG.info( "Ended test thread "+this.testIndex ); 182 } 183 184 185 194 public boolean isDone() 195 { 196 return this.mttr.areThreadsFinished(); 197 } 198 199 200 void setTestRunner( MultiThreadedTestRunner mttr ) 201 { 202 this.mttr = mttr; 203 } 204 } 205 206 | Popular Tags |