From 915997cd2822a9bbb692021f2f76b74846c044fa Mon Sep 17 00:00:00 2001 From: dsyer Date: Thu, 6 Mar 2008 13:19:55 +0000 Subject: [PATCH] RESOLVED - issue BATCH-76: ItemWriter for SQL batch updates http://jira.springframework.org/browse/BATCH-76 --- .../io/support/BatchSqlUpdateItemWriter.java | 227 ++++++++++++++++++ .../support/ItemPreparedStatementSetter.java | 39 +++ .../BatchSqlUpdateItemWriterTests.java | 208 ++++++++++++++++ .../HibernateAwareItemWriterTests.java | 1 + .../BatchSqlCustomerCreditIncreaseWriter.java | 69 ++++++ ...erCreditUpdatePreparedStatementSetter.java | 39 +++ .../mapping/CustomerCreditRowMapper.java | 2 + .../main/resources/jobs/batchUpdateJob.xml | 45 ++++ .../resources/jobs/fixedLengthImportJob.xml | 2 - .../src/main/resources/jobs/parallelJob.xml | 2 - .../BatchSqlUpdateJobFunctionalTests.java | 10 + ...hSqlCustomerCreditIncreaseWriterTests.java | 101 ++++++++ ...ditUpdatePreparedStatementSetterTests.java | 65 +++++ .../mapping/CustomerCreditRowMapperTests.java | 7 + ...tchSqlUpdateJobFunctionalTests-context.xml | 10 + 15 files changed, 823 insertions(+), 4 deletions(-) create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriter.java create mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/ItemPreparedStatementSetter.java create mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriterTests.java create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/BatchSqlCustomerCreditIncreaseWriter.java create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetter.java create mode 100644 spring-batch-samples/src/main/resources/jobs/batchUpdateJob.xml create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/BatchSqlUpdateJobFunctionalTests.java create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/item/writer/BatchSqlCustomerCreditIncreaseWriterTests.java create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetterTests.java create mode 100644 spring-batch-samples/src/test/resources/org/springframework/batch/sample/BatchSqlUpdateJobFunctionalTests-context.xml diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriter.java new file mode 100644 index 000000000..be0f41043 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriter.java @@ -0,0 +1,227 @@ +/* + * Copyright 2006-2007 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.batch.io.support; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.exception.ClearFailedException; +import org.springframework.batch.item.exception.FlushFailedException; +import org.springframework.batch.repeat.RepeatContext; +import org.springframework.batch.repeat.synch.RepeatSynchronizationManager; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.PreparedStatementCallback; +import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.util.Assert; + +/** + * {@link ItemWriter} that uses the batching features from + * {@link PreparedStatement} if available and can take some rudimentary steps to + * locate a failure during a flush, and identify the items that failed. When one + * of those items is encountered again the batch is flushed aggressively so that + * the bad item is eventually identified and can be dealt with in isolation.
+ * + * The user must provide an SQL query and a special callback + * {@link ItemPreparedStatementSetter}, which is responsible for mapping the + * item to a PreparedStatement. + * + * @author Dave Syer + * + */ +public class BatchSqlUpdateItemWriter implements ItemWriter, InitializingBean { + + /** + * Key for items processed in the current transaction {@link RepeatContext}. + */ + protected static final String ITEMS_PROCESSED = BatchSqlUpdateItemWriter.class.getName() + ".ITEMS_PROCESSED"; + + private Set failed = new HashSet(); + + private JdbcOperations jdbcTemplate; + + private ItemPreparedStatementSetter preparedStatementSetter; + + private String sql; + + /** + * Public setter for the query string to execute on write. The parameters + * should correspond to those known to the + * {@link ItemPreparedStatementSetter}. + * @param sql the query to set + */ + public void setSql(String sql) { + this.sql = sql; + } + + /** + * Public setter for the {@link ItemPreparedStatementSetter}. + * @param preparedStatementSetter the {@link ItemPreparedStatementSetter} to + * set + */ + public void setItemPreparedStatementSetter(ItemPreparedStatementSetter preparedStatementSetter) { + this.preparedStatementSetter = preparedStatementSetter; + } + + /** + * Public setter for the {@link JdbcOperations}. + * @param jdbcTemplate the {@link JdbcOperations} to set + */ + public void setJdbcTemplate(JdbcOperations jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + /** + * Check mandatory properties - there must be a delegate. + * + * @see org.springframework.dao.support.DaoSupport#initDao() + */ + public void afterPropertiesSet() throws Exception { + Assert.notNull(jdbcTemplate, "BatchSqlUpdateItemWriter requires an data source."); + Assert.notNull(preparedStatementSetter, "BatchSqlUpdateItemWriter requires a ItemPreparedStatementSetter"); + } + + /** + * Buffer the item in a transaction resource, but flush aggressively if the + * item was previously part of a failed chunk. + * + * @throws Exception + * + * @see org.springframework.batch.io.OutputSource#write(java.lang.Object) + */ + public void write(Object output) throws Exception { + bindTransactionResources(); + getProcessed().add(output); + flushIfNecessary(output); + } + + /** + * Accessor for the list of processed items in this transaction. + * + * @return the processed + */ + private Set getProcessed() { + Assert.state(TransactionSynchronizationManager.hasResource(ITEMS_PROCESSED), + "Processed items not bound to transaction."); + Set processed = (Set) TransactionSynchronizationManager.getResource(ITEMS_PROCESSED); + return processed; + } + + /** + * Set up the {@link RepeatContext} as a transaction resource. + * + * @param context the context to set + */ + private void bindTransactionResources() { + if (TransactionSynchronizationManager.hasResource(ITEMS_PROCESSED)) { + return; + } + TransactionSynchronizationManager.bindResource(ITEMS_PROCESSED, new HashSet()); + } + + /** + * Remove the transaction resource associated with this context. + */ + private void unbindTransactionResources() { + if (!TransactionSynchronizationManager.hasResource(ITEMS_PROCESSED)) { + return; + } + TransactionSynchronizationManager.unbindResource(ITEMS_PROCESSED); + } + + /** + * Accessor for the context property. + * + * @param output + * + * @return the context + */ + private void flushIfNecessary(Object output) throws Exception { + boolean flush; + synchronized (failed) { + flush = failed.contains(output); + } + if (flush) { + RepeatContext context = RepeatSynchronizationManager.getContext(); + // Force early completion to commit aggressively if we encounter a + // failed item (from a failed chunk but we don't know which one was + // the problem). + context.setCompleteOnly(); + // Flush now, so that if there is a failure this record can be + // skipped. + doFlush(); + } + } + + /** + * Flush the hibernate session from within a repeat context. + */ + private void doFlush() { + final Set processed = getProcessed(); + try { + if (!processed.isEmpty()) { + jdbcTemplate.execute(sql, new PreparedStatementCallback() { + public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { + for (Iterator iterator = processed.iterator(); iterator.hasNext();) { + Object item = (Object) iterator.next(); + preparedStatementSetter.setValues(item, ps); + ps.addBatch(); + } + return ps.executeBatch(); + } + }); + } + } + catch (RuntimeException e) { + synchronized (failed) { + failed.addAll(processed); + } + throw e; + } + finally { + getProcessed().clear(); + } + } + + /** + * Unbind transaction resources, effectively clearing the item buffer. + * + * @see org.springframework.batch.item.ItemWriter#clear() + */ + public void clear() throws ClearFailedException { + unbindTransactionResources(); + } + + /** + * Flush the internal item buffer and record failures if there are any. + * + * @see org.springframework.batch.item.ItemWriter#flush() + */ + public void flush() throws FlushFailedException { + try { + doFlush(); + } + finally { + unbindTransactionResources(); + } + } + +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/ItemPreparedStatementSetter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/ItemPreparedStatementSetter.java new file mode 100644 index 000000000..46ec69e01 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/support/ItemPreparedStatementSetter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2006-2007 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.batch.io.support; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.springframework.jdbc.core.RowMapper; + +/** + * A convenient strategy for SQL updates, acting effectively as the inverse of + * {@link RowMapper}. + * @author Dave Syer + * + */ +public interface ItemPreparedStatementSetter { + /** + * Set parameter values on the given PreparedStatement as determined from + * the provided item. + * @param ps the PreparedStatement to invoke setter methods on + * @throws SQLException if a SQLException is encountered (i.e. there is no + * need to catch SQLException) + */ + void setValues(Object item, PreparedStatement ps) throws SQLException; + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriterTests.java new file mode 100644 index 000000000..976a999ff --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/BatchSqlUpdateItemWriterTests.java @@ -0,0 +1,208 @@ +/* + * Copyright 2006-2007 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.batch.io.support; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.batch.repeat.RepeatContext; +import org.springframework.batch.repeat.context.RepeatContextSupport; +import org.springframework.batch.repeat.synch.RepeatSynchronizationManager; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.UncategorizedSQLException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.PreparedStatementCallback; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** + * @author Dave Syer + * + */ +public class BatchSqlUpdateItemWriterTests extends TestCase { + + private BatchSqlUpdateItemWriter writer = new BatchSqlUpdateItemWriter(); + + private JdbcTemplate jdbcTemplate; + + protected List list = new ArrayList(); + + private RepeatContext context = new RepeatContextSupport(null); + + private PreparedStatement ps; + + private MockControl control = MockControl.createControl(PreparedStatement.class); + + /* + * (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + ps = (PreparedStatement) control.getMock(); + jdbcTemplate = new JdbcTemplate() { + public Object execute(String sql, PreparedStatementCallback action) throws DataAccessException { + list.add(sql); + try { + return action.doInPreparedStatement(ps); + } + catch (SQLException e) { + throw new UncategorizedSQLException("doInPreparedStatement", sql, e); + } + } + }; + writer.setSql("SQL"); + writer.setJdbcTemplate(jdbcTemplate); + writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter() { + public void setValues(Object item, PreparedStatement ps) throws SQLException { + list.add(item); + } + }); + TransactionSynchronizationManager.bindResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED, new HashSet( + Collections.singleton("spam"))); + RepeatSynchronizationManager.register(context); + } + + /* + * (non-Javadoc) + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + Map map = TransactionSynchronizationManager.getResourceMap(); + for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) { + String key = (String) iterator.next(); + TransactionSynchronizationManager.unbindResource(key); + } + RepeatSynchronizationManager.clear(); + } + + /** + * Test method for + * {@link org.springframework.batch.io.support.BatchSqlUpdateItemWriter#afterPropertiesSet()}. + * @throws Exception + */ + public void testAfterPropertiesSet() throws Exception { + try { + writer.afterPropertiesSet(); + } + catch (IllegalArgumentException e) { + // expected + String message = e.getMessage().toLowerCase(); + assertTrue("Message does not contain 'query'.", message.indexOf("query") >= 0); + } + } + + /** + * Test method for + * {@link org.springframework.batch.io.support.BatchSqlUpdateItemWriter#write(java.lang.Object)}. + * @throws Exception + */ + public void testWrite() throws Exception { + writer.setSql("foo"); + writer.write("bar"); + // Nothing happens till we flush + assertEquals(0, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.io.support.BatchSqlUpdateItemWriter#clear()}. + */ + public void testClear() { + assertTrue(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + writer.clear(); + assertFalse(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + } + + /** + * Test method for + * {@link org.springframework.batch.io.support.BatchSqlUpdateItemWriter#flush()}. + */ + public void testFlush() { + assertTrue(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + writer.flush(); + assertFalse(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + assertEquals(2, list.size()); + assertTrue(list.contains("SQL")); + } + + /** + * Test method for + * {@link org.springframework.batch.io.support.BatchSqlUpdateItemWriter#flush()}. + * @throws Exception + */ + public void testWriteAndFlush() throws Exception { + assertTrue(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + writer.write("bar"); + writer.flush(); + assertFalse(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + assertEquals(3, list.size()); + assertTrue(list.contains("SQL")); + } + + public void testFlushWithFailure() throws Exception{ + tearDown(); + try { + writer.flush(); + fail("Expected IllegalStateException"); + } catch (IllegalStateException e) { + assertTrue("Message should contain hint about transaction: "+e.getMessage(), e.getMessage().indexOf("transaction")>=0); + } + } + + public void testWriteAndFlushWithFailure() throws Exception { + final RuntimeException ex = new RuntimeException("bar"); + writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter() { + public void setValues(Object item, PreparedStatement ps) throws SQLException { + list.add(item); + throw ex; + } + }); + ps.addBatch(); + control.setVoidCallable(); + control.expectAndReturn(ps.executeBatch(), new int[] {123}); + control.replay(); + writer.write("foo"); + try { + writer.flush(); + fail("Expected RuntimeException"); + } catch (RuntimeException e) { + assertEquals("bar", e.getMessage()); + } + assertFalse(TransactionSynchronizationManager.hasResource(BatchSqlUpdateItemWriter.ITEMS_PROCESSED)); + assertEquals(2, list.size()); + writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter() { + public void setValues(Object item, PreparedStatement ps) throws SQLException { + list.add(item); + } + }); + writer.write("foo"); + writer.flush(); + control.verify(); + assertEquals(4, list.size()); + assertTrue(list.contains("SQL")); + assertTrue(list.contains("foo")); + assertTrue(context.isCompleteOnly()); + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/HibernateAwareItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/HibernateAwareItemWriterTests.java index f34349c8b..73c3a1d91 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/HibernateAwareItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/HibernateAwareItemWriterTests.java @@ -88,6 +88,7 @@ public class HibernateAwareItemWriterTests extends TestCase { String key = (String) iterator.next(); TransactionSynchronizationManager.unbindResource(key); } + RepeatSynchronizationManager.clear(); } /** diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/BatchSqlCustomerCreditIncreaseWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/BatchSqlCustomerCreditIncreaseWriter.java new file mode 100644 index 000000000..f33abe84e --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/BatchSqlCustomerCreditIncreaseWriter.java @@ -0,0 +1,69 @@ +package org.springframework.batch.sample.item.writer; + +import java.math.BigDecimal; + +import org.springframework.batch.io.support.BatchSqlUpdateItemWriter; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.exception.ClearFailedException; +import org.springframework.batch.item.exception.FlushFailedException; +import org.springframework.batch.sample.domain.CustomerCredit; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +/** + * Increases customer's credit by fixed amount, delegating to a + * {@link BatchSqlUpdateItemWriter} to push the result out to persistent + * storage. + * + * @author Dave Syer + */ +public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter, InitializingBean { + + private ItemWriter delegate; + + public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000); + + /** + * Public setter for the {@link ItemWriter}, which must be an instance of + * {@link BatchSqlUpdateItemWriter}. + * @param delegate the delegate to set + */ + public void setDelegate(ItemWriter delegate) { + this.delegate = delegate; + } + + /* + * (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception { + Assert.state(delegate instanceof BatchSqlUpdateItemWriter, "Delegate must be set and must be an instance of BatchSqlUpdateItemWriter"); + } + + /* + * (non-Javadoc) + * @see org.springframework.batch.item.processor.DelegatingItemWriter#doProcess(java.lang.Object) + */ + public void write(Object data) throws Exception { + CustomerCredit customerCredit = (CustomerCredit) data; + customerCredit.increaseCreditBy(FIXED_AMOUNT); + delegate.write(customerCredit); + } + + /** + * @throws ClearFailedException + * @see org.springframework.batch.io.support.BatchSqlUpdateItemWriter#clear() + */ + public void clear() throws ClearFailedException { + delegate.clear(); + } + + /** + * @throws FlushFailedException + * @see org.springframework.batch.io.support.BatchSqlUpdateItemWriter#flush() + */ + public void flush() throws FlushFailedException { + delegate.flush(); + } + +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetter.java new file mode 100644 index 000000000..1a69707cf --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2006-2007 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.batch.sample.item.writer; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.springframework.batch.io.support.ItemPreparedStatementSetter; +import org.springframework.batch.sample.domain.CustomerCredit; + +/** + * @author Dave Syer + * + */ +public class CustomerCreditUpdatePreparedStatementSetter implements ItemPreparedStatementSetter { + + /* (non-Javadoc) + * @see org.springframework.batch.io.support.ItemPreparedStatementSetter#setValues(java.lang.Object, java.sql.PreparedStatement) + */ + public void setValues(Object item, PreparedStatement ps) throws SQLException { + CustomerCredit customerCredit = (CustomerCredit) item; + ps.setBigDecimal(1, customerCredit.getCredit()); + ps.setLong(2, customerCredit.getId()); + } + +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapper.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapper.java index 3ad2252e8..33c4eee03 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapper.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapper.java @@ -8,12 +8,14 @@ import org.springframework.jdbc.core.RowMapper; public class CustomerCreditRowMapper implements RowMapper { + public static final String ID_COLUMN = "id"; public static final String NAME_COLUMN = "name"; public static final String CREDIT_COLUMN = "credit"; public Object mapRow(ResultSet rs, int rowNum) throws SQLException { CustomerCredit customerCredit = new CustomerCredit(); + customerCredit.setId(rs.getInt(ID_COLUMN)); customerCredit.setName(rs.getString(NAME_COLUMN)); customerCredit.setCredit(rs.getBigDecimal(CREDIT_COLUMN)); diff --git a/spring-batch-samples/src/main/resources/jobs/batchUpdateJob.xml b/spring-batch-samples/src/main/resources/jobs/batchUpdateJob.xml new file mode 100644 index 000000000..65f84f1ba --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/batchUpdateJob.xml @@ -0,0 +1,45 @@ + + + + Example for SQL Batch Update integration. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml index b08d55f14..8f33f3467 100644 --- a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml @@ -86,6 +86,4 @@ - - \ No newline at end of file diff --git a/spring-batch-samples/src/main/resources/jobs/parallelJob.xml b/spring-batch-samples/src/main/resources/jobs/parallelJob.xml index c2017d8bb..7b853ff0a 100644 --- a/spring-batch-samples/src/main/resources/jobs/parallelJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/parallelJob.xml @@ -122,8 +122,6 @@ - - =0); + } + } + + /** + * Test method for + * {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#write(java.lang.Object)}. + * @throws Exception + */ + public void testWrite() throws Exception { + delegate.write(customerCredit); + control.setVoidCallable(); + control.replay(); + writer.write(customerCredit); + control.verify(); + } + + /** + * Test method for + * {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#clear()}. + */ + public void testClear() { + delegate.clear(); + control.setVoidCallable(); + control.replay(); + writer.clear(); + control.verify(); + } + + /** + * Test method for + * {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#flush()}. + */ + public void testFlush() { + delegate.flush(); + control.setVoidCallable(); + control.replay(); + writer.flush(); + control.verify(); + } + +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetterTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetterTests.java new file mode 100644 index 000000000..5b6db21df --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/item/writer/CustomerCreditUpdatePreparedStatementSetterTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2006-2007 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.batch.sample.item.writer; + +import java.math.BigDecimal; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.batch.sample.domain.CustomerCredit; + +/** + * @author Dave Syer + * + */ +public class CustomerCreditUpdatePreparedStatementSetterTests extends TestCase { + + private CustomerCreditUpdatePreparedStatementSetter setter = new CustomerCreditUpdatePreparedStatementSetter(); + + private CustomerCredit credit; + + private PreparedStatement ps; + + private MockControl control = MockControl.createControl(PreparedStatement.class); + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + ps = (PreparedStatement) control.getMock(); + credit = new CustomerCredit(); + credit.setId(13); + credit.setCredit(new BigDecimal(12000)); + credit.setName("foo"); + } + /** + * Test method for {@link org.springframework.batch.sample.item.writer.CustomerCreditUpdatePreparedStatementSetter#setValues(java.lang.Object, java.sql.PreparedStatement)}. + * @throws SQLException + */ + public void testSetValues() throws SQLException { + ps.setBigDecimal(1, credit.getCredit()); + control.setVoidCallable(); + ps.setLong(2, credit.getId()); + control.setVoidCallable(); + control.replay(); + setter.setValues(credit, ps); + control.verify(); + } + +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapperTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapperTests.java index e1f6c26ed..eac3aad86 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapperTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/mapping/CustomerCreditRowMapperTests.java @@ -11,11 +11,16 @@ import org.springframework.jdbc.core.RowMapper; public class CustomerCreditRowMapperTests extends AbstractRowMapperTests { + /** + * + */ + private static final int ID = 12; private static final String CUSTOMER = "Jozef Mak"; private static final BigDecimal CREDIT = new BigDecimal(0.1); protected Object expectedDomainObject() { CustomerCredit credit = new CustomerCredit(); + credit.setId(ID); credit.setCredit(CREDIT); credit.setName(CUSTOMER); return credit; @@ -26,6 +31,8 @@ public class CustomerCreditRowMapperTests extends AbstractRowMapperTests { } protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException { + rs.getInt(CustomerCreditRowMapper.ID_COLUMN); + rsControl.setReturnValue(ID); rs.getString(CustomerCreditRowMapper.NAME_COLUMN); rsControl.setReturnValue(CUSTOMER); rs.getBigDecimal(CustomerCreditRowMapper.CREDIT_COLUMN); diff --git a/spring-batch-samples/src/test/resources/org/springframework/batch/sample/BatchSqlUpdateJobFunctionalTests-context.xml b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/BatchSqlUpdateJobFunctionalTests-context.xml new file mode 100644 index 000000000..decb1e3f0 --- /dev/null +++ b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/BatchSqlUpdateJobFunctionalTests-context.xml @@ -0,0 +1,10 @@ + + + + + + +