package com.oreilly.jent.people.jsp;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.io.IOException;

import javax.servlet.ServletException;

import junit.framework.Test;

import org.apache.cactus.JspTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;

import com.oreilly.jent.people.PersonDAO;

/**
 * Class: TestSearchPage
 * 
 * Tests the search JSP from the People Finder sample application.
 * 
 * @author <a href="mailto:jim@jimfarley.org">Jim Farley</a>
 *
 */
public class TestSearchJSP extends JspTestCase {

    /** Default constructor */
    public TestSearchJSP() {
        super();
    }

    /** Constructor with test name */
    public TestSearchJSP(String name) {
        super(name);
    }

    /** Constructor used to wrap another test with a Cactus test */
    public TestSearchJSP(String name, Test child) {
        super(name, child);
    }
    
    // ----------------------------------------------------------------------
    
    /** Invoked on the "client-side" (within the test runner) before
     *  running the badSearchArgument test. */
    public void beginValidSearch(WebRequest request) {
        // Add a search parameter
        request.addParameter(PersonDAO.FIRST_NAME, "John");
    }
    
    /** Ensure that a valid response is generated from a valid search */
    public void testValidSearch() {
        // Simply invoke the servlet's doGet() method, using
        // the request constructed in the begin method
        invokePage();
    }
    
    /** Invoked on the "client-side" (within the test runner) after the
     *  validSearch test completes and sends a response to 
     *  the test runner. */
    public void endValidSearch(WebResponse response) {
        // There should be some response from the JSP
        assertNotNull("No response text received from JSP",
                      response.getText());
        // Since this was a valid search, there should be no "Error"
        // text in the response HTML
        assertTrue("Unexpected \"Error:\" indicator in response",
                   response.getText().indexOf("Error:") < 0);
        // In a valid search response, there should be a results header
        assertTrue("No results banner seen in response",
                   response.getText().indexOf("Search Results") >= 0);
    }
    
    private void invokePage() {
        try {
            this.pageContext.forward("/search");
        }
        catch (ServletException se) {
            fail("Unexpected servlet exception: " + se.getMessage());
        }
        catch (IOException ioe) {
            fail("Unexpected I/O exception: " + ioe.getMessage());
        }
    }
}
