The Artima Developer Community
Sponsored Link

Create an XML Reporter for Your Unit Tests
by Bill Venners
February 24, 2003

<<  Page 3 of 8  >>


Create a No-Arg Constructor for your Reporter

Before a run, org.suiterunner.Runner (Runner) instantiates each Reporter via the Reporter's no-arg constructor. Therefore, when you create a custom Reporter, you must give it a public no-arg constructor.

In the XMLReporter's no-arg constructor, I initialize the three private variables of the class: pw, configuration, and validConfigChars. pw is a buffered PrintWriter that wraps System.out. The event handler methods use pw to print XML to the standard output. validConfigChars is a convenience Set of all 11 valid configuration Character constants (see Figure 3). configuration is a Set that holds the current configuration of the XMLReporter.

Here are XMLReporter's instance variables:

public class XMLReporter implements Reporter {

    // A PrintWriter that wraps the standard output
    private PrintWriter pw;

    // A Set that holds this Reporter's current configuration
    private Set configuration;

    // A Set that contains all valid configuration characters, which
    // are defined in interface Reporter.
    private Set validConfigChars;

Every Reporter has a default configuration. A default configuration is a set of configuration Character constants defined by the Reporter's designer. When you create a custom Reporter, therefore, you must decide what its default configuration will be.

For XMLReporter, I defined the default configuration to include all 11 configuration Character constants. In other words, by default, an XMLReporter will report every event handler method invocation in its XML output stream. In the constructor, therefore, I initialize the configuration instance variable to a copy of the validConfigChars Set, which contains all 11 configuration Character constants.

If you use as a template when creating your own custom Reporter, you can most likely reuse the configuration and validConfigChars variables as is. Unless you want to print test results in some form to the standard output, however, you will likely want to replace pw with something else, such as a FileOutputStream, a database connection, a socket, a log, a reference to a GUI component -- whatever you will be sending your results.

Here's XMLReporter's no-arg constructor:

     * Construct an <code>XMLReporter</code>, which writes test results
     * in XML to the standard output stream. The <code>XMLReporter</code> is
     * created with a default configuration that includes all valid configuration
     * characters.
    public XMLReporter() {

        pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(System.out)));

        // Build a set that contains all valid configuration characters
        Set validSet = new HashSet();

        validConfigChars = Collections.unmodifiableSet(validSet);

        // Initialize configuration to validConfigChars, because the default
        // configuration for this Reporter is defined to be everything. (See
        // the JavaDoc comment for the entire class.)
        configuration = new HashSet(validConfigChars);

<<  Page 3 of 8  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use