RESOLVED - issue BATCH-724: create tests for non-default table prefix. More extensive test using Job.

This commit is contained in:
dsyer
2010-01-04 14:08:09 +00:00
parent c07b2882f3
commit bfd6fccb7d
6 changed files with 63 additions and 50 deletions

View File

@@ -121,7 +121,7 @@ public class TaskletStep extends AbstractStep {
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.notNull(transactionManager, "TransactionManager is mandatory");
Assert.state(transactionManager!=null, "A transaction manager must be provided");
}
/**

View File

@@ -21,8 +21,15 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
@@ -34,7 +41,10 @@ import org.springframework.test.jdbc.SimpleJdbcTestUtils;
public class TablePrefixTests {
@Autowired
private JobRepository jobRepository;
private JobLauncher jobLauncher;
@Autowired
private Job job;
private SimpleJdbcTemplate simpleJdbcTemplate;
@@ -45,7 +55,17 @@ public class TablePrefixTests {
@Test
public void testJobLaunch() throws Exception {
jobRepository.createJobExecution("prefix-test", new JobParameters());
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PREFIX_JOB_INSTANCE"));
}
public static class TestTasklet implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
return RepeatStatus.FINISHED;
}
}
}

View File

@@ -25,7 +25,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
@@ -52,19 +51,17 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
*
*/
public class DataSourceInitializer implements InitializingBean, DisposableBean {
public class DataSourceInitializer implements InitializingBean {
private static final Log logger = LogFactory.getLog(DataSourceInitializer.class);
private Resource[] initScripts;
private Resource[] destroyScripts;
private DataSource dataSource;
private boolean ignoreFailedDrop = true;
private static boolean initialized = false;
private boolean initialized = false;
/**
* Main method as convenient entry point.
@@ -76,34 +73,6 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
DataSourceInitializer.class.getSimpleName() + "-context.xml"));
}
/**
* @throws Throwable
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
super.finalize();
initialized = false;
logger.debug("finalize called");
}
public void destroy() {
if (destroyScripts==null) return;
for (int i = 0; i < destroyScripts.length; i++) {
Resource destroyScript = initScripts[i];
try {
doExecuteScript(destroyScript);
}
catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.warn("Could not execute destroy script [" + destroyScript + "]", e);
}
else {
logger.warn("Could not execute destroy script [" + destroyScript + "]");
}
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(dataSource);
initialize();
@@ -111,11 +80,10 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
private void initialize() {
if (!initialized) {
destroy();
if (initScripts != null) {
for (int i = 0; i < initScripts.length; i++) {
Resource initScript = initScripts[i];
doExecuteScript(initScript);
Resource script = initScripts[i];
doExecuteScript(script);
}
}
initialized = true;
@@ -176,10 +144,6 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
this.initScripts = initScripts;
}
public void setDestroyScripts(Resource[] destroyScripts) {
this.destroyScripts = destroyScripts;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

View File

@@ -1,6 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="job" class="org.springframework.batch.core.job.SimpleJob">
<property name="jobRepository" ref="jobRepository"/>
<property name="steps">
<bean id="step" class="org.springframework.batch.core.step.tasklet.TaskletStep">
<property name="transactionManager" ref="transactionManager"/>
<property name="jobRepository" ref="jobRepository"/>
<property name="tasklet">
<bean class="org.springframework.batch.core.repository.dao.TablePrefixTests$TestTasklet"/>
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource" />

View File

@@ -5,6 +5,7 @@
<property name="dataSource" ref="dataSource" />
<property name="initScripts">
<list>
<value>org/springframework/batch/core/schema-drop-hsqldb.sql</value>
<value>org/springframework/batch/core/schema-hsqldb.sql</value>
<value>foo.sql</value>
</list>

View File

@@ -1,9 +1,20 @@
DROP TABLE PREFIX_STEP_EXECUTION_CONTEXT IF EXISTS;
DROP TABLE PREFIX_JOB_EXECUTION_CONTEXT IF EXISTS;
DROP TABLE PREFIX_STEP_EXECUTION IF EXISTS;
DROP TABLE PREFIX_JOB_EXECUTION IF EXISTS;
DROP TABLE PREFIX_JOB_PARAMS IF EXISTS;
DROP TABLE PREFIX_JOB_INSTANCE IF EXISTS;
DROP TABLE PREFIX_STEP_EXECUTION_SEQ IF EXISTS;
DROP TABLE PREFIX_JOB_EXECUTION_SEQ IF EXISTS;
DROP TABLE PREFIX_JOB_SEQ IF EXISTS;
CREATE TABLE PREFIX_JOB_INSTANCE (
JOB_INSTANCE_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
JOB_NAME VARCHAR(100) NOT NULL,
JOB_KEY VARCHAR(32) NOT NULL,
constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
constraint PREFIX_JOB_INST_UN unique (JOB_NAME, JOB_KEY)
) ;
CREATE TABLE PREFIX_JOB_EXECUTION (
@@ -17,7 +28,7 @@ CREATE TABLE PREFIX_JOB_EXECUTION (
EXIT_CODE VARCHAR(20) ,
EXIT_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP,
constraint JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID)
constraint PREFIX_JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID)
references PREFIX_JOB_INSTANCE(JOB_INSTANCE_ID)
) ;
@@ -29,7 +40,7 @@ CREATE TABLE PREFIX_JOB_PARAMS (
DATE_VAL TIMESTAMP DEFAULT NULL ,
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION ,
constraint JOB_INST_PARAMS_FK foreign key (JOB_INSTANCE_ID)
constraint PREFIX_JOB_INST_PARAMS_FK foreign key (JOB_INSTANCE_ID)
references PREFIX_JOB_INSTANCE(JOB_INSTANCE_ID)
) ;
@@ -52,7 +63,7 @@ CREATE TABLE PREFIX_STEP_EXECUTION (
EXIT_CODE VARCHAR(20) ,
EXIT_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP,
constraint JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID)
constraint PREFIX_JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID)
references PREFIX_JOB_EXECUTION(JOB_EXECUTION_ID)
) ;
@@ -60,7 +71,7 @@ CREATE TABLE PREFIX_STEP_EXECUTION_CONTEXT (
STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
SHORT_CONTEXT VARCHAR(2500) NOT NULL,
SERIALIZED_CONTEXT LONGVARCHAR ,
constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
constraint PREFIX_STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
references PREFIX_STEP_EXECUTION(STEP_EXECUTION_ID)
) ;
@@ -68,7 +79,7 @@ CREATE TABLE PREFIX_JOB_EXECUTION_CONTEXT (
JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
SHORT_CONTEXT VARCHAR(2500) NOT NULL,
SERIALIZED_CONTEXT LONGVARCHAR ,
constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
constraint PREFIX_JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
references PREFIX_JOB_EXECUTION(JOB_EXECUTION_ID)
) ;