INT-1202: add serializer strategy to JDBC message store
This commit is contained in:
@@ -24,6 +24,11 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.integration.test.matcher.PayloadAndHeaderMatcher.sameExceptIgnorableHeaders;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -33,7 +38,10 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.commons.serializer.InputStreamingConverter;
|
||||
import org.springframework.commons.serializer.OutputStreamingConverter;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.store.MessageGroupCallback;
|
||||
import org.springframework.integration.store.MessageGroupStore;
|
||||
@@ -76,6 +84,30 @@ public class JdbcMessageStoreTests {
|
||||
assertNotNull(result.getHeaders().get(JdbcMessageStore.CREATED_DATE_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSerializer() throws Exception {
|
||||
// N.B. these serializers are not realistic (just for test purposes)
|
||||
messageStore.setSerializer(new OutputStreamingConverter<Message<?>>() {
|
||||
public void convert(Message<?> object, OutputStream outputStream) throws IOException {
|
||||
outputStream.write(object.getPayload().toString().getBytes());
|
||||
outputStream.flush();
|
||||
}
|
||||
});
|
||||
messageStore.setDeserializer(new InputStreamingConverter<Message<?>>() {
|
||||
public Message<?> convert(InputStream inputStream) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
return new GenericMessage<String>(reader.readLine());
|
||||
}
|
||||
});
|
||||
Message<String> message = MessageBuilder.withPayload("foo").build();
|
||||
Message<String> saved = messageStore.addMessage(message);
|
||||
assertNull(messageStore.getMessage(message.getHeaders().getId()));
|
||||
Message<?> result = messageStore.getMessage(saved.getHeaders().getId());
|
||||
assertNotNull(result);
|
||||
assertEquals("foo", result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testAddAndGetWithDifferentRegion() throws Exception {
|
||||
|
||||
@@ -3,11 +3,19 @@ package org.springframework.integration.jdbc.config;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.commons.serializer.java.JavaStreamingConverter;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.jdbc.JdbcMessageStore;
|
||||
import org.springframework.integration.store.MessageStore;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -29,7 +37,16 @@ public class JdbcMessageStoreParserTests {
|
||||
assertTrue(store instanceof JdbcMessageStore);
|
||||
}
|
||||
|
||||
@After
|
||||
@Test
|
||||
public void testSimpleMessageStoreWithSerializer() {
|
||||
setUp("serializerJdbcMessageStore.xml", getClass());
|
||||
MessageStore store = context.getBean("messageStore", MessageStore.class);
|
||||
Object serializer = TestUtils.getPropertyValue(store, "serializer.streamingConverter");
|
||||
assertTrue(serializer instanceof EnhancedSerializer);
|
||||
Object deserializer = TestUtils.getPropertyValue(store, "deserializer.streamingConverter");
|
||||
assertTrue(deserializer instanceof EnhancedSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageStoreWithAttributes() {
|
||||
setUp("soupedUpJdbcMessageStore.xml", getClass());
|
||||
@@ -50,4 +67,18 @@ public class JdbcMessageStoreParserTests {
|
||||
context = new ClassPathXmlApplicationContext(name, cls);
|
||||
}
|
||||
|
||||
public static class EnhancedSerializer extends JavaStreamingConverter {
|
||||
@Override
|
||||
public Object convert(InputStream inputStream) throws IOException {
|
||||
Message<?> message = (Message<?>) super.convert(inputStream);
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convert(Object object, OutputStream outputStream) throws IOException {
|
||||
Message<?> message = (Message<?>) object;
|
||||
super.convert(MessageBuilder.fromMessage(message).setHeader("serializer", "CUSTOM").build(), outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL" />
|
||||
|
||||
<bean id="serializer" class="org.springframework.integration.jdbc.config.JdbcMessageStoreParserTests$EnhancedSerializer" />
|
||||
|
||||
<int-jdbc:message-store id="messageStore" data-source="dataSource" deserializer="serializer" serializer="serializer"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2010 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.jdbc.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for static utility to help with serialization.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SerializationUtilsTests {
|
||||
|
||||
private static BigInteger FOO = new BigInteger(
|
||||
"-9702942423549012526722364838327831379660941553432801565505143675386108883970811292563757558516603356009681061" +
|
||||
"5697574744209306031461371833798723505120163874786203211176873686513374052845353833564048");
|
||||
|
||||
@Test
|
||||
public void testSerializeCycleSunnyDay() throws Exception {
|
||||
assertEquals("foo", SerializationUtils.deserialize(SerializationUtils.serialize("foo")));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testDeserializeUndefined() throws Exception {
|
||||
byte[] bytes = FOO.toByteArray();
|
||||
Object foo = SerializationUtils.deserialize(bytes);
|
||||
assertNotNull(foo);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSerializeNonSerializable() throws Exception {
|
||||
SerializationUtils.serialize(new Object());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDeserializeNonSerializable() throws Exception {
|
||||
SerializationUtils.deserialize("foo".getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeNull() throws Exception {
|
||||
assertNull(SerializationUtils.serialize(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeNull() throws Exception {
|
||||
assertNull(SerializationUtils.deserialize(null));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user