BATCH-760: first cut of JdbcPagingItemReader - works for HSQL, need to test other platforms
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.batch.item.CommonItemStreamItemReaderTests;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class JdbcPagingItemReaderCommonTests extends CommonItemStreamItemReaderTests {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
protected ItemReader<Foo> getItemReader() throws Exception {
|
||||
|
||||
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReader<Foo>();
|
||||
reader.setSelectClause("select ID, NAME, VALUE");
|
||||
reader.setFromClause("from T_FOOS");
|
||||
reader.setSortKey("ID");
|
||||
reader.setDataSource(dataSource);
|
||||
reader.setParameterizedRowMapper(
|
||||
new ParameterizedRowMapper<Foo>() {
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
);
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
reader.setSaveState(true);
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
|
||||
JdbcPagingItemReader<Foo> reader = (JdbcPagingItemReader<Foo>) tested;
|
||||
reader.close(new ExecutionContext());
|
||||
reader.setSelectClause("select ID, NAME, VALUE");
|
||||
reader.setFromClause("from T_FOOS");
|
||||
reader.setWhereClause("where id = -1");
|
||||
reader.setDataSource(dataSource);
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
reader.open(new ExecutionContext());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Tests for {@link JpaPagingItemReader}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "data-source-context.xml")
|
||||
public class JdbcPagingItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
|
||||
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
JdbcPagingItemReader<Foo> inputSource = new JdbcPagingItemReader<Foo>();
|
||||
inputSource.setSelectClause("select ID, NAME, VALUE");
|
||||
inputSource.setFromClause("from T_FOOS");
|
||||
inputSource.setSortKey("ID");
|
||||
inputSource.setDataSource(dataSource);
|
||||
inputSource.setParameterizedRowMapper(
|
||||
new ParameterizedRowMapper<Foo>() {
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
);
|
||||
inputSource.setPageSize(3);
|
||||
inputSource.afterPropertiesSet();
|
||||
inputSource.setSaveState(true);
|
||||
|
||||
return inputSource;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user