IN PROGRESS - BATCH-672: updated tests to use SimpleJdbcTemplate

This commit is contained in:
trisberg
2008-07-28 18:00:45 +00:00
parent bbe8a7270a
commit d6cd23f1c7
5 changed files with 45 additions and 52 deletions

View File

@@ -16,9 +16,8 @@ import org.apache.commons.io.IOUtils;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -36,7 +35,7 @@ public class CompositeItemWriterSampleFunctionalTests extends AbstractValidating
"Trade: [isin=UK21341EAH44,quantity=214,price=34.11,customer=customer4]" +
"Trade: [isin=UK21341EAH45,quantity=215,price=35.11,customer=customer5]";
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
private int activeRow = 0;
@@ -44,15 +43,15 @@ public class CompositeItemWriterSampleFunctionalTests extends AbstractValidating
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
/* (non-Javadoc)
* @see org.springframework.batch.sample.AbstractLifecycleSpringContextTests#validatePreConditions()
*/
protected void validatePreConditions() throws Exception {
jdbcTemplate.update("DELETE from TRADE");
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
simpleJdbcTemplate.update("DELETE from TRADE");
before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
}
protected void validatePostConditions() throws Exception {
@@ -69,11 +68,11 @@ public class CompositeItemWriterSampleFunctionalTests extends AbstractValidating
add(new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5"));
}};
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
assertEquals(before+5, after);
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
simpleJdbcTemplate.getJdbcOperations().query(GET_TRADES, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Trade trade = trades.get(activeRow++);
@@ -99,7 +98,4 @@ public class CompositeItemWriterSampleFunctionalTests extends AbstractValidating
assertEquals(EXPECTED_OUTPUT_FILE, output);
}
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}

View File

@@ -37,9 +37,8 @@ import org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMappe
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -51,14 +50,14 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
private static final int LINE_LENGTH = 29;
//auto-injected attributes
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
private Resource fileLocator;
private FlatFileItemReader<Trade> inputSource;
private LineTokenizer lineTokenizer;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Autowired
@@ -69,7 +68,7 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
@Before
public void onSetUp() throws Exception {
jdbcTemplate.update("delete from TRADE");
simpleJdbcTemplate.update("delete from TRADE");
fileLocator = new ClassPathResource("data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt");
inputSource = new FlatFileItemReader<Trade>();
@@ -89,23 +88,24 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
inputSource.open(new ExecutionContext());
jdbcTemplate.query("SELECT ID, ISIN, QUANTITY, PRICE, CUSTOMER FROM trade ORDER BY id", new RowCallbackHandler() {
simpleJdbcTemplate.getJdbcOperations().query(
"SELECT ID, ISIN, QUANTITY, PRICE, CUSTOMER FROM trade ORDER BY id",
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Trade trade;
try {
trade = inputSource.read();
}
catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
assertEquals(trade.getIsin(), rs.getString(2));
assertEquals(trade.getQuantity(),rs.getLong(3));
assertEquals(trade.getPrice(), rs.getBigDecimal(4));
assertEquals(trade.getCustomer(), rs.getString(5));
}
public void processRow(ResultSet rs) throws SQLException {
Trade trade;
try {
trade = (Trade)inputSource.read();
}
catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
assertEquals(trade.getIsin(), rs.getString(2));
assertEquals(trade.getQuantity(),rs.getLong(3));
assertEquals(trade.getPrice(), rs.getBigDecimal(4));
assertEquals(trade.getCustomer(), rs.getString(5));
}
});
});
assertNull(inputSource.read());
}
@@ -114,7 +114,7 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
* fixed-length file is expected on input
*/
protected void validatePreConditions() throws Exception{
BufferedReader reader = null;
BufferedReader reader;
reader = new BufferedReader(new FileReader(fileLocator.getFile()));
String line;

View File

@@ -6,8 +6,7 @@ import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -15,15 +14,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration()
public class FootballJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
protected void validatePostConditions() throws Exception {
int count = jdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY");
int count = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY");
assertTrue(count>0);
}

View File

@@ -7,8 +7,7 @@ import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.common.StagingItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -16,24 +15,24 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration()
public class ParallelJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
protected void validatePostConditions() throws Exception {
int count;
count = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
new Object[] {StagingItemWriter.NEW});
StagingItemWriter.NEW);
assertEquals(0, count);
int total = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING");
count = jdbcTemplate.queryForInt(
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
new Object[] {StagingItemWriter.DONE});
StagingItemWriter.DONE);
assertEquals(total, count);
}

View File

@@ -28,8 +28,7 @@ import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
@@ -45,11 +44,11 @@ import org.springframework.test.context.transaction.BeforeTransaction;
public class RestartFunctionalTests extends AbstractBatchLauncherTests {
// auto-injected attributes
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
/*
@@ -58,7 +57,7 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
*/
@BeforeTransaction
public void onTearDown() throws Exception {
jdbcTemplate.update("DELETE FROM TRADE");
simpleJdbcTemplate.update("DELETE FROM TRADE");
}
/**
@@ -72,7 +71,7 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
@Test
public void testRestart() throws Exception {
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
try {
runJobForRestartTest();
@@ -84,13 +83,13 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
"planned") >= 0);
}
int medium = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
int medium = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
// assert based on commit interval = 2
assertEquals(before + 2, medium);
runJobForRestartTest();
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
assertEquals(before + 5, after);
}