From aaa7c03b2bd7f1ee783acf6451939dc0b6108a1b Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 8 Nov 2008 16:10:14 +0000 Subject: [PATCH] Tidy up base classes in batch-test --- ...nSequentialDecisionJobFunctionalTests.java | 11 ++- .../NonSequentialJobFunctionalTests.java | 12 ++- ...lowJobTests.java => AbstractJobTests.java} | 43 ++++----- .../batch/test/AbstractSimpleJobTests.java | 87 ++----------------- .../batch/test/SampleJobTests.java | 2 +- 5 files changed, 42 insertions(+), 113 deletions(-) rename spring-batch-test/src/main/java/org/springframework/batch/test/{AbstractFlowJobTests.java => AbstractJobTests.java} (69%) diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialDecisionJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialDecisionJobFunctionalTests.java index 925ec2470..2cd9a44bc 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialDecisionJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialDecisionJobFunctionalTests.java @@ -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 diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialJobFunctionalTests.java index 2d2ffd649..58c0b2ff2 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/NonSequentialJobFunctionalTests.java @@ -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 diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractFlowJobTests.java b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java similarity index 69% rename from spring-batch-test/src/main/java/org/springframework/batch/test/AbstractFlowJobTests.java rename to spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java index dd48ad6dc..bf8a70f44 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractFlowJobTests.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java @@ -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 parameters = new HashMap(); diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractSimpleJobTests.java b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractSimpleJobTests.java index fb1fae886..6b7daec0e 100755 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractSimpleJobTests.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractSimpleJobTests.java @@ -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 stepMap = new HashMap(); private List stepList = new ArrayList(); - @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 parameters = new HashMap(); - parameters.put("timestamp", new JobParameter(new Date().getTime())); - return new JobParameters(parameters); - } } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SampleJobTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SampleJobTests.java index c9b1e867e..898f68834 100755 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SampleJobTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SampleJobTests.java @@ -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);