diff --git a/org.springframework.integration.jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParser.java b/org.springframework.integration.jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParser.java new file mode 100644 index 0000000000..1be9cc738f --- /dev/null +++ b/org.springframework.integration.jdbc/src/main/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParser.java @@ -0,0 +1,65 @@ +/* + * Copyright 2002-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.config; + +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * Parser for {@link org.springframework.integration.jdbc.JdbcMessageStore}. + * + * @author Dave Syer + * @since 2.0 + */ +public class JdbcMessageStoreParser extends AbstractBeanDefinitionParser { + + @Override + protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { + + Object source = parserContext.extractSource(element); + + BeanDefinitionBuilder builder = BeanDefinitionBuilder + .genericBeanDefinition("org.springframework.integration.jdbc.JdbcMessageStore"); + + String dataSourceRef = element.getAttribute("data-source"); + String simpleJdbcOperationsRef = element.getAttribute("jdbc-operations"); + boolean refToDataSourceSet = StringUtils.hasText(dataSourceRef); + boolean refToSimpleJdbcOperaitonsSet = StringUtils.hasText(simpleJdbcOperationsRef); + if ((refToDataSourceSet && refToSimpleJdbcOperaitonsSet) + || (!refToDataSourceSet && !refToSimpleJdbcOperaitonsSet)) { + parserContext.getReaderContext().error( + "Exactly one of the attributes data-source or " + + "simple-jdbc-operations should be set for the JDBC inbound-channel-adapter", source); + } + + if (refToDataSourceSet) { + builder.addPropertyReference("dataSource", dataSourceRef); + } else { + builder.addPropertyReference("jdbcTemplate", simpleJdbcOperationsRef); + } + + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "lob-handler"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "table-prefix", "tablePrefix"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "region", "region"); + + return builder.getBeanDefinition(); + + } + +} diff --git a/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParserTests.java b/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParserTests.java new file mode 100644 index 0000000000..b48875bad7 --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcMessageStoreParserTests.java @@ -0,0 +1,53 @@ +package org.springframework.integration.jdbc.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.jdbc.JdbcMessageStore; +import org.springframework.integration.store.MessageStore; +import org.springframework.jdbc.support.lob.LobHandler; +import org.springframework.test.util.ReflectionTestUtils; + +public class JdbcMessageStoreParserTests { + + private ClassPathXmlApplicationContext context; + + @Test + public void testSimpleMessageStoreWithDataSource() { + setUp("defaultJdbcMessageStore.xml", getClass()); + MessageStore store = context.getBean("messageStore", MessageStore.class); + assertTrue(store instanceof JdbcMessageStore); + } + + @Test + public void testSimpleMessageStoreWithTemplate() { + setUp("jdbcOperationsJdbcMessageStore.xml", getClass()); + MessageStore store = context.getBean("messageStore", MessageStore.class); + assertTrue(store instanceof JdbcMessageStore); + } + + @After + @Test + public void testMessageStoreWithAttributes() { + setUp("soupedUpJdbcMessageStore.xml", getClass()); + MessageStore store = context.getBean("messageStore", MessageStore.class); + assertEquals("FOO", ReflectionTestUtils.getField(store, "region")); + assertEquals("BAR_", ReflectionTestUtils.getField(store, "tablePrefix")); + assertEquals(context.getBean(LobHandler.class), ReflectionTestUtils.getField(store, "lobHandler")); + } + + @After + public void tearDown() { + if (context != null) { + context.close(); + } + } + + public void setUp(String name, Class cls) { + context = new ClassPathXmlApplicationContext(name, cls); + } + +} diff --git a/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java b/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java new file mode 100644 index 0000000000..e44d9866db --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcPollingChannelAdapterParserTests.java @@ -0,0 +1,87 @@ +package org.springframework.integration.jdbc.config; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.sql.DataSource; + +import org.junit.After; +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +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.simple.SimpleJdbcTemplate; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public class JdbcPollingChannelAdapterParserTests { + + + final long receiveTimeout = 5000; + + SimpleJdbcTemplate jdbcTemplate; + + MessageChannelTemplate channelTemplate; + + ConfigurableApplicationContext appCtx; + + + + @Test + public void testSimpleInboundChannelAdapter(){ + setUp("pollingForMapJdbcInboundChannelAdapterTest.xml", getClass()); + 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); + } + + + @Test + public void testSimpleInboundChannelAdapterWithUpdate(){ + setUp("pollingForMapJdbcInboundChannelAdapterWithUpdateTest.xml", getClass()); + this.jdbcTemplate.update("insert into item values(1,2)"); + Message message = channelTemplate.receive(); + assertNotNull(message); + message = channelTemplate.receive(); + assertNull(channelTemplate.receive()); + } + + @Test + public void testExtendedInboundChannelAdapter(){ + setUp("pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml", getClass()); + this.jdbcTemplate.update("insert into item values(1,2)"); + Message message = channelTemplate.receive(); + assertNotNull(message); + } + + @After + public void tearDown(){ + if(appCtx != null){ + appCtx.close(); + } + } + + public void setUp(String name, Class cls){ + appCtx = new ClassPathXmlApplicationContext(name, cls); + setupJdbcTemplate(); + setupMessageChannelTemplate(); + } + + + protected void setupMessageChannelTemplate(){ + PollableChannel pollableChannel = this.appCtx.getBean("target", PollableChannel.class); + this.channelTemplate = new MessageChannelTemplate(pollableChannel); + this.channelTemplate.setReceiveTimeout(500); + } + + protected void setupJdbcTemplate(){ + this.jdbcTemplate = new SimpleJdbcTemplate(this.appCtx.getBean("dataSource",DataSource.class)); + } + +} diff --git a/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/defaultJdbcMessageStore.xml b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/defaultJdbcMessageStore.xml new file mode 100644 index 0000000000..705a2846ae --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/defaultJdbcMessageStore.xml @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/jdbcOperationsJdbcMessageStore.xml b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/jdbcOperationsJdbcMessageStore.xml new file mode 100644 index 0000000000..494e36d2f7 --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/jdbcOperationsJdbcMessageStore.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml new file mode 100644 index 0000000000..f504fc6ad0 --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/soupedUpJdbcMessageStore.xml b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/soupedUpJdbcMessageStore.xml new file mode 100644 index 0000000000..6ba45f2f1d --- /dev/null +++ b/org.springframework.integration.jdbc/src/test/resources/org/springframework/integration/jdbc/config/soupedUpJdbcMessageStore.xml @@ -0,0 +1,16 @@ + + + + + + + + + +