Added PayloadSerializingTransformer and PayloadDeserializingTransformer (INT-513).
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PayloadDeserializingTransformerTests {
|
||||
|
||||
@Test
|
||||
public void deserializeString() throws Exception {
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
|
||||
objectStream.writeObject("foo");
|
||||
byte[] serialized = byteStream.toByteArray();
|
||||
PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer();
|
||||
Message<?> result = transformer.transform(new GenericMessage<byte[]>(serialized));
|
||||
Object payload = result.getPayload();
|
||||
assertNotNull(payload);
|
||||
assertEquals(String.class, payload.getClass());
|
||||
assertEquals("foo", payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeObject() throws Exception {
|
||||
TestBean testBean = new TestBean("test");
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
|
||||
objectStream.writeObject(testBean);
|
||||
byte[] serialized = byteStream.toByteArray();
|
||||
PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer();
|
||||
Message<?> result = transformer.transform(new GenericMessage<byte[]>(serialized));
|
||||
Object payload = result.getPayload();
|
||||
assertNotNull(payload);
|
||||
assertEquals(TestBean.class, payload.getClass());
|
||||
assertEquals(testBean.name, ((TestBean) payload).name);
|
||||
}
|
||||
|
||||
@Test(expected = MessageTransformationException.class)
|
||||
public void invalidPayload() {
|
||||
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||
PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer();
|
||||
transformer.transform(new GenericMessage<byte[]>(bytes));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestBean implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PayloadSerializingTransformerTests {
|
||||
|
||||
@Test
|
||||
public void serializeString() throws Exception {
|
||||
PayloadSerializingTransformer transformer = new PayloadSerializingTransformer();
|
||||
Message<?> result = transformer.transform(new StringMessage("foo"));
|
||||
Object payload = result.getPayload();
|
||||
assertNotNull(payload);
|
||||
assertTrue(payload instanceof byte[]);
|
||||
ByteArrayInputStream byteStream = new ByteArrayInputStream((byte[]) payload);
|
||||
ObjectInputStream objectStream = new ObjectInputStream(byteStream);
|
||||
Object deserialized = objectStream.readObject();
|
||||
assertEquals("foo", deserialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeObject() throws Exception {
|
||||
PayloadSerializingTransformer transformer = new PayloadSerializingTransformer();
|
||||
TestBean testBean = new TestBean("test");
|
||||
Message<?> result = transformer.transform(new GenericMessage<TestBean>(testBean));
|
||||
Object payload = result.getPayload();
|
||||
assertNotNull(payload);
|
||||
assertTrue(payload instanceof byte[]);
|
||||
ByteArrayInputStream byteStream = new ByteArrayInputStream((byte[]) payload);
|
||||
ObjectInputStream objectStream = new ObjectInputStream(byteStream);
|
||||
Object deserialized = objectStream.readObject();
|
||||
assertEquals(TestBean.class, deserialized.getClass());
|
||||
assertEquals(testBean.name, ((TestBean) deserialized).name);
|
||||
}
|
||||
|
||||
@Test(expected = MessageTransformationException.class)
|
||||
public void invalidPayload() {
|
||||
PayloadSerializingTransformer transformer = new PayloadSerializingTransformer();
|
||||
transformer.transform(new GenericMessage<Object>(new Object()));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestBean implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user