junit.extensions.jfcunit
Class JFCTestCase

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended byjunit.extensions.jfcunit.JFCTestCase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
JFCXMLTestCase, RobotTest

public class JFCTestCase
extends junit.framework.TestCase

Extend this class to create new tests for Swing based interfaces. This is a subclass of TestCase, and therefore provides all of the facilities that you would normally expect from TestCase. An important point to realize about coding with Swing classes is that most methods are single threaded. This means that once a component has been shown, its methods should normally only be accessed by the AWT thread. While JFCTestCase runs its tests, the AWT thread is temporarily blocked to prevent multi-threading issues. The effect of this is that any method calls that you make on Swing components during a test will have no impact on the GUI, until the AWT Thread is restarted. This can occur in one of two ways: (1) The test runs to completion (2) The method "awtSleep" defined within this class is called.

Author:
Matt Caswell, Vijay Aravamudhan : ThoughtWorks Inc., Kevin Wilson

Nested Class Summary
(package private)  class JFCTestCase.JFCSecurityManager
          Security manager which proxies all calls to the original security manager.
 
Field Summary
private static long DEFAULTLOCKWAIT
          Time to wait between lock attempts.
private static long DEFAULTSLEEP
          Default time to sleep.
private  boolean m_assertExit
          Assert Exit was called.
private  boolean m_cont
          Continuation flag.
private  java.lang.Throwable m_err
          Exception thrown by testcase.
private  boolean m_exited
          True if exited.
private  boolean m_forcedWait
          Forcibly wait for full time.
private  TestHelper m_helper
          The test helper to be used in testing.
private  java.lang.Object m_lock
          Lock instance.
private  long m_lockWait
          Lock Wait time.
private  boolean m_pausingAWT
          Run the AWTThread free from interruption.
private  boolean m_runBare
          True if runBare has begun.
private  long m_sleepTime
          Time to sleep.
private  boolean m_waiting
          Waiting flag.
 
Fields inherited from class junit.framework.TestCase
 
Constructor Summary
protected JFCTestCase()
          Constructs a new JFCTestCase (default Constructor if using JUnit ver 3.8).
  JFCTestCase(java.lang.String name)
          Constructs a new JFCTestCase.
 
Method Summary
private  void acquire()
          Acquire AWT Thread Control.
 void awtSleep()
          Suspends the test for up to a maximum period of time, and allows the AWT Thread to resume temporarily.
 void awtSleep(long sleepTime)
          Suspends the test for up to the specified (maximum) period of time, and allows the AWT Thread to resume temporarily.
protected  java.lang.SecurityManager createNoExitSecurityManager()
          This method creates a replacement for the default system manager.
 void flushAWT()
          Flush all events currently in the AWTEventQueue.
 boolean getAssertExit()
          Get the current value of the assertExit property.
private  boolean getContinue()
          Returns the cont.
protected  java.lang.Throwable getError()
          Returns the error.
 TestHelper getHelper()
          Get the test helper.
protected  long getLockWait()
          Get the current duration between checking locks.
private  long getSleepTime()
          Returns the value of sleepTime.
protected  boolean hasError()
          Checks of the current test case has any errors.
 boolean isAWTRunning()
          Get the AWT state.
protected  void pause()
          Pause the test until the Option Pane is acknowleged.
 void pauseAWT()
          Pause the awt event queue until it is released by releaseAWT() or the end of the test is reached.
private  void release()
          Release the AWT Thread Control.
protected  void resetError()
          Resets the err to the default value (null).
protected  void resetForcedWait()
          Resets the forcedWait to the default value (false).
 void resetSleepTime()
          Resets the sleepTime to the default value (DEFAULTSLEEP).
 void resumeAWT()
          Resume the awt event queue.
 void runBare()
          Sets up, executes and then tears down a test.
protected  void runCode(java.lang.Runnable code)
          Run the code of the test case.
