org.apache.qpid.test.framework.listeners
Class XMLTestListener

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

public class XMLTestListener
extends Object
implements org.apache.qpid.junit.extensions.listeners.TKTestListener, org.apache.qpid.junit.extensions.ShutdownHookable

Listens for test results for a named test and outputs these in the standard JUnit XML format to the specified writer.

The API for this listener accepts notifications about different aspects of a tests results through different methods, so some assumption needs to be made as to which test result a notification refers to. For example startTest(junit.framework.Test) will be called, then possibly timing(junit.framework.Test, long, java.lang.Long) will be called, even though the test instance is passed in both cases, it is not enough to distinguish a particular run of the test, as the test case instance may be being shared between multiple threads, or being run a repeated number of times, and can therfore be re-used between calls. The listeners make the assumption that, for every test, a unique thread will call startTest(junit.framework.Test) and endTest(junit.framework.Test) to delimit each test. All calls to set test parameters, timings, state and so on, will occur between the start and end and will be given with the same thread id as the start and end, so the thread id provides a unqiue value to identify a particular test run against.

CRC Card
Responsibilities Collaborations
Listen to test lifecycle notifications.
Listen to test errors and failures.
Listen to test timings.
Listen to test memory usages.
Listen to parameterized test parameters.
Responsibilities

Todo:
Merge this class with CSV test listener, making the collection of results common to both, and only factoring out the results printing code into sub-classes. Provide a simple XML results formatter with the same format as the ant XML formatter, and a more structured one for outputing results with timings and summaries from performance tests.

Nested Class Summary
protected static class XMLTestListener.Result
          Used to capture the results of a particular test run.
 
Field Summary
protected  int errors
          Holds the overall error count.
protected  int failures
          Holds the overall failure count.
(package private)  List<XMLTestListener.Result> results
          Holds results for tests that have ended.
protected  int runs
          Holds the overall tests run count.
(package private)  String testClassName
          Holds the name of the class that tests are being run for.
(package private)  Map<Long,XMLTestListener.Result> threadLocalResults
          Map for holding results on a per thread basis as they come in.
protected  Writer writer
          The results file writer.
 
Constructor Summary
XMLTestListener(Writer writer, String testClassName)
          Creates a new XML results output listener that writes to the specified location.
 
Method Summary
 void addError(junit.framework.Test test, Throwable t)
          An error occurred.
 void addFailure(junit.framework.Test test, junit.framework.AssertionFailedError t)
          A failure occurred.
 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)
          Notifies listeners of the end of a complete run of tests.
 void endTest(junit.framework.Test test)
          Notification that a test ended.
 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, parameter zero, test passed.
 void startBatch()
          Notifies listeners of the start of a complete run of tests.
 void startTest(junit.framework.Test test)
          Notification that a test started.
 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

writer

protected Writer writer
The results file writer.


threadLocalResults

Map<Long,XMLTestListener.Result> 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.


results

List<XMLTestListener.Result> results
Holds results for tests that have ended. Transferring these results here from the per-thread results map, means that the thread id is freed for the thread to generate more results.


errors

protected int errors
Holds the overall error count.


failures

protected int failures
Holds the overall failure count.


runs

protected int runs
Holds the overall tests run count.


testClassName

String testClassName
Holds the name of the class that tests are being run for.

Constructor Detail

XMLTestListener

public XMLTestListener(Writer writer,
                       String testClassName)
Creates a new XML results output listener that writes to the specified location.

Parameters:
writer - The location to write results to.
testClassName - The name of the test class to include in the test results.
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, parameter zero, test passed.

Specified by:
reset in interface org.apache.qpid.junit.extensions.listeners.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.

startTest

public void startTest(junit.framework.Test test)
Notification that a test started.

Specified by:
startTest in interface junit.framework.TestListener
Parameters:
test - The test that 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 org.apache.qpid.junit.extensions.listeners.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 org.apache.qpid.junit.extensions.listeners.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 org.apache.qpid.junit.extensions.listeners.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 org.apache.qpid.junit.extensions.listeners.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.

Specified by:
concurrencyLevel in interface org.apache.qpid.junit.extensions.listeners.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.

properties

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

Specified by:
properties in interface org.apache.qpid.junit.extensions.listeners.TKTestListener
Parameters:
properties - The tests read/set properties.

endTest

public void endTest(junit.framework.Test test)
Notification that a test ended.

Specified by:
endTest in interface junit.framework.TestListener
Parameters:
test - The test that ended.

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 org.apache.qpid.junit.extensions.listeners.TKTestListener
Parameters:
test - The test which completed.
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)
An error occurred.

Specified by:
addError in interface junit.framework.TestListener
Parameters:
test - The test in which the error occurred.
t - The throwable that resulted from the error.

addFailure

public void addFailure(junit.framework.Test test,
                       junit.framework.AssertionFailedError t)
A failure occurred.

Specified by:
addFailure in interface junit.framework.TestListener
Parameters:
test - The test in which the failure occurred.
t - The JUnit assertions that led to the 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 org.apache.qpid.junit.extensions.listeners.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.

startBatch

public void startBatch()
Notifies listeners of the start of a complete run of tests.

Specified by:
startBatch in interface org.apache.qpid.junit.extensions.listeners.TKTestListener

endBatch

public void endBatch(Properties parameters)
Notifies listeners of the end of a complete run of tests.

Specified by:
endBatch in interface org.apache.qpid.junit.extensions.listeners.TKTestListener
Parameters:
parameters - The optional test parameters to log out with the batch results.

getShutdownHook

public Thread getShutdownHook()
Supplies the shutdown hook.

Specified by:
getShutdownHook in interface org.apache.qpid.junit.extensions.ShutdownHookable
Returns:
The shut down hook.


Licensed to the Apache Software Foundation