package cnam.cours.web; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebForm; import com.meterware.httpunit.WebResponse; import junit.framework.TestCase; import junit.framework.TestSuite; /** * This class tests the HTML Pages and servlets */ public class WebTest extends TestCase { private WebConversation webConversation = new WebConversation(); private static final String URL_ROOT = "http://localhost:8080/exX"; public WebTest(final String s) { super(s); } public static TestSuite suite() { return new TestSuite(WebTest.class); } //================================== //= Test cases = //================================== /** * Checks that all HTML and JSP pages are deployed */ public void testWebCheckPages() { try { webConversation.getResponse(URL_ROOT); } catch (Exception e) { fail("Root " + URL_ROOT + " hasn't been found"); } try { webConversation.getResponse(URL_ROOT + "/login.jsp"); } catch (Exception e) { fail("login.jsp hasn't been found"); } } /** * Checks that all servlets are deployed */ public void testWebCheckServlets() { try { webConversation.getResponse(URL_ROOT + "/login"); } catch (Exception e) { fail("The login servlet hasn't been found"); } } /** * Checks invalid login */ public void testBadLogin() throws Exception { WebResponse page; page = webConversation.getResponse(URL_ROOT + "/betterLogin.jsp"); WebForm form = page.getFormWithName("loginform"); form.setParameter("login", "zzz"); form.setParameter("password", "zzz"); // Submits the form form.submit(); // After clicking the submit button // the page should not be the welcome page WebResponse newPage = webConversation.getCurrentPage(); String title = newPage.getTitle(); boolean isNewPage = ( title.indexOf("welcome.jsp") != -1 ); assertFalse("The page displayed after a bad login should not be the welcome page", isNewPage); } /** * Checks valid login */ public void testValidLogin() throws Exception { WebResponse page; page = webConversation.getResponse(URL_ROOT + "/betterLogin.jsp"); WebForm form = page.getFormWithName("loginform"); form.setParameter("login", "pg"); form.setParameter("password", "pg"); // Submits the form form.submit(); // After clicking the submit button // the page should be the welcome page WebResponse newPage = webConversation.getCurrentPage(); String title = newPage.getTitle(); boolean isNewPage = ( title.indexOf("welcome.jsp") != -1 ); assertTrue("The page displayed after a valid login should be the welcome page", isNewPage); } }