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

This commit is contained in:
trisberg
2008-07-28 18:55:50 +00:00
parent d6cd23f1c7
commit 4018507753
10 changed files with 106 additions and 107 deletions

View File

@@ -8,7 +8,7 @@ import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.support.ItemTrackingItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
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;
@@ -24,24 +24,24 @@ public class SkipSampleFunctionalTests extends AbstractValidatingBatchLauncherTe
int before = -1;
JdbcTemplate jdbcTemplate;
SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
ItemTrackingItemWriter<?> writer;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Before
public void onSetUp() throws Exception {
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
}
protected void validatePostConditions() throws Exception {
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
// 5 input records, 1 skipped => 4 written to output
assertEquals(before + 4, after);

View File

@@ -24,7 +24,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -37,9 +36,8 @@ import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
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;
import org.springframework.transaction.annotation.Transactional;
@@ -56,23 +54,21 @@ public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTest
private List<Trade> trades;
private int activeRow = 0;
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
private Map<String, Double> credits = new HashMap<String, Double>();
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@SuppressWarnings("unchecked")
@Before
public void onSetUp() throws Exception {
// super.onSetUp();
jdbcTemplate.update("delete from TRADE");
List<Map<?, ?>> list = jdbcTemplate.queryForList("select name, CREDIT from customer");
for (Iterator<Map<?,?>> iterator = list.iterator(); iterator.hasNext();) {
Map<?,?> map = iterator.next();
credits.put((String) map.get("NAME"), new Double(((Number)map.get("CREDIT")).doubleValue()));
simpleJdbcTemplate.update("delete from TRADE");
List<Map<String, Object>> list = simpleJdbcTemplate.queryForList("select name, CREDIT from customer");
for (Map<String, Object> map : list) {
credits.put((String) map.get("NAME"), ((Number) map.get("CREDIT")).doubleValue());
}
}
@@ -85,10 +81,10 @@ public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTest
// assertTrue(((Resource)applicationContext.getBean("customerFileLocator")).exists());
customers = new ArrayList<Customer>() {{add(new Customer("customer1", (credits.get("customer1").doubleValue() - 98.34)));
add(new Customer("customer2", (credits.get("customer2").doubleValue() - 18.12 - 12.78)));
add(new Customer("customer3", (credits.get("customer3").doubleValue() - 109.25)));
add(new Customer("customer4", (credits.get("customer4").doubleValue() - 123.39)));}};
customers = new ArrayList<Customer>() {{add(new Customer("customer1", (credits.get("customer1") - 98.34)));
add(new Customer("customer2", (credits.get("customer2") - 18.12 - 12.78)));
add(new Customer("customer3", (credits.get("customer3") - 109.25)));
add(new Customer("customer4", credits.get("customer4") - 123.39));}};
trades = new ArrayList<Trade>() {{add(new Trade("UK21341EAH45", 978, new BigDecimal("98.34"), "customer1"));
add(new Trade("UK21341EAH46", 112, new BigDecimal("18.12"), "customer2"));
@@ -97,10 +93,10 @@ public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTest
add(new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));}};
// check content of the trade table
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
simpleJdbcTemplate.getJdbcOperations().query(GET_TRADES, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Trade trade = (Trade)trades.get(activeRow++);
Trade trade = trades.get(activeRow++);
assertTrue(trade.getIsin().equals(rs.getString(1)));
assertTrue(trade.getQuantity() == rs.getLong(2));
@@ -113,10 +109,10 @@ public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTest
// check content of the customer table
activeRow = 0;
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
simpleJdbcTemplate.getJdbcOperations().query(GET_CUSTOMERS, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
Customer customer = (Customer)customers.get(activeRow++);
Customer customer = customers.get(activeRow++);
assertEquals(customer.getName(),rs.getString(1));
assertEquals(customer.getCredit(), rs.getDouble(2), .01);

View File

@@ -13,11 +13,8 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.sample.common.StagingItemReader;
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;
import org.springframework.transaction.PlatformTransactionManager;
@@ -31,7 +28,7 @@ import org.springframework.transaction.support.TransactionTemplate;
@ContextConfiguration()
public class StagingItemReaderTests {
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
private PlatformTransactionManager transactionManager;
@@ -45,13 +42,9 @@ public class StagingItemReaderTests {
private Long jobId = 11L;
public JdbcOperations getJdbcTemplate() {
return jdbcTemplate;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Before
@@ -70,23 +63,23 @@ public class StagingItemReaderTests {
@After
public void onTearDownAfterTransaction() throws Exception {
reader.close(null);
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
simpleJdbcTemplate.update("DELETE FROM BATCH_STAGING");
}
@Transactional @Test
public void testReaderUpdatesProcessIndicator() throws Exception {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
long id = simpleJdbcTemplate.queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
jobId);
String before = simpleJdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, id);
assertEquals(StagingItemWriter.NEW, before);
String item = reader.read();
assertEquals("FOO", item);
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
String after = simpleJdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, id);
assertEquals(StagingItemWriter.DONE, after);
}
@@ -98,11 +91,11 @@ public class StagingItemReaderTests {
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
long id = simpleJdbcTemplate.queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
jobId);
String before =
(String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
simpleJdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, id);
assertEquals(StagingItemWriter.DONE, before);
return null;
}
@@ -119,8 +112,8 @@ public class StagingItemReaderTests {
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
new Object[] { jobId, StagingItemWriter.NEW });
int count = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
jobId, StagingItemWriter.NEW);
assertEquals(4, count);
Object item = reader.read();
@@ -178,10 +171,10 @@ public class StagingItemReaderTests {
final Long idToUse = (Long)txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
long id = simpleJdbcTemplate.queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
jobId);
String before = simpleJdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, id);
assertEquals(StagingItemWriter.NEW, before);
Object item = reader.read();
@@ -200,8 +193,8 @@ public class StagingItemReaderTests {
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { idToUse }, String.class);
String after = simpleJdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, idToUse);
assertEquals(StagingItemWriter.NEW, after);
Object item = reader.read();

