From: Jeff M. <cus...@us...> - 2002-08-28 17:36:31
|
Update of /cvsroot/mockobjects/mockobjects-java/src/j2ee/common/com/mockobjects/helpers In directory usw-pr-cvs1:/tmp/cvs-serv22703/src/j2ee/common/com/mockobjects/helpers Added Files: ServletTestHelper.java TagTestHelper.java Log Message: Add first versions of test helpers for Servlets and Tags --- NEW FILE: ServletTestHelper.java --- package com.mockobjects.helpers; import com.mockobjects.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.ServletException; import java.io.IOException; /** * Sets up mock servlet objects in a common configuration. * HttpSession is attached to request, RequestDispatcher is connected to HttpSession * @see com.mockobjects.servlet.MockHttpServletRequest#setSession * @see com.mockobjects.servlet.MockServletContext#setupGetRequestDispatcher */ public class ServletTestHelper { protected final MockHttpServletRequest request = new MockHttpServletRequest(); protected final MockHttpServletResponse response = new MockHttpServletResponse(); protected final MockHttpSession httpSession = new MockHttpSession(); protected final MockRequestDispatcher requestDispatcher = new MockRequestDispatcher(); protected final MockServletContext servletContext = new MockServletContext(); protected final MockServletConfig servletConfig = new MockServletConfig(); private final HttpServlet testSubject; public ServletTestHelper(HttpServlet testSubject) { this.testSubject = testSubject; request.setSession(httpSession); servletContext.setupGetRequestDispatcher(requestDispatcher); } protected ServletTestHelper(){ this.testSubject = null; request.setSession(httpSession); servletContext.setupGetRequestDispatcher(requestDispatcher); } public MockHttpServletRequest getRequest() { return request; } public MockHttpSession getHttpSession() { return httpSession; } public MockRequestDispatcher getRequestDispatcher() { return requestDispatcher; } public MockHttpServletResponse getResponse() { return response; } public MockServletContext getServletContext() { return servletContext; } public MockServletConfig getServletConfig() { return servletConfig; } /** * Calls HttpServlet.init passing it the MockServletConfig */ public void testServletInit() throws ServletException { testSubject.init(servletConfig); } /** * Calls HttpServlet.service passing it the MockHttpServletRequest & MockHttpServletResponse */ public void testServlet() throws ServletException, IOException { testSubject.service(request, response); } } --- NEW FILE: TagTestHelper.java --- package com.mockobjects.helpers; import com.mockobjects.servlet.MockBodyContent; import com.mockobjects.servlet.MockJspWriter; import com.mockobjects.servlet.MockPageContext; import junit.framework.Assert; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTag; import javax.servlet.jsp.tagext.IterationTag; import javax.servlet.jsp.tagext.Tag; /** * Sets up mock tag objects in a common configuration. * MockHttpServletRequest, MockServletContext and MockHttpSession are attached to MockPageContext * @see MockPageContext#setRequest(); * @see MockPageContext#setServletContext(); * @see MockPageContext#setSession(); */ public class TagTestHelper extends ServletTestHelper { private final MockPageContext pageContext = new MockPageContext(); private final MockBodyContent bodyContent = new MockBodyContent(); private final MockJspWriter outWriter = new MockJspWriter(); private final MockJspWriter enclosingWriter = new MockJspWriter(); private final Tag testSubject; private final String getReturnValueName(int returnValue) { switch (returnValue) { case BodyTag.EVAL_BODY_INCLUDE: return "EVAL_BODY_INCLUDE"; case BodyTag.EVAL_PAGE: return "EVAL_PAGE"; case BodyTag.SKIP_BODY: return "SKIP_BODY"; case BodyTag.SKIP_PAGE: return "SKIP_PAGE"; case BodyTag.EVAL_BODY_BUFFERED: return "EVAL_BODY_BUFFERED|EVAL_BODY_AGAIN"; default: return "Unknown return value (" + returnValue + ")"; } } /** * @param testSubject The Tag to be tested */ public TagTestHelper(Tag testSubject) { this.testSubject = testSubject; pageContext.setRequest(getRequest()); pageContext.setServletContext(getServletContext()); pageContext.setSession(getHttpSession()); pageContext.setJspWriter(outWriter); bodyContent.setupGetEnclosingWriter(enclosingWriter); } /** * @return The writer use when making calls to PageContext.getOut */ public MockJspWriter getOutWriter() { return outWriter; } public MockPageContext getPageContext() { return pageContext; } /** * Assert that the return value of doStartTag is equal to an expectedValue * @param expectedValue value to check against doStartTag */ public void assertDoStartTag(final int expectedValue) throws JspException { testSubject.setPageContext(pageContext); checkReturnValue("doStartTag", expectedValue, testSubject.doStartTag()); } private final void checkReturnValue(final String methodName, final int expectedValue, final int returnValue) { Assert.assertEquals(methodName + " expected value " + getReturnValueName(expectedValue) + " but was" + getReturnValueName(expectedValue), expectedValue, returnValue); } /** * Invoke doInitBody on the test subject */ public void testDoInitBody() throws JspException { Assert.assertTrue("Test subject not an instance of BodyTag", testSubject instanceof BodyTag); ((BodyTag) testSubject).setBodyContent(bodyContent); ((BodyTag) testSubject).doInitBody(); } /** * Assert that the return value of doAfterBody is equal to an expectedValue * @param expectedValue value to check against doAfterBody */ public void assertDoAfterBody(int expectedValue) throws JspException { Assert.assertTrue("Test subject not an instance of BodyTag", testSubject instanceof IterationTag); checkReturnValue("doAfterTag", expectedValue, ((IterationTag) testSubject).doAfterBody()); } /** * Assert that the return value of doEndTag is equal to an expectedValue * @param expectedValue value to check against doEndTag */ public void assertDoEndTag(int expectedValue) throws JspException { Assert.assertEquals("doEndTag returned unexpected value" + getReturnValueName(expectedValue), expectedValue, testSubject.doEndTag()); } } |