INT-1281: BeanFactoryTypeConverter gives access in SpEL expressions to standard BeanFactory type conversion
This commit is contained in:
@@ -14,6 +14,9 @@
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -22,10 +25,10 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.internal.matchers.TypeSafeMatcher;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.integration.core.GenericMessage;
|
||||
import org.springframework.integration.core.StringMessage;
|
||||
@@ -50,6 +53,28 @@ public class ExpressionEvaluatingMessageProcessorTests {
|
||||
assertEquals("foo", processor.processMessage(new StringMessage("foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessMessageWithParameterCoercion() {
|
||||
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor("#target.stringify(payload)");
|
||||
processor.getEvaluationContext().setVariable("target", new TestTarget());
|
||||
assertEquals("2", processor.processMessage(new StringMessage("2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessMessageWithVoidResult() {
|
||||
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor("#target.ping(payload)");
|
||||
processor.getEvaluationContext().setVariable("target", new TestTarget());
|
||||
assertEquals(null, processor.processMessage(new StringMessage("2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessMessageWithParameterCoercionToNonPrimitive() {
|
||||
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor("#target.find(payload)");
|
||||
processor.getEvaluationContext().setVariable("target", new TestTarget());
|
||||
String result = (String) processor.processMessage(new StringMessage("classpath:*.properties"));
|
||||
assertTrue("Wrong result: "+result, result.contains("log4j.properties"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessMessageWithDollarInBrackets() {
|
||||
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor("headers['$id']");
|
||||
@@ -162,6 +187,22 @@ public class ExpressionEvaluatingMessageProcessorTests {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestTarget {
|
||||
|
||||
public String stringify(int number) {
|
||||
return number+"";
|
||||
}
|
||||
|
||||
public String find(Resource[] resources) {
|
||||
return Arrays.asList(resources).toString();
|
||||
}
|
||||
|
||||
public void ping(String input) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static final class CheckedException extends Exception {
|
||||
@@ -169,5 +210,5 @@ public class ExpressionEvaluatingMessageProcessorTests {
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.internal.matchers.TypeSafeMatcher;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.annotation.Header;
|
||||
@@ -150,6 +149,14 @@ public class MethodInvokingMessageProcessorTests {
|
||||
assertEquals("testing-1", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPayloadCoercedToString() {
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(
|
||||
new TestBean(), "acceptPayloadAndReturnObject");
|
||||
Object result = processor.processMessage(new GenericMessage<Integer>(123456789));
|
||||
assertEquals("123456789-1", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadAsMethodParameterAndMessageAsReturnValue() {
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(
|
||||
@@ -260,7 +267,7 @@ public class MethodInvokingMessageProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testProcessMessageBadExpression() throws Exception {
|
||||
expected.expect(new ExceptionCauseMatcher(ConversionFailedException.class));
|
||||
expected.expect(new ExceptionCauseMatcher(NumberFormatException.class));
|
||||
AnnotatedTestService service = new AnnotatedTestService();
|
||||
Method method = service.getClass().getMethod("integerMethod", Integer.class);
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
|
||||
@@ -310,7 +317,7 @@ public class MethodInvokingMessageProcessorTests {
|
||||
|
||||
@Test
|
||||
public void filterSelectsAnnotationMethodsOnly() {
|
||||
AmbiguousMethodBean bean = new AmbiguousMethodBean();
|
||||
OverloadedMethodBean bean = new OverloadedMethodBean();
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(bean, ServiceActivator.class);
|
||||
processor.processMessage(MessageBuilder.withPayload(123).build());
|
||||
assertNotNull(bean.lastArg);
|
||||
@@ -320,7 +327,7 @@ public class MethodInvokingMessageProcessorTests {
|
||||
|
||||
@Test
|
||||
public void filterSelectsNonVoidReturningMethodsOnly() {
|
||||
AmbiguousMethodBean bean = new AmbiguousMethodBean();
|
||||
OverloadedMethodBean bean = new OverloadedMethodBean();
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(bean, "foo", true);
|
||||
processor.processMessage(MessageBuilder.withPayload(true).build());
|
||||
assertNotNull(bean.lastArg);
|
||||
@@ -328,7 +335,16 @@ public class MethodInvokingMessageProcessorTests {
|
||||
assertEquals("true", bean.lastArg);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOverloadedNonVoidReturningMethodsWithExactMatchForType() {
|
||||
AmbiguousMethodBean bean = new AmbiguousMethodBean();
|
||||
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(bean, "foo", true);
|
||||
processor.processMessage(MessageBuilder.withPayload("true").build());
|
||||
assertNotNull(bean.lastArg);
|
||||
assertEquals(String.class, bean.lastArg.getClass());
|
||||
assertEquals("true", bean.lastArg);
|
||||
}
|
||||
|
||||
private static class ExceptionCauseMatcher extends TypeSafeMatcher<Exception> {
|
||||
private Throwable cause;
|
||||
private Class<? extends Exception> type;
|
||||
@@ -462,7 +478,6 @@ public class MethodInvokingMessageProcessorTests {
|
||||
this.lastArg = b;
|
||||
}
|
||||
|
||||
@ServiceActivator
|
||||
public String foo(String s) {
|
||||
this.lastArg = s;
|
||||
return s;
|
||||
@@ -475,4 +490,24 @@ public class MethodInvokingMessageProcessorTests {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method names create ambiguities, but the MethodResolver implementation
|
||||
* should filter out based on the annotation or the 'requiresReply' flag.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private static class OverloadedMethodBean {
|
||||
|
||||
private volatile Object lastArg = null;
|
||||
|
||||
public void foo(boolean b) {
|
||||
this.lastArg = b;
|
||||
}
|
||||
|
||||
@ServiceActivator
|
||||
public String foo(String s) {
|
||||
this.lastArg = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user