Search in sources :

Example 1 with MessagingMethodInvokerHelper

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"));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) Optional(java.util.Optional) Header(org.springframework.messaging.handler.annotation.Header) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with MessagingMethodInvokerHelper

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)));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Test(org.junit.Test)

Example 3 with MessagingMethodInvokerHelper

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)));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with MessagingMethodInvokerHelper

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)));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with MessagingMethodInvokerHelper

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"));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 MessagingMethodInvokerHelper (org.springframework.integration.handler.support.MessagingMethodInvokerHelper)8 GenericMessage (org.springframework.messaging.support.GenericMessage)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 Message (org.springframework.messaging.Message)3 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ServiceActivator (org.springframework.integration.annotation.ServiceActivator)1 GatewayProxyFactoryBean (org.springframework.integration.gateway.GatewayProxyFactoryBean)1 Header (org.springframework.messaging.handler.annotation.Header)1