INT-4197: Fix Annotation Case when not messages
JIRA: https://jira.spring.io/browse/INT-4197 Since `Collection<Message<?>>` can be possible only if we are dealing with messages as a group (`MethodInvokingMessageListProcessor`), the case with the `List<?>` param for Messaging Annotation method should not be treated as candidate for `messages` collection. * Add `this.canProcessMessageList` condition to avoid `messages` SpEL expression when we are not in the `MethodInvokingMessageListProcessor` environment **Cherry-pick to 4.3.x** * Fix Array creating formatting * Add comment about `ReflectiveMethodExecutor` to the `testRouterWithListParam()` * Send one test data as as `Collection` to be sure that `@Router` parameters mapping works well
This commit is contained in:
committed by
Gary Russell
parent
3ce1fcb110
commit
dd72b9b63d
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -752,8 +752,9 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
|
||||
sb.append("message");
|
||||
this.setExclusiveTargetParameterType(parameterTypeDescriptor, methodParameter);
|
||||
}
|
||||
else if ((parameterTypeDescriptor.isAssignableTo(messageListTypeDescriptor)
|
||||
|| parameterTypeDescriptor.isAssignableTo(messageArrayTypeDescriptor))) {
|
||||
else if (this.canProcessMessageList &&
|
||||
(parameterTypeDescriptor.isAssignableTo(messageListTypeDescriptor)
|
||||
|| parameterTypeDescriptor.isAssignableTo(messageArrayTypeDescriptor))) {
|
||||
sb.append("messages");
|
||||
this.setExclusiveTargetParameterType(parameterTypeDescriptor, methodParameter);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,34 +18,47 @@ package org.springframework.integration.config.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.integration.annotation.MessageEndpoint;
|
||||
import org.springframework.integration.annotation.Router;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.test.util.TestUtils.TestApplicationContext;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class RouterAnnotationPostProcessorTests {
|
||||
|
||||
private TestApplicationContext context = TestUtils.createTestApplicationContext();
|
||||
private final TestApplicationContext context = TestUtils.createTestApplicationContext();
|
||||
|
||||
private DirectChannel inputChannel = new DirectChannel();
|
||||
private final DirectChannel inputChannel = new DirectChannel();
|
||||
|
||||
private QueueChannel outputChannel = new QueueChannel();
|
||||
private final QueueChannel outputChannel = new QueueChannel();
|
||||
|
||||
private final DirectChannel routingChannel = new DirectChannel();
|
||||
|
||||
private final QueueChannel integerChannel = new QueueChannel();
|
||||
|
||||
private final QueueChannel stringChannel = new QueueChannel();
|
||||
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
context.registerChannel("input", inputChannel);
|
||||
context.registerChannel("output", outputChannel);
|
||||
context.registerChannel("routingChannel", routingChannel);
|
||||
context.registerChannel("integerChannel", integerChannel);
|
||||
context.registerChannel("stringChannel", stringChannel);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +76,26 @@ public class RouterAnnotationPostProcessorTests {
|
||||
context.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRouterWithListParam() {
|
||||
MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
|
||||
postProcessor.setBeanFactory(context.getBeanFactory());
|
||||
postProcessor.afterPropertiesSet();
|
||||
TestRouter testRouter = new TestRouter();
|
||||
postProcessor.postProcessAfterInitialization(testRouter, "test");
|
||||
context.refresh();
|
||||
|
||||
routingChannel.send(new GenericMessage<>(Collections.singletonList("foo")));
|
||||
Message<?> replyMessage = stringChannel.receive(0);
|
||||
assertEquals(Collections.singletonList("foo"), replyMessage.getPayload());
|
||||
|
||||
// The SpEL ReflectiveMethodExecutor does a conversion of a single value to a List
|
||||
routingChannel.send(new GenericMessage<Integer>(2));
|
||||
replyMessage = integerChannel.receive(0);
|
||||
assertEquals(2, replyMessage.getPayload());
|
||||
context.stop();
|
||||
}
|
||||
|
||||
|
||||
@MessageEndpoint
|
||||
public static class TestRouter {
|
||||
@@ -71,6 +104,20 @@ public class RouterAnnotationPostProcessorTests {
|
||||
public String test(String s) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Router(inputChannel = "routingChannel")
|
||||
public String route(List<?> payload) {
|
||||
if (payload.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (payload.get(0) instanceof Integer) {
|
||||
return "integerChannel";
|
||||
}
|
||||
else {
|
||||
return "stringChannel";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -96,6 +96,6 @@
|
||||
</router>
|
||||
|
||||
<beans:bean id="testChannelResolver"
|
||||
class="org.springframework.integration.router.config.RouterParserTests$TestChannelResover"/>
|
||||
class="org.springframework.integration.router.config.RouterParserTests$TestChannelResolver"/>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -285,7 +285,7 @@ public class RouterParserTests {
|
||||
}
|
||||
|
||||
|
||||
static class TestChannelResover implements DestinationResolver<MessageChannel> {
|
||||
static class TestChannelResolver implements DestinationResolver<MessageChannel> {
|
||||
|
||||
@Override
|
||||
public MessageChannel resolveDestination(String channelName) {
|
||||
|
||||
Reference in New Issue
Block a user