INT-1403 verifying a valid payload expression for void-returning methods with @Publisher
This commit is contained in:
@@ -79,6 +79,11 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
|
||||
? methodPayloadAnnotation.value()
|
||||
: "#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME;
|
||||
}
|
||||
if (payloadExpression == null || payloadExpression.contains("#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME)) {
|
||||
Assert.isTrue(!void.class.equals(method.getReturnType()),
|
||||
"When defining @Publisher on a void-returning method, an explicit payload " +
|
||||
"expression that does not rely upon a #return value is required.");
|
||||
}
|
||||
Annotation[][] annotationArray = method.getParameterAnnotations();
|
||||
for (int i = 0; i < annotationArray.length; i++) {
|
||||
Annotation[] parameterAnnotations = annotationArray[i];
|
||||
|
||||
@@ -37,12 +37,21 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void channelName() {
|
||||
Method method = getMethod("methodWithChannelAndReturnAsPayload");
|
||||
public void channelNameAndExplicitReturnValuePayload() {
|
||||
Method method = getMethod("methodWithChannelAndExplicitReturnAsPayload");
|
||||
String channelName = source.getChannelName(method);
|
||||
String payloadExpression = source.getPayloadExpression(method);
|
||||
assertEquals("foo", channelName);
|
||||
assertEquals("#method", payloadExpression);
|
||||
assertEquals("#return", payloadExpression);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void channelNameAndEmptyPayloadAnnotation() {
|
||||
Method method = getMethod("methodWithChannelAndEmptyPayloadAnnotation");
|
||||
String channelName = source.getChannelName(method);
|
||||
String payloadExpression = source.getPayloadExpression(method);
|
||||
assertEquals("foo", channelName);
|
||||
assertEquals("#return", payloadExpression);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,6 +76,21 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
assertEquals("#args['2']", headerMap.get("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void voidReturnWithValidPayloadExpression() {
|
||||
Method method = getMethod("methodWithVoidReturnAndMethodNameAsPayload");
|
||||
String channelName = source.getChannelName(method);
|
||||
String payloadExpression = source.getPayloadExpression(method);
|
||||
assertEquals("foo", channelName);
|
||||
assertEquals("#method", payloadExpression);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void voidReturnWithInvalidPayloadExpression() {
|
||||
Method method = getMethod("methodWithVoidReturnAndReturnValueAsPayload");
|
||||
source.getPayloadExpression(method);
|
||||
}
|
||||
|
||||
|
||||
private static Method getMethod(String name, Class<?> ... params) {
|
||||
try {
|
||||
@@ -83,9 +107,27 @@ public class MethodAnnotationExpressionSourceTests {
|
||||
public void methodWithPayloadAnnotation(String arg1, int arg2) {
|
||||
}
|
||||
|
||||
@Publisher(channel="foo")
|
||||
@Payload("#return")
|
||||
public String methodWithChannelAndExplicitReturnAsPayload() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@Publisher(channel="foo")
|
||||
@Payload
|
||||
public String methodWithChannelAndEmptyPayloadAnnotation() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
|
||||
@Publisher(channel="foo")
|
||||
@Payload("#method")
|
||||
public void methodWithChannelAndReturnAsPayload() {
|
||||
public void methodWithVoidReturnAndMethodNameAsPayload() {
|
||||
}
|
||||
|
||||
@Publisher(channel="foo")
|
||||
@Payload("#return")
|
||||
public void methodWithVoidReturnAndReturnValueAsPayload() {
|
||||
}
|
||||
|
||||
@Publisher
|
||||
|
||||
Reference in New Issue
Block a user