Search in sources :

Example 81 with MockRequest

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

the class DataListInterceptor_Test method testInterecptor.

@Test
public void testInterecptor() throws XpathException, SAXException, IOException {
    String tableKey = TestLookupTable.CACHEABLE_DAY_OF_WEEK_TABLE;
    // Create interceptor
    DataListInterceptor interceptor = new DataListInterceptor();
    interceptor.attachUI(new DefaultWComponent());
    // Action phase
    MockRequest request = new MockRequest();
    request.setParameter(WServlet.DATA_LIST_PARAM_NAME, tableKey);
    interceptor.serviceRequest(request);
    // Render phase
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(response.getWriter())));
    String xml = response.getWriterOutput();
    // Ensure that the data matches the test table.
    List<TestLookupTable.TableEntry> table = (List<TestLookupTable.TableEntry>) Factory.newInstance(LookupTable.class).getTable(tableKey);
    assertXpathEvaluatesTo(String.valueOf(table.size()), "count(/ui:datalist/ui:option)", xml);
    for (int i = 0; i < table.size(); i++) {
        assertXpathEvaluatesTo(table.get(i).getCode(), "/ui:datalist/ui:option[" + (i + 1) + "]/@value", xml);
        assertXpathEvaluatesTo(table.get(i).getDesc(), "/ui:datalist/ui:option[" + (i + 1) + "]/text()", xml);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) TestLookupTable(com.github.bordertech.wcomponents.TestLookupTable) List(java.util.List) DefaultWComponent(com.github.bordertech.wcomponents.DefaultWComponent) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 82 with MockRequest

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

the class AjaxErrorInterceptor_Test method testHandleActionAjaxError.

@Test
public void testHandleActionAjaxError() throws IOException {
    final AjaxTriggerException excp = new AjaxTriggerException("Simulate ajax trigger error");
    // Throw trigger exception in chain
    InterceptorComponent chain = new InterceptorComponent() {

        @Override
        public void serviceRequest(final Request request) {
            throw excp;
        }
    };
    // Setup interceptor
    AjaxErrorInterceptor ajax = new AjaxErrorInterceptor();
    ajax.setBackingComponent(chain);
    // Process Action
    try {
        ajax.serviceRequest(new MockRequest());
        Assert.fail("Trigger exception not handled correctly");
    } catch (ErrorCodeEscape e) {
        Assert.assertTrue("Incorrect trigger exception message", e.getMessage().contains(AJAX_ERROR));
        Assert.assertEquals("Incorrect escape code for trigger exception", HttpServletResponse.SC_BAD_REQUEST, e.getCode());
        Assert.assertEquals("Cause should be the original trigger exception", excp, e.getCause());
    }
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Request(com.github.bordertech.wcomponents.Request) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 83 with MockRequest

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

the class AjaxErrorInterceptor_Test method testHandleActionSessionToken.

@Test
public void testHandleActionSessionToken() throws IOException {
    final SessionTokenException excp = new SessionTokenException("Simulate session error");
    // Throw session exception in chain
    InterceptorComponent chain = new InterceptorComponent() {

        @Override
        public void serviceRequest(final Request request) {
            throw excp;
        }
    };
    // Setup interceptor
    AjaxErrorInterceptor ajax = new AjaxErrorInterceptor();
    ajax.setBackingComponent(chain);
    // Process Action
    try {
        ajax.serviceRequest(new MockRequest());
        Assert.fail("Session exception not handled correctly");
    } catch (ErrorCodeEscape e) {
        Assert.assertTrue("Incorrect session exception message", e.getMessage().contains(SESSION_ERROR));
        Assert.assertEquals("Incorrect escape code for session exception", HttpServletResponse.SC_BAD_REQUEST, e.getCode());
        Assert.assertEquals("Cause should be the original session exception", excp, e.getCause());
    }
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Request(com.github.bordertech.wcomponents.Request) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 84 with MockRequest

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

the class AjaxErrorInterceptor_Test method testHandleActionEscape.

@Test(expected = Escape.class)
public void testHandleActionEscape() throws IOException {
    // Throw escape exception in chain
    InterceptorComponent chain = new InterceptorComponent() {

        @Override
        public void serviceRequest(final Request request) {
            throw new Escape();
        }
    };
    // Setup interceptor
    AjaxErrorInterceptor ajax = new AjaxErrorInterceptor();
    ajax.setBackingComponent(chain);
    // Process Action
    ajax.serviceRequest(new MockRequest());
}
Also used : Escape(com.github.bordertech.wcomponents.Escape) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Request(com.github.bordertech.wcomponents.Request) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 85 with MockRequest

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

the class AjaxSetupInterceptor_Test method testTriggerIdValid.

@Test
public void testTriggerIdValid() throws IOException {
    // Setup App
    MyApp app = setupApp();
    // Setup request
    MockRequest request = setupRequest(app);
    // Do Service Request
    doAjaxServiceRequest(app, request);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Aggregations

MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)250 Test (org.junit.Test)216 UIContext (com.github.bordertech.wcomponents.UIContext)22 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)16 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)13 PrintWriter (java.io.PrintWriter)13 Request (com.github.bordertech.wcomponents.Request)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)8 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)7 StringWriter (java.io.StringWriter)7 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)6 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 BigDecimal (java.math.BigDecimal)5 Escape (com.github.bordertech.wcomponents.Escape)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3