com.clarkware.junitperf

Class TimedTest


public class TimedTest
extends TestDecorator

The TimedTest is a test decorator that runs a test and measures the elapsed time of the test.

A TimedTest is constructed with a specified maximum elapsed time. By default, a TimedTest will wait for the completion of its decorated test and then fail if the maximum elapsed time was exceeded. Alternatively, a TimedTest can be constructed to immediately signal a failure when the maximum elapsed time of its decorated test is exceeded. In other words, the TimedTest will not wait for its decorated test to run to completion if the maximum elapsed time is exceeded.

For example, to decorate the ExampleTest as a TimedTest that waits for the ExampleTest test case to run to completion and then fails if the maximum elapsed time of 2 seconds is exceeded, use:

 Test timedTest = new TimedTest(new TestSuite(ExampleTest.class), 2000);
 
or, to time a single test method, use:
 Test timedTest = new TimedTest(new ExampleTest("testSomething"), 2000);
 

Alternatively, to decorate the ExampleTest.testSomething() test as a TimedTest that fails immediately when the maximum elapsed time of 2 seconds is exceeded, use:

 Test timedTest = new TimedTest(new ExampleTest("testSomething"), 2000, false);
 
Authors:
Mike Clark
Clarkware Consulting, Inc.
Ervin Varga

Constructor Summary

TimedTest(Test test, long maxElapsedTime)
Constructs a TimedTest to decorate the specified test with the specified maximum elapsed time.
TimedTest(Test test, long maxElapsedTime, boolean waitForCompletion)
Constructs a TimedTest to decorate the specified test with the specified maximum elapsed time.

Method Summary

int
countTestCases()
Returns the number of tests in this timed test.
protected long
getElapsedTime(long beginTime)
boolean
outOfTime()
Determines whether the maximum elapsed time of the test was exceeded.
protected void
printElapsedTime(long elapsedTime)
void
run(TestResult result)
Runs the test.
protected void
runUntilTestCompletion(TestResult result)
Runs the test until test completion and then signals a failure if the maximum elapsed time was exceeded.
protected void
runUntilTimeExpires(TestResult result)
Runs the test and immediately signals a failure when the maximum elapsed time is exceeded.
void
setQuiet()
Disables the output of the test's elapsed time.
String
toString()
Returns the test description.

Constructor Details

TimedTest

public TimedTest(Test test,
                 long maxElapsedTime)
Constructs a TimedTest to decorate the specified test with the specified maximum elapsed time.

The TimedTest will wait for the completion of its decorated test and then fail if the maximum elapsed time was exceeded.

Parameters:
test - Test to decorate.
maxElapsedTime - Maximum elapsed time (ms).

TimedTest

public TimedTest(Test test,
                 long maxElapsedTime,
                 boolean waitForCompletion)
Constructs a TimedTest to decorate the specified test with the specified maximum elapsed time.
Parameters:
test - Test to decorate.
maxElapsedTime - Maximum elapsed time (ms).
waitForCompletion - true (default) to indicate that the TimedTest should wait for its decorated test to run to completion and then fail if the maximum elapsed time was exceeded; false to indicate that the TimedTest should immediately signal a failure when the maximum elapsed time is exceeded.

Method Details

countTestCases

public int countTestCases()
Returns the number of tests in this timed test.
Returns:
Number of tests.

getElapsedTime

protected long getElapsedTime(long beginTime)

outOfTime

public boolean outOfTime()
Determines whether the maximum elapsed time of the test was exceeded.
Returns:
true if the max elapsed time was exceeded; false otherwise.

printElapsedTime

protected void printElapsedTime(long elapsedTime)

run

public void run(TestResult result)
Runs the test.
Parameters:
result - Test result.

runUntilTestCompletion

protected void runUntilTestCompletion(TestResult result)
Runs the test until test completion and then signals a failure if the maximum elapsed time was exceeded.
Parameters:
result - Test result.

runUntilTimeExpires

protected void runUntilTimeExpires(TestResult result)
Runs the test and immediately signals a failure when the maximum elapsed time is exceeded.
Parameters:
result - Test result.

setQuiet

public void setQuiet()
Disables the output of the test's elapsed time.

toString

public String toString()
Returns the test description.
Returns:
Description.

Copyright B) 1999-2005 Clarkware Consulting, Inc.