Fix for BATCH-146:Unclear error message and possible inappropriate exception type in SimpleJobRepository. Changed the exception type to BatchRestartException, and added additional unit tests.

This commit is contained in:
lucasward
2007-09-28 16:03:02 +00:00
parent 09862eaa97
commit 735603c59e
2 changed files with 58 additions and 1 deletions

View File

@@ -85,6 +85,8 @@ public class SimpleJobRepository implements JobRepository {
* </p>
*
* @see JobRepository#findOrCreateJob(JobConfiguration, JobIdentifier)
* @throws BatchRestartException if more than one JobInstance if found
* or if JobInstance.getJobExecutionCount() is greater than JobConfiguration.getStartLimit()
*/
public JobInstance findOrCreateJob(JobConfiguration jobConfiguration, JobIdentifier runtimeInformation) {
@@ -115,7 +117,7 @@ public class SimpleJobRepository implements JobRepository {
}
else {
// More than one job found, throw exception
throw new NoSuchBatchDomainObjectException("Error obtaining" + "previous job run: "
throw new BatchRestartException("Error restarting job, more than one JobInstance found for: "
+ jobConfiguration.toString());
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInstance;
import org.springframework.batch.core.repository.BatchRestartException;
import org.springframework.batch.core.runtime.SimpleJobIdentifier;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.execution.repository.dao.JobDao;
@@ -162,9 +163,63 @@ public class SimpleJobRepositoryTests extends TestCase {
assertTrue(step.getStepExecutionCount() == 1);
}
//Test that a restartable job that has multiple instances throws an exception.
public void testFindRestartableJobWithMultipleInstances(){
List jobs = new ArrayList();
jobs.add(databaseJob);
jobs.add(new JobInstance());
jobDao.findJobs(jobRuntimeInformation);
jobDaoControl.setReturnValue(jobs);
jobDaoControl.replay();
try{
jobRepository.findOrCreateJob(jobConfiguration, jobRuntimeInformation);
fail();
}catch(BatchRestartException ex){
//expected
}
jobDaoControl.verify();
}
public void testRestartJobStartLimitExceeded(){
jobConfiguration.setStartLimit(1);
List jobs = new ArrayList();
jobDao.findJobs(jobRuntimeInformation);
jobs.add(databaseJob);
jobDaoControl.setReturnValue(jobs);
stepDao.findStep(databaseJob, "TestStep1");
stepDaoControl.setReturnValue(databaseStep1);
stepDao.getStepExecutionCount(databaseStep1.getId());
stepDaoControl.setReturnValue(1);
stepDao.findStep(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
stepDao.getStepExecutionCount(databaseStep2.getId());
stepDaoControl.setReturnValue(1);
stepDaoControl.replay();
jobDao.getJobExecutionCount(databaseJob.getId());
//return a greater execution count then the start limit, should throw exception
jobDaoControl.setReturnValue(2);
jobDaoControl.replay();
try{
jobRepository.findOrCreateJob(jobConfiguration, jobRuntimeInformation);
fail();
}catch(BatchRestartException ex){
//expected
}
jobDaoControl.verify();
stepDaoControl.verify();
}
public void testCreateNonRestartableJob(){
List jobs = new ArrayList();
jobConfiguration.setRestartable(false);
jobDao.findJobs(jobRuntimeInformation);
jobDaoControl.setReturnValue(jobs);