IN PROGRESS - BATCH-711: Upgrade ItemWriter and implementations to use parameterized types
This commit is contained in:
@@ -11,24 +11,28 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* The implementation is thread-safe if the delegate is thread-safe.
|
||||
*
|
||||
* <code>I</code> is the type of item expected as input, <code>O</code> it the
|
||||
* type of item after transformation that is passed to
|
||||
* {@link #setDelegate(ItemWriter)}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class DelegatingItemWriter implements ItemWriter, InitializingBean {
|
||||
public class DelegatingItemWriter<I, O> implements ItemWriter<I>, InitializingBean {
|
||||
|
||||
private ItemWriter<O> delegate;
|
||||
|
||||
private ItemWriter delegate;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public DelegatingItemWriter() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param itemWriter
|
||||
*/
|
||||
public DelegatingItemWriter(ItemWriter itemWriter) {
|
||||
public DelegatingItemWriter(ItemWriter<O> itemWriter) {
|
||||
this();
|
||||
this.delegate = itemWriter;
|
||||
}
|
||||
@@ -36,28 +40,29 @@ public class DelegatingItemWriter implements ItemWriter, InitializingBean {
|
||||
/**
|
||||
* Calls {@link #doProcess(Object)} and then writes the result to the
|
||||
* delegate {@link ItemWriter}.
|
||||
* @throws Exception
|
||||
* @throws Exception
|
||||
*
|
||||
* @see ItemWriter#write(Object)
|
||||
*/
|
||||
public void write(Object item) throws Exception {
|
||||
Object result = doProcess(item);
|
||||
public void write(I item) throws Exception {
|
||||
O result = doProcess(item);
|
||||
delegate.write(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* By default returns the argument. This method is an extension point meant
|
||||
* to be overridden by subclasses that implement processing logic.
|
||||
* @throws Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
protected Object doProcess(Object item) throws Exception {
|
||||
return item;
|
||||
@SuppressWarnings("unchecked")
|
||||
protected O doProcess(I item) throws Exception {
|
||||
return (O) item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for {@link ItemWriter}.
|
||||
*/
|
||||
public void setDelegate(ItemWriter writer) {
|
||||
public void setDelegate(ItemWriter<O> writer) {
|
||||
this.delegate = writer;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||
*/
|
||||
public class BatchSqlUpdateItemWriterTests extends TestCase {
|
||||
|
||||
private BatchSqlUpdateItemWriter writer = new BatchSqlUpdateItemWriter();
|
||||
private BatchSqlUpdateItemWriter<String> writer = new BatchSqlUpdateItemWriter<String>();
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
};
|
||||
}
|
||||
|
||||
private class StubItemWriter implements ItemWriter {
|
||||
private class StubItemWriter implements ItemWriter<Object> {
|
||||
public void write(Object item) {
|
||||
list.add(item);
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
HibernateAwareItemWriter writer = new HibernateAwareItemWriter();
|
||||
HibernateAwareItemWriter<Object> writer = new HibernateAwareItemWriter<Object>();
|
||||
|
||||
final List<Object> list = new ArrayList<Object>();
|
||||
|
||||
@@ -95,7 +95,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
writer = new HibernateAwareItemWriter();
|
||||
writer = new HibernateAwareItemWriter<Object>();
|
||||
try {
|
||||
writer.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
|
||||
@@ -16,7 +16,7 @@ public class HibernateCursorItemReaderCommonTests extends CommonDatabaseItemStre
|
||||
|
||||
String hsqlQuery = "from Foo";
|
||||
|
||||
HibernateCursorItemReader reader = new HibernateCursorItemReader();
|
||||
HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReader<Foo>();
|
||||
reader.setQueryString(hsqlQuery);
|
||||
reader.setSessionFactory(sessionFactory);
|
||||
reader.setUseStatelessSession(true);
|
||||
@@ -36,8 +36,8 @@ public class HibernateCursorItemReaderCommonTests extends CommonDatabaseItemStre
|
||||
|
||||
}
|
||||
|
||||
protected void pointToEmptyInput(ItemReader tested) throws Exception {
|
||||
HibernateCursorItemReader reader = (HibernateCursorItemReader) tested;
|
||||
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
|
||||
HibernateCursorItemReader<Foo> reader = (HibernateCursorItemReader<Foo>) tested;
|
||||
reader.close(new ExecutionContext());
|
||||
reader.setQueryString("from Foo foo where foo.id = -1");
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
|
||||
/**
|
||||
* Tests for {@link HibernateCursorItemReader} using standard hibernate {@link Session}.
|
||||
@@ -20,13 +21,13 @@ public class HibernateCursorItemReaderStatefulIntegrationTests extends Hibernate
|
||||
//Ensure close is called on the stateful session correctly.
|
||||
public void testStatfulClose(){
|
||||
|
||||
MockControl sessionFactoryControl = MockControl.createControl(SessionFactory.class);
|
||||
SessionFactory sessionFactory = (SessionFactory) sessionFactoryControl.getMock();
|
||||
MockControl sessionControl = MockControl.createControl(Session.class);
|
||||
Session session = (Session) sessionControl.getMock();
|
||||
MockControl resultsControl = MockControl.createNiceControl(Query.class);
|
||||
Query scrollableResults = (Query) resultsControl.getMock();
|
||||
HibernateCursorItemReader itemReader = new HibernateCursorItemReader();
|
||||
MockControl<SessionFactory> sessionFactoryControl = MockControl.createControl(SessionFactory.class);
|
||||
SessionFactory sessionFactory = sessionFactoryControl.getMock();
|
||||
MockControl<Session> sessionControl = MockControl.createControl(Session.class);
|
||||
Session session = sessionControl.getMock();
|
||||
MockControl<Query> resultsControl = MockControl.createNiceControl(Query.class);
|
||||
Query scrollableResults = resultsControl.getMock();
|
||||
HibernateCursorItemReader<Foo> itemReader = new HibernateCursorItemReader<Foo>();
|
||||
itemReader.setSessionFactory(sessionFactory);
|
||||
itemReader.setQueryString("testQuery");
|
||||
itemReader.setUseStatelessSession(false);
|
||||
|
||||
@@ -18,7 +18,7 @@ import org.springframework.test.AbstractTransactionalDataSourceSpringContextTest
|
||||
*/
|
||||
public class HibernateCursorProjectionItemReaderIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected ItemReader reader;
|
||||
protected ItemReader<?> reader;
|
||||
protected ExecutionContext executionContext;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
@@ -36,7 +36,7 @@ public class HibernateCursorProjectionItemReaderIntegrationTests extends Abstrac
|
||||
}
|
||||
|
||||
|
||||
protected ItemReader createItemReader() throws Exception {
|
||||
protected ItemReader<?> createItemReader() throws Exception {
|
||||
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
|
||||
factoryBean.setDataSource(super.getJdbcTemplate().getDataSource());
|
||||
factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) });
|
||||
@@ -46,7 +46,7 @@ public class HibernateCursorProjectionItemReaderIntegrationTests extends Abstrac
|
||||
|
||||
String hsqlQuery = "select f.value, f.name from Foo f";
|
||||
|
||||
HibernateCursorItemReader inputSource = new HibernateCursorItemReader();
|
||||
HibernateCursorItemReader<Object> inputSource = new HibernateCursorItemReader<Object>();
|
||||
inputSource.setQueryString(hsqlQuery);
|
||||
inputSource.setSessionFactory(sessionFactory);
|
||||
inputSource.afterPropertiesSet();
|
||||
|
||||
Reference in New Issue
Block a user