From 34bd1fc44629bb46eebf5bcebd463b8f7694683c Mon Sep 17 00:00:00 2001 From: robokaso Date: Wed, 20 Feb 2008 12:24:10 +0000 Subject: [PATCH] RESOLVED - issue BATCH-373: jobExecution.getJobInstance().getJob() return null when re-run http://jira.springframework.org/browse/BATCH-373 added testcase and fixed the bug --- .../repository/SimpleJobRepository.java | 1 + .../SimpleJobRepositoryIntegrationTests.java | 70 +++++++++++++++++++ .../execution/repository/dao/sql-dao-test.xml | 6 ++ 3 files changed, 77 insertions(+) create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java 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 4686388ff..3fc4e90ac 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 @@ -190,6 +190,7 @@ public class SimpleJobRepository implements JobRepository { } jobInstance.setLastExecution(lastExecution); jobInstance.setStepNames(getStepNames(job)); + jobInstance.setJob(job); } else if (jobs.size() == 0) { // no job found, create one diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java new file mode 100644 index 000000000..7d748b638 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryIntegrationTests.java @@ -0,0 +1,70 @@ +package org.springframework.batch.execution.repository; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.batch.core.domain.Job; +import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.JobParameters; +import org.springframework.batch.core.domain.JobSupport; +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; +import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; +import org.springframework.util.ClassUtils; + +/** + * Repository tests using JDBC DAOs (rather than mocks). + * + * @author Robert Kasanicky + */ +public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests { + + protected String[] getConfigLocations() { + return new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "dao/sql-dao-test.xml") }; + } + + private SimpleJobRepository jobRepository; + + public void setJobRepository(SimpleJobRepository jobRepository) { + this.jobRepository = jobRepository; + } + + /** + * Create two job executions for same job+parameters tuple. Check both + * executions belong to the same job instance and job. + */ + public void testCreateAndFind() throws Exception { + + JobSupport job = new JobSupport("testJob"); + job.setRestartable(true); + + Map stringParams = new HashMap() { + { + put("stringKey", "stringValue"); + } + }; + Map longParams = new HashMap() { + { + put("longKey", new Long(1)); + } + }; + Map dateParams = new HashMap() { + { + put("dateKey", new Date(1)); + } + }; + JobParameters jobParams = new JobParameters(stringParams, longParams, dateParams); + + JobExecution firstExecution = jobRepository.createJobExecution(job, jobParams); + + assertEquals(job, firstExecution.getJobInstance().getJob()); + + jobRepository.saveOrUpdate(firstExecution); + firstExecution.stop(); + jobRepository.saveOrUpdate(firstExecution); + JobExecution secondExecution = jobRepository.createJobExecution(job, jobParams); + + assertEquals(firstExecution.getJobInstance(), secondExecution.getJobInstance()); + assertEquals(job, secondExecution.getJobInstance().getJob()); + } +} \ No newline at end of file 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 34d7ca997..b2ee99cf2 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 @@ -4,6 +4,12 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> + + + + + +