View File

@@ -26,10 +26,8 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
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;
import org.springframework.transaction.annotation.Transactional;
@@ -38,23 +36,19 @@ import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration()
public class StagingItemWriterTests {
private JdbcOperations jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
private StagingItemWriter<String> writer;
public JdbcOperations getJdbcTemplate() {
return jdbcTemplate;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Before
public void onSetUpBeforeTransaction() throws Exception {
StepExecution stepExecution = new StepExecution("stepName", new JobExecution(new JobInstance(new Long(12L),
StepExecution stepExecution = new StepExecution("stepName", new JobExecution(new JobInstance(12L,
new JobParameters(), "testJob")));
writer.beforeStep(stepExecution);
}
@@ -62,9 +56,9 @@ public class StagingItemWriterTests {
@Transactional
@Test
public void testProcessInsertsNewItem() throws Exception {
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from BATCH_STAGING");
writer.write("FOO");
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from BATCH_STAGING");
assertEquals(before + 1, after);
}

View File

@@ -26,10 +26,9 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.batch.sample.domain.football.internal.JdbcPlayerDao;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.transaction.annotation.Transactional;
@@ -48,14 +47,14 @@ public class JdbcPlayerDaoIntegrationTests {
private static final String GET_PLAYER = "SELECT * from PLAYERS";
private JdbcTemplate jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void init(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
playerDao = new JdbcPlayerDao();
playerDao.setJdbcTemplate(this.jdbcTemplate);
playerDao.setDataSource(dataSource);
player = new Player();
player.setID("AKFJDL00");
@@ -71,7 +70,7 @@ public class JdbcPlayerDaoIntegrationTests {
@Before
public void onSetUpInTransaction() throws Exception {
jdbcTemplate.execute("delete from PLAYERS");
simpleJdbcTemplate.getJdbcOperations().execute("delete from PLAYERS");
}
@@ -80,7 +79,7 @@ public class JdbcPlayerDaoIntegrationTests {
playerDao.savePlayer(player);
jdbcTemplate.query(GET_PLAYER, new RowCallbackHandler(){
simpleJdbcTemplate.getJdbcOperations().query(GET_PLAYER, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");

View File

@@ -23,10 +23,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.football.PlayerSummary;
import org.springframework.batch.sample.domain.football.internal.JdbcPlayerSummaryDao;
import org.springframework.batch.sample.domain.football.internal.PlayerSummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
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.transaction.annotation.Transactional;
@@ -44,14 +42,14 @@ public class JdbcPlayerSummaryDaoIntegrationTests {
private PlayerSummary summary;
private JdbcTemplate jdbcTemplate;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void init(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
playerSummaryDao = new JdbcPlayerSummaryDao();
playerSummaryDao.setJdbcTemplate(this.jdbcTemplate);
playerSummaryDao.setDataSource(dataSource);
summary = new PlayerSummary();
summary.setId("AikmTr00");
@@ -72,7 +70,7 @@ public class JdbcPlayerSummaryDaoIntegrationTests {
@Before
public void onSetUpInTransaction() throws Exception {
jdbcTemplate.execute("delete from PLAYER_SUMMARY");
simpleJdbcTemplate.getJdbcOperations().execute("delete from PLAYER_SUMMARY");
}
@@ -81,10 +79,11 @@ public class JdbcPlayerSummaryDaoIntegrationTests {
playerSummaryDao.write(summary);
PlayerSummary testSummary = (PlayerSummary) jdbcTemplate.queryForObject("SELECT * FROM PLAYER_SUMMARY",
PlayerSummary testSummary = simpleJdbcTemplate.queryForObject("SELECT * FROM PLAYER_SUMMARY",
new PlayerSummaryMapper());
assertEquals(testSummary, summary);
assertEquals(summary, testSummary);
}
}