protected  void runTest()
          Executes a test.
 void setAssertExit(boolean aValue)
          Set the value of the assert exit property.
private  void setContinue(boolean c)
          Sets the cont.
protected  void setError(java.lang.Throwable error)
          Sets the err.
protected  void setForcedWait(boolean forced)
          Sets the forcedWait.
 void setHelper(TestHelper helper)
          Sets the helper.
protected  void setLockWait(long duration)
          Set the duration between checking the lock.
 void setSleepTime(long time)
          Sets the sleepTime.
protected  void setUp()
          Default setUp which does nothing.
 void sleep(long delay)
          Suspends the test for a period of time, and allows the AWT Thread to resume during this period if it has been paused.
protected  void tearDown()
          Default tearDown which does nothing.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULTLOCKWAIT

private static final long DEFAULTLOCKWAIT
Time to wait between lock attempts.

See Also:
Constant Field Values

DEFAULTSLEEP

private static final long DEFAULTSLEEP
Default time to sleep.

See Also:
Constant Field Values

m_lock

private java.lang.Object m_lock
Lock instance.


m_err

private java.lang.Throwable m_err
Exception thrown by testcase.


m_assertExit

private transient boolean m_assertExit
Assert Exit was called.


m_helper

private TestHelper m_helper
The test helper to be used in testing. This has been made protected, since most users already have a super class in their test hierarchy where it is protected. Make sure to actually instantiate this in your setUp() method or at the beginning of the test methods.


m_cont

private boolean m_cont
Continuation flag.


m_exited

private boolean m_exited
True if exited.


m_runBare

private boolean m_runBare
True if runBare has begun. Used when setting the assertExit.


m_forcedWait

private boolean m_forcedWait
Forcibly wait for full time.


m_pausingAWT

private volatile boolean m_pausingAWT
Run the AWTThread free from interruption.


m_waiting

private volatile boolean m_waiting
Waiting flag.


m_lockWait

private long m_lockWait
Lock Wait time.


m_sleepTime

private long m_sleepTime
Time to sleep.

Constructor Detail

JFCTestCase

protected JFCTestCase()
Constructs a new JFCTestCase (default Constructor if using JUnit ver 3.8).


JFCTestCase

public JFCTestCase(java.lang.String name)
Constructs a new JFCTestCase.

Parameters:
name - The name of the test.
Method Detail

setAssertExit

public final void setAssertExit(boolean aValue)
Set the value of the assert exit property. When true the System.exit() call will throw an junit.extensions.jfcunit.ExitException to interrupt the exit process, the application will not exit. The test case will assert that system exit was called.

Parameters:
aValue - true if System.exit() should be asserted, to allow the test case to complete.

getAssertExit

public final boolean getAssertExit()
Get the current value of the assertExit property. see setAssertExit for a details description of the operation.

Returns:
true if System.exit() should be asserted. to allow the test case to complete.

setHelper

public final void setHelper(TestHelper helper)
Sets the helper.

Parameters:
helper - TestHelper to be used.

getHelper

public final TestHelper getHelper()
Get the test helper.

Returns:
The test helper.

setSleepTime

public final void setSleepTime(long time)
Sets the sleepTime.

Parameters:
time - New value for sleepTime

awtSleep

public final void awtSleep()
Suspends the test for up to a maximum period of time, and allows the AWT Thread to resume temporarily. If the AWT Thread has not been paused, this api has no effect. The time period is what was set by the user by calling 'setSleepTime()' or 'awtSleep(sleepTime)'. If the notify occurs before this time has elapsed, the thread will continue.


awtSleep

public final void awtSleep(long sleepTime)
Suspends the test for up to the specified (maximum) period of time, and allows the AWT Thread to resume temporarily. If the AWT Thread has not been paused, this api has no effect. Note that the sleep time for subsequent calls to awtSleep() will use the time specified in the current call. If the notify occurs before this time has elapsed, the thread will continue.

