IN PROGRESS - BATCH-672: updated tests to use SimpleJdbcTemplate
This commit is contained in:
@@ -26,10 +26,9 @@ import javax.sql.DataSource;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.sample.domain.trade.Trade;
|
||||
import org.springframework.batch.sample.domain.trade.internal.JdbcTradeDao;
|
||||
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.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -39,30 +38,27 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@ContextConfiguration(locations = {"/data-source-context.xml"})
|
||||
public class JdbcTradeWriterTests {
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
private AbstractDataFieldMaxValueIncrementer incrementer;
|
||||
private JdbcTradeDao writer;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
this.writer = new JdbcTradeDao();
|
||||
this.writer.setDataSource(dataSource);
|
||||
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setIncrementer(AbstractDataFieldMaxValueIncrementer incrementer) {
|
||||
this.incrementer = incrementer;
|
||||
incrementer.setIncrementerName("TRADE_SEQ");
|
||||
this.writer.setIncrementer(incrementer);
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testWrite() {
|
||||
|
||||
JdbcTradeDao writer = new JdbcTradeDao();
|
||||
|
||||
incrementer.setIncrementerName("TRADE_SEQ");
|
||||
|
||||
writer.setIncrementer(incrementer);
|
||||
writer.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setCustomer("testCustomer");
|
||||
trade.setIsin("5647238492");
|
||||
@@ -71,7 +67,7 @@ public class JdbcTradeWriterTests {
|
||||
|
||||
writer.writeTrade(trade);
|
||||
|
||||
jdbcTemplate.query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", new RowCallbackHandler() {
|
||||
simpleJdbcTemplate.getJdbcOperations().query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals("testCustomer", rs.getString("CUSTOMER"));
|
||||
assertEquals(new BigDecimal(Double.toString(99.69)), rs.getBigDecimal("PRICE"));
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -41,7 +40,7 @@ import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
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.test.context.transaction.AfterTransaction;
|
||||
@@ -64,7 +63,7 @@ public class JdbcJobRepositoryTests {
|
||||
|
||||
private List<Serializable> list = new ArrayList<Serializable>();
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@@ -73,7 +72,7 @@ public class JdbcJobRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@@ -90,26 +89,23 @@ public class JdbcJobRepositoryTests {
|
||||
public void onSetUpInTransaction() throws Exception {
|
||||
jobConfiguration = new JobSupport("test-job");
|
||||
jobConfiguration.setRestartable(true);
|
||||
jdbcTemplate.update("DELETE FROM BATCH_EXECUTION_CONTEXT");
|
||||
jdbcTemplate.update("DELETE FROM BATCH_STEP_EXECUTION");
|
||||
jdbcTemplate.update("DELETE FROM BATCH_JOB_EXECUTION");
|
||||
jdbcTemplate.update("DELETE FROM BATCH_JOB_PARAMS");
|
||||
jdbcTemplate.update("DELETE FROM BATCH_JOB_INSTANCE");
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_EXECUTION_CONTEXT");
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_STEP_EXECUTION");
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_JOB_EXECUTION");
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_JOB_PARAMS");
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_JOB_INSTANCE");
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void onTearDownAfterTransaction() throws Exception {
|
||||
for (Iterator<Long> iterator = jobExecutionIds.iterator(); iterator.hasNext();) {
|
||||
Long id = iterator.next();
|
||||
jdbcTemplate.update("DELETE FROM BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", new Object[] { id });
|
||||
for (Long id : jobExecutionIds) {
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", id);
|
||||
}
|
||||
for (Iterator<Long> iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = iterator.next();
|
||||
jdbcTemplate.update("DELETE FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
for (Long id : jobIds) {
|
||||
simpleJdbcTemplate.update("DELETE FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", id);
|
||||
}
|
||||
for (Iterator<Long> iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = iterator.next();
|
||||
int count = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
for (Long id : jobIds) {
|
||||
int count = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", id);
|
||||
assertEquals(0, count);
|
||||
}
|
||||
}
|
||||
@@ -117,9 +113,9 @@ public class JdbcJobRepositoryTests {
|
||||
@Transactional @Test
|
||||
public void testFindOrCreateJob() throws Exception {
|
||||
jobConfiguration.setName("foo");
|
||||
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(before + 1, after);
|
||||
assertNotNull(execution.getId());
|
||||
}
|
||||
@@ -129,10 +125,9 @@ public class JdbcJobRepositoryTests {
|
||||
|
||||
jobConfiguration.setName("bar");
|
||||
|
||||
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(0, before);
|
||||
|
||||
JobExecution execution = null;
|
||||
long t0 = System.currentTimeMillis();
|
||||
try {
|
||||
doConcurrentStart();
|
||||
@@ -143,13 +138,11 @@ public class JdbcJobRepositoryTests {
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
if (execution == null) {
|
||||
execution = (JobExecution) list.get(0);
|
||||
}
|
||||
JobExecution execution = (JobExecution) list.get(0);
|
||||
|
||||
assertNotNull(execution);
|
||||
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before + 1, after);
|
||||
|
||||
@@ -168,7 +161,7 @@ public class JdbcJobRepositoryTests {
|
||||
repository.update(execution);
|
||||
execution.setStatus(BatchStatus.FAILED);
|
||||
|
||||
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(1, before);
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
@@ -181,7 +174,7 @@ public class JdbcJobRepositoryTests {
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before, after);
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
@@ -85,8 +85,7 @@ public class InitializingDataSourceFactoryBean extends AbstractFactoryBean {
|
||||
logger.debug("Could not execute destroy script [" + destroyScript + "]", e);
|
||||
}
|
||||
if (initScripts != null) {
|
||||
for (int i = 0; i < initScripts.length; i++) {
|
||||
Resource initScript = initScripts[i];
|
||||
for (Resource initScript : initScripts) {
|
||||
doExecuteScript(initScript);
|
||||
}
|
||||
}
|
||||
@@ -103,7 +102,7 @@ public class InitializingDataSourceFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
String[] scripts;
|
||||
try {
|
||||
scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource
|
||||
@@ -112,14 +111,14 @@ public class InitializingDataSourceFactoryBean extends AbstractFactoryBean {
|
||||
catch (IOException e) {
|
||||
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
||||
}
|
||||
for (int i = 0; i < scripts.length; i++) {
|
||||
String script = scripts[i].trim();
|
||||
for (String script1 : scripts) {
|
||||
String script = script1.trim();
|
||||
if (StringUtils.hasText(script)) {
|
||||
try {
|
||||
jdbcTemplate.execute(script);
|
||||
jdbcTemplate.getJdbcOperations().execute(script);
|
||||
} catch (DataAccessException e) {
|
||||
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): "+script);
|
||||
logger.debug("DROP script failed (ignoring): " + script);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
@@ -137,7 +136,7 @@ public class InitializingDataSourceFactoryBean extends AbstractFactoryBean {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (String line : list) {
|
||||
if (!line.startsWith("//") && !line.startsWith("--")) {
|
||||
buffer.append(line + "\n");
|
||||
buffer.append(line).append("\n");
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
|
||||
Reference in New Issue
Block a user