package com.oreilly.javaxp.httpunit;
import com.meterware.httpunit.*;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import junit.framework.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 $
*/
public class TestNewsletter extends TestCase {
private WebConversation webConversation;
public TestNewsletter(String name) {
super(name);
}
public void setUp() throws Exception {
this.webConversation = new WebConversation();
}
/**
* Notes:
* <pre>
* - 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
* </pre>
* @throws Exception
*/
public void testIndexPageExists() 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 void testIndexPageAlternateSyntax() throws Exception {
WebResponse response =
webConversation.getResponse("http://localhost:8080/news");
}
public void testIndexPageSimplified() throws Exception {
webConversation.getResponse("http://localhost:8080/news");
}
public void testIndexPageLinksExist() throws Exception {
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 void testFollowLinkToSubscriptionPage() throws Exception {
// 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 void testPersonTable() throws Exception {
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 void testAccountTable() throws Exception {
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 void testSubscriptionForm() throws Exception {
WebForm form = getBlankSubscriptionForm();
assertEquals("subscription form action",
"subscription", form.getAction());
assertEquals("subscription form method",
"post", form.getMethod().toLowerCase());
}
private WebForm getBlankSubscriptionForm() throws Exception {
WebResponse response = getBlankSubscriptionPage();
WebForm form = response.getFormWithID("subscriptionForm");
return form;
}
public void testButtonsOnSubscriptionForm() throws Exception {
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 void testFieldsOnSubscriptionForm() throws Exception {
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 void testSubmitSubscriptionWithoutRequiredField()
throws Exception {
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 void testCookies() throws Exception {
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 void testViewSubscribersWithoutLogin() throws Exception {
try {
this.webConversation.getResponse(
"http://localhost:8080/news/viewSubscribers");
fail("viewSubscribers should require a login");
} catch (AuthorizationRequiredException expected) {
// ignored
}
}
public void testViewSubscribersWithLogin() throws Exception {
this.webConversation.setAuthorization("eric", "secret");
this.webConversation.getResponse(
"http://localhost:8080/news/viewSubscribers");
}
// public void testViewSubscribersDirectly() throws Exception {
//
//
// ServletRunner runner = new ServletRunner(
// "httpunit_chapter/web.xml");
// ServletUnitClient client = runner.newClient();
//
// try {
// client.getResponse("http://localhost/viewSubscribers");
// fail("viewSubscribers should require a login");
// } catch (AuthorizationRequiredException expected) {
// // ignored
// }
//
//
// // notice that the 'password' is actually the role name
// client.setAuthorization("eric", "manager");
// WebResponse response =
// client.getResponse("http://localhost/viewSubscribers");
//
// }
private WebResponse getBlankSubscriptionPage() throws Exception {
return this.webConversation.getResponse(
"http://localhost:8080/news/subscription");
}
}
|