Parameters:
sleepTime - The number of ms to sleep

flushAWT

public final void flushAWT()
Flush all events currently in the AWTEventQueue. The event queue is left running afterwards.


pauseAWT

public final void pauseAWT()
Pause the awt event queue until it is released by releaseAWT() or the end of the test is reached.


resetSleepTime

public final void resetSleepTime()
Resets the sleepTime to the default value (DEFAULTSLEEP).


resumeAWT

public final void resumeAWT()
Resume the awt event queue.


sleep

public final void sleep(long delay)
Suspends the test for a period of time, and allows the AWT Thread to resume during this period if it has been paused.

Parameters:
delay - The minimum amount of time the test case thread should be delayed.

isAWTRunning

public boolean isAWTRunning()
Get the AWT state.

Returns:
boolean specifying whether the AWT Thread is still running or not.

runBare

public void runBare()
             throws java.lang.Throwable
Sets up, executes and then tears down a test.

Throws:
java.lang.Throwable - exceptions thrown by code.

setError

protected final void setError(java.lang.Throwable error)
Sets the err.

Parameters:
error - New value for err.

getError

protected final java.lang.Throwable getError()
Returns the error.

Returns:
error.

setForcedWait

protected final void setForcedWait(boolean forced)
Sets the forcedWait.

Parameters:
forced - New value for forcedWait.

setLockWait

protected final void setLockWait(long duration)
Set the duration between checking the lock.

Parameters:
duration - in milliseconds. A value less than or equal to zero will reset the lockwait to the default value.

getLockWait

protected final long getLockWait()
Get the current duration between checking locks.

Returns:
duration in milliseconds.

setUp

protected void setUp()
              throws java.lang.Exception
Default setUp which does nothing. Override this to set up the environment for your test.

Throws:
java.lang.Exception - An instance of java.lang.Exception can be thrown

createNoExitSecurityManager

protected final java.lang.SecurityManager createNoExitSecurityManager()
This method creates a replacement for the default system manager. The goal is to intercept System.exit calls and make it throw an exception instead so that a System.exit in a task does not fully terminate the test run.

Returns:
An instance of a SecurityManager that will "consume" a call to System.exit()

hasError

protected final boolean hasError()
Checks of the current test case has any errors.

Returns:
true if the current test case has any errors.

resetError

protected final void resetError()
Resets the err to the default value (null).


resetForcedWait

protected final void resetForcedWait()
Resets the forcedWait to the default value (false).


runCode

protected void runCode(java.lang.Runnable code)
                throws java.lang.Throwable
Run the code of the test case. Note: This method will wait till the thread running code.run() completes The above behavior is overridden if the setForcedWait() has been called with 'true' Then the behavior is that the thread will wait for the time specified when awtSleep() was called

Parameters:
code - Code which is to be executed.
Throws:
java.lang.Throwable - exceptions thrown by code.

runTest

protected void runTest()
                throws java.lang.Throwable
Executes a test.

Throws:
java.lang.Throwable - exceptions thrown by code.

tearDown

protected void tearDown()
                 throws java.lang.Exception
Default tearDown which does nothing. Override this to tear down the environment for your test.

Throws:
java.lang.Exception - An instance of java.lang.Exception can be thrown

setContinue

private void setContinue(boolean c)
Sets the cont.

Parameters:
c - New value for cont.

getContinue

private boolean getContinue()
Returns the cont.

Returns:
cont.

getSleepTime

private long getSleepTime()
Returns the value of sleepTime.

Returns:
sleepTime.

acquire

private void acquire()
              throws java.lang.InterruptedException
Acquire AWT Thread Control.

Throws:
java.lang.InterruptedException - Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

release

private void release()
Release the AWT Thread Control.


pause

protected void pause()
Pause the test until the Option Pane is acknowleged.



Licensed under: GNU Lesser General Public License, Ver 2.1, February 1999