INT-1347: add convertExceptions to request handler and error-key/code to controller

This commit is contained in:
David Syer
2010-09-01 17:23:23 +00:00
parent 79b3998572
commit 754b9a198d
11 changed files with 407 additions and 179 deletions

View File

@@ -22,12 +22,13 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;
/**
@@ -136,4 +137,29 @@ public class HttpRequestHandlingControllerTests {
assertEquals("ABC", ((Message<?>) reply).getPayload());
}
@Test
public void testSendWithError() throws Exception {
QueueChannel requestChannel = new QueueChannel() {
@Override
protected boolean doSend(Message<?> message, long timeout) {
throw new RuntimeException("Planned");
}
};
HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
controller.setRequestChannel(requestChannel);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContent("hello".getBytes());
request.setContentType("text/plain");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView modelAndView = controller.handleRequest(request, response);
assertEquals(1, modelAndView.getModel().size());
Errors errors = (Errors) modelAndView.getModel().get("errors");
assertEquals(1, errors.getErrorCount());
ObjectError error = errors.getAllErrors().get(0);
assertEquals(3, error.getArguments().length);
assertTrue("Wrong message: "+error, ((String)error.getArguments()[1]).startsWith("failed to send Message"));
}
}

View File

@@ -19,8 +19,18 @@ package org.springframework.integration.http;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -92,4 +102,50 @@ public class HttpRequestHandlingMessagingGatewayTests {
assertEquals("HELLO", response.getContentAsString());
}
@Test
public void testExceptionConversion() throws Exception {
QueueChannel requestChannel = new QueueChannel() {
@Override
protected boolean doSend(Message<?> message, long timeout) {
throw new RuntimeException("Planned");
}
};
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setRequestChannel(requestChannel);
gateway.setConvertExceptions(true);
gateway.setMessageConverters(Arrays.<HttpMessageConverter<?>>asList(new DumbHttpMessageConverter()));
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept", "application/x-java-serialized-object");
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
gateway.handleRequest(request, response);
String content = response.getContentAsString();
assertEquals("Planned", content);
}
private static class DumbHttpMessageConverter extends AbstractHttpMessageConverter<Exception> {
public DumbHttpMessageConverter() {
setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
}
@Override
protected Exception readInternal(Class<? extends Exception> clazz, HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
return null;
}
@Override
protected boolean supports(Class<?> clazz) {
return true;
}
@Override
protected void writeInternal(Exception t, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
new PrintWriter(outputMessage.getBody()).append(t.getCause().getMessage()).flush();
}
}
}

View File

@@ -20,4 +20,6 @@
<inbound-channel-adapter id="putOrDeleteAdapter" channel="requests" supported-methods="PUT, delete"/>
<inbound-channel-adapter id="inboundController" channel="requests" view-name="foo" error-code="oops"/>
</beans:beans>

View File

@@ -36,6 +36,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.http.HttpRequestHandlingController;
import org.springframework.integration.http.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -62,6 +63,9 @@ public class HttpInboundChannelAdapterParserTests {
@Autowired
private HttpRequestHandlingMessagingGateway putOrDeleteAdapter;
@Autowired
private HttpRequestHandlingController inboundController;
@Test
@SuppressWarnings("unchecked")
@@ -137,6 +141,13 @@ public class HttpInboundChannelAdapterParserTests {
assertTrue(supportedMethods.contains(HttpMethod.DELETE));
}
@Test
public void testController() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundController);
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
}
@SuppressWarnings("serial")
private static class TestObject implements Serializable {

View File

@@ -17,6 +17,8 @@
<si:queue/>
</si:channel>
<inbound-gateway id="inboundGateway" request-channel="requests" reply-channel="responses"/>
<inbound-gateway id="inboundGateway" request-channel="requests" reply-channel="responses" convert-exceptions="true"/>
<inbound-gateway id="inboundController" request-channel="requests" reply-channel="responses" view-name="foo" error-code="oops"/>
</beans:beans>

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.http.config;
import static org.hamcrest.CoreMatchers.any;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.springframework.integration.test.util.TestUtils.getPropertyValue;
@@ -28,10 +29,12 @@ import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.http.HttpRequestHandlingController;
import org.springframework.integration.http.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.MockHttpServletRequest;
import org.springframework.integration.http.MockHttpServletResponse;
@@ -49,6 +52,9 @@ public class HttpInboundGatewayParserTests {
@Autowired
private HttpRequestHandlingMessagingGateway gateway;
@Autowired
private HttpRequestHandlingController inboundController;
@Autowired
private SubscribableChannel requests;
@@ -60,6 +66,7 @@ public class HttpInboundGatewayParserTests {
public void checkConfig() {
assertNotNull(gateway);
assertThat((Boolean) getPropertyValue(gateway, "expectReply"), is(true));
assertThat((Boolean) getPropertyValue(gateway, "convertExceptions"), is(true));
assertThat((PollableChannel) getPropertyValue(gateway, "replyChannel"), is(responses));
}
@@ -76,4 +83,11 @@ public class HttpInboundGatewayParserTests {
assertThat(response.getContentType(), is("application/x-java-serialized-object"));
}
@Test
public void testController() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundController);
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
}
}