Search in sources :

Example 11 with MockHttpSession

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

the class WServlet_Test method testServiceAjaxRequest.

@Test
public void testServiceAjaxRequest() throws ServletException, IOException {
    AjaxTestUI ui = new AjaxTestUI();
    ui.setLocked(true);
    MockHttpSession session1 = new MockHttpSession();
    UIContextImpl uic1 = new UIContextImpl();
    uic1.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
    uic1.setUI(ui);
    uic1.getEnvironment().setSessionToken("1");
    uic1.getEnvironment().setStep(1);
    MockHttpSession session2 = new MockHttpSession();
    UIContextImpl uic2 = new UIContextImpl();
    uic2.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
    uic2.setUI(ui);
    uic2.getEnvironment().setSessionToken("2");
    // Request cycle for session 1
    setActiveContext(uic1);
    ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session1)));
    ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session1)));
    ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    // Request cycle for session 2
    setActiveContext(uic2);
    ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session2)));
    ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session2)));
    ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    // check handle request / paint counts for each session - label should not have been painted for either.
    setActiveContext(uic1);
    Assert.assertEquals("HandleRequest should have been called for main panel in session1", 1, ui.mainPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for ajax panel in session1", 1, ui.ajaxPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for label panel in session1", 1, ui.label.getHandleRequestCount());
    Assert.assertEquals("Main panel should have painted in session1", 1, ui.mainPanel.getPaintCount());
    Assert.assertEquals("Ajax panel should have painted in session1", 1, ui.ajaxPanel.getPaintCount());
    Assert.assertEquals("Label should not have painted in session1", 0, ui.label.getPaintCount());
    setActiveContext(uic2);
    Assert.assertEquals("HandleRequest should have been called for main panel in session2", 1, ui.mainPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for ajax panel in session2", 1, ui.ajaxPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for label panel in session2", 1, ui.label.getHandleRequestCount());
    Assert.assertEquals("Main panel should have painted in session2", 1, ui.mainPanel.getPaintCount());
    Assert.assertEquals("Ajax panel should have painted in session2", 1, ui.ajaxPanel.getPaintCount());
    Assert.assertEquals("Label should not have painted in session2", 0, ui.label.getPaintCount());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 12 with MockHttpSession

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

the class WServlet_Test method testServiceNormalRequest.

@Test
public void testServiceNormalRequest() throws ServletException, IOException {
    MockServletConfig config = new MockServletConfig();
    config.setInitParameter(WServlet.WServletHelper.ONGOING_URL_SUFFIX, "foo");
    MockContainer content = new MockContainer();
    content.add(new WText(LABEL_TEXT));
    content.setLocked(true);
    MyWServlet servlet = new MyWServlet(content);
    servlet.init(config);
    MockHttpSession session1 = new MockHttpSession();
    MockHttpSession session2 = new MockHttpSession();
    sendRequest(session1, servlet);
    sendRequest(session2, servlet);
    sendRequest(session1, servlet);
    // check handle request / paint counts for each session
    UIContext uic = getContextForSession(servlet, session1);
    setActiveContext(uic);
    Assert.assertEquals("Incorrect handle request count for session1", 2, content.getHandleRequestCount());
    Assert.assertEquals("Incorrect paint count for session1", 2, content.getPaintCount());
    uic = getContextForSession(servlet, session2);
    setActiveContext(uic);
    Assert.assertEquals("Incorrect handle request count for session2", 1, content.getHandleRequestCount());
    Assert.assertEquals("Incorrect paint count for session2", 1, content.getPaintCount());
}
Also used : MockContainer(com.github.bordertech.wcomponents.MockContainer) WText(com.github.bordertech.wcomponents.WText) UIContext(com.github.bordertech.wcomponents.UIContext) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Example 13 with MockHttpSession

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

the class WServlet_Test method testSubSessionsEnabledNoSSID.

@Test
public void testSubSessionsEnabledNoSSID() 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();
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, servlet.lastInvocationCount);
    // No SSID should trigger a new subsession
    servlet.service(new MockHttpServletRequest(session), new MockHttpServletResponse());
    Assert.assertEquals("Incorrect invocation count", 1, 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 14 with MockHttpSession

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

the class ServletRequest_Test method testRenderParameterAccessors.

@Test
public void testRenderParameterAccessors() {
    String renderParamName = "ServletRequest_Test.testRenderParameterAccessors.renderParamName";
    String renderParamValue = "ServletRequest_Test.testRenderParameterAccessors.renderParamValue";
    // Set the render parameters on a request
    HttpSession session = new MockHttpSession();
    MockHttpServletRequest backing = new MockHttpServletRequest(session);
    ServletRequest request = new ServletRequest(backing);
    request.setRenderParameter(renderParamName, renderParamValue);
    Assert.assertEquals("Incorrect render parameter value after setRenderParameter", renderParamValue, request.getRenderParameter(renderParamName));
    // Test that the render parameters were stored in the session
    request = new ServletRequest(new MockHttpServletRequest(session));
    Assert.assertEquals("Incorrect render parameter value from another request", renderParamValue, request.getRenderParameter(renderParamName));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Example 15 with MockHttpSession

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

the class ServletRequest_Test method testAppSessionAttributeAccessors.

@Test
public void testAppSessionAttributeAccessors() {
    String attributeName = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeName";
    String attributeValue1 = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeValue1";
    String attributeValue2 = "ServletRequest_Test.testAppSessionAttributeAccessors.attributeValue2";
    // Test with no session
    MockHttpServletRequest backing = new MockHttpServletRequest();
    ServletRequest request = new ServletRequest(backing);
    Assert.assertNull("Attribute should be null if no session", request.getAppSessionAttribute(attributeName));
    request.setAppSessionAttribute(attributeName, attributeValue1);
    Assert.assertEquals("Incorrect attribute value after setAppSessionAttribute", attributeValue1, request.getAppSessionAttribute(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.getAppSessionAttribute(attributeName));
    request.setAppSessionAttribute(attributeName, attributeValue2);
    Assert.assertEquals("Incorrect attribute value after setAppSessionAttribute", attributeValue2, request.getAppSessionAttribute(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)

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