INT-1173: added sql parameter source support

This commit is contained in:
David Syer
2010-06-14 09:46:54 +00:00
parent ceb4036b38
commit 986a383450
12 changed files with 313 additions and 173 deletions

View File

@@ -0,0 +1,23 @@
<?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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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>
</beans>

View File

@@ -25,7 +25,7 @@ public class JdbcMessageHandlerParserTests {
private ConfigurableApplicationContext context;
@Test
public void testSimpleInboundChannelAdapter(){
public void testSimpleOutboundChannelAdapter(){
setUp("handlingWithJdbcOperationsJdbcOutboundChannelAdapterTest.xml", getClass());
Message<?> message = MessageBuilder.withPayload("foo").setHeader("business.key", "FOO").build();
channel.send(message);
@@ -35,7 +35,7 @@ public class JdbcMessageHandlerParserTests {
}
@Test
public void testDollarHeaderInboundChannelAdapter(){
public void testDollarHeaderOutboundChannelAdapter(){
setUp("handlingDollarHeaderJdbcOutboundChannelAdapterTest.xml", getClass());
Message<?> message = MessageBuilder.withPayload("foo").build();
channel.send(message);
@@ -45,13 +45,23 @@ public class JdbcMessageHandlerParserTests {
}
@Test
public void testMapPayloadInboundChannelAdapter(){
public void testMapPayloadOutboundChannelAdapter(){
setUp("handlingMapPayloadJdbcOutboundChannelAdapterTest.xml", getClass());
Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build();
channel.send(message);
Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS");
assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID"));
assertEquals("Wrong id", "bar", map.get("name"));
assertEquals("Wrong name", "bar", map.get("name"));
}
@Test
public void testParameterSourceOutboundChannelAdapter(){
setUp("handlingParameterSourceJdbcOutboundChannelAdapterTest.xml", getClass());
Message<?> message = MessageBuilder.withPayload("foo").build();
channel.send(message);
Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS");
assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID"));
assertEquals("Wrong name", "foo", map.get("name"));
}
@After

View File

@@ -1,10 +1,12 @@
package org.springframework.integration.jdbc.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
@@ -24,18 +26,16 @@ public class JdbcPollingChannelAdapterParserTests {
final long receiveTimeout = 5000;
SimpleJdbcTemplate jdbcTemplate;
MessageChannelTemplate channelTemplate;
ConfigurableApplicationContext appCtx;
private SimpleJdbcTemplate jdbcTemplate;
private MessageChannelTemplate channelTemplate;
private ConfigurableApplicationContext appCtx;
@Test
public void testSimpleInboundChannelAdapter(){
setUp("pollingForMapJdbcInboundChannelAdapterTest.xml", getClass());
this.jdbcTemplate.update("insert into item values(1,2)");
this.jdbcTemplate.update("insert into item values(1,'',2)");
Message<?> message = channelTemplate.receive();
assertNotNull("No message found ", message);
assertTrue("Wrong payload type expected instance of List", message.getPayload() instanceof List<?>);
@@ -45,7 +45,7 @@ public class JdbcPollingChannelAdapterParserTests {
@Test
public void testSimpleInboundChannelAdapterWithUpdate(){
setUp("pollingForMapJdbcInboundChannelAdapterWithUpdateTest.xml", getClass());
this.jdbcTemplate.update("insert into item values(1,2)");
this.jdbcTemplate.update("insert into item values(1,'',2)");
Message<?> message = channelTemplate.receive();
assertNotNull(message);
message = channelTemplate.receive();
@@ -55,11 +55,22 @@ public class JdbcPollingChannelAdapterParserTests {
@Test
public void testExtendedInboundChannelAdapter(){
setUp("pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml", getClass());
this.jdbcTemplate.update("insert into item values(1,2)");
this.jdbcTemplate.update("insert into item values(1,'',2)");
Message<?> message = channelTemplate.receive();
assertNotNull(message);
}
@Test
public void testParameterSourceInboundChannelAdapter(){
setUp("pollingWithParameterSourceJdbcInboundChannelAdapterTest.xml", getClass());
this.jdbcTemplate.update("insert into item values(1,'',2)");
Message<?> message = channelTemplate.receive();
assertNotNull(message);
List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM item WHERE status=1");
assertEquals(1, list.size());
assertEquals("bar", list.get(0).get("NAME"));
}
@After
public void tearDown(){
if(appCtx != null){

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/jdbc"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:si="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jdbc
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
<outbound-channel-adapter query="insert into foos (id, status, name) values (:headers[$id], 0, :payload)"
channel="target" data-source="dataSource" sql-parameter-source-factory="sqlParameterSourceFactory"/>
<beans:import resource="jdbcOutboundChannelAdapterCommonConfig.xml" />
<beans:bean id="sqlParameterSourceFactory" class="org.springframework.integration.jdbc.DefaultSqlParameterSourceFactory">
<beans:property name="staticParameters">
<beans:map><beans:entry key="foo" value="bar"/></beans:map>
</beans:property>
</beans:bean>
</beans:beans>

View File

@@ -1 +1 @@
create table item(id int,status int);
create table item(id int, name varchar(20), status int);

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/jdbc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:si="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jdbc
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
<inbound-channel-adapter query="select * from item where status=2" channel="target" update="update item set status=1, name=:foo where id in (:idList)"
jdbc-operations="jdbcTemplate" sql-parameter-source-factory="sqlParameterSourceFactory" />
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
<beans:bean id="sqlParameterSourceFactory" class="org.springframework.integration.jdbc.DefaultSqlParameterSourceFactory">
<beans:property name="staticParameters">
<beans:map><beans:entry key="foo" value="bar"/></beans:map>
</beans:property>
</beans:bean>
</beans:beans>