/** * Times the other servlet execution looping the given number of times and returns the elapsed * time. * * @param count the number of times to loop. * @return the elapsed time, in nanoseconds. */ private long timeOtherServlet(final int count) throws Exception { final SimpleServlet servlet = new SimpleServlet(); servlet.init(new MockServletConfig()); final MockHttpSession simpleServletSession = new MockHttpSession(); // JIT warm-up for (int i = 0; i < count; i++) { sendOtherServletRequest(servlet, simpleServletSession, i); } simpleServletSession.getAttributes().clear(); Runnable runnable = new Runnable() { public void run() { try { for (int i = 0; i < count; i++) { sendOtherServletRequest(servlet, simpleServletSession, i); } } catch (Exception e) { log.error("Failed to execute test", e); } } }; return time(runnable); }
/** * Basic sanity-test to ensure that the other app is performing all the processing that it should. */ @Test public void testOtherServletAppCorrectness() throws Exception { SimpleServlet servlet = new SimpleServlet(); servlet.init(new MockServletConfig()); MockHttpSession session = new MockHttpSession(); sendOtherServletRequest(servlet, session, 0); sendOtherServletRequest(servlet, session, 1); SimpleFormBean bean = servlet.getFormBean(new MockHttpServletRequest(session)); Assert.assertEquals("Incorrect property1 value", "p1_1", bean.getProperty1()); Assert.assertEquals("Incorrect property2 value", "p2_1", bean.getProperty2()); }
/** * Invokes the other servlet request processing. * * @param servlet the servlet to invoke request processing on. * @param uic the user context to use. */ private void sendOtherServletRequest( final SimpleServlet servlet, final HttpSession session, final int step) throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(session); MockHttpServletResponse response = new MockHttpServletResponse(); if (step > 0) { request.setMethod("POST"); request.setRequestURI("http://localhost/app"); request.setParameter("formBean.property1", "p1_" + step); request.setParameter("formBean.property2", "p2_" + step); request.setParameter("submit", "Submit"); } else { request.setMethod("GET"); request.setRequestURI("http://localhost/app"); } servlet.service(request, response); }