net.sourceforge.jthreadunit
Class TestThread

java.lang.Object
  extended by java.lang.Thread
      extended by net.sourceforge.jthreadunit.TestThread
All Implemented Interfaces:
Runnable

public abstract class TestThread
extends Thread

A thread that runs in an endless loop performing actions. A controlling thread should call the various methods of this class to control the actions performed, and finally call kill() when there are no more actions to be performed. Calling Thread.interrupt() will not kill the thread, but merely reset it to the state of waiting for a command. That is, if the thread is blocked, Thread.interrupt() will unblock it and make it ready to respond to performAction(String).

Each action must be implemented in a subclass by a parameterless method with void return type named by prepending do to the capitalized action name. For example, performAction("foo") results in a call to the method with signature public void doFoo().


Nested Class Summary
 
Nested classes inherited from class java.lang.Thread
Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TestThread(ThreadGroup group, String name)
           
 
Method Summary
 void actionShouldBlock(String actionName)
          Called to indicate that this thread should initiate the named action but expect to block before completing it.
 void assertStillBlocked()
          Called to indicate that a previously blocked action should still be blocked.
static void checkpoint(String actionName)
          Called from arbitrary code to mark the current action as complete and wait for the next action without returning from the first action method.
 void completeBlockedAction()
          Called to indicate that this thread should finish a previously blocked action.
 void completeBlockedActionWithActions(String... actionNames)
          Complete one action and perform several more.
protected  void expectAction(String actionName)
          Called from within an action method to mark the current action as complete and wait for the next action without returning from the first action method.
 void kill()
          Kill this thread.
 void performAction(String actionName)
          Called to indicate that this thread should perform the named action completely without blocking.
 void performActions(String... actionNames)
          Perform several actions.
 void run()
           
 void start()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TestThread

public TestThread(ThreadGroup group,
                  String name)
Method Detail

performAction

public void performAction(String actionName)
                   throws InterruptedException
Called to indicate that this thread should perform the named action completely without blocking.

Throws:
AssertionFailedError - If this thread blocks before completing the action.
InterruptedException

performActions

public void performActions(String... actionNames)
                    throws InterruptedException
Perform several actions. Equivalent to calling performAction(String) once for each of the given actions. This is convenient when the code being tested contains checkpoint(String) calls.

Throws:
InterruptedException

actionShouldBlock

public void actionShouldBlock(String actionName)
                       throws InterruptedException
Called to indicate that this thread should initiate the named action but expect to block before completing it.

Throws:
AssertionFailedError - If this thread completes the action without blocking.
InterruptedException

completeBlockedAction

public void completeBlockedAction()
Called to indicate that this thread should finish a previously blocked action.

Throws:
AssertionFailedError - If this thread is still blocked without having completed the action.

completeBlockedActionWithActions

public void completeBlockedActionWithActions(String... actionNames)
                                      throws InterruptedException
Complete one action and perform several more. Equivalent to calling completeBlockedAction() once followed by calling performAction(String) once for each of the given actions. This is convenient when the code being tested contains checkpoint(String) calls.

Throws:
InterruptedException

assertStillBlocked

public void assertStillBlocked()
Called to indicate that a previously blocked action should still be blocked.

Throws:
AssertionFailedError - If this thread has completed the action without blocking.

expectAction

protected void expectAction(String actionName)
                     throws InterruptedException
Called from within an action method to mark the current action as complete and wait for the next action without returning from the first action method.

Throws:
InterruptedException

checkpoint

public static void checkpoint(String actionName)
                       throws InterruptedException
Called from arbitrary code to mark the current action as complete and wait for the next action without returning from the first action method. Equivalent to calling expectAction(String) directly from an action method, unless the current thread is not a TestThread, in which case does nothing.

Throws:
InterruptedException

kill

public void kill()
Kill this thread. Sets a flag and interrupts the thread, as an alternative to the deprecated Thread.stop().


start

public void start()
Overrides:
start in class Thread

run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread