日期:2014-05-20 浏览次数:20771 次
package jsps; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.*; import javax.servlet.ServletException; import javax.servlet.http.*; import junit.framework.*; import com.dongantech.eshop.model.*; import com.dongantech.eshop.services.requests.*; import com.dongantech.eshop.services.responses.*; import com.dongantech.eshop.web.servlets.*; import com.dongantech.eshop.services.handlers.*; import org.easymock.*; import com.meterware.servletunit.*; import com.meterware.httpunit.*; public class TestMemberManagerServletResponseJsps extends DongAnShopJspFixture{ private ServletRunner servletRunner = null ; private RequestHandler mockFindHandler = null; private MockControl requestHandlerMockControl = null ; public void setUp(){ try { servletRunner = new ServletRunner(new File(DongAnShopJspFixture.getWebDeploymentDescriptorPath()),"/donganshop"); requestHandlerMockControl = MockControl.createControl(RequestHandler.class); mockFindHandler = (RequestHandler) requestHandlerMockControl.getMock(); } catch (Exception e) { e.printStackTrace(); fail("fail to new ServletRunner!"); } } public void testFoundResponseJsp() throws Exception{ Member preparedMember = new Member("lj"); Request preparedRequest = new SimpleRequest(); Map requestContext = new HashMap(); requestContext.put("memberFound",preparedMember); preparedRequest.setContext(requestContext); preparedRequest.setName(SimpleRequest.TYPE_FIND); mockFindHandler.process(preparedRequest); requestHandlerMockControl.setReturnValue(createMemberFoundResponse()); requestHandlerMockControl.replay(); //////////////////////////////////////////////////////////////////////////////////////////////////////// ServletUnitClient client = servletRunner.newClient(); WebRequest request = new GetMethodWebRequest("http://localhost/donganshop/members/memberManager"); request.setParameter("request",SimpleRequest.TYPE_FIND); request.setParameter("username","lj"); InvocationContext invocationContext = client.newInvocation(request); MemberManagerServlet servlet = (MemberManagerServlet) invocationContext.getServlet(); servlet.putIntoRequestHandlers("FIND",mockFindHandler); HttpServletRequest httpReq = invocationContext.getRequest(); HttpServletResponse httpResp = invocationContext.getResponse(); servlet.getPages().put(SimpleResponse.TYPE_FOUND,MemberManagerServlet.PAGE_FOUND); servlet.service(httpReq, httpResp); WebResponse response = invocationContext.getServletResponse(); System.out.println(response.getText()); } public void tearDown(){ requestHandlerMockControl.verify(); } public Response createMemberFoundResponse(){ Response resp = new SimpleResponse(); List<Member> foundMembers = new ArrayList(); foundMembers.add(new Member("lj")); foundMembers.add(new Member("lj520")); resp.setType(SimpleResponse.TYPE_FOUND); resp.getContext().put("members", foundMembers); return resp; } }