From 7487b4dfd2bab233c5830e6ad4d90a4290c5441a Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Tue, 3 Feb 2009 18:08:32 +0000 Subject: [PATCH] BATCH-1053: Modified so that AbstractJob.getStep() returns null if the step is not found and Flow.getState() returns null if the state is not found. Javadocs and unit tests were updated to reflect this change. --- .../batch/core/job/AbstractJob.java | 6 +-- .../batch/core/job/SimpleJob.java | 2 +- .../batch/core/job/flow/Flow.java | 4 +- .../batch/core/job/flow/FlowJob.java | 2 +- .../core/job/flow/support/SimpleFlow.java | 6 +-- .../batch/core/job/SimpleJobTests.java | 19 +++++++ .../batch/core/job/flow/FlowJobTests.java | 51 +++++++++++++++++++ .../job/flow/support/SimpleFlowTests.java | 6 ++- .../batch/test/AbstractJobTests.java | 8 ++- 9 files changed, 89 insertions(+), 15 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java index 39ec0cddc..330617bf3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java @@ -126,11 +126,11 @@ public abstract class AbstractJob implements Job, BeanNameAware, InitializingBea } /** - * Retrieve the step with the given name. An IllegalStateException is thrown - * if there is no Step with the given name. + * Retrieve the step with the given name. If there is no Step with the + * given name, then return null. * * @param stepName - * @return the step + * @return the Step */ public abstract Step getStep(String stepName); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java index 6a4d7fe42..0afbac13d 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java @@ -71,7 +71,7 @@ public class SimpleJob extends AbstractJob { return step; } } - throw new IllegalStateException("No Step found with name: [" + stepName + "]"); + return null; } /** diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java index fe77e0cfd..9a8fa564b 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java @@ -29,8 +29,8 @@ public interface Flow { String getName(); /** - * Retrieve the State with the given name. An IllegalStateException is - * thrown if there is no State with the given name. + * Retrieve the State with the given name. If there is no State with the + * given name, then return null. * * @param stateName * @return the State diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java index bae06462a..c02cf0812 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java @@ -68,7 +68,7 @@ public class FlowJob extends AbstractJob { if(state instanceof StepState){ return ((StepState) state).getStep(); } - throw new IllegalStateException("State is not a StepState: [" + stepName + "]"); + return null; } /** diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java index 46fda44d9..28fb04ff4 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java @@ -86,11 +86,7 @@ public class SimpleFlow implements Flow, InitializingBean { * @see org.springframework.batch.core.job.flow.Flow#getState(java.lang.String) */ public State getState(String stateName) { - State state = stateMap.get(stateName); - if(state == null){ - throw new IllegalStateException("No State found with name: [" + stateName + "]"); - } - return state; + return stateMap.get(stateName); } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java index f908589a5..f5d10a577 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java @@ -446,6 +446,25 @@ public class SimpleJobTests { assertNull("Second step was not supposed to be executed", step2.passedInStepContext); } + + @Test + public void testGetStepExists() { + step1 = new StubStep("step1", jobRepository); + step2 = new StubStep("step2", jobRepository); + job.setSteps(Arrays.asList(new Step[] { step1, step2 })); + Step step = job.getStep("step2"); + assertNotNull(step); + assertEquals("step2", step.getName()); + } + + @Test + public void testGetStepNotExists() { + step1 = new StubStep("step1", jobRepository); + step2 = new StubStep("step2", jobRepository); + job.setSteps(Arrays.asList(new Step[] { step1, step2 })); + Step step = job.getStep("foo"); + assertNull(step); + } /* * Check JobRepository to ensure status is being saved. diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java index 46ad4a501..6a1549616 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java @@ -17,6 +17,7 @@ package org.springframework.batch.core.job.flow; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.util.ArrayList; @@ -292,6 +293,56 @@ public class FlowJobTests { } + @Test + public void testGetStepExists() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + job.setFlow(flow); + job.afterPropertiesSet(); + + + Step step = job.getStep("step2"); + assertNotNull(step); + assertEquals("step2", step.getName()); + } + + @Test + public void testGetStepNotExists() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + job.setFlow(flow); + job.afterPropertiesSet(); + + Step step = job.getStep("foo"); + assertNull(step); + } + + @Test + public void testGetStepNotStepState() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(BatchStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + job.setFlow(flow); + job.afterPropertiesSet(); + + Step step = job.getStep("end0"); + assertNull(step); + } + /** * @author Dave Syer * diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/support/SimpleFlowTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/support/SimpleFlowTests.java index fbce6be67..9bec2771f 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/support/SimpleFlowTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/support/SimpleFlowTests.java @@ -17,6 +17,7 @@ package org.springframework.batch.core.job.flow.support; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -215,12 +216,13 @@ public class SimpleFlowTests { assertEquals("step1", state.getName()); } - @Test(expected = IllegalStateException.class) + @Test public void testGetStateDoesNotExist() throws Exception { flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState( "step1")))); flow.afterPropertiesSet(); - flow.getState("bar"); + State state = flow.getState("bar"); + assertNull(state); } private List collect(StateTransition s1, StateTransition s2) { diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java index 2ce7b9c3b..c57d5079f 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/AbstractJobTests.java @@ -142,9 +142,15 @@ public abstract class AbstractJobTests { * if there is no Step with the given name. * * @param stepName + * @return JobExecution */ public JobExecution launchStep(String stepName) { - return getStepRunner().launchStep(this.job.getStep(stepName)); + Step step = this.job.getStep(stepName); + if(step == null) + { + throw new IllegalStateException("No Step found with name: [" + stepName + "]"); + } + return getStepRunner().launchStep(step); } /**