Tutorial -> Task 1 -> Step 1: Invoking the pool editor

Invoking the Pool Editor

In this step, you will learn how to:
• Initialize ServletUnit
• Invoke a servlet
• Specify a username and password for basic authentication

The first step will simply be to verify that we can register and access the servlet, which we will name PoolEditorServlet. A GET method to this page should return the editor form itself, while updates will be handled by a POST method to the same address. Since we are working with servlets, we can bypass the web server and use the servletunit package to run our tests.

Here is the initial test code:

package tutorial;

import com.meterware.httpunit.*;
import com.meterware.servletunit.*;
import java.util.*;
import junit.framework.*;
import tutorial.persistence.*;

public class PoolEditorTest extends TestCase {

    public static void main( String args[] ) {
        junit.textui.TestRunner.run( suite() );
    }

    public static TestSuite suite() {
        return new TestSuite( PoolEditorTest.class );
    }

    public PoolEditorTest( String s ) {
        super( s );
    }

    public void testGetForm() throws Exception {
        ServletRunner sr = new ServletRunner( "web.xml" );       // (1) use the web.xml file to define mappings
        ServletUnitClient client = sr.newClient();               // (2) create a client to invoke the application

        try {
            client.getResponse( "http://localhost/PoolEditor" ); // (3) invoke the servlet w/o authorization
            fail( "PoolEditor is not protected" );
        } catch (AuthorizationRequiredException e) {             // (4) verify that access is denied
        }

        client.setAuthorization( "aUser", "pool-admin" );        // (5) specify authorization and
        client.getResponse( "http://localhost/PoolEditor" );     //     invoke the servlet again
    }

}

This code uses JUnit and ServletUnit to verify that a servlet is present at the specified address. The significant points in the code are:

  1. Creating the ServletRunner class which represents access to a Servlet application. The application is defined by an XML file which maps URL information to servlet classes.
  2. Creating a client which can access the application and maintain state across multiple invocations.
  3. Invoking the servlet via its URL. Note that ServletUnit ignores any host and port information. All URL patterns are treated as being relative to the root ("/").
  4. Catching an exception which indicates that authentication is required.
  5. Specifying the authorization information. ServletUnit does not maintain a database of users, no any username is accepted, and the password is interpreted as a comma-separated list of role names associated with the user.

To run this code, you will also need the web.xml file in your current directory. This file maps the request URL to the Pool Editor servlet.

This code should fail with a HttpNotFoundException, because we have not yet created the servlet class. We can now proceed to do so. Here is a simple implementation:

package tutorial;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;
import javax.servlet.ServletException;

import tutorial.persistence.*;

public class PoolEditorServlet extends HttpServlet {

    protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        response.setContentType( "text/html" );
        PrintWriter pw = response.getWriter();

        pw.println( "<html><head></head><body>" );
        printBody( pw );
        pw.println( "</body></html>" );
    }

    private void printBody( PrintWriter pw ) {
        pw.println( "A simple page" );
    }
}

With this code in place, the first test will now pass and we can move to the next task.