From dd102fa2e0142e88fdf3f73656daf37508be8d25 Mon Sep 17 00:00:00 2001 From: David Syer Date: Sat, 10 Jul 2010 12:00:41 +0000 Subject: [PATCH] INT-1233: add sql-query-parameter-source support for JDBC inbound adapter --- spring-integration-ip/.classpath | 7 ++- spring-integration-ip/.project | 13 ++++++ .../.settings/org.eclipse.jdt.core.prefs | 4 +- .../jdbc/JdbcPollingChannelAdapter.java | 10 ++++ .../JdbcPollingChannelAdapterParser.java | 1 + .../config/spring-integration-jdbc-2.0.xsd | 15 ++++++ ...PollingChannelAdapterIntegrationTests.java | 46 +++++++++++++++++-- .../JdbcPollingChannelAdapterParserTests.java | 24 +++++++++- ...ersForMapJdbcInboundChannelAdapterTest.xml | 20 ++++++++ 9 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml diff --git a/spring-integration-ip/.classpath b/spring-integration-ip/.classpath index ddc9103c5c..028ca809b7 100644 --- a/spring-integration-ip/.classpath +++ b/spring-integration-ip/.classpath @@ -5,6 +5,11 @@ - + + + + + + diff --git a/spring-integration-ip/.project b/spring-integration-ip/.project index 3b05a20b74..7914c0d9f9 100644 --- a/spring-integration-ip/.project +++ b/spring-integration-ip/.project @@ -5,6 +5,11 @@ + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder @@ -20,10 +25,18 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.springframework.ide.eclipse.core.springnature org.maven.ide.eclipse.maven2Nature org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature diff --git a/spring-integration-ip/.settings/org.eclipse.jdt.core.prefs b/spring-integration-ip/.settings/org.eclipse.jdt.core.prefs index 9628cbe9c2..61810d8175 100644 --- a/spring-integration-ip/.settings/org.eclipse.jdt.core.prefs +++ b/spring-integration-ip/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,9 @@ -#Wed May 26 00:58:46 CEST 2010 +#Sat Jul 10 12:27:30 BST 2010 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning org.eclipse.jdt.core.compiler.problem.deadCode=warning @@ -11,6 +12,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java index e732e4bf50..57158d73a6 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java @@ -94,6 +94,16 @@ public class JdbcPollingChannelAdapter implements MessageSource { public void setSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) { this.sqlParameterSourceFactory = sqlParameterSourceFactory; } + + /** + * A source of parameters for the select query used for polling. + * + * @param sqlQueryParameterSource the sql query parameter source to set + */ + public void setSqlQueryParameterSource( + SqlParameterSource sqlQueryParameterSource) { + this.sqlQueryParameterSource = sqlQueryParameterSource; + } /** * Executes the query. If a query result set contains one or more rows, the Message diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParser.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParser.java index 6b397f5a27..0ed9ef43dd 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParser.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParser.java @@ -69,6 +69,7 @@ public class JdbcPollingChannelAdapterParser extends AbstractPollingInboundChann builder.addConstructorArgValue(query); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "row-mapper"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "sql-parameter-source-factory"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "sql-query-parameter-source"); if (update!=null) { builder.addPropertyValue("updateSql", update); } diff --git a/spring-integration-jdbc/src/main/resources/org/springframework/integration/jdbc/config/spring-integration-jdbc-2.0.xsd b/spring-integration-jdbc/src/main/resources/org/springframework/integration/jdbc/config/spring-integration-jdbc-2.0.xsd index 30ec77b1c5..f2a4269c84 100644 --- a/spring-integration-jdbc/src/main/resources/org/springframework/integration/jdbc/config/spring-integration-jdbc-2.0.xsd +++ b/spring-integration-jdbc/src/main/resources/org/springframework/integration/jdbc/config/spring-integration-jdbc-2.0.xsd @@ -216,6 +216,21 @@ + + + + + Reference to a SqlParameterSource for the SELECT query used for polling. If + that query has placeholders (e.g. "SELECT * from FOO where KEY=:key") they + will be bound from this source by name. + + + + + + + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java index f4517ecd00..b138be5cb9 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.List; import java.util.Map; @@ -14,6 +15,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.integration.core.Message; import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; @@ -28,7 +30,6 @@ public class JdbcPollingChannelAdapterIntegrationTests { private SimpleJdbcTemplate jdbcTemplate; - @Before public void setUp() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); @@ -55,13 +56,49 @@ public class JdbcPollingChannelAdapterIntegrationTests { assertTrue("Wrong payload type", payload instanceof List); List rows = (List) payload; assertEquals("Wrong number of elements", 1, rows.size()); - assertTrue("Returned row not a map", rows.get(0) instanceof Map); + assertTrue("Returned row not a map", rows.get(0) instanceof Map); Map row = (Map) rows.get(0); assertEquals("Wrong id", 1, row.get("id")); assertEquals("Wrong status", 2, row.get("status")); } - + + @Test + public void testParameterizedPollForListOfMapsNoUpdate() { + JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter( + this.embeddedDatabase, + "select * from item where status=:status"); + adapter.setSqlQueryParameterSource(new SqlParameterSource() { + + public boolean hasValue(String name) { + return "status".equals(name); + } + + public Object getValue(String name) throws IllegalArgumentException { + return 2; + } + + public String getTypeName(String name) { + return null; + } + + public int getSqlType(String name) { + return Types.INTEGER; + } + }); + this.jdbcTemplate.update("insert into item values(1,2)"); + Message message = adapter.receive(); + Object payload = message.getPayload(); + assertTrue("Wrong payload type", payload instanceof List); + List rows = (List) payload; + assertEquals("Wrong number of elements", 1, rows.size()); + assertTrue("Returned row not a map", rows.get(0) instanceof Map); + Map row = (Map) rows.get(0); + assertEquals("Wrong id", 1, row.get("id")); + assertEquals("Wrong status", 2, row.get("status")); + + } + @Test public void testSimplePollForListWithRowMapperNoUpdate() { JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter( @@ -146,14 +183,13 @@ public class JdbcPollingChannelAdapterIntegrationTests { countOfStatusTen); } - + @Test public void testEmptyPoll() { JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter( this.embeddedDatabase, "select * from item"); Message message = adapter.receive(); assertNull("Message received when no rows in table", message); - } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java index 82657b7078..4ec8d811f9 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java @@ -17,6 +17,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.MessageChannelTemplate; import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.core.Message; +import org.springframework.jdbc.core.namedparam.AbstractSqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.transaction.annotation.Transactional; @@ -71,7 +72,7 @@ public class JdbcPollingChannelAdapterParserTests { } @Test - public void testParameterSourceInboundChannelAdapter(){ + public void testParameterSourceFactoryInboundChannelAdapter(){ setUp("pollingWithParameterSourceJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message message = channelTemplate.receive(); @@ -81,6 +82,14 @@ public class JdbcPollingChannelAdapterParserTests { assertEquals("bar", list.get(0).get("NAME")); } + @Test + public void testParameterSourceInboundChannelAdapter(){ + setUp("pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml", getClass()); + this.jdbcTemplate.update("insert into item values(1,'',2)"); + Message message = channelTemplate.receive(); + assertNotNull(message); + } + @After public void tearDown(){ if(appCtx != null){ @@ -104,5 +113,18 @@ public class JdbcPollingChannelAdapterParserTests { protected void setupJdbcTemplate(){ this.jdbcTemplate = new SimpleJdbcTemplate(this.appCtx.getBean("dataSource",DataSource.class)); } + + public static class TestSqlParameterSource extends AbstractSqlParameterSource { + + public Object getValue(String paramName) + throws IllegalArgumentException { + return 2; + } + + public boolean hasValue(String paramName) { + return true; + } + + } } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml new file mode 100644 index 0000000000..b80a751dd5 --- /dev/null +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml @@ -0,0 +1,20 @@ + + + + + + + + + +