INT-2706: fix 'expected-response-type' primitives

* HttpRequestExecutingMessageHandler: change Class.forName() to ClassUtils.forName()
* HttpRequestExecutingMessageHandler: some polishing about DRY
* OutboundResponseTypeTests: tests about "byte[]" & "[B" values for 'expectedResponseTypeExpression'
* OutboundResponseTypeTests: polishing to make it more quickly
* OutboundResponseTypeTests contexts: versionless XSD

JIRA: https://jira.springsource.org/browse/INT-2706
This commit is contained in:
Artem Bilan
2012-08-09 12:22:34 +03:00
committed by Oleg Zhurakousky
parent 1cf52b92a8
commit 16d5377e8b
4 changed files with 91 additions and 64 deletions

View File

@@ -5,16 +5,16 @@
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd">
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeSetChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"
expected-response-type-expression="payload"/>
<int:channel id="replyChannel">
<int:queue/>
</int:channel>

View File

@@ -5,18 +5,23 @@
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd">
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="requestChannel"
reply-channel="replyChannel"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeSetChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resPrimitiveStringPresentationChannel"
reply-channel="replyChannel"
expected-response-type="[B"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeExpressionSetChannel"
reply-channel="replyChannel"

View File

@@ -17,6 +17,7 @@ package org.springframework.integration.http.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.OutputStream;
@@ -26,6 +27,10 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
@@ -34,6 +39,7 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
@@ -41,14 +47,35 @@ import com.sun.net.httpserver.HttpServer;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.2
*
* see https://jira.springsource.org/browse/INT-2397
* <p/>
* see https://jira.springsource.org/browse/INT-2397
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class OutboundResponseTypeTests {
private static HttpServer server;
private static MyHandler httpHandler;
@Autowired
private QueueChannel replyChannel;
@Autowired
private MessageChannel requestChannel;
@Autowired
private MessageChannel resTypeSetChannel;
@Autowired
private MessageChannel resPrimitiveStringPresentationChannel;
@Autowired
private MessageChannel resTypeExpressionSetChannel;
@BeforeClass
public static void createServer() throws Exception {
httpHandler = new MyHandler();
@@ -56,89 +83,84 @@ public class OutboundResponseTypeTests {
server.createContext("/testApps/outboundResponse", httpHandler);
server.start();
}
@AfterClass
public static void stopServer() throws Exception {
server.stop(0);
}
@Test
public void testDefaultResponseType() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context.xml", this.getClass());
MessageChannel channel = context.getBean("requestChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
channel.send(new GenericMessage<String>("Hello"));
Message<?> message = replyChannel.receive(5000);
public void testDefaultResponseType() throws Exception {
this.requestChannel.send(new GenericMessage<String>("Hello"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof ResponseEntity);
}
@Test
public void testWithResponseTypeSet() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context.xml", this.getClass());
MessageChannel channel = context.getBean("resTypeSetChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
channel.send(new GenericMessage<String>("Hello"));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeSet() throws Exception {
this.resTypeSetChannel.send(new GenericMessage<String>("Hello"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test
public void testWithResponseTypeExpressionSet() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context.xml", this.getClass());
MessageChannel channel = context.getBean("resTypeExpressionSetChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
channel.send(new GenericMessage<String>("java.lang.String"));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeExpressionSet() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<String>("java.lang.String"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test
public void testWithResponseTypeExpressionSetAsClass() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context.xml", this.getClass());
MessageChannel channel = context.getBean("resTypeExpressionSetChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
channel.send(new GenericMessage<Class<?>>(String.class));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeExpressionSetAsClass() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<Class<?>>(String.class));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test(expected=BeanDefinitionParsingException.class)
public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception{
@Test
public void testInt2706ResponseTypeExpressionAsPrimitive() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<String>("byte[]"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof byte[]);
}
new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context-fail.xml", this.getClass());
@Test
public void testInt2706ResponseTypePrimitiveArrayClassAsString() throws Exception {
this.resPrimitiveStringPresentationChannel.send(new GenericMessage<byte[]>("hello".getBytes()));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof byte[]);
}
@Test
public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception {
try {
new ClassPathXmlApplicationContext("OutboundResponseTypeTests-context-fail.xml", this.getClass());
fail("Expected BeansException");
}
catch (BeansException e) {
assertTrue(e instanceof BeanDefinitionParsingException);
assertTrue(e.getMessage().contains("The 'expected-response-type' and 'expected-response-type-expression' are mutually exclusive"));
}
}
static class MyHandler implements HttpHandler {
private String httpMethod = "POST";
public void setHttpMethod(String httpMethod){
public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
}
public void handle(HttpExchange t) throws IOException {
String requestMethod = t.getRequestMethod();
String response = null;
if (requestMethod.equalsIgnoreCase(this.httpMethod)){
if (requestMethod.equalsIgnoreCase(this.httpMethod)) {
response = httpMethod;
t.getResponseHeaders().add("Content-Type", MediaType.TEXT_PLAIN.toString()); //Required for Spring 3.0.x
t.sendResponseHeaders(200, response.length());