Search in sources :

Example 1 with MockHttpSession

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

the class LookupTableHelper_Test method testRegisterList.

@Test
public void testRegisterList() {
    final String key = "LookupTableHelper_Test.testRegisterList.key";
    UIContext uic = createUIContext();
    UIContextHolder.pushContext(uic);
    MockHttpSession session = new MockHttpSession();
    ServletRequest request = new ServletRequest(new MockHttpServletRequest(session));
    LookupTableHelper.registerList(key, request);
    // Use a new request to ensure that it was stored as a session attribute
    request = new ServletRequest(new MockHttpServletRequest(session));
    Assert.assertSame("Incorrect context returned", uic, LookupTableHelper.getContext(key, request));
}
Also used : ServletRequest(com.github.bordertech.wcomponents.servlet.ServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) UIContext(com.github.bordertech.wcomponents.UIContext) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Example 2 with MockHttpSession

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

the class WServlet_Test method testSubSessionsDisabledNoSSID.

@Test
public void testSubSessionsDisabledNoSSID() throws ServletException, IOException {
    Config.getInstance().setProperty(ConfigurationProperties.SERVLET_ENABLE_SUBSESSIONS, "false");
    MyWServlet servlet = new MyWServlet(new WText("test"));
    servlet.init(new MockServletConfig());
    MockHttpSession session = new MockHttpSession();
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, servlet.lastInvocationCount);
}
Also used : WText(com.github.bordertech.wcomponents.WText) 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) Test(org.junit.Test)

Example 3 with MockHttpSession

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

the class WServlet_Test method testSubSessionsEnabled.

@Test
public void testSubSessionsEnabled() throws ServletException, IOException {
    Config.getInstance().setProperty(ConfigurationProperties.SERVLET_ENABLE_SUBSESSIONS, "true");
    MyWServlet servlet = new MyWServlet(new WText("test"));
    servlet.init(new MockServletConfig());
    MockHttpSession session = new MockHttpSession();
    // Initial request will be missing a SSID, will create a new subsession (id = 0)
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    // Passing in an invalid SSID should return same subsession (id = 0)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "asdf");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, servlet.lastInvocationCount);
    // Missing SSID should create a new subession (id = 1)
    request = new MockHttpServletRequest(session);
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    // Should update the primary session (id = 0)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "0");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 3, servlet.lastInvocationCount);
    // Should update the secondary session (id = 1)
    request = new MockHttpServletRequest(session);
    request.setParameter("ssid", "1");
    servlet.service(request, new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 2, servlet.lastInvocationCount);
}
Also used : WText(com.github.bordertech.wcomponents.WText) 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) Test(org.junit.Test)

Example 4 with MockHttpSession

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

the class ServletRequest_Test method testSessionAttributeAccessors.

@Test
public void testSessionAttributeAccessors() {
    String attributeName = "ServletRequest_Test.testSessionAttributeAccessors.attributeName";
    String attributeValue1 = "ServletRequest_Test.testSessionAttributeAccessors.attributeValue1";
    String attributeValue2 = "ServletRequest_Test.testSessionAttributeAccessors.attributeValue2";
    // Test with no session
    MockHttpServletRequest backing = new MockHttpServletRequest();
    ServletRequest request = new ServletRequest(backing);
    Assert.assertNull("Attribute should be null if no session", request.getSessionAttribute(attributeName));
    request.setSessionAttribute(attributeName, attributeValue1);
    Assert.assertEquals("Incorrect attribute value after setSessionAttribute", attributeValue1, request.getSessionAttribute(attributeName));
    // Test with a session
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(attributeName, attributeValue1);
    backing = new MockHttpServletRequest(session);
    request = new ServletRequest(backing);
    Assert.assertEquals("Incorrect attribute value", attributeValue1, request.getSessionAttribute(attributeName));
    request.setSessionAttribute(attributeName, attributeValue2);
    Assert.assertEquals("Incorrect attribute value after setSessionAttribute", attributeValue2, request.getSessionAttribute(attributeName));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Example 5 with MockHttpSession

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession 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)

Aggregations

MockHttpSession (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession)18 Test (org.junit.Test)15 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)13 MockServletConfig (com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig)9 MockHttpServletResponse (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)6 WText (com.github.bordertech.wcomponents.WText)5 UIContext (com.github.bordertech.wcomponents.UIContext)3 WServletHelper (com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 MockContainer (com.github.bordertech.wcomponents.MockContainer)1 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)1 ServletRequest (com.github.bordertech.wcomponents.servlet.ServletRequest)1 WServletPerformance_Test (com.github.bordertech.wcomponents.servlet.WServletPerformance_Test)1 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)1 PrintWriter (java.io.PrintWriter)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1