added a BatchResourceFactoryBean to fixedlengthImportJob and fixed a repository error it illuminated.

This commit is contained in:
lucasward
2008-01-28 15:41:43 +00:00
parent c412bce1f0
commit f092cb8a99
7 changed files with 82 additions and 37 deletions

View File

@@ -6,6 +6,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.batch.sample.item.processor.CustomerCreditIncreaseProcessor;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
@@ -28,6 +29,13 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
protected PlatformTransactionManager transactionManager;
private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseProcessor.FIXED_AMOUNT;
private static String[] customers = { "INSERT INTO customer (id, version, name, credit) VALUES (1, 0, 'customer1', 100000)",
"INSERT INTO customer (id, version, name, credit) VALUES (2, 0, 'customer2', 100000)",
"INSERT INTO customer (id, version, name, credit) VALUES (3, 0, 'customer3', 100000)",
"INSERT INTO customer (id, version, name, credit) VALUES (4, 0, 'customer4', 100000)"};
private static String DELETE_CUSTOMERS = "DELETE FROM customer";
private static final String ALL_CUSTOMERS = "select * from CUSTOMER order by ID";
@@ -36,6 +44,8 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
protected static final String ID_COLUMN = "ID";
private List creditsBeforeUpdate;
private SessionFactory sessionFactory;
/**
* @param jdbcTemplate
@@ -44,6 +54,10 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
this.jdbcTemplate = jdbcTemplate;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Public setter for the PlatformTransactionManager.
* @param transactionManager the transactionManager to set
@@ -57,8 +71,10 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
*/
protected void validatePreConditions() throws Exception {
super.validatePreConditions();
ensureState();
creditsBeforeUpdate = (List) new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getBigDecimal(CREDIT_COLUMN);
@@ -68,6 +84,24 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
});
}
/*
* Ensure the state of the database is accurate by delete all the contents of the
* customer table and reading the expected defaults.
*/
private void ensureState(){
new TransactionTemplate(transactionManager).execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
jdbcTemplate.update(DELETE_CUSTOMERS);
for(int i = 0; i < customers.length;i++){
jdbcTemplate.update(customers[i]);
}
return null;
}
});
}
/**
* Credit was increased by CREDIT_INCREASE
*/
@@ -94,8 +128,7 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida
});
assertEquals(getExpectedMatches(), matches.size());
checkMatches(matches);
checkMatches(matches);
}
/**

View File

@@ -21,8 +21,14 @@ import java.io.FileReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.batch.io.file.DefaultFlatFileItemReader;
import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.io.file.transform.LineTokenizer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.sample.domain.Trade;
import org.springframework.batch.sample.mapping.TradeFieldSetMapper;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowCallbackHandler;
@@ -35,11 +41,17 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
//auto-injected attributes
private JdbcOperations jdbcTemplate;
private Resource fileLocator;
private ItemReader inputSource;
private DefaultFlatFileItemReader inputSource;
private LineTokenizer lineTokenizer;
protected void onSetUp() throws Exception {
super.onSetUp();
jdbcTemplate.update("delete from TRADE");
fileLocator = new ClassPathResource("data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt");
inputSource = new DefaultFlatFileItemReader();
inputSource.setFieldSetMapper(new TradeFieldSetMapper());
inputSource.setTokenizer(lineTokenizer);
inputSource.setResource(fileLocator);
}
protected String[] getConfigLocations() {
@@ -89,12 +101,9 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setFileLocator(Resource fileLocator) {
this.fileLocator = fileLocator;
public void setLineTokenizer(LineTokenizer lineTokenizer) {
this.lineTokenizer = lineTokenizer;
}
public void setItemReader(ItemReader inputSource){
this.inputSource = inputSource;
}
}