Tidy up base classes in batch-test

This commit is contained in:
dsyer
2008-11-08 16:10:14 +00:00
parent 5746df5b42
commit aaa7c03b2b
5 changed files with 42 additions and 113 deletions

View File

@@ -7,15 +7,16 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.test.AbstractFlowJobTests;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/nonSequentialDecisionJob.xml" })
public class NonSequentialDecisionJobFunctionalTests extends AbstractFlowJobTests {
public class NonSequentialDecisionJobFunctionalTests extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@@ -23,11 +24,13 @@ public class NonSequentialDecisionJobFunctionalTests extends AbstractFlowJobTest
public void testWithSkips() throws Exception {
simpleJdbcTemplate.update("DELETE from ERROR_LOG");
simpleJdbcTemplate.update("DELETE from PLAYER_SUMMARY");
simpleJdbcTemplate.update("DELETE from PLAYERS");
simpleJdbcTemplate.update("DELETE from GAMES");
assertEquals(BatchStatus.COMPLETED, this.launchJob().getStatus());
assertEquals(1, simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from ERROR_LOG"));
assertEquals(9, simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY"));
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(9, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYER_SUMMARY"));
}
@Autowired

View File

@@ -7,27 +7,31 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.test.AbstractFlowJobTests;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/nonSequentialJob.xml" })
public class NonSequentialJobFunctionalTests extends AbstractFlowJobTests {
public class NonSequentialJobFunctionalTests extends AbstractJobTests {
private SimpleJdbcTemplate simpleJdbcTemplate;
@Test
public void testWithSkips() throws Exception {
simpleJdbcTemplate.update("DELETE from ERROR_LOG");
simpleJdbcTemplate.update("DELETE from PLAYER_SUMMARY");
simpleJdbcTemplate.update("DELETE from PLAYERS");
simpleJdbcTemplate.update("DELETE from GAMES");
assertEquals(BatchStatus.COMPLETED, this.launchJob().getStatus());
assertEquals(1, simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from ERROR_LOG"));
assertEquals(9, simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY"));
assertEquals(1, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG"));
assertEquals(9, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "PLAYER_SUMMARY"));
}
@Autowired

View File

@@ -6,15 +6,12 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.job.flow.FlowJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
/**
@@ -34,25 +31,25 @@ import org.springframework.beans.factory.annotation.Autowired;
* @author Dan Garrette
* @since 2.0
*/
public abstract class AbstractFlowJobTests {
public abstract class AbstractJobTests {
/** Logger */
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private JobLauncher launcher;
private FlowJob job;
@Autowired
public void setLauncher(JobLauncher bootstrap) {
this.launcher = bootstrap;
}
private Job job;
@Autowired
public void setJob(FlowJob job) {
this.job = job;
private JobRepository jobRepository;
public JobRepository getJobRepository() {
return jobRepository;
}
public FlowJob getJob() {
public Job getJob() {
return job;
}
@@ -61,7 +58,7 @@ public abstract class AbstractFlowJobTests {
*
* @return the launcher
*/
protected JobLauncher getLauncher() {
protected JobLauncher getJobLauncher() {
return launcher;
}
@@ -69,8 +66,9 @@ public abstract class AbstractFlowJobTests {
* Launch the entire job, including all steps, in order.
*
* @return JobExecution, so that the test may validate the exit status
* @throws Exception
*/
public JobExecution launchJob() {
public JobExecution launchJob() throws Exception {
return this.launchJob(this.makeUniqueJobParameters());
}
@@ -79,22 +77,15 @@ public abstract class AbstractFlowJobTests {
*
* @param jobParameters
* @return JobExecution, so that the test may validate the exit status
* @throws Exception
*/
public JobExecution launchJob(JobParameters jobParameters) {
try {
return getLauncher().run(this.job, jobParameters);
} catch (JobExecutionAlreadyRunningException e) {
throw new RuntimeException(e);
} catch (JobRestartException e) {
throw new RuntimeException(e);
} catch (JobInstanceAlreadyCompleteException e) {
throw new RuntimeException(e);
}
public JobExecution launchJob(JobParameters jobParameters) throws Exception {
return getJobLauncher().run(this.job, jobParameters);
}
/**
* @return a new JobParameters object containing only a parameter for the
* current timestamp, to ensure that the job instance will be unique
* current timestamp, to ensure that the job instance will be unique
*/
private JobParameters makeUniqueJobParameters() {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();

View File

@@ -1,24 +1,17 @@
package org.springframework.batch.test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Base class for testing batch jobs using the SimpleJob implementation.
@@ -36,57 +29,30 @@ import org.springframework.beans.factory.annotation.Autowired;
* @author Dan Garrette
* @since 2.0
*/
public abstract class AbstractSimpleJobTests {
public abstract class AbstractSimpleJobTests extends AbstractJobTests {
/** Logger */
protected final Log logger = LogFactory.getLog(getClass());
private JobLauncher launcher;
private JobRepository jobRepository;
private SimpleJob job;
private StepRunner stepRunner;
private Map<String, Step> stepMap = new HashMap<String, Step>();
private List<Step> stepList = new ArrayList<Step>();
@Autowired
public void setLauncher(JobLauncher bootstrap) {
this.launcher = bootstrap;
}
@Autowired
public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
@Autowired
public void setJob(SimpleJob job) {
this.job = job;
for (Step step : job.getSteps()) {
@Before
public void setUpSteps() {
for (Step step : (getSimpleJob()).getSteps()) {
stepMap.put(step.getName(), step);
stepList.add(step);
}
}
public StepRunner getStepRunner() {
protected StepRunner getStepRunner() {
if(stepRunner == null){
stepRunner = new StepRunner(launcher, jobRepository);
stepRunner = new StepRunner(getJobLauncher(), getJobRepository());
}
return stepRunner;
}
public SimpleJob getJob() {
return job;
}
/**
* Public getter for the launcher.
*
* @return the launcher
*/
protected JobLauncher getLauncher() {
return launcher;
public SimpleJob getSimpleJob() {
return (SimpleJob)getJob();
}
public Step getStep(String stepName){
@@ -96,32 +62,6 @@ public abstract class AbstractSimpleJobTests {
}
return stepMap.get(stepName);
}
/**
* Launch the entire job, including all steps, in order.
*
* @return JobExecution, so that the test may validate the exit status
*/
public JobExecution launchJob() {
return this.launchJob(this.makeUniqueJobParameters());
}
/**
* Launch the entire job, including all steps, in order.
*
* @param jobParameters
* @return JobExecution, so that the test may validate the exit status
*/
public JobExecution launchJob(JobParameters jobParameters) {
try {
return getLauncher().run(job, jobParameters);
} catch (JobExecutionAlreadyRunningException e) {
throw new RuntimeException(e);
} catch (JobRestartException e) {
throw new RuntimeException(e);
} catch (JobInstanceAlreadyCompleteException e) {
throw new RuntimeException(e);
}
}
/**
* Launch just the specified step in the job.
@@ -142,13 +82,4 @@ public abstract class AbstractSimpleJobTests {
return getStepRunner().launchStep(getStep(stepName), jobParameters);
}
/**
* @return a new JobParameters object containing only a parameter for the
* current timestamp, to ensure that the job instance will be unique
*/
private JobParameters makeUniqueJobParameters() {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("timestamp", new JobParameter(new Date().getTime()));
return new JobParameters(parameters);
}
}

View File

@@ -44,7 +44,7 @@ public class SampleJobTests extends AbstractSimpleJobTests {
}
@Test
public void testJob() {
public void testJob() throws Exception {
assertEquals(BatchStatus.COMPLETED,this.launchJob().getStatus());
this.verifyTasklet(1);
this.verifyTasklet(2);