Search in sources :

Example 6 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class ServletResponse_Test method testGetWriter.

@Test
public void testGetWriter() throws IOException {
    MockHttpServletResponse backing = new MockHttpServletResponse();
    ServletResponse response = new ServletResponse(backing);
    Assert.assertSame("Incorrect writer", backing.getWriter(), response.getWriter());
}
Also used : MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class ServletResponse_Test method testSendError.

@Test
public void testSendError() throws IOException {
    // something relatively obscure
    int errorCode = 505;
    MockHttpServletResponse backing = new MockHttpServletResponse();
    ServletResponse response = new ServletResponse(backing);
    response.sendError(errorCode, "dummy");
    Assert.assertEquals("Incorrect error", errorCode, backing.getStatus());
}
Also used : MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class WServletPerformance_Test method sendWServletRequest.

/**
 * Invokes WComponent servlet processing.
 *
 * @param servlet the servlet to invoke request processing on.
 * @param session the current session.
 * @param step the step count
 * @param token the session token
 * @throws Exception an exception
 */
private void sendWServletRequest(final WServlet servlet, final HttpSession session, final int step, final String token) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (step > 0) {
        request.setMethod("POST");
        request.setRequestURI("http://localhost/app");
        // These are hard-coded to avoid overhead during performance testing
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(step));
        request.setParameter(Environment.SESSION_TOKEN_VARIABLE, token);
        request.setParameter("_0b", "p1_" + step);
        request.setParameter("_0d", "p2_" + step);
        request.setParameter("_0e", "x");
    } else {
        request.setMethod("GET");
        request.setRequestURI("http://localhost/app");
    }
    servlet.service(request, response);
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Example 9 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class WServletPerformance_Test method timeWServlet.

/**
 * Times the WServlet 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.
 * @throws Exception an exception
 */
private long timeWServlet(final int count) throws Exception {
    final SimpleWServlet servlet = new SimpleWServlet();
    servlet.init(new MockServletConfig());
    final MockHttpSession simpleWServletSession = new MockHttpSession();
    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);
    // Get token
    WServletHelper helper = new WServletHelper(servlet, new MockHttpServletRequest(simpleWServletSession), new MockHttpServletResponse());
    String token = helper.getUIContext().getEnvironment().getSessionToken();
    // JIT warm-up
    for (int i = 1; i < count; i++) {
        sendWServletRequest(servlet, simpleWServletSession, i, token);
    }
    simpleWServletSession.getAttributes().clear();
    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);
    final String token2 = helper.getUIContext().getEnvironment().getSessionToken();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 1; i < count; i++) {
                    sendWServletRequest(servlet, simpleWServletSession, i, token2);
                }
            } catch (Exception e) {
                LOG.error("Failed to execute test", e);
            }
        }
    };
    return time(runnable);
}
Also used : WServletHelper(com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) ServletException(javax.servlet.ServletException)

Example 10 with MockHttpServletResponse

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse in project wcomponents by BorderTech.

the class WComponentsPerformance_Test method sendSimpleRequest.

/**
 * Invokes simple request processing.
 *
 * @param session the http session
 * @param step the step count
 */
private void sendSimpleRequest(final HttpSession session, final int step) {
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    request.setParameter("formBean.property1", "p1_" + step);
    request.setParameter("formBean.property2", "p2_" + step);
    request.setParameter("submit", "Submit");
    doSimpleRequest(request, new MockHttpServletResponse());
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Aggregations

MockHttpServletResponse (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)21 Test (org.junit.Test)15 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)13 MockServletConfig (com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig)8 MockHttpSession (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession)6 WText (com.github.bordertech.wcomponents.WText)5 WServletHelper (com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper)2 UIContext (com.github.bordertech.wcomponents.UIContext)1 ServletException (javax.servlet.ServletException)1