From 695cf0817dbdcf316596d0444f200ca1be2d9cee Mon Sep 17 00:00:00 2001 From: robokaso Date: Wed, 13 Feb 2008 16:13:11 +0000 Subject: [PATCH] IN PROGRESS - issue BATCH-340: Refactor JobRepository for greater clarity and consistency. http://jira.springframework.org/browse/BATCH-340 Added getLastStepExecution method to StepExecutionDao interface and liberated JdbcStepInstanceDao from StepExecutionDao. Removed the STEP_INSTANCE.LAST_EXECUTION_ID column from schema. --- .../repository/SimpleJobRepository.java | 17 +-- .../repository/dao/JdbcStepExecutionDao.java | 22 +++ .../repository/dao/JdbcStepInstanceDao.java | 44 +++--- .../execution/repository/dao/MapStepDao.java | 16 +++ .../repository/dao/StepExecutionDao.java | 4 + .../repository/dao/StepInstanceDao.java | 16 +-- .../src/main/resources/schema-db2.sql | 3 +- .../src/main/resources/schema-derby.sql | 3 +- .../src/main/resources/schema-hsqldb.sql | 3 +- .../src/main/resources/schema-mysql.sql | 3 +- .../src/main/resources/schema-oracle10g.sql | 3 +- .../src/main/resources/schema-postgresql.sql | 3 +- .../src/main/sql/init.sql.vpp | 3 +- .../execution/repository/MockStepDao.java | 5 + .../repository/SimpleJobRepositoryTests.java | 136 +++++++++++------- .../repository/dao/AbstractStepDaoTests.java | 15 +- .../dao/JdbcStepDaoPrefixTests.java | 18 +-- .../batch/execution/repository/dao/init.sql | 3 +- .../execution/repository/dao/sql-dao-test.xml | 3 +- .../resources/simple-container-definition.xml | 1 - 20 files changed, 195 insertions(+), 126 deletions(-) diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java index 517ab37f3..4793bc085 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java @@ -57,7 +57,7 @@ import org.springframework.util.Assert; */ public class SimpleJobRepository implements JobRepository { - private JobInstanceDao jobDao; + private JobInstanceDao jobInstanceDao; private JobExecutionDao jobExecutionDao; @@ -75,7 +75,7 @@ public class SimpleJobRepository implements JobRepository { public SimpleJobRepository(JobInstanceDao jobDao, JobExecutionDao jobExecutionDao, StepInstanceDao stepInstanceDao, StepExecutionDao stepExecutionDao) { super(); - this.jobDao = jobDao; + this.jobInstanceDao = jobDao; this.jobExecutionDao = jobExecutionDao; this.stepInstanceDao = stepInstanceDao; this.stepExecutionDao = stepExecutionDao; @@ -165,7 +165,7 @@ public class SimpleJobRepository implements JobRepository { * thread or process will block until this transaction has finished. */ - jobs = jobDao.findJobInstances(job.getName(), jobParameters); + jobs = jobInstanceDao.findJobInstances(job.getName(), jobParameters); } if (jobs.size() == 1) { @@ -247,7 +247,7 @@ public class SimpleJobRepository implements JobRepository { Assert.notNull(job.getId(), "Job cannot be updated if it's ID is null. It must be obtained" + "from SimpleJobRepository.findOrCreateJob to be considered valid."); - jobDao.updateJobInstance(job); + jobInstanceDao.updateJobInstance(job); } /** @@ -288,7 +288,8 @@ public class SimpleJobRepository implements JobRepository { Assert.notNull(step.getId(), "Step cannot be updated if it's ID is null. It must be obtained" + "from SimpleJobRepository.findOrCreateJob to be considered valid."); - stepInstanceDao.updateStepInstance(step); + //TODO no-op to be removed + //stepInstanceDao.updateStepInstance(step); } @@ -299,7 +300,7 @@ public class SimpleJobRepository implements JobRepository { */ private JobInstance createJobInstance(Job job, JobParameters jobParameters) { - JobInstance jobInstance = jobDao.createJobInstance(job.getName(), jobParameters); + JobInstance jobInstance = jobInstanceDao.createJobInstance(job.getName(), jobParameters); jobInstance.setJob(job); jobInstance.setStepInstances(createStepInstances(jobInstance, job.getSteps())); return jobInstance; @@ -321,7 +322,7 @@ public class SimpleJobRepository implements JobRepository { return stepInstances; } - /* + /** * Find Steps for the given list of Steps with a given JobId */ protected List findStepInstances(List steps, JobInstance jobInstance) { @@ -332,7 +333,7 @@ public class SimpleJobRepository implements JobRepository { Step stepConfiguration = (Step) i.next(); StepInstance stepInstance = stepInstanceDao.findStepInstance(jobInstance, stepConfiguration.getName()); if (stepInstance != null) { - + stepInstance.setLastExecution(stepExecutionDao.getLastStepExecution(stepInstance)); if (stepInstance.getLastExecution() != null) { ExecutionAttributes executionAttributes = stepExecutionDao.findExecutionAttributes(stepInstance .getLastExecution().getId()); diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java index 1f3890665..e81c4f961 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java @@ -84,6 +84,11 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao private static final String UPDATE_STEP_EXECUTION = "UPDATE %PREFIX%STEP_EXECUTION set START_TIME = ?, END_TIME = ?, " + "STATUS = ?, COMMIT_COUNT = ?, TASK_COUNT = ?, TASK_STATISTICS = ?, CONTINUABLE = ? , EXIT_CODE = ?, " + "EXIT_MESSAGE = ?, VERSION = ? where STEP_EXECUTION_ID = ? and VERSION = ?"; + + private static final String FIND_LAST_STEP_EXECUTION = "SELECT STEP_EXECUTION_ID, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT," + + " TASK_COUNT, TASK_STATISTICS, CONTINUABLE, EXIT_CODE, EXIT_MESSAGE from %PREFIX%STEP_EXECUTION where STEP_INSTANCE_ID = ?" + + " and START_TIME = (SELECT max(START_TIME) FROM %PREFIX%STEP_EXECUTION where STEP_INSTANCE_ID = ?)"; + private static final int EXIT_MESSAGE_LENGTH = 250; @@ -169,6 +174,21 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao return stepExecution; } + + public StepExecution getLastStepExecution(StepInstance stepInstance) { + Long stepInstanceId = stepInstance.getId(); + List executions = getJdbcTemplate().query(getQuery(FIND_LAST_STEP_EXECUTION), + new Object[] { stepInstanceId, stepInstanceId }, new StepExecutionRowMapper(stepInstance)); + + Assert.state(executions.size() <= 1, "There must be at most one latest execution"); + + if (executions.size() == 0) { + return null; + } + else { + return (StepExecution) executions.get(0); + } + } public int getStepExecutionCount(StepInstance step) { @@ -500,4 +520,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao return null; } } + + } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepInstanceDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepInstanceDao.java index c4f5f5e9f..469580da7 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepInstanceDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepInstanceDao.java @@ -5,7 +5,6 @@ import java.sql.SQLException; import java.util.List; import org.springframework.batch.core.domain.JobInstance; -import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.domain.StepInstance; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.IncorrectResultSizeDataAccessException; @@ -36,17 +35,13 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements private static final String CREATE_STEP = "INSERT into %PREFIX%STEP_INSTANCE(STEP_INSTANCE_ID, JOB_INSTANCE_ID, STEP_NAME) values (?, ?, ?)"; - private static final String FIND_STEP = "SELECT STEP_INSTANCE_ID, LAST_STEP_EXECUTION_ID from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ? " + private static final String FIND_STEP = "SELECT STEP_INSTANCE_ID from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ? " + "and STEP_NAME = ?"; - private static final String FIND_STEPS = "SELECT STEP_INSTANCE_ID, LAST_STEP_EXECUTION_ID, STEP_NAME from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ?"; - - private static final String UPDATE_STEP = "UPDATE %PREFIX%STEP_INSTANCE set LAST_STEP_EXECUTION_ID = ? where STEP_INSTANCE_ID = ?"; + private static final String FIND_STEPS = "SELECT STEP_INSTANCE_ID, STEP_NAME from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ?"; private DataFieldMaxValueIncrementer stepIncrementer; - private StepExecutionDao stepExecutionDao; - /** * Create a step with the given job's id, and the provided step name. A * unique id is created for the step using an incrementer. (@link @@ -129,19 +124,19 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements return getJdbcTemplate().query(getQuery(FIND_STEPS), parameters, rowMapper); } - /** - * @see StepDao#updateStepInstance(StepInstance) - * @throws IllegalArgumentException if step, or it's status and id is null. - */ - public void updateStepInstance(final StepInstance step) { - - Assert.notNull(step, "Step cannot be null."); - Assert.notNull(step.getId(), "Step Id cannot be null."); - - Object[] parameters = new Object[] { step.getLastExecution().getId(), step.getId() }; - - getJdbcTemplate().update(getQuery(UPDATE_STEP), parameters); - } +// /** +// * @see StepDao#updateStepInstance(StepInstance) +// * @throws IllegalArgumentException if step, or it's status and id is null. +// */ +// public void updateStepInstance(final StepInstance step) { +// +// Assert.notNull(step, "Step cannot be null."); +// Assert.notNull(step.getId(), "Step Id cannot be null."); +// +// Object[] parameters = new Object[] { step.getLastExecution().getId(), step.getId() }; +// +// getJdbcTemplate().update(getQuery(UPDATE_STEP), parameters); +// } public void setStepIncrementer(DataFieldMaxValueIncrementer stepIncrementer) { this.stepIncrementer = stepIncrementer; @@ -161,23 +156,16 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements public Object mapRow(ResultSet rs, int rowNum) throws SQLException { if (stepName == null) { - stepName = rs.getString(3); + stepName = rs.getString(2); } StepInstance stepInstance = new StepInstance(jobInstance, stepName, new Long(rs.getLong(1))); - StepExecution lastExecution = stepExecutionDao.getStepExecution(new Long(rs.getLong(2)), stepInstance); - stepInstance.setLastExecution(lastExecution); return stepInstance; } } - public void setStepExecutionDao(StepExecutionDao stepExecutionDao) { - this.stepExecutionDao = stepExecutionDao; - } - public void afterPropertiesSet() throws Exception { Assert.notNull(stepIncrementer, "StepIncrementer cannot be null."); - Assert.notNull(stepExecutionDao, "StepExecutionDao cannot be null."); } } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java index cc170cb8e..89c135a25 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java @@ -161,5 +161,21 @@ public class MapStepDao implements StepDao { public void updateExecutionAttributes(Long executionId, ExecutionAttributes executionAttributes) { } + + public StepExecution getLastStepExecution(StepInstance stepInstance) { + List executions = findStepExecutions(stepInstance); + StepExecution lastExec = null; + for (Iterator iterator = executions.iterator(); iterator.hasNext();) { + StepExecution exec = (StepExecution) iterator.next(); + if (lastExec == null) { + lastExec = exec; + continue; + } + if (lastExec.getStartTime().getTime() < exec.getStartTime().getTime()) { + lastExec = exec; + } + } + return lastExec; + } } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java index 56629b5fa..9f1dd5b53 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java @@ -83,4 +83,8 @@ public interface StepExecutionDao { */ void updateExecutionAttributes(final Long executionId, ExecutionAttributes executionAttributes); + /** + * @return the last execution of the given instance + */ + StepExecution getLastStepExecution(StepInstance stepInstance); } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepInstanceDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepInstanceDao.java index cde87b7bc..0a575d07b 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepInstanceDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepInstanceDao.java @@ -35,12 +35,12 @@ public interface StepInstanceDao { */ StepInstance createStepInstance(JobInstance jobInstance, String stepName); - /** - * Update an existing StepInstance. - * - * Preconditions: StepInstance must have an ID. - * - * @param job - */ - void updateStepInstance(StepInstance stepInstance); +// /** +// * Update an existing StepInstance. +// * +// * Preconditions: StepInstance must have an ID. +// * +// * @param job +// */ +// void updateStepInstance(StepInstance stepInstance); } diff --git a/spring-batch-execution/src/main/resources/schema-db2.sql b/spring-batch-execution/src/main/resources/schema-db2.sql index 931c3fe81..37665bd19 100644 --- a/spring-batch-execution/src/main/resources/schema-db2.sql +++ b/spring-batch-execution/src/main/resources/schema-db2.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT PRIMARY KEY , diff --git a/spring-batch-execution/src/main/resources/schema-derby.sql b/spring-batch-execution/src/main/resources/schema-derby.sql index bccb55fb5..df37702c5 100644 --- a/spring-batch-execution/src/main/resources/schema-derby.sql +++ b/spring-batch-execution/src/main/resources/schema-derby.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, diff --git a/spring-batch-execution/src/main/resources/schema-hsqldb.sql b/spring-batch-execution/src/main/resources/schema-hsqldb.sql index aa4097caf..6ef6ca2e7 100644 --- a/spring-batch-execution/src/main/resources/schema-hsqldb.sql +++ b/spring-batch-execution/src/main/resources/schema-hsqldb.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT IDENTITY PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY , diff --git a/spring-batch-execution/src/main/resources/schema-mysql.sql b/spring-batch-execution/src/main/resources/schema-mysql.sql index c1f96ef69..79cd03e37 100644 --- a/spring-batch-execution/src/main/resources/schema-mysql.sql +++ b/spring-batch-execution/src/main/resources/schema-mysql.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT unsigned PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT unsigned PRIMARY KEY , diff --git a/spring-batch-execution/src/main/resources/schema-oracle10g.sql b/spring-batch-execution/src/main/resources/schema-oracle10g.sql index f54450b7f..88f5ce1ed 100644 --- a/spring-batch-execution/src/main/resources/schema-oracle10g.sql +++ b/spring-batch-execution/src/main/resources/schema-oracle10g.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID NUMBER(38) PRIMARY KEY , VERSION NUMBER(38), JOB_INSTANCE_ID NUMBER(38) NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID NUMBER(38) PRIMARY KEY , diff --git a/spring-batch-execution/src/main/resources/schema-postgresql.sql b/spring-batch-execution/src/main/resources/schema-postgresql.sql index 931c3fe81..37665bd19 100644 --- a/spring-batch-execution/src/main/resources/schema-postgresql.sql +++ b/spring-batch-execution/src/main/resources/schema-postgresql.sql @@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT PRIMARY KEY , diff --git a/spring-batch-execution/src/main/sql/init.sql.vpp b/spring-batch-execution/src/main/sql/init.sql.vpp index f66c7890f..aafb9e134 100644 --- a/spring-batch-execution/src/main/sql/init.sql.vpp +++ b/spring-batch-execution/src/main/sql/init.sql.vpp @@ -29,8 +29,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED}, VERSION ${BIGINT}, JOB_INSTANCE_ID ${BIGINT} NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED}, diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java index a30a02ba0..0d7be4195 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java @@ -89,4 +89,9 @@ public class MockStepDao implements StepDao { return null; } + public StepExecution getLastStepExecution(StepInstance stepInstance) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java index a5cee4b56..c5b73dafc 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java @@ -40,12 +40,12 @@ import org.springframework.batch.execution.repository.dao.StepDao; import org.springframework.batch.item.ExecutionAttributes; /** - * Test SimpleJobRepository. The majority of test cases are tested using EasyMock, - * however, there were some issues with using it for the stepDao when testing finding - * or creating steps, so an actual mock class had to be written. - * + * Test SimpleJobRepository. The majority of test cases are tested using + * EasyMock, however, there were some issues with using it for the stepDao when + * testing finding or creating steps, so an actual mock class had to be written. + * * @author Lucas Ward - * + * */ public class SimpleJobRepositoryTests extends TestCase { @@ -76,7 +76,7 @@ public class SimpleJobRepositoryTests extends TestCase { StepInstance databaseStep2; List steps; - + ExecutionAttributes executionAttributes; private JobExecution jobExecution; @@ -86,10 +86,9 @@ public class SimpleJobRepositoryTests extends TestCase { jobDao = (JobDao) jobDaoControl.getMock(); stepDao = (StepDao) stepDaoControl.getMock(); - jobRepository = new SimpleJobRepository(jobDao, jobDao, stepDao, stepDao); + jobRepository = new SimpleJobRepository(jobDao, jobDao, stepDao, stepDao); jobParameters = new JobParametersBuilder().toJobParameters(); - jobConfiguration = new JobSupport(); jobConfiguration.setBeanName("RepositoryTest"); @@ -104,7 +103,7 @@ public class SimpleJobRepositoryTests extends TestCase { stepConfigurations.add(stepConfiguration2); jobConfiguration.setSteps(stepConfigurations); - + databaseJob = new JobInstance(new Long(1), jobParameters) { public JobExecution createJobExecution() { jobExecution = super.createJobExecution(); @@ -120,7 +119,7 @@ public class SimpleJobRepositoryTests extends TestCase { steps = new ArrayList(); steps.add(databaseStep1); steps.add(databaseStep2); - + executionAttributes = new ExecutionAttributes(); } @@ -140,12 +139,13 @@ public class SimpleJobRepositoryTests extends TestCase { stepDao.createStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); jobDao.saveJobExecution(new JobExecution(databaseJob)); - jobDaoControl.setMatcher(new ArgumentsMatcher(){ + jobDaoControl.setMatcher(new ArgumentsMatcher() { public boolean matches(Object[] expected, Object[] actual) { return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob); } + public String toString(Object[] arguments) { - return ""+arguments[0]; + return "" + arguments[0]; } }); stepDaoControl.replay(); @@ -160,20 +160,35 @@ public class SimpleJobRepositoryTests extends TestCase { assertTrue(step.equals(databaseStep2)); } - public void testRestartedJob() throws Exception{ + public void testRestartedJob() throws Exception { + + final List executions = new ArrayList(); + JobExecution execution = databaseJob.createJobExecution(); + executions.add(execution); + // For this test it is important that the execution is finished + // and the executions in the list contain one with an end date + execution.setEndTime(new Date(System.currentTimeMillis())); + + StepExecution databaseStep1Exec = new StepExecution(databaseStep1, execution, new Long(1)); + StepExecution databaseStep2Exec = new StepExecution(databaseStep2, execution, new Long(2)); + List jobs = new ArrayList(); jobDao.findJobInstances(jobConfiguration.getName(), jobParameters); jobs.add(databaseJob); jobDaoControl.setReturnValue(jobs); stepDao.findStepInstance(databaseJob, "TestStep1"); stepDaoControl.setReturnValue(databaseStep1); - stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep1); + stepDaoControl.setReturnValue(databaseStep1Exec); + stepDao.findExecutionAttributes(databaseStep1Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep1); stepDaoControl.setReturnValue(1); stepDao.findStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); - stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep2); + stepDaoControl.setReturnValue(databaseStep2Exec); + stepDao.findExecutionAttributes(databaseStep2Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep2); stepDaoControl.setReturnValue(1); @@ -181,22 +196,17 @@ public class SimpleJobRepositoryTests extends TestCase { jobDao.getJobExecutionCount(databaseJob.getId()); jobDaoControl.setReturnValue(1); jobDao.findJobExecutions(databaseJob); - final List executions = new ArrayList(); - JobExecution execution =databaseJob.createJobExecution(); - executions.add(execution); - // For this test it is important that the execution is finished - // and the executions in the list contain one with an end date - execution.setEndTime(new Date(System.currentTimeMillis())); jobDaoControl.setReturnValue(executions); jobDao.updateJobInstance(databaseJob); jobDao.saveJobExecution(new JobExecution(databaseJob)); - jobDaoControl.setMatcher(new ArgumentsMatcher(){ + jobDaoControl.setMatcher(new ArgumentsMatcher() { public boolean matches(Object[] expected, Object[] actual) { JobExecution execution = (JobExecution) actual[0]; return execution.getJobInstance().equals(databaseJob); } + public String toString(Object[] arguments) { - return ""+arguments[0]; + return "" + arguments[0]; } }); jobDaoControl.setVoidCallable(); @@ -213,8 +223,9 @@ public class SimpleJobRepositoryTests extends TestCase { assertTrue(step.getStepExecutionCount() == 1); } - //Test that a restartable job that has multiple instances throws an exception. - public void testFindRestartableJobWithMultipleInstances() throws Exception{ + // Test that a restartable job that has multiple instances throws an + // exception. + public void testFindRestartableJobWithMultipleInstances() throws Exception { List jobs = new ArrayList(); jobs.add(databaseJob); @@ -223,54 +234,64 @@ public class SimpleJobRepositoryTests extends TestCase { jobDaoControl.setReturnValue(jobs); jobDaoControl.replay(); - try{ + try { jobRepository.createJobExecution(jobConfiguration, jobParameters); fail("Expected BatchRestartException"); - }catch(BatchRestartException e){ - //expected + } + catch (BatchRestartException e) { + // expected } jobDaoControl.verify(); } - public void testRestartJobStartLimitExceeded() throws Exception{ + public void testRestartJobStartLimitExceeded() throws Exception { jobConfiguration.setStartLimit(1); + StepExecution databaseStep1Exec = new StepExecution(databaseStep1, null, new Long(1)); + StepExecution databaseStep2Exec = new StepExecution(databaseStep2, null, new Long(2)); + List jobs = new ArrayList(); jobDao.findJobInstances(jobConfiguration.getName(), jobParameters); jobs.add(databaseJob); jobDaoControl.setReturnValue(jobs); stepDao.findStepInstance(databaseJob, "TestStep1"); stepDaoControl.setReturnValue(databaseStep1); - stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep1); + stepDaoControl.setReturnValue(databaseStep1Exec); + stepDao.findExecutionAttributes(databaseStep1Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep1); stepDaoControl.setReturnValue(1); stepDao.findStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); - stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep2); + stepDaoControl.setReturnValue(databaseStep2Exec); + stepDao.findExecutionAttributes(databaseStep2Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep2); stepDaoControl.setReturnValue(1); stepDaoControl.replay(); jobDao.getJobExecutionCount(databaseJob.getId()); - //return a greater execution count then the start limit, should throw exception + // return a greater execution count then the start limit, should throw + // exception jobDaoControl.setReturnValue(2); jobDaoControl.replay(); - try{ + try { jobRepository.createJobExecution(jobConfiguration, jobParameters); fail(); - }catch(BatchRestartException ex){ - //expected + } + catch (BatchRestartException ex) { + // expected } jobDaoControl.verify(); stepDaoControl.verify(); } - public void testCreateNonRestartableJob() throws Exception{ + public void testCreateNonRestartableJob() throws Exception { List jobs = new ArrayList(); jobConfiguration.setRestartable(false); @@ -284,12 +305,13 @@ public class SimpleJobRepositoryTests extends TestCase { stepDao.createStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); jobDao.saveJobExecution(new JobExecution(databaseJob)); - jobDaoControl.setMatcher(new ArgumentsMatcher(){ + jobDaoControl.setMatcher(new ArgumentsMatcher() { public boolean matches(Object[] expected, Object[] actual) { return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob); } + public String toString(Object[] arguments) { - return ""+arguments[0]; + return "" + arguments[0]; } }); stepDaoControl.replay(); @@ -307,7 +329,7 @@ public class SimpleJobRepositoryTests extends TestCase { public void testUpdateJob() { // failure scenario - no ID - JobInstance updateJob; + JobInstance updateJob; try { updateJob = new JobInstance(null, jobParameters); jobRepository.update(updateJob); @@ -370,12 +392,12 @@ public class SimpleJobRepositoryTests extends TestCase { // successful update step = new StepInstance(new Long(0L)); - stepDao.updateStepInstance(step); + // stepDao.updateStepInstance(step); stepDaoControl.replay(); jobRepository.update(step); } - public void testUpdateStepExecution(){ + public void testUpdateStepExecution() { StepExecution stepExecution = new StepExecution(new StepInstance(new Long(10L)), null, new Long(1)); stepExecution.setId(new Long(11)); ExecutionAttributes executionAttributes = new ExecutionAttributes(); @@ -387,7 +409,7 @@ public class SimpleJobRepositoryTests extends TestCase { stepDaoControl.verify(); } - public void testSaveExistingStepExecution(){ + public void testSaveExistingStepExecution() { StepExecution stepExecution = new StepExecution(new StepInstance(new Long(10L)), null, null); ExecutionAttributes executionAttributes = new ExecutionAttributes(); stepExecution.setExecutionAttributes(executionAttributes); @@ -413,10 +435,10 @@ public class SimpleJobRepositoryTests extends TestCase { } /* - * Test to ensure that if a StepDao returns invalid - * restart data, it is corrected. + * Test to ensure that if a StepDao returns invalid restart data, it is + * corrected. */ - public void testCreateStepsFixesInvalidExecutionAttributes() throws Exception{ + public void testCreateStepsFixesInvalidExecutionAttributes() throws Exception { List jobs = new ArrayList(); @@ -429,12 +451,13 @@ public class SimpleJobRepositoryTests extends TestCase { stepDao.createStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); jobDao.saveJobExecution(new JobExecution(databaseJob)); - jobDaoControl.setMatcher(new ArgumentsMatcher(){ + jobDaoControl.setMatcher(new ArgumentsMatcher() { public boolean matches(Object[] expected, Object[] actual) { return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob); } + public String toString(Object[] arguments) { - return ""+arguments[0]; + return "" + arguments[0]; } }); stepDaoControl.replay(); @@ -448,20 +471,28 @@ public class SimpleJobRepositoryTests extends TestCase { assertTrue(step.equals(databaseStep2)); } - public void testFindStepsFixesInvalidExecutionAttributes() throws Exception{ + public void testFindStepsFixesInvalidExecutionAttributes() throws Exception { + + StepExecution databaseStep1Exec = new StepExecution(databaseStep1, null, new Long(1)); + StepExecution databaseStep2Exec = new StepExecution(databaseStep2, null, new Long(2)); + List jobs = new ArrayList(); jobDao.findJobInstances(jobConfiguration.getName(), jobParameters); jobs.add(databaseJob); jobDaoControl.setReturnValue(jobs); stepDao.findStepInstance(databaseJob, "TestStep1"); stepDaoControl.setReturnValue(databaseStep1); - stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep1); + stepDaoControl.setReturnValue(databaseStep1Exec); + stepDao.findExecutionAttributes(databaseStep1Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep1); stepDaoControl.setReturnValue(1); stepDao.findStepInstance(databaseJob, "TestStep2"); stepDaoControl.setReturnValue(databaseStep2); - stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId()); + stepDao.getLastStepExecution(databaseStep2); + stepDaoControl.setReturnValue(databaseStep2Exec); + stepDao.findExecutionAttributes(databaseStep2Exec.getId()); stepDaoControl.setReturnValue(executionAttributes); stepDao.getStepExecutionCount(databaseStep2); stepDaoControl.setReturnValue(1); @@ -473,12 +504,13 @@ public class SimpleJobRepositoryTests extends TestCase { jobDao.updateJobInstance(databaseJob); jobDaoControl.setVoidCallable(); jobDao.saveJobExecution(new JobExecution(databaseJob)); - jobDaoControl.setMatcher(new ArgumentsMatcher(){ + jobDaoControl.setMatcher(new ArgumentsMatcher() { public boolean matches(Object[] expected, Object[] actual) { return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob); } + public String toString(Object[] arguments) { - return ""+arguments[0]; + return "" + arguments[0]; } }); jobDaoControl.replay(); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java index cfa43d98e..1fbcb4177 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java @@ -100,7 +100,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour stepExecution.setStartTime(new Date(System.currentTimeMillis())); stepExecutionDao.saveStepExecution(stepExecution); step1.setLastExecution(stepExecution); - stepInstanceDao.updateStepInstance(step1); + //stepInstanceDao.updateStepInstance(step1); executionAttributes = new ExecutionAttributes(); executionAttributes.putString("1", "testString1"); @@ -158,7 +158,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour public void testUpdateStepWithoutExecutionAttributes() { - stepInstanceDao.updateStepInstance(step1); + //stepInstanceDao.updateStepInstance(step1); StepInstance tempStep = stepInstanceDao.findStepInstance(jobInstance, step1.getName()); assertEquals(tempStep, step1); } @@ -265,5 +265,16 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour attributes = stepExecutionDao.findExecutionAttributes(stepExecution.getId()); assertEquals(executionAttributes, attributes); } + + public void testGetLastStepExecution() { + StepExecution lastExecution = new StepExecution(step1, jobExecution, null); + lastExecution.setStatus(BatchStatus.STARTED); + + int JUMP_INTO_FUTURE = 1000; // makes sure start time is 'greatest' + lastExecution.setStartTime(new Date(System.currentTimeMillis() + JUMP_INTO_FUTURE)); + stepExecutionDao.saveStepExecution(lastExecution); + + assertEquals(lastExecution, stepExecutionDao.getLastStepExecution(step1)); + } } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcStepDaoPrefixTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcStepDaoPrefixTests.java index 83b8a33bc..73c9daa11 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcStepDaoPrefixTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcStepDaoPrefixTests.java @@ -83,11 +83,11 @@ public class JdbcStepDaoPrefixTests extends TestCase { assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP_EXECUTION") != -1); } - public void testModifiedUpdateStep(){ - stepInstanceDao.setTablePrefix("FOO_"); - stepInstanceDao.updateStepInstance(step); - assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP") != -1); - } +// public void testModifiedUpdateStep(){ +// stepInstanceDao.setTablePrefix("FOO_"); +// stepInstanceDao.updateStepInstance(step); +// assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP") != -1); +// } public void testModifiedCreateStep(){ stepInstanceDao.setTablePrefix("FOO_"); @@ -143,10 +143,10 @@ public class JdbcStepDaoPrefixTests extends TestCase { assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1); } - public void testDefaultUpdateStep(){ - stepInstanceDao.updateStepInstance(step); - assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1); - } +// public void testDefaultUpdateStep(){ +// stepInstanceDao.updateStepInstance(step); +// assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1); +// } public void testDefaultFindStepExecutions(){ stepExecutionDao.findStepExecutions(step); diff --git a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql index ab03580d8..ae0d34d88 100644 --- a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql +++ b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/init.sql @@ -29,8 +29,7 @@ CREATE TABLE BATCH_STEP_INSTANCE ( STEP_INSTANCE_ID BIGINT IDENTITY PRIMARY KEY , VERSION BIGINT, JOB_INSTANCE_ID BIGINT NOT NULL, - STEP_NAME VARCHAR(100) NOT NULL, - LAST_STEP_EXECUTION_ID BIGINT); + STEP_NAME VARCHAR(100) NOT NULL); CREATE TABLE BATCH_STEP_EXECUTION ( STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY , diff --git a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/sql-dao-test.xml b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/sql-dao-test.xml index 2ac4dd0b2..cb41e9dbc 100644 --- a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/sql-dao-test.xml +++ b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/repository/dao/sql-dao-test.xml @@ -18,8 +18,7 @@ - - + diff --git a/spring-batch-samples/src/main/resources/simple-container-definition.xml b/spring-batch-samples/src/main/resources/simple-container-definition.xml index 062004d2c..f49668815 100644 --- a/spring-batch-samples/src/main/resources/simple-container-definition.xml +++ b/spring-batch-samples/src/main/resources/simple-container-definition.xml @@ -68,7 +68,6 @@ class="org.springframework.batch.execution.repository.dao.JdbcStepInstanceDao"> -