com.mockrunner.test.web.HTMLOutputModuleTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.test.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import junit.framework.TestCase;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jdom.Element;
import com.mockrunner.base.HTMLOutputModule;
import com.mockrunner.base.VerifyFailedException;
import com.mockrunner.base.WebTestModule;
import com.mockrunner.mock.web.ActionMockObjectFactory;
import com.mockrunner.mock.web.MockActionForward;
import com.mockrunner.servlet.ServletTestModule;
import com.mockrunner.struts.ActionTestModule;
import com.mockrunner.tag.TagTestModule;
import com.mockrunner.util.common.StreamUtil;
public class HTMLOutputModuleTest extends TestCase
{
private final static String testHTML = "";
private ActionMockObjectFactory actionWebFactory;
protected void setUp() throws Exception
{
super.setUp();
actionWebFactory = new ActionMockObjectFactory();
}
public void testActionTestModuleOutput()
{
ActionTestModule module = new ActionTestModule(actionWebFactory);
module.actionPerform(TestOutputAction.class);
doTestOutputAsString(module);
doTestOutputAsBufferedReader(module);
doTestOutputAsJDOMDocument(module);
doTestVerifyOutput(module);
doTestVerifyOutputContains(module);
doTestVerifyOutputRegularExpression(module);
}
public void testActionTestModuleAttributes()
{
ActionTestModule module = new ActionTestModule(actionWebFactory);
doTestAttributes(module);
}
public void testServletTestModuleOutput()
{
ServletTestModule module = new ServletTestModule(actionWebFactory);
module.createServlet(TestOutputServlet.class);
module.doGet();
doTestOutputAsString(module);
doTestOutputAsBufferedReader(module);
doTestOutputAsJDOMDocument(module);
doTestVerifyOutput(module);
doTestVerifyOutputContains(module);
doTestVerifyOutputRegularExpression(module);
}
public void testServletTestModuleAttributes()
{
ServletTestModule module = new ServletTestModule(actionWebFactory);
doTestAttributes(module);
}
public void testTagTestModuleOutput()
{
TagTestModule module = new TagTestModule(actionWebFactory);
module.createTag(TestOutputTag.class);
module.doStartTag();
doTestOutputAsString(module);
doTestOutputAsBufferedReader(module);
doTestOutputAsJDOMDocument(module);
doTestVerifyOutput(module);
doTestVerifyOutputContains(module);
doTestVerifyOutputRegularExpression(module);
}
public void testTagTestModuleAttributes()
{
TagTestModule module = new TagTestModule(actionWebFactory);
doTestAttributes(module);
}
private void doTestOutputAsString(HTMLOutputModule module)
{
assertEquals(testHTML, module.getOutput());
}
private void doTestOutputAsBufferedReader(HTMLOutputModule module)
{
assertEquals(testHTML, StreamUtil.getReaderAsString(module.getOutputAsBufferedReader()));
}
private void doTestOutputAsJDOMDocument(HTMLOutputModule module)
{
Element root = module.getOutputAsJDOMDocument().getRootElement();
assertEquals("html", root.getName());
Element body = root.getChild("body");
assertNotNull(body);
Element tag = body.getChild("tag");
assertNotNull(tag);
}
private void doTestVerifyOutput(HTMLOutputModule module)
{
module.setCaseSensitive(true);
module.verifyOutput("");
try
{
module.verifyOutput("");
fail();
}
catch(VerifyFailedException exc)
{
//should throw exception
}
module.setCaseSensitive(false);
module.verifyOutput("");
try
{
module.verifyOutput("xyz");
fail();
}
catch(VerifyFailedException exc)
{
//should throw exception
}
}
private void doTestVerifyOutputContains(HTMLOutputModule module)
{
module.setCaseSensitive(true);
module.verifyOutputContains("");
try
{
module.verifyOutputContains("");
fail();
}
catch(VerifyFailedException exc)
{
//should throw exception
}
module.setCaseSensitive(false);
module.verifyOutputContains("");
try
{
module.verifyOutputContains("boddy");
fail();
}
catch(VerifyFailedException exc)
{
//should throw exception
}
}
private void doTestVerifyOutputRegularExpression(HTMLOutputModule module)
{
module.setCaseSensitive(true);
module.verifyOutputRegularExpression(".*