Search in sources :

Example 6 with ActionEscape

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

the class WrongStepAjaxInterceptor_Test method doAjaxRequest.

/**
 * Does an AJAX request for the app.
 *
 * @param app the MyApp instance to do an AJAX request for.
 * @param clientStep the client-side step counter
 * @param serverStep the server-side step counter
 * @return the response object.
 */
private MockResponse doAjaxRequest(final MyApp app, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment(APP_POSTPATH, "http://localhost", "");
    env.setStep(serverStep);
    env.setSessionToken("T");
    uic.setEnvironment(env);
    uic.setUI(app);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    MockResponse response = new MockResponse();
    // Create interceptors
    AjaxSetupInterceptor ajaxSetupInterceptor = new AjaxSetupInterceptor();
    WrongStepAjaxInterceptor wrongStepInterceptor = new WrongStepAjaxInterceptor();
    AjaxPageShellInterceptor ajaxPageInterceptor = new AjaxPageShellInterceptor();
    AjaxInterceptor ajaxInterceptor = new AjaxInterceptor();
    ajaxPageInterceptor.setBackingComponent(ajaxInterceptor);
    wrongStepInterceptor.setBackingComponent(ajaxPageInterceptor);
    ajaxSetupInterceptor.setBackingComponent(wrongStepInterceptor);
    ajaxSetupInterceptor.attachUI(app);
    ajaxSetupInterceptor.attachResponse(response);
    // Action phase
    try {
        AjaxHelper.registerComponent(app.target.getId(), app.trigger.getId());
        request.setParameter(WServlet.AJAX_TRIGGER_PARAM_NAME, app.trigger.getId());
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
        ajaxSetupInterceptor.serviceRequest(request);
        ajaxSetupInterceptor.preparePaint(request);
        // Render phase
        ajaxSetupInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ActionEscape ignored) {
    // is thrown to skip render phase
    }
    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) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet)

Example 7 with ActionEscape

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

the class WrongStepServerInterceptor_Test method testServiceRequestIncorrectSequenceWithRedirect.

@Test
public void testServiceRequestIncorrectSequenceWithRedirect() {
    // Set redirect parameter
    Configuration config = Config.copyConfiguration(originalConfig);
    config.addProperty(ConfigurationProperties.STEP_ERROR_URL, "test.url");
    Config.setConfiguration(config);
    uic.getEnvironment().setStep(3);
    request.setParameter(Environment.STEP_VARIABLE, "1");
    interceptor.attachResponse(new MockResponse());
    try {
        interceptor.serviceRequest(request);
        Assert.fail("Interceptor did not detect wrong step");
    } catch (ActionEscape e) {
        Assert.assertFalse("Action phase should not have occurred for step error with redirect", component.handleRequestCalled);
        Assert.assertFalse("Handle Step Error should not have occurred for redirect", component.handleStepErrorCalled);
        Assert.assertEquals("Step should not have been incremented", 3, uic.getEnvironment().getStep());
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) Configuration(org.apache.commons.configuration.Configuration) Test(org.junit.Test)

Aggregations

ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 UIContext (com.github.bordertech.wcomponents.UIContext)6 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)3 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)2 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 IOException (java.io.IOException)2 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)1 ContentEscape (com.github.bordertech.wcomponents.ContentEscape)1 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)1 Escape (com.github.bordertech.wcomponents.Escape)1 Request (com.github.bordertech.wcomponents.Request)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 Configuration (org.apache.commons.configuration.Configuration)1 Test (org.junit.Test)1