IN PROGRESS - issue INT-1010: Implement JdbcMessageStore

First draft of working message store.  Interface changes are inevitable.
This commit is contained in:
David Syer
2010-03-04 09:42:55 +00:00
parent 2de00f0964
commit 9d5500d796
8 changed files with 606 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
package org.springframework.integration.jdbc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import javax.sql.DataSource;
import org.junit.Before;
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.core.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class JdbcMessageStoreTests {
@Autowired
private DataSource dataSource;
@Autowired
@Qualifier("messageIncrementer")
private DataFieldMaxValueIncrementer messageIncrementer;
private JdbcMessageStore messageStore;
private SimpleJdbcTemplate jdbcTemplate;
@Before
public void init() {
messageStore = new JdbcMessageStore(dataSource, messageIncrementer);
jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Test
@Transactional
public void testGetNonExistent() throws Exception {
Message<?> result = messageStore.get(12345);
assertNull(result);
}
@Test
@Transactional
public void testAddAndGet() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("X").build();
message = messageStore.put(message);
Message<?> result = messageStore.get(message.getHeaders().get(JdbcMessageStore.ID_KEY));
assertNotNull(result);
assertEquals(message.getPayload(), result.getPayload());
}
@Test
@Transactional
public void testAddAndUpdate() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("X").build();
message = messageStore.put(message);
message = MessageBuilder.fromMessage(message).setCorrelationId("Y").build();
message = messageStore.put(message);
assertEquals("Y", messageStore.get(message.getHeaders().get(JdbcMessageStore.ID_KEY)).getHeaders().getCorrelationId());
}
@Test
@Transactional
public void testAddAndList() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").build();
messageStore.put(message);
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "INT_MESSAGE"));
assertEquals(1, messageStore.list().size());
}
@Test
@Transactional
public void testAddAndListByCorrelationId() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("X").build();
messageStore.put(message);
assertEquals(1, messageStore.list("X").size());
}
@Test
@Transactional
public void testAddAndDelete() throws Exception {
Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("X").build();
message = messageStore.put(message);
assertNotNull(messageStore.delete(message.getHeaders().get(JdbcMessageStore.ID_KEY)));
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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));
}
}

View File

@@ -0,0 +1,3 @@
# Placeholders for Derby:
int.schema.script=classpath:/org/springframework/integration/jdbc/schema-derby.sql
int.database.incrementer.class=org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer

View File

@@ -0,0 +1,36 @@
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<jdbc:embedded-database id="dataSource" type="DERBY">
<jdbc:script location="${int.schema.script}" />
</jdbc:embedded-database>
<bean id="placeholderProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:int-${ENVIRONMENT:derby}.properties" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="incrementerParent" class="${int.database.incrementer.class}"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="columnName" value="ID" />
</bean>
<bean id="messageIncrementer" parent="incrementerParent">
<property name="incrementerName" value="INT_MESSAGE_SEQ" />
</bean>
</beans>