INT-3516: Allow Optional<> in POJO Method Args

JIRA: https://jira.spring.io/browse/INT-3516

* Add `Optional<>` support for `@Header` in the `MessagingMethodInvokerHelper`
* Add `Optional<>` test-case
* Change `sourceCompatibility` for test to the Java 8

INT-3516: Revert SF version to 4.1.1

Add Docs on the matter

Fix WS Tests; Revert StubJavaMailSender
This commit is contained in:
Artem Bilan
2014-09-12 15:18:59 +03:00
committed by Gary Russell
parent c84b16e2b5
commit a3d8776b5c
9 changed files with 105 additions and 37 deletions

View File

@@ -738,7 +738,8 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
+ "disabled or header name is not explicitly provided via @Header annotation.");
String headerRetrievalExpression = "headers['" + headerName + "']";
String fullHeaderExpression = headerRetrievalExpression + relativeExpression;
String fallbackExpression = (annotationAttributes.getBoolean("required"))
String fallbackExpression = (annotationAttributes.getBoolean("required")
&& !methodParameter.getParameterType().getName().equals("java.util.Optional"))
? "T(org.springframework.util.Assert).isTrue(false, 'required header not available: "
+ headerName + "')"
: "null";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2014 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.
@@ -32,6 +32,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.integration.handler.LoggingHandler.Level;
import org.springframework.messaging.support.GenericMessage;
@@ -41,6 +42,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
@ContextConfiguration
@@ -90,12 +92,12 @@ public class LoggingHandlerTests {
expression = spy(expression);
accessor.setPropertyValue("expression", expression);
when(log.isInfoEnabled()).thenReturn(false);
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
verify(expression, never()).getValue(Mockito.any(EvaluationContext.class), Mockito.any());
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(expression, never()).getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class));
when(log.isInfoEnabled()).thenReturn(true);
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
verify(expression, times(1)).getValue(Mockito.any(EvaluationContext.class), Mockito.any());
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(expression, times(1)).getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class));
}
@Test
@@ -106,12 +108,12 @@ public class LoggingHandlerTests {
log = spy(log);
accessor.setPropertyValue("messageLogger", log);
when(log.isInfoEnabled()).thenReturn(true);
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(log, times(1)).info(Mockito.anyString());
verify(log, never()).warn(Mockito.anyString());
loggingHandler.setLevel(Level.WARN);
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(log, times(1)).info(Mockito.anyString());
verify(log, times(1)).warn(Mockito.anyString());
}
@@ -134,6 +136,7 @@ public class LoggingHandlerTests {
public int getAge() {
return this.age;
}
}
}

View File

@@ -26,7 +26,9 @@ import static org.mockito.Mockito.mock;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import org.apache.commons.logging.Log;
@@ -40,7 +42,6 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
import org.springframework.integration.gateway.RequestReplyExchanger;
@@ -48,6 +49,7 @@ import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.util.MessagingMethodInvokerHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.support.GenericMessage;
@@ -58,6 +60,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Dave Syer
* @author Gary Russell
* @author Gunnar Hillert
* @author Artem Bilan
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class MethodInvokingMessageProcessorTests {
@@ -517,6 +520,36 @@ public class MethodInvokingMessageProcessorTests {
assertEquals("true", bean.lastArg);
}
@Test
public void testOptionalArgs() throws Exception {
class Foo {
private final Map<String, Object> arguments = new LinkedHashMap<String, Object>();
public void optionalHeaders(Optional<String> foo, @Header(value="foo", required=false) String foo1,
@Header(value="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"));
}
private static class ExceptionCauseMatcher extends TypeSafeMatcher<Exception> {
private Throwable cause;