org.apache.qpid.junit.extensions.listeners
Class CSVTestListener

java.lang.Object
  extended by org.apache.qpid.junit.extensions.listeners.CSVTestListener
All Implemented Interfaces:
junit.framework.TestListener, TKTestListener, ShutdownHookable

public class CSVTestListener
extends Object
implements junit.framework.TestListener, TKTestListener, ShutdownHookable

CSVTestListener is both a test listener, a timings listener, a memory listener and a parameter listener. It listens for test completion events and then writes out all the data that it has listened to into a '.csv' (comma seperated values) file.

CRC Card
Responsibilities Collaborations
Listen to test events; start, end, fail, error.
Listen to test timings.
Listen to test memory usage.
Listen to parameterized test parameters.
Output all test data to a CSV file.

Todo:
Write an XML output class. Write a transform to convert it into an HTML page with timings as graphs.

Field Summary
(package private)  Map<Long,org.apache.qpid.junit.extensions.listeners.CSVTestListener.TestResult> threadLocalResults
          Map for holding results on a per thread basis as they come in.
 
Constructor Summary
CSVTestListener(Writer writer)
          Creates a new CSVTestListener object.
 
Method Summary
 void addError(junit.framework.Test test, Throwable t)
          Called when a test results in an error.
 void addFailure(junit.framework.Test test, junit.framework.AssertionFailedError t)
          Called when a test results in a failure.
 void addFailure(junit.framework.Test test, junit.framework.AssertionFailedError e, Long threadId)
          Called when a test completes to mark it as a test fail.
 void concurrencyLevel(junit.framework.Test test, int threads, Long threadId)
          Should be called every time a test completes with the current number of test threads running.
 void endBatch(Properties parameters)
          Takes a time stamp for the end of the batch to calculate the total run time.
 void endTest(junit.framework.Test test)
          Called when a test completes.
 void endTest(junit.framework.Test test, Long threadId)
          Called when a test completes.
 Thread getShutdownHook()
          Supplies the shutdown hook.
 void memoryUsed(junit.framework.Test test, long memStart, long memEnd, Long threadId)
          Should be called every time a test completed with the amount of memory used before and after the test was run.
 void parameterValue(junit.framework.Test test, int parameter, Long threadId)
          Should be called every time a parameterized test completed with the int value of its test parameter.
 void properties(Properties properties)
          Notifies listeners of the tests read/set properties.
 void reset(junit.framework.Test test, Long threadId)
          Resets the test results to the default state of time zero, memory usage zero, test passed.
 void startBatch()
          Takes a time stamp for the beginning of the batch and resets stats counted for the batch.
 void startTest(junit.framework.Test test)
          Called when a test starts.
 void timing(junit.framework.Test test, long nanos, Long threadId)
          Should be called every time a test completes with the run time of that test.
 void timing2(junit.framework.Test test, Long nanos, Long threadId)
          Optionally called every time a test completes with the second timing test.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadLocalResults

Map<Long,org.apache.qpid.junit.extensions.listeners.CSVTestListener.TestResult> threadLocalResults
Map for holding results on a per thread basis as they come in. A ThreadLocal is not used as sometimes an explicit thread id must be used, where notifications come from different threads than the ones that called the test method.

Constructor Detail

CSVTestListener

public CSVTestListener(Writer writer)
Creates a new CSVTestListener object.

Parameters:
writer - A writer where this CSV listener should write out its output to.
Method Detail

reset

public void reset(junit.framework.Test test,
                  Long threadId)
Resets the test results to the default state of time zero, memory usage zero, test passed.

Specified by:
reset in interface TKTestListener
Parameters:
test - The test to resest any results for.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

addError

public void addError(junit.framework.Test test,
                     Throwable t)
Called when a test results in an error.

Specified by:
addError in interface junit.framework.TestListener
Parameters:
test - The test which is in error.
t - Any Throwable raised by the test in error.

addFailure

