From 917f31be0cfe7e200575c82d7dd5a7ba5552e359 Mon Sep 17 00:00:00 2001
From: dsyer
Date: Fri, 5 Sep 2008 16:08:19 +0000
Subject: [PATCH] OPEN - issue BATCH-814: JobRepository should not require Step
or Job (only their names)
Moved isJobInstanceExists check to JobRepository
---
.../batch/core/explore/JobExplorer.java | 8 -------
.../explore/support/SimpleJobExplorer.java | 8 -------
.../launch/support/SimpleJobOperator.java | 2 +-
.../batch/core/repository/JobRepository.java | 10 ++++++++
.../support/SimpleJobRepository.java | 7 ++++++
.../support/SimpleJobExplorerTests.java | 18 ---------------
.../support/SimpleJobOperatorTests.java | 12 +++++-----
.../support/SimpleJobRepositoryTests.java | 23 +++++++++++++++++--
.../batch/core/step/JobRepositorySupport.java | 8 +++++++
9 files changed, 53 insertions(+), 43 deletions(-)
diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java
index cbf0df82f..90b4e8f8b 100644
--- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java
+++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java
@@ -20,7 +20,6 @@ import java.util.Set;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
-import org.springframework.batch.core.JobParameters;
/**
* @author Dave Syer
@@ -35,13 +34,6 @@ public interface JobExplorer {
*/
List getLastJobInstances(String jobName, int count);
- /**
- * @param jobName the name of the job
- * @param jobParameters the parameters to match
- * @return true if a {@link JobInstance} already exists for this job name and job parameters
- */
- boolean isJobInstanceExists(String jobName, JobParameters jobParameters);
-
/**
* @param executionId
* @return the {@link JobExecution} with this id, or null if not found
diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java
index d686f2278..c480d6e51 100644
--- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java
+++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java
@@ -21,7 +21,6 @@ import java.util.Set;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
-import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.repository.dao.JobExecutionDao;
import org.springframework.batch.core.repository.dao.JobInstanceDao;
@@ -109,13 +108,6 @@ public class SimpleJobExplorer implements JobExplorer {
return jobInstanceDao.getLastJobInstances(jobName, count);
}
- /* (non-Javadoc)
- * @see org.springframework.batch.core.explore.JobExplorer#isJobInstanceExists(java.lang.String, org.springframework.batch.core.JobParameters)
- */
- public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
- return jobInstanceDao.getJobInstance(jobName, jobParameters)!=null;
- }
-
/*
* Find all dependencies for a JobExecution, including JobInstance (which requires JobParameters)
* plus StepExecutions
diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java
index 632b576b5..07617cfd3 100644
--- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java
+++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java
@@ -283,7 +283,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
JobParameters jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter
.stringToProperties(parameters));
- if (jobExplorer.isJobInstanceExists(jobName, jobParameters)) {
+ if (jobRepository.isJobInstanceExists(jobName, jobParameters)) {
throw new JobInstanceAlreadyExistsException(String.format(
"Cannot start a job instance that already exists with name=%s and parameters=%s", jobName,
parameters));
diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java
index b8a9d96ef..1337fd45e 100644
--- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java
+++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java
@@ -33,10 +33,20 @@ import org.springframework.batch.item.ExecutionContext;
*
*
* @author Lucas Ward
+ * @author Dave Syer
*
*/
public interface JobRepository {
+ /**
+ * Check if an instance of this job already exists with the parameters provided.
+ *
+ * @param jobName the name of the job
+ * @param jobParameters the parameters to match
+ * @return true if a {@link JobInstance} already exists for this job name and job parameters
+ */
+ boolean isJobInstanceExists(String jobName, JobParameters jobParameters);
+
/**
* Find or create a {@link JobExecution} for a given {@link Job} and
* {@link JobParameters}. If the {@link Job} was already executed with these
diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java
index c0abed566..78c2636d2 100644
--- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java
+++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java
@@ -81,6 +81,13 @@ public class SimpleJobRepository implements JobRepository {
this.ecDao = ecDao;
}
+ /**
+ * @see JobRepository#isJobInstanceExists(String, JobParameters)
+ */
+ public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
+ return jobInstanceDao.getJobInstance(jobName, jobParameters)!=null;
+ }
+
/**
*
* Create a {@link JobExecution} based on the passed in {@link Job} and
diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java
index f899cdf65..68aa55b23 100644
--- a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java
+++ b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java
@@ -110,22 +110,4 @@ public class SimpleJobExplorerTests extends TestCase {
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
}
- @Test
- public void testIsJobInstanceFalse() throws Exception {
- jobInstanceDao.getJobInstance("foo", new JobParameters());
- EasyMock.expectLastCall().andReturn(null);
- replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
- assertFalse(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
- verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
- }
-
- @Test
- public void testIsJobInstanceTrue() throws Exception {
- jobInstanceDao.getJobInstance("foo", new JobParameters());
- EasyMock.expectLastCall().andReturn(jobInstance);
- replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
- assertTrue(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
- verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
- }
-
}
diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java
index 1bf1e19dc..96e66cabb 100644
--- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java
+++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java
@@ -165,20 +165,20 @@ public class SimpleJobOperatorTests {
@Test
public void testStartNewInstanceSunnyDay() throws Exception {
jobParameters = new JobParameters();
- jobExplorer.isJobInstanceExists("foo", jobParameters);
+ jobRepository.isJobInstanceExists("foo", jobParameters);
EasyMock.expectLastCall().andReturn(false);
- EasyMock.replay(jobExplorer);
+ EasyMock.replay(jobRepository);
Long value = jobOperator.start("foo", "a=b");
assertEquals(999, value.longValue());
- EasyMock.verify(jobExplorer);
+ EasyMock.verify(jobRepository);
}
@Test
public void testStartNewInstanceAlreadyExists() throws Exception {
jobParameters = new JobParameters();
- jobExplorer.isJobInstanceExists("foo", jobParameters);
+ jobRepository.isJobInstanceExists("foo", jobParameters);
EasyMock.expectLastCall().andReturn(true);
- EasyMock.replay(jobExplorer);
+ EasyMock.replay(jobRepository);
try {
jobOperator.start("foo", "a=b");
fail("Expected JobInstanceAlreadyExistsException");
@@ -186,7 +186,7 @@ public class SimpleJobOperatorTests {
catch (JobInstanceAlreadyExistsException e) {
// expected
}
- EasyMock.verify(jobExplorer);
+ EasyMock.verify(jobRepository);
}
@Test
diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java
index 1c5e52b15..838082b38 100644
--- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java
+++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
+import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
@@ -66,7 +67,7 @@ public class SimpleJobRepositoryTests {
ExecutionContextDao ecDao;
- JobInstance databaseJob;
+ JobInstance jobInstance;
String databaseStep1;
@@ -102,7 +103,7 @@ public class SimpleJobRepositoryTests {
job.setSteps(stepConfigurations);
- databaseJob = new JobInstance(new Long(1), jobParameters, job.getName());
+ jobInstance = new JobInstance(new Long(1), jobParameters, job.getName());
databaseStep1 = "dbStep1";
databaseStep2 = "dbStep2";
@@ -198,4 +199,22 @@ public class SimpleJobRepositoryTests {
assertTrue(stepExecution.isTerminateOnly());
}
+ @Test
+ public void testIsJobInstanceFalse() throws Exception {
+ jobInstanceDao.getJobInstance("foo", new JobParameters());
+ EasyMock.expectLastCall().andReturn(null);
+ replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
+ assertFalse(jobRepository.isJobInstanceExists("foo", new JobParameters()));
+ verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
+ }
+
+ @Test
+ public void testIsJobInstanceTrue() throws Exception {
+ jobInstanceDao.getJobInstance("foo", new JobParameters());
+ EasyMock.expectLastCall().andReturn(jobInstance);
+ replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
+ assertTrue(jobRepository.isJobInstanceExists("foo", new JobParameters()));
+ verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
+ }
+
}
diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java
index 63ac38df5..dfe43ffed 100644
--- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java
+++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java
@@ -72,4 +72,12 @@ public class JobRepositorySupport implements JobRepository {
public void updateExecutionContext(StepExecution stepExecution) {
}
+ /* (non-Javadoc)
+ * @see org.springframework.batch.core.repository.JobRepository#isJobInstanceExists(java.lang.String, org.springframework.batch.core.JobParameters)
+ */
+ public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
}