INT-2853 ObjectToStringTransformer Improvements

* Convert byte[] and char[] to String rather than simply using toString().
* Allow the charset to be configured for byte[] payloads.
* Add tests.
* Add reference documentation updates.
This commit is contained in:
Gary Russell
2013-02-05 17:06:07 -05:00
committed by Gunnar Hillert
parent d8dc140b17
commit 83fc5e449a
8 changed files with 115 additions and 12 deletions

View File

@@ -23,4 +23,7 @@
<poller fixed-delay="10000"/>
</object-to-string-transformer>
<object-to-string-transformer id="withCharset" charset="FOO"
input-channel="charsetChannel" output-channel="nullChannel"/>
</beans:beans>

View File

@@ -21,13 +21,14 @@ 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.beans.factory.annotation.Qualifier;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -50,6 +51,8 @@ public class ObjectToStringTransformerParserTests {
@Qualifier("output")
private PollableChannel output;
@Autowired
private AbstractEndpoint withCharset;
@Test
public void directChannelWithStringMessage() {
@@ -83,9 +86,14 @@ public class ObjectToStringTransformerParserTests {
assertEquals("test", result.getPayload());
}
@Test
public void charset() {
assertEquals("FOO", TestUtils.getPropertyValue(this.withCharset, "handler.transformer.charset"));
}
private static class TestBean {
@Override
public String toString() {
return "test";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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,13 +18,16 @@ package org.springframework.integration.transformer;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.nio.charset.Charset;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
* @author Andrew Cowlin
* @author Gary Russell
*/
public class ObjectToStringTransformerTests {
@@ -42,9 +45,31 @@ public class ObjectToStringTransformerTests {
assertEquals("test", result.getPayload());
}
@Test
public void byteArrayPayload() throws Exception {
Transformer transformer = new ObjectToStringTransformer();
Message<?> result = transformer.transform(new GenericMessage<byte[]>(("foo" + '\u0fff').getBytes("UTF-8")));
assertEquals("foo" + '\u0fff', result.getPayload());
}
@Test
public void byteArrayPayloadCharset() throws Exception {
String defaultCharsetName = Charset.defaultCharset().toString();
Transformer transformer = new ObjectToStringTransformer(defaultCharsetName);
Message<?> result = transformer.transform(new GenericMessage<byte[]>("foo".getBytes(defaultCharsetName)));
assertEquals("foo", result.getPayload());
}
@Test
public void charArrayPayload() {
Transformer transformer = new ObjectToStringTransformer();
Message<?> result = transformer.transform(new GenericMessage<char[]>("foo".toCharArray()));
assertEquals("foo", result.getPayload());
}
private static class TestBean {
@Override
public String toString() {
return "test";
}