com.google.testing.threadtester
Class TestThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.google.testing.threadtester.TestThread
All Implemented Interfaces:
Runnable

public class TestThread
extends Thread

Implementation of Thread that catches and stores any Exception thrown by the Thread.run() method.

Author:
alasdair.mackintosh@gmail.com (Alasdair Mackintosh)

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TestThread(Runnable target, String name)
          Creates a new TestThread that executes the given Runnable.
TestThread(String name)
          Creates a new TestThread with the given name.
TestThread(ThrowingRunnable target, String name)
          Creates a new TestThread that executes the given TestRunnable.
 
Method Summary
 void finish()
          Waits for this thread to finish.
 Throwable getException()
          Gets the Throwable thrown by the Thread.run() method, or null if there is no such exception.
 void throwExceptionsIfAny()
          Rethrows the Throwable thrown by the Thread.run() method, wrapped in a RuntimeException if necessary.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TestThread

public TestThread(Runnable target,
                  String name)
Creates a new TestThread that executes the given Runnable. Any RuntimeExceptions thrown when the runnable runs will be caught.


TestThread

public TestThread(ThrowingRunnable target,
                  String name)
Creates a new TestThread that executes the given TestRunnable. Any Exceptions thrown when the runnable's ThrowingRunnable.run() method is invoked will be caught.


TestThread

public TestThread(String name)
Creates a new TestThread with the given name.

Method Detail

finish

public void finish()
            throws InterruptedException,
                   TestTimeoutException
Waits for this thread to finish. Will not wait longer than Options.timeout. Throws an IllegalStateException if this thread has not terminated by the specified time.

Throws:
InterruptedException
TestTimeoutException

getException

public Throwable getException()
Gets the Throwable thrown by the Thread.run() method, or null if there is no such exception.


throwExceptionsIfAny

public void throwExceptionsIfAny()
Rethrows the Throwable thrown by the Thread.run() method, wrapped in a RuntimeException if necessary. If there was no exception thrown, does nothing.



Copyright © 2013. All Rights Reserved.