FileDocCategorySizeDatePackage
TestNewsletter.javaAPI DocExample9622Wed Aug 07 19:26:20 BST 2002com.oreilly.javaxp.httpunit

TestNewsletter

public class TestNewsletter extends TestCase
A series of unit tests from the HttpUnit chapter.
author
Eric M. Burke
version
$Id: TestNewsletter.java,v 1.1 2002/08/08 00:26:21 jepc Exp $

Fields Summary
private WebConversation
webConversation
Constructors Summary
public TestNewsletter(String name)

        super(name);
    
Methods Summary
private WebFormgetBlankSubscriptionForm()

        WebResponse response = getBlankSubscriptionPage();
        WebForm form = response.getFormWithID("subscriptionForm");
        return form;
    
private WebResponsegetBlankSubscriptionPage()

        return this.webConversation.getResponse(
                "http://localhost:8080/news/subscription");
    
public voidsetUp()

        this.webConversation = new WebConversation();
    
public voidtestAccountTable()

        WebResponse response = webConversation.getResponse(
                "http://localhost:8080/news/sampleTable.html");

        WebTable accountTable = response.getTableWithID("accountInfoTbl");
        assertNotNull("account table", accountTable);

        // get the checking account number
        TableCell checkingCell =
                accountTable.getTableCellWithID("checkingAcctNbr");
        assertEquals("Checking account number",
                "12345", checkingCell.asText());
    
public voidtestButtonsOnSubscriptionForm()

        WebForm form = getBlankSubscriptionForm();
        SubmitButton subscribeBtn = form.getSubmitButton("subscribeBtn");
        SubmitButton unsubscribeBtn = form.getSubmitButton("unsubscribeBtn");

        assertNotNull("subscribeBtn should not be null", subscribeBtn);
        assertNotNull("unsubscribeBtn should not be null", unsubscribeBtn);
    
public voidtestCookies()


        this.webConversation.addCookie("shoppingCartId", "98765");
        WebRequest req = new GetMethodWebRequest(
                "http://localhost:8080/news/cookieDemo.jsp");
        WebResponse cookieDemoPage = this.webConversation.getResponse(req);

        // the JSP should have created the customerId cookie
        assertEquals("customer ID cookie", "12345",
                this.webConversation.getCookieValue("customerId"));

        // make sure the cookie we generated on the client was found
        // somewhere in the HTML page
        String pageSource = cookieDemoPage.getText();
        assertTrue("shoppingCardId cookie was not found",
                pageSource.indexOf("shoppingCartId") > -1);
    
public voidtestFieldsOnSubscriptionForm()

        WebForm form = getBlankSubscriptionForm();

        // get the values of the two text fields
        // HttpUnit treats most HTML form elements the same way
        String nameFieldValue = form.getParameterValue("nameField");
        String emailFieldValue = form.getParameterValue("emailField");

        // the empty fields should contain empty strings. If they are
        // null, this indicates they are not present on the page.
        assertEquals("nameField", "", nameFieldValue);
        assertEquals("emailFieldValue", "", emailFieldValue);
    
public voidtestFollowLinkToSubscriptionPage()

        // get the home page
        WebResponse response = webConversation.getResponse(
                "http://localhost:8080/news");

        WebLink subscriptionLink = response.getLinkWith("subscription");

        // get a request to simulate clicking on the link
        WebRequest clickRequest = subscriptionLink.getRequest();

        // throws HttpNotFoundException if the link is broken
        WebResponse subscriptionPage =
                webConversation.getResponse(clickRequest);
    
public voidtestIndexPageAlternateSyntax()

        WebResponse response =
                webConversation.getResponse("http://localhost:8080/news");
    
public voidtestIndexPageExists()
Notes:
- runs fine under JDK 1.4
- under JDK 1.3.1, you must add a JAXP-compliant parser to the
CLASSPATH. Otherwise, you get java.lang.NoClassDefFoundError with
this description: "javax/xml/parsers/ParserConfigurationException".
To fix, add crimson.jar and jaxp.jar, from the ANT_HOME/lib
directory, to the CLASSPATH. Or add xerces.jar from the
TOMCAT_HOME/common/lib directory.
- you get a NoClassDefFoundError when running under Ant using the
junit task. The easy fix is to copy httpunit.jar and Tidy.jar
to the ANT_HOME/lib directory

throws
Exception

        WebRequest request =
                new GetMethodWebRequest("http://localhost:8080/news");

        // this next line fails if the URL is not found. It will throw
        // a com.meterware.httpunit.HttpNotFoundException with the following
        // message: "Error on HTTP request: 404 Not found"
        WebResponse response =
                webConversation.getResponse(request);
    
public voidtestIndexPageLinksExist()

        HttpUnitOptions.setParserWarningsEnabled(true);

        WebResponse response =
                webConversation.getResponse("http://localhost:8080/news");

// bogus links return null
//        WebLink bogusLink = response.getLinkWith("bogus!!!");
//        assertNotNull("bogus! link", bogusLink);

        WebLink subscriptionLink = response.getLinkWith("subscription");
        assertNotNull("subscription link", subscriptionLink);

        WebLink viewSubscribersLink = response.getLinkWith("viewSubscribers");
        assertNotNull("viewSubscribers link", viewSubscribersLink);
    
public voidtestIndexPageSimplified()

        webConversation.getResponse("http://localhost:8080/news");
    
public voidtestPersonTable()

        WebResponse response = webConversation.getResponse(
                "http://localhost:8080/news/sampleTable.html");

        // get the HTML table with 'First Name' as the text of its
        // first non-blank cell
        WebTable table = response.getTableStartingWith("First Name");
        assertEquals("column count", 2, table.getColumnCount());
        assertEquals("row count", 3, table.getRowCount());

        // get the cell at row 2, column 0
        TableCell cell = table.getTableCell(2, 0);
        assertEquals("cell text", "Tanner", cell.asText());
    
public voidtestSubmitSubscriptionWithoutRequiredField()

        WebForm form = getBlankSubscriptionForm();
        form.setParameter("nameField", "Eric Burke");
        WebRequest request = form.getRequest("subscribeBtn");

        // Submit the page. The web app should return us right back to
        // the subscription page because the Email address is not specified
        WebResponse response = this.webConversation.getResponse(request);

        // make sure the user is warned about the missing field
        String pageText = response.getText();
        assertTrue("Required fields warning is not present",
                pageText.indexOf("Email address is required") > -1);

        // make sure the nameField has the original text
        form = response.getFormWithID("subscriptionForm");
        assertEquals("Name field should be pre-filled",
                "Eric Burke", form.getParameterValue("nameField"));
    
public voidtestSubscriptionForm()

        WebForm form = getBlankSubscriptionForm();

        assertEquals("subscription form action",
                "subscription", form.getAction());
        assertEquals("subscription form method",
                "post", form.getMethod().toLowerCase());
    
public voidtestViewSubscribersWithLogin()

        this.webConversation.setAuthorization("eric", "secret");
        this.webConversation.getResponse(
                "http://localhost:8080/news/viewSubscribers");
    
public voidtestViewSubscribersWithoutLogin()

        try {
            this.webConversation.getResponse(
                    "http://localhost:8080/news/viewSubscribers");
            fail("viewSubscribers should require a login");
        } catch (AuthorizationRequiredException expected) {
            // ignored
        }