// Contains test cases to test the custom tag FormTag
// of the PetStore application.
//
// Written by Vahan Harput
//
// Last update: 07.06.2003
// 

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.cactus.*;

import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.*;

import com.sun.j2ee.blueprints.waf.view.taglibs.smart.FormTag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

public class TestFormTag extends JspTestCase
{
    FormTag myTag;
    BodyContent tagContent;
    int result;

    /**
     * The logger
     */
    private static final Log LOGGER =
        LogFactory.getLog(TestFormTag.class);

    public TestFormTag(String theName)
    {
        super(theName);
    }

    public static Test suite()
    {
        return new TestSuite(TestFormTag.class);
    }

    public void test_FormTag() throws JspException, IOException
    {
	myTag = new FormTag();
        
	myTag.setPageContext(this.pageContext);

	/* set tag attributes */
	myTag.setName("LoginForm");
	myTag.setMethod("POST");
	myTag.setAction("LoginServlet");

	myTag.putValidatedField("name", "text");
	myTag.putValidatedField("password", "password");
	
	result = myTag.doStartTag();

	assertEquals(result, myTag.EVAL_BODY_BUFFERED);

	/* set the body content of the tag */
        tagContent = this.pageContext.pushBody();
        myTag.setBodyContent(tagContent);

	myTag.doInitBody();

	/* fill the body content with the fields.
	   they must be included in the result */
	this.tagContent.println("<input name=\"name\" type=\"text\">");
	this.tagContent.println("<input name=\"password\" type=\"password\">");
	this.tagContent.println("<input type=\"submit\" value=\"Submit\">");

	result = myTag.doAfterBody();

	assertEquals(result, myTag.SKIP_BODY);
	assertEquals(this.tagContent.getString(), "");

	result = myTag.doEndTag();

	assertEquals(result, myTag.EVAL_PAGE);

	this.pageContext.popBody();
    }

    public void end_FormTag(WebResponse response)
    {
	String result = response.getText();

	LOGGER.info("response.getText():\n" + result);

	assertEquals(result, "");
    }

}

