diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/HibernateCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/HibernateCursorItemReader.java index cbfe8d72c..915b172db 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/HibernateCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/HibernateCursorItemReader.java @@ -24,10 +24,10 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.StatelessSession; import org.springframework.batch.io.Skippable; -import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ExecutionAttributes; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.reader.AbstractItemStreamItemReader; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -51,8 +51,7 @@ import org.springframework.util.StringUtils; * @author Robert Kasanicky * @author Dave Syer */ -public class HibernateCursorItemReader extends AbstractItemStreamItemReader implements Skippable, InitializingBean, - DisposableBean { +public class HibernateCursorItemReader extends AbstractItemStreamItemReader implements Skippable, InitializingBean { private static final String RESTART_DATA_ROW_NUMBER_KEY = ClassUtils.getShortName(HibernateCursorItemReader.class) + ".rowNumber"; @@ -148,10 +147,6 @@ public class HibernateCursorItemReader extends AbstractItemStreamItemReader impl Assert.hasLength(queryString); } - public void destroy() throws Exception { - close(); - } - /** * @param queryString HQL query string */ @@ -232,7 +227,8 @@ public class HibernateCursorItemReader extends AbstractItemStreamItemReader impl return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.stream.ItemStreamAdapter#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { @@ -242,7 +238,8 @@ public class HibernateCursorItemReader extends AbstractItemStreamItemReader impl } } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.stream.ItemStreamAdapter#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/JdbcCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/JdbcCursorItemReader.java index 118a3577e..7384e9972 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/JdbcCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/cursor/JdbcCursorItemReader.java @@ -30,10 +30,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.io.Skippable; import org.springframework.batch.io.support.AbstractTransactionalIoSource; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.KeyedItemReader; -import org.springframework.batch.item.ExecutionAttributes; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessResourceUsageException; @@ -108,7 +107,7 @@ import org.springframework.util.StringUtils; * @author Lucas Ward * @author Peter Zozom */ -public class JdbcCursorItemReader extends AbstractTransactionalIoSource implements KeyedItemReader, DisposableBean, +public class JdbcCursorItemReader extends AbstractTransactionalIoSource implements KeyedItemReader, InitializingBean, ItemStream, Skippable { private static Log log = LogFactory.getLog(JdbcCursorItemReader.class); @@ -275,16 +274,6 @@ public class JdbcCursorItemReader extends AbstractTransactionalIoSource implemen skipCount = 0; } - /** - * Calls close to ensure that bean factories can close and always release - * resources. - * - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - close(); - } - // Check the result set is in synch with the currentRow attribute. This is // important // to ensure that the user hasn't modified the current row. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/driving/DrivingQueryItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/driving/DrivingQueryItemReader.java index 12b36084c..7b8650f17 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/driving/DrivingQueryItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/driving/DrivingQueryItemReader.java @@ -19,10 +19,9 @@ import java.util.Iterator; import java.util.List; import org.springframework.batch.io.support.AbstractTransactionalIoSource; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.KeyedItemReader; -import org.springframework.batch.item.ExecutionAttributes; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; @@ -48,9 +47,8 @@ import org.springframework.util.Assert; * @author Lucas Ward * @since 1.0 */ -public class DrivingQueryItemReader extends AbstractTransactionalIoSource - implements KeyedItemReader, InitializingBean, - DisposableBean, ItemStream { +public class DrivingQueryItemReader extends AbstractTransactionalIoSource implements KeyedItemReader, InitializingBean, + ItemStream { private boolean initialized = false; @@ -84,7 +82,7 @@ public class DrivingQueryItemReader extends AbstractTransactionalIoSource * be called. * * @return next key in the list if not index is not at the last element, - * null otherwise. + * null otherwise. */ public Object read() { if (!initialized) { @@ -132,50 +130,37 @@ public class DrivingQueryItemReader extends AbstractTransactionalIoSource * {@link BatchTransactionSynchronizationManager} in order to ensure it is * notified about commits and rollbacks. * - * @throws IllegalStateException - * if the keys list is null or initialized is true. + * @throws IllegalStateException if the keys list is null or initialized is + * true. */ public void open() { - Assert.state(keys == null || initialized, - "Cannot open an already opened input source" - + ", call close() first."); + Assert.state(keys == null || initialized, "Cannot open an already opened input source" + + ", call close() first."); keys = keyGenerator.retrieveKeys(); keysIterator = keys.listIterator(); initialized = true; } - /* - * (non-Javadoc) - * - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - close(); - } - /** * Restore input source to previous state. If the input source has already * been initialized before calling restore (meaning, read has been called) * then an IllegalStateException will be thrown, since all input sources * should be restored before being read from, otherwise already processed - * data could be returned. The {@link ExecutionAttributes} attempting to be restored from - * must have been obtained from the same input source as the one - * being restored from otherwise it is invalid. + * data could be returned. The {@link ExecutionAttributes} attempting to be + * restored from must have been obtained from the same input source + * as the one being restored from otherwise it is invalid. * - * @throws IllegalArgumentException - * if restart data or it's properties is null. - * @throws IllegalStateException - * if the input source has already been initialized. + * @throws IllegalArgumentException if restart data or it's properties is + * null. + * @throws IllegalStateException if the input source has already been + * initialized. */ public final void restoreFrom(ExecutionAttributes data) { Assert.notNull(data, "ExecutionAttributes must not be null."); - Assert.notNull(data.getProperties(), - "ExecutionAttributes properties must not be null."); - Assert.state(!initialized, - "Cannot restore when already intialized. Call" - + " close() first before restore()"); + Assert.notNull(data.getProperties(), "ExecutionAttributes properties must not be null."); + Assert.state(!initialized, "Cannot restore when already intialized. Call" + " close() first before restore()"); if (data.getProperties().size() == 0) { return; @@ -216,6 +201,7 @@ public class DrivingQueryItemReader extends AbstractTransactionalIoSource /** * Return the item itself (which is already a key). + * * @see org.springframework.batch.item.ItemReader#getKey(java.lang.Object) */ public Object getKey(Object item) { @@ -234,14 +220,18 @@ public class DrivingQueryItemReader extends AbstractTransactionalIoSource return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.springframework.batch.io.support.AbstractTransactionalIoSource#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { lastCommitIndex = currentIndex; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.springframework.batch.io.support.AbstractTransactionalIoSource#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/FlatFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/FlatFileItemWriter.java index 1832a7002..1e4c4bc9b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/FlatFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/FlatFileItemWriter.java @@ -31,12 +31,11 @@ import java.util.Properties; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.io.exception.BatchEnvironmentException; import org.springframework.batch.io.support.AbstractTransactionalIoSource; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemWriter; -import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.StreamException; import org.springframework.batch.item.writer.ItemTransformer; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessResourceFailureException; @@ -59,7 +58,7 @@ import org.springframework.util.Assert; * @author Dave Syer */ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements ItemWriter, ItemStream, - InitializingBean, DisposableBean { + InitializingBean { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); @@ -198,22 +197,12 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements * @see ResourceLifecycle#close() */ public void close() { - if (state!=null) { + if (state != null) { getOutputState().close(); state = null; } } - /** - * Calls close to ensure that bean factories can close and always release - * resources. - * - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - close(); - } - /** * Sets encoding for output template. */ @@ -269,7 +258,7 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements // Returns object representing state. private OutputState getOutputState() { - if (state==null) { + if (state == null) { state = new OutputState(); } return (OutputState) state; @@ -549,14 +538,16 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.io.support.AbstractTransactionalIoSource#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { getOutputState().mark(); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.io.support.AbstractTransactionalIoSource#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/separator/ResourceLineReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/separator/ResourceLineReader.java index 9c09251a5..9ee964182 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/separator/ResourceLineReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/file/separator/ResourceLineReader.java @@ -29,7 +29,6 @@ import org.springframework.batch.io.exception.BatchEnvironmentException; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.stream.ItemStreamAdapter; -import org.springframework.beans.factory.DisposableBean; import org.springframework.core.io.Resource; import org.springframework.util.Assert; @@ -56,8 +55,7 @@ import org.springframework.util.Assert; * @author Dave Syer * @author Rob Harrop */ -public class ResourceLineReader extends ItemStreamAdapter implements LineReader, ItemReader, - DisposableBean { +public class ResourceLineReader extends ItemStreamAdapter implements LineReader, ItemReader { private static final Collection DEFAULT_COMMENTS = Collections.singleton("#"); @@ -186,15 +184,6 @@ public class ResourceLineReader extends ItemStreamAdapter implements LineReader, } } - /** - * Calls close to ensure that bean factory releases all resources. - * - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - close(); - } - /** * Getter for current line count (not the current number of lines returned). * @@ -203,7 +192,7 @@ public class ResourceLineReader extends ItemStreamAdapter implements LineReader, public int getPosition() { return getState().getCurrentLineCount(); } - + /** * Mark is supported as long as this {@link ItemStream} is used in a * single-threaded environment. The state backing the mark is a single diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemReader.java index b57b33b22..eef7ec0a4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemReader.java @@ -16,11 +16,10 @@ import org.springframework.batch.io.xml.stax.DefaultFragmentEventReader; import org.springframework.batch.io.xml.stax.DefaultTransactionalEventReader; import org.springframework.batch.io.xml.stax.FragmentEventReader; import org.springframework.batch.io.xml.stax.TransactionalEventReader; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; -import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.reader.AbstractItemReader; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessResourceFailureException; @@ -36,8 +35,8 @@ import org.springframework.util.Assert; * * @author Robert Kasanicky */ -public class StaxEventItemReader extends AbstractItemReader implements ItemReader, - Skippable, ItemStream, InitializingBean, DisposableBean { +public class StaxEventItemReader extends AbstractItemReader implements ItemReader, Skippable, ItemStream, + InitializingBean { public static final String READ_COUNT_STATISTICS_NAME = "StaxEventReaderItemReader.readCount"; @@ -90,7 +89,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade public void close() { initialized = false; - if (fragmentReader==null && inputStream==null) { + if (fragmentReader == null && inputStream == null) { return; } try { @@ -132,8 +131,8 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade } /** - * @param eventReaderDeserializer maps xml fragments corresponding to records - * to objects + * @param eventReaderDeserializer maps xml fragments corresponding to + * records to objects */ public void setFragmentDeserializer(EventReaderDeserializer eventReaderDeserializer) { this.eventReaderDeserializer = eventReaderDeserializer; @@ -186,7 +185,8 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade * Restores the input source for the given restart data by rereading and * skipping the number of records stored in the {@link ExecutionAttributes}. * - * @param ExecutionAttributes that holds the line count from the last commit. + * @param ExecutionAttributes that holds the line count from the last + * commit. * @throws IllegalStateException if the ItemReader has already been * initialized or if the number of records to read and skip exceeds the * available records. @@ -247,10 +247,6 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade } } - public void destroy() throws Exception { - close(); - } - /** * Mark is supported as long as this {@link ItemStream} is used in a * single-threaded environment. The state backing the mark is a single @@ -263,7 +259,8 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { @@ -272,7 +269,8 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade skipRecords = new ArrayList(); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { @@ -281,5 +279,4 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade fragmentReader.reset(); } - } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemWriter.java index b1adddc65..fb09dd066 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/io/xml/StaxEventItemWriter.java @@ -15,11 +15,10 @@ import javax.xml.stream.XMLStreamException; import org.springframework.batch.io.support.FileUtils; import org.springframework.batch.io.xml.stax.NoStartEndDocumentStreamWriter; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemWriter; -import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.StreamException; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessResourceFailureException; @@ -36,7 +35,7 @@ import org.springframework.util.CollectionUtils; * @author Peter Zozom * */ -public class StaxEventItemWriter implements ItemWriter, ItemStream, InitializingBean, DisposableBean { +public class StaxEventItemWriter implements ItemWriter, ItemStream, InitializingBean { // default encoding private static final String DEFAULT_ENCODING = "UTF-8"; @@ -214,14 +213,6 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing Assert.notNull(serializer); } - /** - * @throws Exception - * @see org.springframework.beans.factory.DisposableBean#destroy() - */ - public void destroy() throws Exception { - close(); - } - /** * Open the output source * @@ -264,7 +255,7 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing } initialized = true; - + } /** @@ -447,7 +438,8 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { @@ -455,7 +447,8 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing lastCommitPointRecordCount = currentRecordCount; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/cursor/HibernateCursorItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/cursor/HibernateCursorItemReaderIntegrationTests.java index 0cc4ac87e..1eecf27e2 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/cursor/HibernateCursorItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/cursor/HibernateCursorItemReaderIntegrationTests.java @@ -45,7 +45,7 @@ public class HibernateCursorItemReaderIntegrationTests extends AbstractDataSourc * called only in uninitialized state. */ public void testSetUseStatelessSession() { - HibernateCursorItemReader inputSource = ((HibernateCursorItemReader) source); + HibernateCursorItemReader inputSource = ((HibernateCursorItemReader) reader); // initialize and call setter => error inputSource.open(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/driving/FooInputSource.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/driving/FooInputSource.java index c381779ba..3bae4d5aa 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/driving/FooInputSource.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/driving/FooInputSource.java @@ -8,9 +8,10 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.JdbcTemplate; -class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader, DisposableBean, InitializingBean{ +class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader, DisposableBean, InitializingBean { DrivingQueryItemReader inputSource; + FooDao fooDao = new SingleKeyFooDao(); public FooItemReader(DrivingQueryItemReader inputSource, JdbcTemplate jdbcTemplate) { @@ -20,9 +21,10 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader public Object read() { Object key = inputSource.read(); - if(key != null){ + if (key != null) { return fooDao.getFoo(key); - }else{ + } + else { return null; } } @@ -36,7 +38,7 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader } public void destroy() throws Exception { - inputSource.destroy(); + inputSource.close(); } public void setFooDao(FooDao fooDao) { @@ -60,14 +62,16 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader return true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.StreamContext) */ public void mark() { inputSource.mark(); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.StreamContext) */ public void reset() { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/AbstractDataSourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/AbstractDataSourceItemReaderIntegrationTests.java index f06e3279d..94f92373e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/AbstractDataSourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/support/AbstractDataSourceItemReaderIntegrationTests.java @@ -2,47 +2,49 @@ package org.springframework.batch.io.support; import org.springframework.batch.io.Skippable; import org.springframework.batch.io.sample.domain.Foo; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; -import org.springframework.batch.item.ExecutionAttributes; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; import org.springframework.util.Assert; /** - * Common scenarios for testing {@link ItemReader} implementations which read data from database. + * Common scenarios for testing {@link ItemReader} implementations which read + * data from database. * * @author Lucas Ward * @author Robert Kasanicky */ -public abstract class AbstractDataSourceItemReaderIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests { +public abstract class AbstractDataSourceItemReaderIntegrationTests extends + AbstractTransactionalDataSourceSpringContextTests { - protected ItemReader source; - + protected ItemReader reader; /** * @return configured input source ready for use */ protected abstract ItemReader createItemReader() throws Exception; - - protected String[] getConfigLocations(){ - return new String[] { "org/springframework/batch/io/sql/data-source-context.xml"}; + + protected String[] getConfigLocations() { + return new String[] { "org/springframework/batch/io/sql/data-source-context.xml" }; } - - /* (non-Javadoc) + + /* + * (non-Javadoc) * @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction() */ protected void onSetUpInTransaction() throws Exception { super.onSetUpInTransaction(); - source = createItemReader(); + reader = createItemReader(); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) * @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction() */ protected void onTearDownAfterTransaction() throws Exception { - getAsDisposableBean(source).destroy(); + getAsItemStream(reader).close(); super.onTearDownAfterTransaction(); } @@ -50,47 +52,47 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends Abstr * Regular scenario - read all rows and eventually return null. */ public void testNormalProcessing() throws Exception { - getAsInitializingBean(source).afterPropertiesSet(); - - Foo foo1 = (Foo) source.read(); + getAsInitializingBean(reader).afterPropertiesSet(); + + Foo foo1 = (Foo) reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) source.read(); + Foo foo2 = (Foo) reader.read(); assertEquals(2, foo2.getValue()); - Foo foo3 = (Foo) source.read(); + Foo foo3 = (Foo) reader.read(); assertEquals(3, foo3.getValue()); - Foo foo4 = (Foo) source.read(); + Foo foo4 = (Foo) reader.read(); assertEquals(4, foo4.getValue()); - Foo foo5 = (Foo) source.read(); + Foo foo5 = (Foo) reader.read(); assertEquals(5, foo5.getValue()); - assertNull(source.read()); + assertNull(reader.read()); } /** - * Restart scenario - read records, save restart data, create new input source - * and restore from restart data - the new input source should continue where - * the old one finished. + * Restart scenario - read records, save restart data, create new input + * source and restore from restart data - the new input source should + * continue where the old one finished. */ public void testRestart() throws Exception { - Foo foo1 = (Foo) source.read(); + Foo foo1 = (Foo) reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) source.read(); + Foo foo2 = (Foo) reader.read(); assertEquals(2, foo2.getValue()); - ExecutionAttributes streamContext = getAsRestartable(source).getExecutionAttributes(); + ExecutionAttributes streamContext = getAsItemStream(reader).getExecutionAttributes(); // create new input source - source = createItemReader(); + reader = createItemReader(); - getAsRestartable(source).restoreFrom(streamContext); + getAsItemStream(reader).restoreFrom(streamContext); - Foo fooAfterRestart = (Foo) source.read(); + Foo fooAfterRestart = (Foo) reader.read(); assertEquals(3, fooAfterRestart.getValue()); } @@ -99,149 +101,147 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends Abstr */ public void testInvalidRestore() throws Exception { - Foo foo1 = (Foo) source.read(); + Foo foo1 = (Foo) reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) source.read(); + Foo foo2 = (Foo) reader.read(); assertEquals(2, foo2.getValue()); - ExecutionAttributes streamContext = getAsRestartable(source).getExecutionAttributes(); + ExecutionAttributes streamContext = getAsItemStream(reader).getExecutionAttributes(); // create new input source - source = createItemReader(); + reader = createItemReader(); - Foo foo = (Foo) source.read(); + Foo foo = (Foo) reader.read(); assertEquals(1, foo.getValue()); try { - getAsRestartable(source).restoreFrom(streamContext); + getAsItemStream(reader).restoreFrom(streamContext); fail(); } catch (IllegalStateException ex) { // expected } } - + /** * Empty restart data should be handled gracefully. - * @throws Exception + * @throws Exception */ public void testRestoreFromEmptyData() throws Exception { ExecutionAttributes streamContext = new ExecutionAttributes(); - - getAsRestartable(source).restoreFrom(streamContext); - - Foo foo = (Foo) source.read(); + + getAsItemStream(reader).restoreFrom(streamContext); + + Foo foo = (Foo) reader.read(); assertEquals(1, foo.getValue()); } - + /** * Rollback scenario - input source rollbacks to last commit point. - * @throws Exception + * @throws Exception */ public void testRollback() throws Exception { - Foo foo1 = (Foo) source.read(); - + Foo foo1 = (Foo) reader.read(); + commit(); - - Foo foo2 = (Foo) source.read(); + + Foo foo2 = (Foo) reader.read(); Assert.state(!foo2.equals(foo1)); - - Foo foo3 = (Foo) source.read(); + + Foo foo3 = (Foo) reader.read(); Assert.state(!foo2.equals(foo3)); - + rollback(); - - assertEquals(foo2, source.read()); + + assertEquals(foo2, reader.read()); } - + /** - * Rollback scenario with skip - input source rollbacks to last commit point. - * @throws Exception + * Rollback scenario with skip - input source rollbacks to last commit + * point. + * @throws Exception */ public void testRollbackAndSkip() throws Exception { - - if (!(source instanceof Skippable)) { + + if (!(reader instanceof Skippable)) { return; } - - Foo foo1 = (Foo) source.read(); - + + Foo foo1 = (Foo) reader.read(); + commit(); - - Foo foo2 = (Foo) source.read(); + + Foo foo2 = (Foo) reader.read(); Assert.state(!foo2.equals(foo1)); - - Foo foo3 = (Foo) source.read(); + + Foo foo3 = (Foo) reader.read(); Assert.state(!foo2.equals(foo3)); - - getAsSkippable(source).skip(); - + + getAsSkippable(reader).skip(); + rollback(); - - assertEquals(foo2, source.read()); - Foo foo4 = (Foo) source.read(); + + assertEquals(foo2, reader.read()); + Foo foo4 = (Foo) reader.read(); assertEquals(4, foo4.getValue()); } /** - * Rollback scenario with skip and restart - input source rollbacks to last commit point. - * @throws Exception + * Rollback scenario with skip and restart - input source rollbacks to last + * commit point. + * @throws Exception */ public void testRollbackSkipAndRestart() throws Exception { - if (!(source instanceof Skippable)) { + if (!(reader instanceof Skippable)) { return; } - Foo foo1 = (Foo) source.read(); - + Foo foo1 = (Foo) reader.read(); + commit(); - - Foo foo2 = (Foo) source.read(); + + Foo foo2 = (Foo) reader.read(); Assert.state(!foo2.equals(foo1)); - - Foo foo3 = (Foo) source.read(); + + Foo foo3 = (Foo) reader.read(); Assert.state(!foo2.equals(foo3)); - - getAsSkippable(source).skip(); - + + getAsSkippable(reader).skip(); + rollback(); - - ExecutionAttributes streamContext = getAsRestartable(source).getExecutionAttributes(); + + ExecutionAttributes streamContext = getAsItemStream(reader).getExecutionAttributes(); // create new input source - source = createItemReader(); + reader = createItemReader(); - getAsRestartable(source).restoreFrom(streamContext); + getAsItemStream(reader).restoreFrom(streamContext); - assertEquals(foo2, source.read()); - Foo foo4 = (Foo) source.read(); + assertEquals(foo2, reader.read()); + Foo foo4 = (Foo) reader.read(); assertEquals(4, foo4.getValue()); } private void commit() { - ((ItemStream) source).mark(); + ((ItemStream) reader).mark(); } - + private void rollback() { - ((ItemStream) source).reset(); + ((ItemStream) reader).reset(); } - + private Skippable getAsSkippable(ItemReader source) { return (Skippable) source; } - - private ItemStream getAsRestartable(ItemReader source) { + + private ItemStream getAsItemStream(ItemReader source) { return (ItemStream) source; } private InitializingBean getAsInitializingBean(ItemReader source) { return (InitializingBean) source; } - - private DisposableBean getAsDisposableBean(ItemReader source) { - return (DisposableBean) source; - } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventItemReaderTests.java index 4f596346d..5b6e20a04 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventItemReaderTests.java @@ -88,7 +88,7 @@ public class StaxEventItemReaderTests extends TestCase { assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments - source.destroy(); + source.close(); } /** @@ -228,7 +228,7 @@ public class StaxEventItemReaderTests extends TestCase { assertNotNull(item); assertTrue(newSource.isOpenCalled()); - newSource.destroy(); // includes close() + newSource.close(); newSource.setOpenCalled(false); // calling read again should require re-initialization because of close item = newSource.read(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventWriterItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventWriterItemWriterTests.java index a870b552c..476c56eef 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventWriterItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/io/xml/StaxEventWriterItemWriterTests.java @@ -42,7 +42,6 @@ public class StaxEventWriterItemWriterTests extends TestCase { private static final int NOT_FOUND = -1; - protected void setUp() throws Exception { resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", "xml")); writer = createItemWriter(); @@ -94,9 +93,10 @@ public class StaxEventWriterItemWriterTests extends TestCase { writer = createItemWriter(); writer.restoreFrom(streamContext); writer.write(record); - writer.destroy(); + writer.close(); - // check the output is concatenation of 'before restart' and 'after restart' writes. + // check the output is concatenation of 'before restart' and 'after + // restart' writes. String outputFile = outputFileContent(); int firstRecord = outputFile.indexOf(TEST_STRING); int secondRecord = outputFile.indexOf(TEST_STRING, firstRecord + TEST_STRING.length()); @@ -115,8 +115,7 @@ public class StaxEventWriterItemWriterTests extends TestCase { final int NUMBER_OF_RECORDS = 10; for (int i = 1; i <= NUMBER_OF_RECORDS; i++) { writer.write(record); - long writeStatistics = - writer.getExecutionAttributes().getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME); + long writeStatistics = writer.getExecutionAttributes().getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME); assertEquals(i, writeStatistics); } @@ -127,9 +126,11 @@ public class StaxEventWriterItemWriterTests extends TestCase { */ public void testOpenAndClose() throws IOException { writer.setRootTagName("testroot"); - writer.setRootElementAttributes(new HashMap() {{ - put("attribute", "value"); - }}); + writer.setRootElementAttributes(new HashMap() { + { + put("attribute", "value"); + } + }); writer.open(); writer.mark(); @@ -191,12 +192,12 @@ public class StaxEventWriterItemWriterTests extends TestCase { Marshaller marshaller = new SimpleMarshaller(); MarshallingEventWriterSerializer serializer = new MarshallingEventWriterSerializer(marshaller); source.setSerializer(serializer); - + source.setEncoding("UTF-8"); source.setRootTagName("root"); source.setVersion("1.0"); source.setOverwriteOutput(true); - + source.afterPropertiesSet(); return source; diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/reader/StagingItemReader.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/reader/StagingItemReader.java index 8ebfa4cde..9eb874fef 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/reader/StagingItemReader.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/item/reader/StagingItemReader.java @@ -11,11 +11,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.execution.scope.StepContext; import org.springframework.batch.execution.scope.StepContextAware; +import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.KeyedItemReader; -import org.springframework.batch.item.ExecutionAttributes; import org.springframework.batch.sample.item.writer.StagingItemWriter; -import org.springframework.beans.factory.DisposableBean; import org.springframework.dao.OptimisticLockingFailureException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; @@ -24,8 +23,7 @@ import org.springframework.jdbc.support.lob.LobHandler; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.Assert; -public class StagingItemReader extends JdbcDaoSupport implements ItemStream, KeyedItemReader, DisposableBean, - StepContextAware { +public class StagingItemReader extends JdbcDaoSupport implements ItemStream, KeyedItemReader, StepContextAware { // Key for buffer in transaction synchronization manager private static final String BUFFER_KEY = StagingItemReader.class.getName() + ".BUFFER"; @@ -54,14 +52,6 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key } } - /** - * @throws Exception - * @see org.springframework.batch.io.driving.DrivingQueryItemReader#destroy() - */ - public void destroy() throws Exception { - close(); - } - /** * * @see org.springframework.batch.io.driving.DrivingQueryItemReader#open() @@ -220,6 +210,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key /* * (non-Javadoc) + * * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionAttributes) */ public void mark() { @@ -228,6 +219,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key /* * (non-Javadoc) + * * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionAttributes) */ public void reset() { @@ -236,6 +228,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key /* * (non-Javadoc) + * * @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.ExecutionAttributes) */ public void restoreFrom(ExecutionAttributes context) { @@ -244,6 +237,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key /* * (non-Javadoc) + * * @see org.springframework.batch.item.ExecutionAttributesProvider#getExecutionAttributes() */ public ExecutionAttributes getExecutionAttributes() { diff --git a/spring-batch-samples/src/main/resources/jobs/xmlStaxJob.xml b/spring-batch-samples/src/main/resources/jobs/xmlStaxJob.xml index daa942364..06f1371fd 100644 --- a/spring-batch-samples/src/main/resources/jobs/xmlStaxJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/xmlStaxJob.xml @@ -52,7 +52,8 @@ + id="tradeStaxWriter" scope="step"> +