The Artima Developer Community
Sponsored Link

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

<<  Page 4 of 8  >>


Implement the setConfiguration Method

In addition to initializing your custom Reporter to its default configuration in the no-arg constructor, you must also allow the configuration to be changed via the setConfiguration method. setConfiguration accepts a single parameter, configs: a Set of configuration Character constants that define the Reporter's new configuration. An empty configs Set indicates the Reporter should return to its default configuration.

In XMLReporter's setConfiguration method, I start by checking for null and invalid input, as required by setConfiguration's method contract defined in interface Reporter. Next, I check to see if the passed Set is empty. An empty Set indicates I should reset the XMLReporter back to its default configuration. Therefore, if configs.size() is equal to 0, I assign to configuration a copy of the validConfigChars Set, which I decided would be the default. Otherwise I assign to the configuration instance variable a copy of the passed Set.

If you use as a template when creating your own custom Reporter, you can most likely reuse the setConfiguration method without any changes. Here's XMLReporter's setConfiguration method:

     * Configures this <code>XMLReporter</code>. If the specified <code>configuration</code>
     * set is zero size, the <code>XMLReporter</code> will be configured to its
     * default configuration. (The default configuration is described in the main
     * comment for class <code>XMLReporter</code>.)
     * @param configuration set of <code>Config</code> objects that indicate the new
     *     configuration for this <code>XMLReporter</code>
     * @exception NullPointerException if <code>configuration</code> reference is <code>null</code>
     * @exception IllegalArgumentException if <code>configuration</code> set contains any objects
     *    whose class isn't <code>org.suiterunner.Config</code>
    public synchronized void setConfiguration(Set configs) {

        if (configs == null) {
            throw new NullPointerException("Parameter configs is null.");

        for (Iterator it = configs.iterator(); it.hasNext();) {

            Object o =;

            if (!(o instanceof Character)) {

                throw new IllegalArgumentException("Passed object is not a Character.");

            if (!validConfigChars.contains(o)) {

                throw new IllegalArgumentException("Passed object is not a valid configuration Character.");

        if (configs.size() == 0) {
            // If passsed Set is empty, reset this Reporter to its default
            // configuration, which is to report everything.
            this.configuration = new HashSet(validConfigChars);
        else {
            this.configuration = new HashSet(configs);

Implement the dispose Method

With the no-arg constructor and setConfiguration methods finished, you may wish to turn your attention to the end of your custom Reporter's lifetime and implement dispose. If your custom Reporter holds onto finite non-memory resources, such as file handles or database connections, you should release them in the dispose method. If your custom Reporter does not hold onto any resources, your dispose method should do nothing (but you still have to implement it, of course).

Because XMLReporter simply writes to the standard output stream, it holds no finite non-memory resources to release in dispose. As a result, XMLReporter's dispose method does nothing.

Here's XMLReporter's do-nothing dispose method:

     * Does nothing, because this object holds no finite non-memory resources.
    public void dispose() {

<<  Page 4 of 8  >>

Sponsored Links

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