use of org.springframework.integration.handler.support.MessagingMethodInvokerHelper in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testOptionalArgs.
@Test
public void testOptionalArgs() throws Exception {
class Foo {
private final Map<String, Object> arguments = new LinkedHashMap<String, Object>();
@SuppressWarnings("unused")
public void optionalHeaders(Optional<String> foo, @Header(value = "foo", required = false) String foo1, @Header("foo") Optional<String> foo2) {
this.arguments.put("foo", (foo.isPresent() ? foo.get() : null));
this.arguments.put("foo1", foo1);
this.arguments.put("foo2", (foo2.isPresent() ? foo2.get() : null));
}
}
Foo targetObject = new Foo();
MessagingMethodInvokerHelper helper = new MessagingMethodInvokerHelper(targetObject, (String) null, false);
helper.process(new GenericMessage<>(Optional.empty()));
assertNull(targetObject.arguments.get("foo"));
assertNull(targetObject.arguments.get("foo1"));
assertNull(targetObject.arguments.get("foo2"));
helper.process(MessageBuilder.withPayload("foo").setHeader("foo", "FOO").build());
assertEquals("foo", targetObject.arguments.get("foo"));
assertEquals("FOO", targetObject.arguments.get("foo1"));
assertEquals("FOO", targetObject.arguments.get("foo2"));
}
use of org.springframework.integration.handler.support.MessagingMethodInvokerHelper in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testInt3199MessageMethods.
@Test
public void testInt3199MessageMethods() throws Exception {
class Foo {
@SuppressWarnings("unused")
public String m1(Message<String> message) {
return message.getPayload();
}
@SuppressWarnings("unused")
public Integer m2(Message<Integer> message) {
return message.getPayload();
}
@SuppressWarnings("unused")
public Object m3(Message<?> message) {
return message.getPayload();
}
}
Foo targetObject = new Foo();
MessagingMethodInvokerHelper helper = new MessagingMethodInvokerHelper(targetObject, (String) null, false);
assertEquals("foo", helper.process(new GenericMessage<Object>("foo")));
assertEquals(1, helper.process(new GenericMessage<Object>(1)));
assertEquals(targetObject, helper.process(new GenericMessage<Object>(targetObject)));
}
use of org.springframework.integration.handler.support.MessagingMethodInvokerHelper in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testInt3199PrecedenceOfCandidates.
@Test
public void testInt3199PrecedenceOfCandidates() throws Exception {
class Foo {
@SuppressWarnings("unused")
public Object m1(Message<String> message) {
fail("This method must not be invoked");
return message;
}
@SuppressWarnings("unused")
public Object m2(String payload) {
return payload;
}
@SuppressWarnings("unused")
public Object m3() {
return "FOO";
}
}
Foo targetObject = new Foo();
MessagingMethodInvokerHelper helper = new MessagingMethodInvokerHelper(targetObject, (String) null, false);
assertEquals("foo", helper.process(new GenericMessage<Object>("foo")));
assertEquals("FOO", helper.process(new GenericMessage<Object>(targetObject)));
}
use of org.springframework.integration.handler.support.MessagingMethodInvokerHelper in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testInt3199TypedMethods.
@Test
public void testInt3199TypedMethods() throws Exception {
class Foo {
@SuppressWarnings("unused")
public String m1(String payload) {
return payload;
}
@SuppressWarnings("unused")
public Integer m2(Integer payload) {
return payload;
}
@SuppressWarnings("unused")
public Object m3(Object payload) {
return payload;
}
}
Foo targetObject = new Foo();
MessagingMethodInvokerHelper helper = new MessagingMethodInvokerHelper(targetObject, (String) null, false);
assertEquals("foo", helper.process(new GenericMessage<Object>("foo")));
assertEquals(1, helper.process(new GenericMessage<Object>(1)));
assertEquals(targetObject, helper.process(new GenericMessage<Object>(targetObject)));
}
use of org.springframework.integration.handler.support.MessagingMethodInvokerHelper in project spring-integration by spring-projects.
the class MethodInvokingMessageProcessorTests method testUseSpelInvoker.
@Test
public void testUseSpelInvoker() throws Exception {
UseSpelInvokerBean bean = new UseSpelInvokerBean();
MessagingMethodInvokerHelper<?> helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("foo", String.class), false);
Message<?> message = new GenericMessage<>("Test");
helper.process(message);
assertEquals(SpelCompilerMode.OFF, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("bar", String.class), false);
helper.process(message);
assertEquals(SpelCompilerMode.IMMEDIATE, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("baz", String.class), false);
helper.process(message);
assertEquals(SpelCompilerMode.MIXED, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("qux", String.class), false);
helper.process(message);
assertEquals(SpelCompilerMode.OFF, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("fiz", String.class), false);
try {
helper.process(message);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("No enum constant org.springframework.expression.spel.SpelCompilerMode.JUNK"));
}
helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("buz", String.class), false);
ConfigurableListableBeanFactory bf = mock(ConfigurableListableBeanFactory.class);
willAnswer(returnsFirstArg()).given(bf).resolveEmbeddedValue(anyString());
helper.setBeanFactory(bf);
try {
helper.process(message);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("UseSpelInvoker.compilerMode: Object of class [java.lang.Object] " + "must be an instance of class java.lang.String"));
}
// Check other CTORs
helper = new MessagingMethodInvokerHelper<>(bean, "bar", false);
helper.process(message);
assertEquals(SpelCompilerMode.IMMEDIATE, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
helper = new MessagingMethodInvokerHelper<>(bean, ServiceActivator.class, false);
helper.process(message);
assertEquals(SpelCompilerMode.MIXED, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
}
Aggregations