Tutorial
Task 1
Step 2: The pool editor form
In this step, you will learn how to:
• Extract a form from a web page, using its ID
• Examine form attributes
• Check text and radio button input fields
• Check form submit buttons
Now that we can invoke our servlet, the next step will be to produce the form in response to a GET command. This form will POST back to the same URL, allow for entry of 10 pairs of team names, allow for selection of a tie-breaker game, and allow the user to either save his entries or save them and open the betting pool. We can write a test for each of these required behaviors.
Let us add the following test:
public void testFormAction() throws Exception {
ServletRunner sr = new ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" ); // (1) obtain the desired form
assertNotNull( "No form found with ID 'pool'", form );
assertEquals( "Form method", "POST", form.getMethod() ); // (2) verify that the form uses POST
assertEquals( "Form action", "", form.getAction() ); // (3) verify that the default action is used
}
The significant points in the code are:
action attribute specified and will therefore default to the URL from
which it was retrieved - in the case, the same servlet that produced it.Again we run the test and fix the reported errors, one by one. The printBody method will now look like this:
private void printBody( PrintWriter pw ) {
pw.println( "<form id='pool' method='POST'>" );
pw.println( "</form>" );
}
and we now have two working tests.
We can now test the input fields in the form:
public void testFormContents() throws Exception {
ServletRunner sr = new ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
assertNotNull( "No form found with ID 'pool'", form );
for (int i = 0; i < 10; i++) {
assertTrue( "Missing home team " + i, form.isTextParameter( "home" + i ) ); // (1) text parameter
assertTrue( "Missing away team " + i, form.isTextParameter( "away" + i ) ); // (1) text parameter
}
assertEquals( "Tie breaker values",
Arrays.asList( new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" } ),
Arrays.asList( form.getOptionValues( "tiebreaker" ) ) ); // (2) radio button
}
The significant points in the code are:
We satisfy this test by making the printBody method look like this:
private void printBody( PrintWriter pw ) {
pw.println( "<form id='pool' method='POST'>" );
pw.println( "<table>" );
pw.println( "<tr><th>Home Team</th><th>Away Team</th><th>Tiebreaker?</th></tr>" );
for (int i = 0; i < 10; i++) {
pw.println( "<tr><td><input name='home" + i + "'></td>" );
pw.println( "<td><input name='away" + i + "'></td>" );
pw.println( "<td><input type='radio' name='tiebreaker' value='" + i + "'/></td></tr>" );
}
pw.println( "</table>" );
pw.println( "</form>" );
}
We confirm the presence of the desired submit buttons as follows:
public void testSubmitButtons() throws Exception {
ServletRunner sr = new ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
assertNotNull( "No form found with ID 'pool'", form );
assertEquals( "Number of submit buttons", 2, form.getSubmitButtons().length ); // (1) count the buttons
assertNotNull( "Save button not found", form.getSubmitButton( "save", "Save" ) ); // (2) look up by name
assertNotNull( "Open Pool button not found", form.getSubmitButton( "save", "Open Pool" ) );
}
In this test, please note the following:
This test will pass once we modify the end of printBody, inserting lines to generate the submit buttons:
}
pw.println( "</table>" );
pw.println( "<input type='submit' name='save' value='Save' />" );
pw.println( "<input type='submit' name='save' value='Open Pool' />" );
pw.println( "</form>" );
}
We now have our form being generated, along with tests to confirm it. In the next step,
we will test and code the response to a form submission.