<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>DogBiscuit</title>
    <description>... mmm, crunchy!</description>
    <link>http://dogbiscuit.org/mdub/weblog</link>
    <language>en-us</language>
    <generator>EvenYetAnotherWeblog</generator>
    <item>
      <title>Jetty as a test-suite decorator</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Java/JettyTestSetup</guid>
      <pubDate>Fri, 09 Jul 2004 16:30:00 +1000</pubDate>
      <description><![CDATA[<p>
<a href='http://www.wrytradesman.com/blog/'>Marty Andrews</a> and I have been working
on a small project together.  It's primarily intended as a demo of
continuous integration, but has also given us the opportunity to play with
some new technologies/ideas.
</p>
<p>
One of the coolest tricks we picked up (from
<a href='http://jakarta.apache.org/cactus/integration/integration_jetty.html'>Cactus</a>)
was to start/stop a web-server as part of running the tests, rather than
depending on having one running already. 
</p>
<p>
(In the past I've typically written Ant scripts that dump a WAR-file in a
magic directory, and wait "a bit" for the server to auto-deploy it, before
running my HTTP-based acceptance-tests.  This is way nicer.)
</p>
<p>
The key is a test decorator that starts <a href='http://jetty.mortbay.com/'>Jetty</a>
to serve our web-app:
</p>
<pre>
package com.thoughtworks.todolist;

import junit.extensions.TestSetup;
import junit.framework.Test;
import org.mortbay.jetty.Server;
import org.mortbay.util.InetAddrPort;

public class JettyTestSetup extends TestSetup {

    private Server _server;

    public JettyTestSetup(Test test) {
        super(test);
    }

    protected void setUp() throws Exception {
        _server = new Server();
        _server.addListener(new InetAddrPort(9999));
        _server.addWebApplication(
            &quot;/todolist&quot;, &quot;build/todolist.war&quot;
        );
        _server.start();
    }

    protected void tearDown() throws Exception {
        _server.stop();
        _server = null;
    }

}
</pre>
<p>
As you can see, it's not hard to get a Jetty server going.  Jetty is nice
and lightweight, too: it's small (less than 600k), and starts up fast (less
than a second here).
</p>
<p>
Now, it's a simple matter to decorate our test-suite with JettyTestSetup:
</p>
<pre>
public class AllAcceptanceTests {

    public static Test suite() throws Exception {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(ViewListTest.class);
        suite.addTestSuite(AddItemTest.class);
        // ... etc ...
        return new JettyTestSetup(suite);
    }

}
</pre>
<p>
That's it.  The server gets started at the beginning of the suite, and
stopped afterward.
</p>
]]></description>
    </item>
  </channel>
</rss>
