From b837b9d28e240b8ab362abcf11c74ee53149d74f Mon Sep 17 00:00:00 2001 From: dsyer Date: Sun, 30 Sep 2007 16:20:38 +0000 Subject: [PATCH] OPEN - issue BATCH-90: StepExecution and StepExecutionContext are parallel domains, and StepExecution is by comparison anaemic http://opensource.atlassian.com/projects/spring/browse/BATCH-90 Remove *ExecutionContext by merging with *Execution --- .../batch/core/domain/JobExecution.java | 170 ++++++++++++++- .../batch/core/domain/StepExecution.java | 59 +++++- .../batch/core/domain/StepInstance.java | 23 +- .../batch/core/executor/JobExecutor.java | 6 +- .../batch/core/executor/StepExecutor.java | 8 +- .../core/runtime/JobExecutionContext.java | 198 ------------------ .../core/runtime/JobExecutionRegistry.java | 14 +- .../core/runtime/StepExecutionContext.java | 105 ---------- .../batch/core/domain/JobExecutionTests.java | 47 ++++- .../batch/core/domain/StepExecutionTests.java | 107 ++++++---- .../batch/core/domain/StepInstanceTests.java | 17 +- .../runtime/StepExecutionContextTests.java | 114 ---------- 12 files changed, 354 insertions(+), 514 deletions(-) delete mode 100644 core/src/main/java/org/springframework/batch/core/runtime/JobExecutionContext.java delete mode 100644 core/src/main/java/org/springframework/batch/core/runtime/StepExecutionContext.java delete mode 100644 core/src/test/java/org/springframework/batch/core/runtime/StepExecutionContextTests.java diff --git a/core/src/main/java/org/springframework/batch/core/domain/JobExecution.java b/core/src/main/java/org/springframework/batch/core/domain/JobExecution.java index 100ae10be..5bd77dcdd 100644 --- a/core/src/main/java/org/springframework/batch/core/domain/JobExecution.java +++ b/core/src/main/java/org/springframework/batch/core/domain/JobExecution.java @@ -17,16 +17,28 @@ package org.springframework.batch.core.domain; import java.sql.Timestamp; +import java.util.Collection; +import java.util.HashSet; + +import org.springframework.batch.core.runtime.JobIdentifier; +import org.springframework.batch.repeat.RepeatContext; /** - * Batch domain object representing the execution of a job. + * Batch domain object representing the execution of a job. * * @author Lucas Ward - * + * */ public class JobExecution extends Entity { -// TODO declare transient or make serializable + private transient JobInstance job; + + private transient Collection stepExecutions = new HashSet(); + + private transient Collection stepContexts = new HashSet(); + + private transient Collection chunkContexts = new HashSet(); + private BatchStatus status = BatchStatus.STARTING; private Timestamp startTime = new Timestamp(System.currentTimeMillis()); @@ -36,18 +48,33 @@ public class JobExecution extends Entity { private Long jobId; private String exitCode = ""; - + // Package private constructor for Hibernate - JobExecution() {} + JobExecution() { + } /** - * Because a JobExecution isn't valid unless the jobId is set, this - * constructor is the only valid one. + * Because a JobExecution isn't valid unless the job is set, this + * constructor is the only valid one from a modelling point of view. * - * @param jobId + * @param job + * the job of which this execution is a part */ - public JobExecution(Long jobId) { - this.jobId = jobId; + public JobExecution(JobInstance job, Long id) { + this.job = job; + if (job != null) { + this.jobId = job.getId(); + } + setId(id); + } + + /** + * Constructor for transient (unsaved) instances. + * + * @param job the enclosing {@link JobInstance} + */ + public JobExecution(JobInstance job) { + this(job, null); } public Timestamp getEndTime() { @@ -74,6 +101,12 @@ public class JobExecution extends Entity { this.status = status; } + /** + * Convenience getter for for the id of the enclosing job. Useful for DAO + * implementations. + * + * @return the id of the enclosing job + */ public Long getJobId() { return jobId; } @@ -84,11 +117,126 @@ public class JobExecution extends Entity { public void setExitCode(String exitCode) { this.exitCode = exitCode; } - + /** * @return the exitCode */ public String getExitCode() { return exitCode; } + + /** + * Accessor for the potentially multiple chunk contexts that are in + * progress. In a single-threaded, sequential execution there would normally + * be only one current chunk, but in more complicated scenarios there might + * be multiple active contexts. + * + * @return all the chunk contexts that have been registered and not + * unregistered. A collection opf {@link RepeatContext} objects. + */ + public Collection getChunkContexts() { + synchronized (chunkContexts) { + return new HashSet(chunkContexts); + } + } + + /** + * Accessor for the runtime information of this execution. + * + * @return the {@link JobRuntimeInformation} that was used to start this job + * execution. + */ + public JobIdentifier getJobIdentifier() { + return job.getIdentifier(); + } + + /** + * Accessor for the potentially multiple step contexts that are in progress. + * In a single-threaded, sequential execution there would normally be only + * one current step, but in more complicated scenarios there might be + * multiple active contexts. + * + * @return all the step contexts that have been registered and not + * unregistered. A collection of {@link RepeatContext} objects. + */ + public Collection getStepContexts() { + synchronized (stepContexts) { + return new HashSet(stepContexts); + } + } + + /** + * Called at the start of a step, before any business logic is processed. + * + * @param context + * the current step context. + */ + public void registerStepContext(RepeatContext stepContext) { + synchronized (stepContexts) { + this.stepContexts.add(stepContext); + } + } + + /** + * Called at the end of a step, after all business logic is processed, or in + * the case of a failure. + * + * @param context + * the current step context. + */ + public void unregisterStepContext(RepeatContext stepContext) { + synchronized (stepContexts) { + this.stepContexts.remove(stepContext); + } + } + + /** + * Called at the start of a chunk, before any business logic is processed. + * + * @param context + * the current chunk context. + */ + public void registerChunkContext(RepeatContext chunkContext) { + synchronized (chunkContexts) { + this.chunkContexts.add(chunkContext); + } + } + + /** + * Called at the end of a chunk, after all business logic is processed, or + * in the case of a failure. + * + * @param context + * the current chunk context. + */ + public void unregisterChunkContext(RepeatContext chunkContext) { + synchronized (chunkContexts) { + this.chunkContexts.remove(chunkContext); + } + } + + /** + * @return the Job that is executing. + */ + public JobInstance getJob() { + return job; + } + + /** + * Accessor for the step executions. + * + * @return the step executions that were registered + */ + public Collection getStepExecutions() { + return stepExecutions; + } + + /** + * Register a step execution with the current job execution. + * + * @param stepExecution + */ + public void registerStepExecution(StepExecution stepExecution) { + this.stepExecutions.add(stepExecution); + } } diff --git a/core/src/main/java/org/springframework/batch/core/domain/StepExecution.java b/core/src/main/java/org/springframework/batch/core/domain/StepExecution.java index f72899529..22509d766 100644 --- a/core/src/main/java/org/springframework/batch/core/domain/StepExecution.java +++ b/core/src/main/java/org/springframework/batch/core/domain/StepExecution.java @@ -28,11 +28,15 @@ import java.util.Properties; * respectively. * * @author Lucas Ward + * @author Dave Syer * */ public class StepExecution extends Entity { - // TODO declare transient or make serializable + private JobExecution jobExecution; + + private StepInstance step; + private BatchStatus status = BatchStatus.STARTING; private int taskCount = 0; @@ -47,10 +51,6 @@ public class StepExecution extends Entity { private Properties statistics = new Properties(); - private Long stepId; - - private Long jobExecutionId; - private String exitCode = ""; private String exitDescription = ""; @@ -64,10 +64,21 @@ public class StepExecution extends Entity { super(); } - public StepExecution(Long stepId, Long jobExecutionId) { + /** + * Constructor with mandatory properties. + * + * @param step the step to which this execution belongs + * @param jobExecution the current job execution + */ + public StepExecution(StepInstance step, JobExecution jobExecution, Long id) { this(); - this.stepId = stepId; - this.jobExecutionId = jobExecutionId; + this.step= step; + this.jobExecution = jobExecution; + setId(id); + } + + public StepExecution(StepInstance step, JobExecution jobExecution) { + this(step, jobExecution, null); } public void incrementCommitCount() { @@ -139,7 +150,10 @@ public class StepExecution extends Entity { } public Long getStepId() { - return stepId; + if (step!=null) { + return step.getId(); + } + return null; } /** @@ -147,13 +161,18 @@ public class StepExecution extends Entity { * @return the jobExecutionId */ public Long getJobExecutionId() { - return jobExecutionId; + if (jobExecution!=null) { + return jobExecution.getId(); + } + return null; } /* (non-Javadoc) * @see org.springframework.batch.container.common.domain.Entity#equals(java.lang.Object) */ public boolean equals(Object obj) { + Object stepId = getStepId(); + Object jobExecutionId = getJobExecutionId(); if (stepId==null && jobExecutionId==null || !(obj instanceof StepExecution) || getId()!=null) { return super.equals(obj); } @@ -168,6 +187,8 @@ public class StepExecution extends Entity { * @see org.springframework.batch.container.common.domain.Entity#hashCode() */ public int hashCode() { + Object stepId = getStepId(); + Object jobExecutionId = getJobExecutionId(); return super.hashCode() + 31*(stepId!=null ? stepId.hashCode() : 0) + 91*(jobExecutionId!=null ? jobExecutionId.hashCode() : 0); } @@ -206,4 +227,22 @@ public class StepExecution extends Entity { public String getExitDescription() { return exitDescription; } + + /** + * Accessor for the step governing this execution. + * @return the step + */ + public StepInstance getStep() { + return step; + } + + /** + * Accessor for the execution context information of the enclosing job. + * @return the {@link jobExecutionContext} that was used to start this step + * execution. + */ + public JobExecution getJobExecution() { + return jobExecution; + } + } diff --git a/core/src/main/java/org/springframework/batch/core/domain/StepInstance.java b/core/src/main/java/org/springframework/batch/core/domain/StepInstance.java index 611beb96e..48237c5f2 100644 --- a/core/src/main/java/org/springframework/batch/core/domain/StepInstance.java +++ b/core/src/main/java/org/springframework/batch/core/domain/StepInstance.java @@ -61,12 +61,25 @@ public class StepInstance extends Entity { private String name; - public StepInstance() { + /** + * Package private constructor for Hibernate only + */ + StepInstance() { this(null); } public StepInstance(Long stepId) { + this(null, null, stepId); + } + + public StepInstance(JobInstance job, String name) { + this(job, name, null); + } + + public StepInstance(JobInstance job, String name, Long stepId) { setId(stepId); + this.job = job; + this.name = name; } public int getStepExecutionCount() { @@ -93,10 +106,6 @@ public class StepInstance extends Entity { this.status = status; } - public void setJob(JobInstance job) { - this.job = job; - } - public JobInstance getJob() { return job; } @@ -109,10 +118,6 @@ public class StepInstance extends Entity { this.stepExecution = stepInstance; } - public void setName(String name) { - this.name = name; - } - public String getName() { return name; } diff --git a/core/src/main/java/org/springframework/batch/core/executor/JobExecutor.java b/core/src/main/java/org/springframework/batch/core/executor/JobExecutor.java index 96e0cb082..3bf6c482f 100644 --- a/core/src/main/java/org/springframework/batch/core/executor/JobExecutor.java +++ b/core/src/main/java/org/springframework/batch/core/executor/JobExecutor.java @@ -17,7 +17,7 @@ package org.springframework.batch.core.executor; import org.springframework.batch.core.configuration.JobConfiguration; -import org.springframework.batch.core.runtime.JobExecutionContext; +import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.repeat.ExitStatus; @@ -27,10 +27,10 @@ import org.springframework.batch.repeat.ExitStatus; * @author Lucas Ward * @author Dave Syer * @see JobConfiguration - * @see JobExecutionContext + * @see JobExecution */ public interface JobExecutor { - public ExitStatus run(JobConfiguration configuration, JobExecutionContext jobExecutionContext) throws BatchCriticalException; + public ExitStatus run(JobConfiguration configuration, JobExecution jobExecution) throws BatchCriticalException; } diff --git a/core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java b/core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java index c7f6d913f..a485b1b2b 100644 --- a/core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java +++ b/core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java @@ -17,7 +17,7 @@ package org.springframework.batch.core.executor; import org.springframework.batch.core.configuration.StepConfiguration; -import org.springframework.batch.core.runtime.StepExecutionContext; +import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.repeat.ExitStatus; @@ -28,7 +28,7 @@ import org.springframework.batch.repeat.ExitStatus; * trackable with the step execution context ({@see Step#getContext()}). The * configuration should be treated as immutable.
* - * Because step execution paramaters and policies can vary from step to step, a + * Because step execution parameters and policies can vary from step to step, a * {@link StepExecutor} should be created by the caller using a * {@link StepExecutorFactory}. * @@ -45,11 +45,11 @@ public interface StepExecutor { * Contains a recipe for the business logic of an individual processing * operation. Also used to determine policies for commit intervals and * exception handling, for instance. - * @param stepExecutionContext an entity representing the step to be executed + * @param stepExecution an entity representing the step to be executed * @throws StepInterruptedException if the step is interrupted externally * @throws BatchCriticalException if there is a problem that needs to be * signalled to the caller */ - ExitStatus process(StepConfiguration configuration, StepExecutionContext stepExecutionContext) throws StepInterruptedException, BatchCriticalException; + ExitStatus process(StepConfiguration configuration, StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException; } diff --git a/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionContext.java b/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionContext.java deleted file mode 100644 index 370ad4972..000000000 --- a/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionContext.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.core.runtime; - -import java.sql.Timestamp; -import java.util.Collection; -import java.util.HashSet; - -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.repeat.RepeatContext; - -/** - * Context for an executing job. Maintains invariants and provides communication - * channel for all components requiring information about the job and its steps. - * - * @author Dave Syer - * - */ -public class JobExecutionContext { - - private JobIdentifier jobIdentifier; - - private final JobInstance job; - - private final JobExecution jobExecution; - - private Collection stepExecutions = new HashSet(); - - private Collection stepContexts = new HashSet(); - - private Collection chunkContexts = new HashSet(); - - /** - * Constructor with all the mandatory properties. - * - * @param jobIdentifier - */ - public JobExecutionContext(JobIdentifier jobIdentifier, JobInstance job) { - super(); - this.jobIdentifier = jobIdentifier; - this.job = job; - this.jobExecution = new JobExecution(job.getId()); - this.jobExecution.setStartTime(new Timestamp(System.currentTimeMillis())); - } - - /** - * Accessor for the potentially multiple chunk contexts that are in - * progress. In a single-threaded, sequential execution there would normally - * be only one current chunk, but in more complicated scenarios there might - * be multiple active contexts. - * @return all the chunk contexts that have been registered and not - * unregistered. A collection opf {@link RepeatContext} objects. - */ - public Collection getChunkContexts() { - synchronized (chunkContexts) { - return new HashSet(chunkContexts); - } - } - - /** - * Accessor for the runtime information of this execution. - * @return the {@link JobRuntimeInformation} that was used to start this job - * execution. - */ - public JobIdentifier getJobIdentifier() { - return jobIdentifier; - } - - /** - * Accessor for the potentially multiple step contexts that are in progress. - * In a single-threaded, sequential execution there would normally be only - * one current step, but in more complicated scenarios there might be - * multiple active contexts. - * @return all the step contexts that have been registered and not - * unregistered. A collection of {@link RepeatContext} objects. - */ - public Collection getStepContexts() { - synchronized (stepContexts) { - return new HashSet(stepContexts); - } - } - - /** - * Called at the start of a step, before any business logic is processed. - * @param context the current step context. - */ - public void registerStepContext(RepeatContext stepContext) { - synchronized (stepContexts) { - this.stepContexts.add(stepContext); - } - } - - /** - * Called at the end of a step, after all business logic is processed, or in - * the case of a failure. - * @param context the current step context. - */ - public void unregisterStepContext(RepeatContext stepContext) { - synchronized (stepContexts) { - this.stepContexts.remove(stepContext); - } - } - - /** - * Called at the start of a chunk, before any business logic is processed. - * @param context the current chunk context. - */ - public void registerChunkContext(RepeatContext chunkContext) { - synchronized (chunkContexts) { - this.chunkContexts.add(chunkContext); - } - } - - /** - * Called at the end of a chunk, after all business logic is processed, or - * in the case of a failure. - * @param context the current chunk context. - */ - public void unregisterChunkContext(RepeatContext chunkContext) { - synchronized (chunkContexts) { - this.chunkContexts.remove(chunkContext); - } - } - - /** - * @return the Job that is executing. - */ - public JobInstance getJob() { - return job; - } - - /** - * @return the current job execution. - */ - public JobExecution getJobExecution() { - return jobExecution; - } - - /** - * Accessor for the step executions. - * @return the step executions that were registered - */ - public Collection getStepExecutions() { - return stepExecutions; - } - - /** - * Register a step execution with the current job execution. - * @param stepExecution - */ - public void registerStepExecution(StepExecution stepExecution) { - this.stepExecutions.add(stepExecution); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object obj) { - if (!(obj instanceof JobExecutionContext)) { - return super.equals(obj); - } - JobExecutionContext other = (JobExecutionContext) obj; - return job.equals(other.getJob()) && jobExecution.equals(other.getJobExecution()); - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - public int hashCode() { - return 23*job.hashCode() + 61*jobExecution.hashCode(); - } - - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - return "identifier=" + jobIdentifier + "; steps=" + stepContexts + "; chunks=" + chunkContexts; - } - -} diff --git a/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionRegistry.java b/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionRegistry.java index 520b02a5e..e45caaf0d 100644 --- a/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionRegistry.java +++ b/core/src/main/java/org/springframework/batch/core/runtime/JobExecutionRegistry.java @@ -40,7 +40,7 @@ public interface JobExecutionRegistry { * * @throws NullPointerException if the first parameter is null. */ - JobExecutionContext register(JobInstance job); + JobExecution register(JobInstance job); /** * Check if a given {@link JobExecution}, or one with the same id property, @@ -60,33 +60,33 @@ public interface JobExecutionRegistry { void unregister(JobIdentifier jobIdentifier); /** - * Find all the currently registered {@link JobExecutionContext} objects. + * Find all the currently registered {@link JobExecution} objects. * * @return all the currently registered contexts. */ Collection findAll(); /** - * Return a collection of {@link JobExecutionContext} objects representing + * Return a collection of {@link JobExecution} objects representing * the currently executing jobs with {@link JobRuntimeInformation} having * the given name. * * @param name the name of the {@link JobRuntimeInformation} as a to key the * search. The name can be null, in which case the key is null, i.e. * {@link JobRuntimeInformation} instances with null name will match. - * @return a {@link Collection} of {@link JobExecutionContext}. + * @return a {@link Collection} of {@link JobExecution}. */ Collection findByName(String name); /** - * Return a {@link JobExecutionContext} representing the currently executing + * Return a {@link JobExecution} representing the currently executing * jobs with the given {@link JobRuntimeInformation}. * * @param runtimeInformation the {@link JobIdentifier} to use as a * search key. - * @return the {@link JobExecutionContext} that was registered under the + * @return the {@link JobExecution} that was registered under the * given key, if there is one, null otherwise. */ - JobExecutionContext get(JobIdentifier jobIdentifier); + JobExecution get(JobIdentifier jobIdentifier); } diff --git a/core/src/main/java/org/springframework/batch/core/runtime/StepExecutionContext.java b/core/src/main/java/org/springframework/batch/core/runtime/StepExecutionContext.java deleted file mode 100644 index dfd57764a..000000000 --- a/core/src/main/java/org/springframework/batch/core/runtime/StepExecutionContext.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.core.runtime; - -import org.springframework.batch.core.domain.StepExecution; -import org.springframework.batch.core.domain.StepInstance; -import org.springframework.util.Assert; - -/** - * Context for an executing step within a job. Maintains invariants and provides - * communication channel for all components requiring information about the - * step. - * - * @author Dave Syer - * - */ -public class StepExecutionContext { - - private JobExecutionContext jobExecutionContext; - - private final StepInstance step; - - private final StepExecution stepExecution; - - /** - * Constructor with all the mandatory properties. - * - * @param jobExecutionContext - */ - public StepExecutionContext(JobExecutionContext jobExecutionContext, StepInstance step) { - super(); - Assert.notNull(jobExecutionContext); - Assert.notNull(jobExecutionContext.getJobExecution(), "The JobExecutionContext must have a JobExecution"); - Assert.notNull(step); - this.jobExecutionContext = jobExecutionContext; - this.step = step; - this.stepExecution = new StepExecution(step.getId(), jobExecutionContext.getJobExecution().getId()); - } - - /** - * Accessor for the step governing this execution. - * @return the step - */ - public StepInstance getStep() { - return step; - } - - /** - * Accessor for the execution context information of the enclosing job. - * @return the {@link jobExecutionContext} that was used to start this step - * execution. - */ - public JobExecutionContext getJobExecutionContext() { - return jobExecutionContext; - } - - /** - * Retrieve the current step execution or create a new one if there is none. - * @return the current step execution. - */ - public StepExecution getStepExecution() { - return stepExecution; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object obj) { - if (!(obj instanceof StepExecutionContext)) { - return super.equals(obj); - } - StepExecutionContext other = (StepExecutionContext) obj; - return step.equals(other.getStep()) && stepExecution.equals(other.getStepExecution()); - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - public int hashCode() { - return 23*step.hashCode() + 61*stepExecution.hashCode(); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - return "step=" + step + "; stepExecution=" + stepExecution; - } - -} diff --git a/core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java b/core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java index 8ff233f1c..6c54a8056 100644 --- a/core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java +++ b/core/src/test/java/org/springframework/batch/core/domain/JobExecutionTests.java @@ -19,13 +19,17 @@ import java.sql.Timestamp; import junit.framework.TestCase; +import org.springframework.batch.core.runtime.SimpleJobIdentifier; +import org.springframework.batch.repeat.context.RepeatContextSupport; + /** * @author Dave Syer * */ public class JobExecutionTests extends TestCase { - private JobExecution execution = new JobExecution(new Long(11)); + private JobExecution execution = new JobExecution(new JobInstance(null, new Long(11))); + private JobExecution context = new JobExecution(new JobInstance(new SimpleJobIdentifier("foo"), new Long(11))); /** * Test method for {@link org.springframework.batch.core.domain.JobExecution#JobExecution()}. @@ -66,7 +70,7 @@ public class JobExecutionTests extends TestCase { */ public void testGetJobId() { assertEquals(11, execution.getJobId().longValue()); - execution = new JobExecution(new Long(23)); + execution = new JobExecution(new JobInstance(null, new Long(23))); assertEquals(23, execution.getJobId().longValue()); } @@ -79,4 +83,43 @@ public class JobExecutionTests extends TestCase { assertEquals("23", execution.getExitCode()); } + public void testContextContainsInfo() throws Exception { + assertEquals("foo", context.getJobIdentifier().getName()); + } + + public void testNullContexts() throws Exception { + assertEquals(0, context.getStepContexts().size()); + assertEquals(0, context.getChunkContexts().size()); + } + + public void testStepContext() throws Exception { + context.registerStepContext(new RepeatContextSupport(null)); + assertEquals(1, context.getStepContexts().size()); + } + + public void testAddAndRemoveStepContext() throws Exception { + context.registerStepContext(new RepeatContextSupport(null)); + assertEquals(1, context.getStepContexts().size()); + context.unregisterStepContext(new RepeatContextSupport(null)); + assertEquals(0, context.getStepContexts().size()); + } + + public void testAddAndRemoveStepExecution() throws Exception { + assertEquals(0, context.getStepExecutions().size()); + context.registerStepExecution(new StepExecution(null, null)); + assertEquals(1, context.getStepExecutions().size()); + } + + public void testAddAndRemoveChunkContext() throws Exception { + context.registerChunkContext(new RepeatContextSupport(null)); + assertEquals(1, context.getChunkContexts().size()); + context.unregisterChunkContext(new RepeatContextSupport(null)); + assertEquals(0, context.getChunkContexts().size()); + } + + public void testRemoveChunkContext() throws Exception { + context.unregisterChunkContext(new RepeatContextSupport(null)); + assertEquals(0, context.getChunkContexts().size()); + } + } diff --git a/core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java b/core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java index 28bfb0cf9..f8aab34c1 100644 --- a/core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java +++ b/core/src/test/java/org/springframework/batch/core/domain/StepExecutionTests.java @@ -22,21 +22,24 @@ import junit.framework.TestCase; /** * @author Dave Syer - * + * */ public class StepExecutionTests extends TestCase { - private StepExecution execution = new StepExecution(new Long(11), new Long(23)); - + private StepExecution execution = newStepExecution(new Long(11), + new Long(23)); + /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#JobExecution()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#JobExecution()}. */ public void testStepExecution() { assertNull(new StepExecution().getId()); } /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#getEndTime()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#getEndTime()}. */ public void testGetEndTime() { assertNull(execution.getEndTime()); @@ -45,7 +48,8 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#getStartTime()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#getStartTime()}. */ public void testGetStartTime() { assertNotNull(execution.getStartTime()); @@ -54,7 +58,8 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#getStatus()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#getStatus()}. */ public void testGetStatus() { assertEquals(BatchStatus.STARTING, execution.getStatus()); @@ -63,53 +68,59 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#getJobId()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#getJobId()}. */ public void testGetJobId() { assertEquals(23, execution.getJobExecutionId().longValue()); } /** - * Test method for {@link org.springframework.batch.core.domain.JobExecution#getExitCode()}. + * Test method for + * {@link org.springframework.batch.core.domain.JobExecution#getExitCode()}. */ public void testGetExitCode() { assertEquals("", execution.getExitCode()); execution.setExitCode("23"); assertEquals("23", execution.getExitCode()); } - + /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#incrementCommitCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#incrementCommitCount()}. */ public void testIncrementCommitCount() { int before = execution.getCommitCount().intValue(); execution.incrementCommitCount(); int after = execution.getCommitCount().intValue(); - assertEquals(before+1, after); + assertEquals(before + 1, after); } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#incrementTaskCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#incrementTaskCount()}. */ public void testIncrementLuwCount() { int before = execution.getTaskCount().intValue(); execution.incrementTaskCount(); int after = execution.getTaskCount().intValue(); - assertEquals(before+1, after); + assertEquals(before + 1, after); } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#incrementRollbackCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#incrementRollbackCount()}. */ public void testIncrementRollbackCount() { int before = execution.getRollbackCount().intValue(); execution.incrementRollbackCount(); int after = execution.getRollbackCount().intValue(); - assertEquals(before+1, after); + assertEquals(before + 1, after); } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#getCommitCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#getCommitCount()}. */ public void testGetCommitCount() { execution.setCommitCount(123); @@ -117,7 +128,8 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#getTaskCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#getTaskCount()}. */ public void testGetTaskCount() { execution.setTaskCount(123); @@ -125,7 +137,8 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#getRollbackCount()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#getRollbackCount()}. */ public void testGetRollbackCount() { execution.setRollbackCount(123); @@ -133,59 +146,73 @@ public class StepExecutionTests extends TestCase { } /** - * Test method for {@link org.springframework.batch.core.domain.StepExecution#getStepId()}. + * Test method for + * {@link org.springframework.batch.core.domain.StepExecution#getStepId()}. */ public void testGetStepId() { assertEquals(11, execution.getStepId().longValue()); } - + public void testToString() throws Exception { - assertTrue("Should contain task count: "+execution.toString(), execution.toString().indexOf("task")>=0); - assertTrue("Should contain commit count: "+execution.toString(), execution.toString().indexOf("commit")>=0); - assertTrue("Should contain rollback count: "+execution.toString(), execution.toString().indexOf("rollback")>=0); + assertTrue("Should contain task count: " + execution.toString(), + execution.toString().indexOf("task") >= 0); + assertTrue("Should contain commit count: " + execution.toString(), + execution.toString().indexOf("commit") >= 0); + assertTrue("Should contain rollback count: " + execution.toString(), + execution.toString().indexOf("rollback") >= 0); } - + public void testStatistics() throws Exception { assertNotNull(execution.getStatistics()); - execution.setStatistics(new Properties() {{ - setProperty("foo", "bar"); - }}); + execution.setStatistics(new Properties() { + { + setProperty("foo", "bar"); + } + }); assertEquals("bar", execution.getStatistics().getProperty("foo")); } - + public void testEqualsWithSameIdentifier() throws Exception { - StepExecution step1 = new StepExecution(new Long(100), new Long(11)); - StepExecution step2 = new StepExecution(new Long(100), new Long(11)); + StepExecution step1 = newStepExecution(new Long(100), new Long(11)); + StepExecution step2 = newStepExecution(new Long(100), new Long(11)); assertEquals(step1, step2); } public void testEqualsWithNull() throws Exception { - StepExecution step = new StepExecution(new Long(100), new Long(11)); + StepExecution step = newStepExecution(new Long(100), new Long(11)); assertFalse(step.equals(null)); } public void testEqualsWithNullIdentifiers() throws Exception { - StepExecution step = new StepExecution(new Long(100), new Long(11)); + StepExecution step = newStepExecution(new Long(100), new Long(11)); assertFalse(step.equals(new StepExecution())); } - + public void testEqualsWithNullJob() throws Exception { - StepExecution step = new StepExecution(null, new Long(11)); + StepExecution step = newStepExecution(null, new Long(11)); assertFalse(step.equals(new StepExecution())); } public void testEqualsWithNullStep() throws Exception { - StepExecution step = new StepExecution(new Long(11), null); + StepExecution step = newStepExecution(new Long(11), null); assertFalse(step.equals(new StepExecution())); } public void testHashCode() throws Exception { - assertTrue("Hash code same as parent", new Entity(execution.getId()).hashCode()!=execution.hashCode()); + assertTrue("Hash code same as parent", new Entity(execution.getId()) + .hashCode() != execution.hashCode()); } public void testHashCodeWithNullIds() throws Exception { - assertTrue("Hash code not same as parent", new Entity(execution.getId()).hashCode()!=new StepExecution().hashCode()); + assertTrue("Hash code not same as parent", + new Entity(execution.getId()).hashCode() != new StepExecution() + .hashCode()); + } + + private StepExecution newStepExecution(Long long1, Long long2) { + JobInstance job = new JobInstance(null); + StepInstance step = new StepInstance(job, "foo", long1); + StepExecution execution = new StepExecution(step, new JobExecution(job, long2)); + return execution; } } - - diff --git a/core/src/test/java/org/springframework/batch/core/domain/StepInstanceTests.java b/core/src/test/java/org/springframework/batch/core/domain/StepInstanceTests.java index e87c4d9d2..1eafb0732 100644 --- a/core/src/test/java/org/springframework/batch/core/domain/StepInstanceTests.java +++ b/core/src/test/java/org/springframework/batch/core/domain/StepInstanceTests.java @@ -72,7 +72,7 @@ public class StepInstanceTests extends TestCase { public void testGetJob() { assertEquals(null, instance.getJob()); JobInstance job = new JobInstance(null); - instance.setJob(job); + instance = new StepInstance(job, null); assertEquals(job, instance.getJob()); } @@ -81,7 +81,7 @@ public class StepInstanceTests extends TestCase { */ public void testGetStepExecution() { assertEquals(null, instance.getStepExecution()); - StepExecution execution = new StepExecution(instance.getId(), new Long(111)); + StepExecution execution = new StepExecution(instance, new JobExecution(instance.getJob(), new Long(111))); instance.setStepExecution(execution); assertNotNull(execution.getJobExecutionId()); assertEquals(execution.getJobExecutionId(), instance.getStepExecution().getJobExecutionId()); @@ -92,7 +92,7 @@ public class StepInstanceTests extends TestCase { */ public void testGetName() { assertEquals(null, instance.getName()); - instance.setName("foo"); + instance = new StepInstance(null, "foo"); assertEquals("foo", instance.getName()); } @@ -101,19 +101,14 @@ public class StepInstanceTests extends TestCase { */ public void testGetJobId() { assertEquals(null, instance.getJobId()); - instance.setJob(new JobInstance(null, new Long(23))); + instance = new StepInstance(new JobInstance(null, new Long(23)), null); assertEquals(23, instance.getJobId().longValue()); } public void testEqualsWithSameIdentifier() throws Exception { JobInstance job = new JobInstance(null, new Long(100)); - StepInstance step1 = new StepInstance(new Long(0)); - StepInstance step2 = new StepInstance(new Long(0)); - step1.setJob(job); - step2.setJob(job); - String stepName = "foo"; - step1.setName(stepName); - step2.setName(stepName); + StepInstance step1 = new StepInstance(job, "foo", new Long(0)); + StepInstance step2 = new StepInstance(job, "foo", new Long(0)); assertEquals(step1, step2); } diff --git a/core/src/test/java/org/springframework/batch/core/runtime/StepExecutionContextTests.java b/core/src/test/java/org/springframework/batch/core/runtime/StepExecutionContextTests.java deleted file mode 100644 index 4332e27fe..000000000 --- a/core/src/test/java/org/springframework/batch/core/runtime/StepExecutionContextTests.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.core.runtime; - -import junit.framework.TestCase; - -import org.springframework.batch.core.domain.JobInstance; -import org.springframework.batch.core.domain.StepInstance; - -/** - * @author Dave Syer - * - */ -public class StepExecutionContextTests extends TestCase { - - private StepExecutionContext context = createContext("foo", 11, 12); - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#hashCode()}. - */ - public void testHashCode() { - assertNotNull(context.getStep().getId()); - assertNull(context.getStepExecution().getId()); - assertTrue("Expecting unequal hash codes before save", context.hashCode() != createContext("foo", 11, 12) - .hashCode()); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#getStep()}. - */ - public void testGetStep() { - assertNotNull(context.getStep()); - assertEquals(12, context.getStep().getId().longValue()); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#getJobExecutionContext()}. - */ - public void testGetJobExecutionContext() { - assertNotNull(context.getJobExecutionContext()); - assertEquals(11, context.getJobExecutionContext().getJob().getId().longValue()); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#getStepExecution()}. - */ - public void testGetStepExecution() { - assertNotNull(context.getStepExecution()); - assertEquals(null, context.getStepExecution().getId()); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#equals(java.lang.Object)}. - */ - public void testEqualsObject() { - assertFalse(context.equals(new Object())); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#equals(java.lang.Object)}. - */ - public void testEqualsNull() { - assertFalse(context.equals(null)); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#equals(java.lang.Object)}. - */ - public void testEqualsContext() { - StepExecutionContext other = createContext("foo", 11, 12); - assertTrue(context.equals(other)); - } - - /** - * Test method for - * {@link org.springframework.batch.core.runtime.StepExecutionContext#toString()}. - */ - public void testToString() { - assertTrue("Step not contained in toString: " + context.toString(), context.toString().indexOf("step=") >= 0); - } - - /** - * @param name - * @param jobId - * @param stepId - * @return - */ - private StepExecutionContext createContext(String name, int jobId, int stepId) { - JobIdentifier jobIdentifier = new SimpleJobIdentifier(name); - JobInstance job = new JobInstance(jobIdentifier, new Long(jobId)); - return new StepExecutionContext(new JobExecutionContext(jobIdentifier, job), new StepInstance( - new Long(stepId))); - } -}