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.
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ public class SimpleJob extends AbstractJob {
|
||||
return step;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No Step found with name: [" + stepName + "]");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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<StateTransition> transitions = new ArrayList<StateTransition>();
|
||||
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<StateTransition> transitions = new ArrayList<StateTransition>();
|
||||
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<StateTransition> transitions = new ArrayList<StateTransition>();
|
||||
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
|
||||
*
|
||||
|
||||
@@ -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<StateTransition> collect(StateTransition s1, StateTransition s2) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user