public void addFailure(junit.framework.Test test,
                       junit.framework.AssertionFailedError t)
Called when a test results in a failure.

Specified by:
addFailure in interface junit.framework.TestListener
Parameters:
test - The test which failed.
t - The AssertionFailedError that encapsulates the test failure.

addFailure

public void addFailure(junit.framework.Test test,
                       junit.framework.AssertionFailedError e,
                       Long threadId)
Called when a test completes to mark it as a test fail. This method should be used when registering a failure from a different thread than the one that started the test.

Specified by:
addFailure in interface TKTestListener
Parameters:
test - The test which failed.
e - The assertion that failed the test.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

endTest

public void endTest(junit.framework.Test test)
Called when a test completes. Success, failure and errors.

Specified by:
endTest in interface junit.framework.TestListener
Parameters:
test - The test which completed.

startTest

public void startTest(junit.framework.Test test)
Called when a test starts.

Specified by:
startTest in interface junit.framework.TestListener
Parameters:
test - The test wich has started.

timing

public void timing(junit.framework.Test test,
                   long nanos,
                   Long threadId)
Should be called every time a test completes with the run time of that test.

Specified by:
timing in interface TKTestListener
Parameters:
test - The name of the test.
nanos - The run time of the test in nanoseconds.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

timing2

public void timing2(junit.framework.Test test,
                    Long nanos,
                    Long threadId)
Optionally called every time a test completes with the second timing test.

Specified by:
timing2 in interface TKTestListener
Parameters:
test - The name of the test.
nanos - The second timing information of the test in nanoseconds.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

memoryUsed

public void memoryUsed(junit.framework.Test test,
                       long memStart,
                       long memEnd,
                       Long threadId)
Should be called every time a test completed with the amount of memory used before and after the test was run.

Specified by:
memoryUsed in interface TKTestListener
Parameters:
test - The test which memory was measured for.
memStart - The total JVM memory used before the test was run.
memEnd - The total JVM memory used after the test was run.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

parameterValue

public void parameterValue(junit.framework.Test test,
                           int parameter,
                           Long threadId)
Should be called every time a parameterized test completed with the int value of its test parameter.

Specified by:
parameterValue in interface TKTestListener
Parameters:
test - The test which memory was measured for.
parameter - The int parameter value.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

concurrencyLevel

public void concurrencyLevel(junit.framework.Test test,
                             int threads,
                             Long threadId)
Should be called every time a test completes with the current number of test threads running. This should not change within a test batch, therefore it is safe to take this as a batch level property value too.

Specified by:
concurrencyLevel in interface TKTestListener
Parameters:
test - The test for which the measurement is being generated.
threads - The number of tests being run concurrently.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

endTest

public void endTest(junit.framework.Test test,
                    Long threadId)
Called when a test completes. Success, failure and errors. This method should be used when registering an end test from a different thread than the one that started the test.

Specified by:
endTest in interface TKTestListener
Parameters:
test - The test which completed.
threadId - Optional thread id if not calling from thread that started the test method. May be null.

startBatch

public void startBatch()
Takes a time stamp for the beginning of the batch and resets stats counted for the batch.

Specified by:
startBatch in interface TKTestListener

endBatch

public void endBatch(Properties parameters)
Takes a time stamp for the end of the batch to calculate the total run time. Write this and other stats out to the tail of the csv file.

Specified by:
endBatch in interface TKTestListener
Parameters:
parameters - The optional test parameters, may be null.

properties

public void properties(Properties properties)
Notifies listeners of the tests read/set properties.

Specified by:
properties in interface TKTestListener
Parameters:
properties - The tests read/set properties.

getShutdownHook

public Thread getShutdownHook()
Supplies the shutdown hook. This attempts to flush the results in the event of the test runner being prematurely suspended before the end of the current test batch.

Specified by:
getShutdownHook in interface ShutdownHookable
Returns:
The shut down hook.


Licensed to the Apache Software Foundation