added assert to JobInstanceDao#createJobInstance(Job, JobParameters) that JobInstance does not already exist
This commit is contained in:
@@ -57,6 +57,8 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
|
||||
Assert.hasLength(job.getName(), "Job must have a name");
|
||||
Assert.notNull(jobParameters, "JobParameters must not be null.");
|
||||
|
||||
Assert.state(getJobInstance(job, jobParameters) == null, "JobInstance must not already exist");
|
||||
|
||||
Long jobId = new Long(jobIncrementer.nextLongValue());
|
||||
Object[] parameters = new Object[] { jobId, job.getName(), createJobKey(jobParameters) };
|
||||
getJdbcTemplate().update(getQuery(CREATE_JOB_INSTANCE), parameters,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.springframework.batch.execution.repository.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.domain.Job;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
@@ -18,7 +16,9 @@ public interface JobInstanceDao {
|
||||
/**
|
||||
* Create a JobInstance with given name and parameters.
|
||||
*
|
||||
* PostConditions: A valid job will be returned which has been persisted and
|
||||
* PreConditions: JobInstance for given name and parameters must not already exist
|
||||
*
|
||||
* PostConditions: A valid job instancewill be returned which has been persisted and
|
||||
* contains an unique Id.
|
||||
*
|
||||
* @param jobName
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.batch.core.domain.Job;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class MapJobInstanceDao implements JobInstanceDao {
|
||||
|
||||
@@ -20,9 +21,7 @@ public class MapJobInstanceDao implements JobInstanceDao {
|
||||
|
||||
public JobInstance createJobInstance(Job job, JobParameters jobParameters) {
|
||||
|
||||
if (getJobInstance(job, jobParameters) != null) {
|
||||
throw new IllegalArgumentException("JobInstance already exists for given job and parameters");
|
||||
}
|
||||
Assert.state(getJobInstance(job, jobParameters) == null, "JobInstance must not already exist");
|
||||
|
||||
JobInstance jobInstance = new JobInstance(new Long(currentId++), jobParameters, job);
|
||||
jobInstances.add(jobInstance);
|
||||
|
||||
@@ -268,4 +268,22 @@ public abstract class AbstractJobDaoTests extends AbstractTransactionalDataSourc
|
||||
|
||||
assertEquals(lastExecution, jobExecutionDao.getLastJobExecution(jobInstance));
|
||||
}
|
||||
|
||||
/**
|
||||
* Trying to create instance twice for the same job+parameters causes error
|
||||
*/
|
||||
public void testCreateDuplicateInstance() {
|
||||
|
||||
jobParameters = new JobParameters();
|
||||
|
||||
jobInstanceDao.createJobInstance(job, jobParameters);
|
||||
|
||||
try {
|
||||
jobInstanceDao.createJobInstance(job, jobParameters);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,12 @@ import org.springframework.batch.execution.job.JobSupport;
|
||||
|
||||
public class MapJobInstanceDaoTests extends TestCase {
|
||||
|
||||
JobInstanceDao dao = new MapJobInstanceDao();
|
||||
|
||||
private JobInstanceDao dao = new MapJobInstanceDao();
|
||||
|
||||
private Job fooJob = new JobSupport("foo");
|
||||
|
||||
private JobParameters fooParams = new JobParametersBuilder().addString("fooKey", "fooValue").toJobParameters();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
MapJobInstanceDao.clear();
|
||||
}
|
||||
@@ -24,15 +28,29 @@ public class MapJobInstanceDaoTests extends TestCase {
|
||||
* Create and retrieve a job instance.
|
||||
*/
|
||||
public void testCreateAndRetrieve() throws Exception {
|
||||
Job fooJob = new JobSupport("foo");
|
||||
JobParameters fooParams = new JobParametersBuilder().addString("fooKey", "fooValue").toJobParameters();
|
||||
|
||||
|
||||
JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams);
|
||||
assertNotNull(fooInstance.getId());
|
||||
assertEquals(fooJob, fooInstance.getJob());
|
||||
assertEquals(fooParams, fooInstance.getJobParameters());
|
||||
|
||||
|
||||
JobInstance retrievedInstance = dao.getJobInstance(fooJob, fooParams);
|
||||
assertEquals(fooInstance, retrievedInstance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trying to create instance twice for the same job+parameters causes error
|
||||
*/
|
||||
public void testCreateDuplicateInstance() {
|
||||
|
||||
dao.createJobInstance(fooJob, fooParams);
|
||||
|
||||
try {
|
||||
dao.createJobInstance(fooJob, fooParams);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user