The contents of this document are a work in progress

Testing our Plexus Component

We create a basic unit test for our component. We do this by extending the PlexusTestCase class rather than the JUnit TestCase. PlexusTestCase sets up a Plexus Container intance for us to allow our component to be executed as it would when its deployed.

Note that if we override the setUp () and tearDown() methods to add some custom set up or clean up bits to our test case, then we need to ensure that super.XXX() is called (where XXX is the method being overridden). Othewise we will need to take care of Plexus container instance setup and clean ups ourselves.

Here is a rudimentary test case for your component.

package org.codehaus.plexus.tutorial;

import java.net.UnknownHostException;

import org.codehaus.plexus.PlexusTestCase;

/**
 * Basic test.
 */
public class WebsiteMonitorTest extends PlexusTestCase {

    /**
     * sets up a Plexus container instance for running test.
     */
    protected void setUp() throws Exception {
        // call this to enable super class to setup a Plexus container test
        // instance and enable component lookup.
        super.setUp ();
    }


    /**
     * Test if we are able to lookup and obtain our component instance from the
     * container.
     *
     * @throws Exception
     */
    public void testBasic() throws Exception {
        WebsiteMonitor component = (WebsiteMonitor) lookup (WebsiteMonitor.ROLE);
        assertNotNull (component);
    }


    /**
     * Test an unkown host.
     *
     * @throws Exception
     */
    public void testUnknownHost() throws Exception {
        WebsiteMonitor component = (WebsiteMonitor) lookup (WebsiteMonitor.ROLE);
        assertNotNull (component);
        Exception e = null;
        try {
            component.monitor ("http://www.yadayadayda.com/");
        } catch (Exception e1) {
            e = e1;
        }
        assertNotNull (e);
        assertEquals (true, (e instanceof UnknownHostException));
    }


    /**
     * Test a page that does not exists.
     *
     * @throws Exception
     */
    public void testNotOKCode() throws Exception {
        WebsiteMonitor component = (WebsiteMonitor) lookup (WebsiteMonitor.ROLE);
        assertNotNull (component);
        Exception e = null;
        try {
            component.monitor ("http://www.google.co.nz/unknown_404_.html");
        } catch (Exception e1) {
            e = e1;
        }
        assertNotNull (e);
    }

}

Time to take our unit test for a spin! Right click on the test case class in the Package Explorer view in Eclipse and choose to "Run As > JUnit Test" - our test case fails as expected. Lets start stubbing in some implementation into the component and fixing the situation. The monitor(String website) is implemented as below to check the status of the provided website URL.

public class DefaultWebsiteMonitor implements WebsiteMonitor {

    /*
     * (non-Javadoc)
     *
     * @see org.codehaus.plexus.tutorial.WebsiteMonitor#monitor(java.lang.String,
     *      long)
     */
    public void monitor(String website) throws Exception {
        HttpClient client = new HttpClient ();
        HttpMethod getMethod = new GetMethod (website);
        getMethod.setFollowRedirects (false);

        try {
            int statusCode = client.executeMethod (getMethod);

            if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
                System.err.println ("HTTP request returned HTTP status code: " + statusCode);
                throw new Exception ("HTTP request returned HTTP status code: " + statusCode);
            }
            System.out.println ("HTTP request returned HTTP status code: " + statusCode);
        } catch (UnknownHostException e) {
            System.err.println ("Specified host '" + website + "' could not be resolved.");
            throw e;
        }
    }

}