From f4504b070e71433048cf80e7ab9ea4b6af5f2fae Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 4 Dec 2007 09:15:21 +0000 Subject: [PATCH] IN PROGRESS - issue BATCH-244: Repeated processing of items does not work http://opensource.atlassian.com/projects/spring/browse/BATCH-244 Fixed bug in ResourceLinereader that causes first chunk to be lost if it fails. --- core/src/site/site.xml | 1 - execution/src/site/site.xml | 1 - .../IbatisDrivingQueryInputSource.java | 2 +- .../io/file/support/ResourceLineReader.java | 1 + infrastructure/src/site/site.xml | 3 +- .../file/support/ResourceLineReaderTests.java | 12 +- .../sample/item/processor/TradeProcessor.java | 19 +++ .../src/main/resources/jobs/rollbackJob.xml | 122 ++++++++++++++++++ samples/src/site/site.xml | 3 +- .../sample/RollbackJobFunctionalTests.java | 36 ++++++ src/site/site.xml | 3 +- 11 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 samples/src/main/resources/jobs/rollbackJob.xml create mode 100644 samples/src/test/java/org/springframework/batch/sample/RollbackJobFunctionalTests.java diff --git a/core/src/site/site.xml b/core/src/site/site.xml index 772a274cf..00e799b26 100644 --- a/core/src/site/site.xml +++ b/core/src/site/site.xml @@ -23,7 +23,6 @@ - diff --git a/execution/src/site/site.xml b/execution/src/site/site.xml index f63e46050..c28fa9c4e 100644 --- a/execution/src/site/site.xml +++ b/execution/src/site/site.xml @@ -23,7 +23,6 @@ - diff --git a/infrastructure/src/main/java/org/springframework/batch/io/driving/IbatisDrivingQueryInputSource.java b/infrastructure/src/main/java/org/springframework/batch/io/driving/IbatisDrivingQueryInputSource.java index 0af20f4b3..a284b7590 100644 --- a/infrastructure/src/main/java/org/springframework/batch/io/driving/IbatisDrivingQueryInputSource.java +++ b/infrastructure/src/main/java/org/springframework/batch/io/driving/IbatisDrivingQueryInputSource.java @@ -34,7 +34,7 @@ public class IbatisDrivingQueryInputSource extends DrivingQueryInputSource { private SqlMapClientTemplate sqlMapClientTemplate; /** - * Overriden read that uses the returned key as arguments to the details query. + * Overridden read() that uses the returned key as arguments to the details query. * * @see org.springframework.batch.io.driving.DrivingQueryInputSource#read() */ diff --git a/infrastructure/src/main/java/org/springframework/batch/io/file/support/ResourceLineReader.java b/infrastructure/src/main/java/org/springframework/batch/io/file/support/ResourceLineReader.java index 7cfddca7d..be369e6cd 100644 --- a/infrastructure/src/main/java/org/springframework/batch/io/file/support/ResourceLineReader.java +++ b/infrastructure/src/main/java/org/springframework/batch/io/file/support/ResourceLineReader.java @@ -291,6 +291,7 @@ class ResourceLineReader implements ResourceLifecycle, InputSource, try { reader = new BufferedReader(new InputStreamReader(resource .getInputStream(), encoding)); + mark(); } catch (IOException e) { throw new BatchEnvironmentException("Could not open resource", e); diff --git a/infrastructure/src/site/site.xml b/infrastructure/src/site/site.xml index 0d7558c3e..28f22faa1 100644 --- a/infrastructure/src/site/site.xml +++ b/infrastructure/src/site/site.xml @@ -28,11 +28,10 @@ - - \ No newline at end of file + diff --git a/infrastructure/src/test/java/org/springframework/batch/io/file/support/ResourceLineReaderTests.java b/infrastructure/src/test/java/org/springframework/batch/io/file/support/ResourceLineReaderTests.java index ac420e1d0..80ccffabc 100644 --- a/infrastructure/src/test/java/org/springframework/batch/io/file/support/ResourceLineReaderTests.java +++ b/infrastructure/src/test/java/org/springframework/batch/io/file/support/ResourceLineReaderTests.java @@ -151,7 +151,17 @@ public class ResourceLineReaderTests extends TestCase { reader.read(); assertEquals(2, reader.getCurrentLineCount()); } - + + public void testMarkOnFirstRead() throws Exception { + Resource resource = new ByteArrayResource("1\n# 2\n3".getBytes()); + ResourceLineReader reader = new ResourceLineReader(resource); + reader.read(); + // The first read should do a mark() so the reset goes back to the beginning. + reader.reset(); + String line = (String) reader.read(); + assertEquals("1", line); + } + public void testNonDefaultRecordSeparatorPolicy() throws Exception { Resource resource = new ByteArrayResource("1\n\"4\n5\"; \n6".getBytes()); ResourceLineReader reader = new ResourceLineReader(resource); diff --git a/samples/src/main/java/org/springframework/batch/sample/item/processor/TradeProcessor.java b/samples/src/main/java/org/springframework/batch/sample/item/processor/TradeProcessor.java index 5c8d22729..bfb25231a 100644 --- a/samples/src/main/java/org/springframework/batch/sample/item/processor/TradeProcessor.java +++ b/samples/src/main/java/org/springframework/batch/sample/item/processor/TradeProcessor.java @@ -27,6 +27,19 @@ import org.springframework.batch.sample.domain.Trade; public class TradeProcessor implements ItemProcessor { private static Log log = LogFactory.getLog(TradeProcessor.class); private TradeWriter writer; + + private int failure = -1; + + private int index = 0; + + /** + * Public setter for the {@link int} property. + * + * @param failure the failure to set + */ + public void setFailure(int failure) { + this.failure = failure; + } public void process(Object data) { if (!(data instanceof Trade)) { @@ -40,6 +53,12 @@ public class TradeProcessor implements ItemProcessor { //TODO put some processing of the trade object here writer.writeTrade(trade); + + if(index++ == failure) { + throw new RuntimeException("Something unexpected happened!"); + } + + } public void setWriter(TradeWriter dao) { diff --git a/samples/src/main/resources/jobs/rollbackJob.xml b/samples/src/main/resources/jobs/rollbackJob.xml new file mode 100644 index 000000000..f921f0189 --- /dev/null +++ b/samples/src/main/resources/jobs/rollbackJob.xml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/src/site/site.xml b/samples/src/site/site.xml index 0d7558c3e..28f22faa1 100644 --- a/samples/src/site/site.xml +++ b/samples/src/site/site.xml @@ -28,11 +28,10 @@ - - \ No newline at end of file + diff --git a/samples/src/test/java/org/springframework/batch/sample/RollbackJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/RollbackJobFunctionalTests.java new file mode 100644 index 000000000..1ea77fb72 --- /dev/null +++ b/samples/src/test/java/org/springframework/batch/sample/RollbackJobFunctionalTests.java @@ -0,0 +1,36 @@ +package org.springframework.batch.sample; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Test for job that rolls back a trade that is processed. + * + * @author Robert Kasanicky + */ +public class RollbackJobFunctionalTests extends AbstractValidatingBatchLauncherTests { + + int before = -1; + + JdbcTemplate jdbcTemplate; + + public void setDataSource(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + + protected String[] getConfigLocations() { + return new String[] {"jobs/rollbackJob.xml"}; + } + + protected void onSetUp() throws Exception { + before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE"); + } + + protected void validatePostConditions() throws Exception { + int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE"); + assertEquals(before+4, after); + } + + +} diff --git a/src/site/site.xml b/src/site/site.xml index 4e703d737..a57ebe167 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -28,8 +28,9 @@ + - +