Merge pull request #522 from garyrussell/INT-2630

* garyrussell-INT-2630:
  INT-2630 MessageHeaders/History Conversion Issue
This commit is contained in:
Gunnar Hillert
2012-06-27 12:19:49 -04:00
4 changed files with 136 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -27,10 +27,13 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.expression.TypeConverter;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.history.MessageHistory;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware {
@@ -98,6 +101,18 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
if ((targetType.getType() == Void.class || targetType.getType() == Void.TYPE) && value == null) {
return null;
}
/*
* INT-2630 Spring 3.1 now converts ALL arguments; we know we don't need to convert MessageHeaders
* or MessageHistory; the MapToMap converter requires a no-arg constructor.
*/
if (sourceType != null && sourceType.getType() == MessageHeaders.class
&& targetType.getType() == MessageHeaders.class) {
return value;
}
if (sourceType != null && sourceType.getType() == MessageHistory.class
&& targetType.getType() == MessageHistory.class) {
return value;
}
if (conversionService.canConvert(sourceType, targetType)) {
return conversionService.convert(value, sourceType, targetType);
}
@@ -109,7 +124,7 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
if (editor != null) { // INT-1441
editor.setValue(value);
String text = editor.getAsText();
if (String.class.isAssignableFrom(targetType.getClass())) {
if (String.class.isAssignableFrom(targetType.getClass())) {
return text;
}
return convertValue(text, TypeDescriptor.valueOf(String.class), targetType);

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:message-history />
<int:channel id="input" />
<int:transformer input-channel="input" output-channel="output">
<bean class="org.springframework.integration.transformer.MessageHistoryParameterTests$MessageHistoryAwareTransformer"/>
</int:transformer>
<int:channel id="output">
<int:queue />
</int:channel>
</beans>

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.transformer;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Headers;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @since 2.2
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class MessageHistoryParameterTests {
@Autowired
private MessageChannel input;
@Autowired
private PollableChannel output;
@Test
public void test() {
input.send(new GenericMessage<String>("foo"));
assertNotNull(output.receive(10000));
}
public static class MessageHistoryAwareTransformer {
@Transformer
public Object transform(@Headers MessageHeaders headers,
@Header("history") MessageHistory history, @Payload Object payload) {
return payload;
}
}
}

View File

@@ -1,9 +1,10 @@
/**
*
*
*/
package org.springframework.integration.util;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.util.ArrayList;
import java.util.Arrays;
@@ -13,6 +14,11 @@ import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.context.NamedComponent;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.message.GenericMessage;
/**
* @author Oleg Zhurakousky
@@ -25,7 +31,7 @@ public class BeanFactoryTypeConverterTests {
public void testEmptyCollectionConversion(){
BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
List<String> sourceObject = new ArrayList<String>();
ArrayList<BeanFactoryTypeConverterTests> convertedCollection =
ArrayList<BeanFactoryTypeConverterTests> convertedCollection =
(ArrayList<BeanFactoryTypeConverterTests>) typeConverter.convertValue(sourceObject, TypeDescriptor.forObject(sourceObject), TypeDescriptor.forObject(new ArrayList<BeanFactoryTypeConverterTests>()));
assertEquals(sourceObject, convertedCollection);
}
@@ -47,4 +53,29 @@ public class BeanFactoryTypeConverterTests {
assertEquals(Arrays.asList(1234), converted);
}
@Test
public void testMessageHeadersNotConverted() {
BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
typeConverter.setBeanFactory(new DefaultListableBeanFactory());
MessageHeaders headers = new GenericMessage<String>("foo").getHeaders();
assertSame(headers, typeConverter.convertValue(headers, TypeDescriptor.valueOf(MessageHeaders.class), TypeDescriptor.valueOf(MessageHeaders.class)));
}
@Test
public void testMessageHistoryNotConverted() {
BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
typeConverter.setBeanFactory(new DefaultListableBeanFactory());
Message<String> message = new GenericMessage<String>("foo");
message = MessageHistory.write(message, new NamedComponent(){
public String getComponentName() {
return "bar";
}
public String getComponentType() {
return "baz";
}
});
MessageHistory history = MessageHistory.read(message);
assertSame(history, typeConverter.convertValue(history, TypeDescriptor.valueOf(MessageHeaders.class), TypeDescriptor.valueOf(MessageHeaders.class)));
}
}