Search in sources :

Example 1 with ErrorCodeEscape

use of com.github.bordertech.wcomponents.ErrorCodeEscape in project wcomponents by BorderTech.

the class WrongStepContentInterceptor_Test method sendContentRequest.

/**
 * Utility method to send a mock request to the application.
 *
 * @param target the target content.
 * @param clientStep the client-side step count
 * @param serverStep the server-side step count
 * @return the response.
 */
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    uic.setUI(WebUtilities.getTop(target));
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
    env.setStep(serverStep);
    uic.setEnvironment(env);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, target.getId());
    request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
    MockResponse response = new MockResponse();
    InterceptorComponent interceptor = new WrongStepContentInterceptor();
    interceptor.attachUI(uic.getUI());
    interceptor.attachResponse(response);
    // Handle the request. This will either return the content or a step error
    try {
        interceptor.serviceRequest(request);
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ContentEscape escape) {
        try {
            // Content has been returned
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write content");
        }
    } catch (ErrorCodeEscape escape) {
        try {
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write error content");
        }
    } catch (ActionEscape ignored) {
    // don't care
    }
    // Step error
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) ContentEscape(com.github.bordertech.wcomponents.ContentEscape) IOException(java.io.IOException) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 2 with ErrorCodeEscape

use of com.github.bordertech.wcomponents.ErrorCodeEscape 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 3 with ErrorCodeEscape

use of com.github.bordertech.wcomponents.ErrorCodeEscape 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 4 with ErrorCodeEscape

use of com.github.bordertech.wcomponents.ErrorCodeEscape in project wcomponents by BorderTech.

the class TargetableErrorInterceptor_Test method testHandlePreparePaintSystemError.

@Test
public void testHandlePreparePaintSystemError() throws IOException {
    final SystemException excp = new SystemException("Simulate prepare paint system error");
    // Throw system exception in chain
    InterceptorComponent chain = new InterceptorComponent() {

        @Override
        public void preparePaint(final Request request) {
            throw excp;
        }
    };
    // Setup interceptor
    TargetableErrorInterceptor interceptor = new TargetableErrorInterceptor();
    interceptor.setBackingComponent(chain);
    // Process Action
    try {
        interceptor.preparePaint(new MockRequest());
        Assert.fail("System exception not handled correctly in prepare paint");
    } catch (ErrorCodeEscape e) {
        Assert.assertTrue("Incorrect system exception message in prepare paint", e.getMessage().contains(CONTENT_ERROR));
        Assert.assertEquals("Incorrect escape code for system exception in prepare paint", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getCode());
        Assert.assertEquals("Cause should be the original system exception in prepare paint", excp, e.getCause());
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) 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 5 with ErrorCodeEscape

use of com.github.bordertech.wcomponents.ErrorCodeEscape in project wcomponents by BorderTech.

the class TargetableErrorInterceptor_Test method testHandleActionSystemError.

@Test
public void testHandleActionSystemError() throws IOException {
    final SystemException excp = new SystemException("Simulate action phase system error");
    // Throw system exception in chain
    InterceptorComponent chain = new InterceptorComponent() {

        @Override
        public void serviceRequest(final Request request) {
            throw excp;
        }
    };
    // Setup interceptor
    TargetableErrorInterceptor interceptor = new TargetableErrorInterceptor();
    interceptor.setBackingComponent(chain);
    // Process Action
    try {
        interceptor.serviceRequest(new MockRequest());
        Assert.fail("System exception not handled correctly");
    } catch (ErrorCodeEscape e) {
        Assert.assertTrue("Incorrect system exception message", e.getMessage().contains(CONTENT_ERROR));
        Assert.assertEquals("Incorrect escape code for system exception", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getCode());
        Assert.assertEquals("Cause should be the original system exception", excp, e.getCause());
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) 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)

Aggregations

ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)9 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)9 Request (com.github.bordertech.wcomponents.Request)8 Test (org.junit.Test)8 SystemException (com.github.bordertech.wcomponents.util.SystemException)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)1 ContentEscape (com.github.bordertech.wcomponents.ContentEscape)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)1 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)1 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)1 IOException (java.io.IOException)1