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;
}
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());
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations