BATCH-1053: Moved getStep() logic from AbstractJobTests to Job implementations. Added getStep(String) to AbstractJob. Added getState(String) to Flow.

This commit is contained in:
dhgarrette
2009-02-02 15:04:20 +00:00
parent 0edb22e4b8
commit ee11f65556
8 changed files with 85 additions and 88 deletions

View File

@@ -125,6 +125,14 @@ public abstract class AbstractJob implements Job, BeanNameAware, InitializingBea
return name;
}
/**
* Retrieve the step with the given name
*
* @param stepName
* @return the step
*/
public abstract Step getStep(String stepName);
/**
* Boolean flag to prevent categorically a job from restarting, even if it
* has failed previously.

View File

@@ -60,11 +60,18 @@ public class SimpleJob extends AbstractJob {
this.steps.add(step);
}
/**
* @return the steps currently set for this job
/*
* (non-Javadoc)
* @see org.springframework.batch.core.job.AbstractJob#getStep(java.lang.String)
*/
public List<Step> getSteps(){
return steps;
public Step getStep(String stepName){
for (Step step : this.steps) {
if(step.getName().equals(stepName))
{
return step;
}
}
throw new IllegalStateException("No Step found with name: [" + stepName + "]");
}
/**

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.batch.core.job.flow;
import org.springframework.batch.core.job.flow.support.State;
/**
* @author Dave Syer
*
@@ -26,6 +28,12 @@ public interface Flow {
*/
String getName();
/**
* @param stateName
* @return the State in the flow with given name
*/
public State getState(String stateName);
/**
* @throws FlowExecutionException
*/

View File

@@ -23,6 +23,8 @@ import org.springframework.batch.core.StartLimitExceededException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.job.flow.support.State;
import org.springframework.batch.core.job.flow.support.state.StepState;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.util.Assert;
@@ -57,11 +59,14 @@ public class FlowJob extends AbstractJob {
this.flow = flow;
}
/**
* @return the flow
/*
* (non-Javadoc)
* @see org.springframework.batch.core.job.AbstractJob#getStep(java.lang.String)
*/
public Flow getFlow(){
return this.flow;
public Step getStep(String stepName){
State state = this.flow.getState(stepName);
Assert.isInstanceOf(StepState.class, state, "State is not a StepState: [" + stepName + "]");
return ((StepState) state).getStep();
}
/**

View File

@@ -30,6 +30,7 @@ import org.springframework.batch.core.job.flow.FlowExecution;
import org.springframework.batch.core.job.flow.FlowExecutionException;
import org.springframework.batch.core.job.flow.FlowExecutor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* A {@link Flow} that branches conditionally depending on the exit status of
@@ -81,12 +82,14 @@ public class SimpleFlow implements Flow, InitializingBean {
this.stateTransitions = stateTransitions;
}
/**
* @param stateName
* @return state with given name, null if not found
/*
* (non-Javadoc)
* @see org.springframework.batch.core.job.flow.Flow#getState(java.lang.String)
*/
public State getState(String stateName) {
return stateMap.get(stateName);
State state = stateMap.get(stateName);
Assert.notNull(state, "No State found with name: [" + stateName + "]");
return state;
}
/**

View File

@@ -25,6 +25,7 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
@@ -170,6 +171,11 @@ public class AbstractJobTests {
return null;
}
@Override
public Step getStep(String stepName) {
return null;
}
}
}

View File

@@ -16,6 +16,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.assertTrue;
import static org.junit.Assert.fail;
@@ -203,6 +204,24 @@ public class SimpleFlowTests {
assertEquals(FlowExecution.COMPLETED, execution.getStatus());
assertEquals("step3", execution.getName());
}
@Test
public void testGetStateExists() throws Exception {
flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState(
"step1"))));
flow.afterPropertiesSet();
State state = flow.getState("step1");
assertNotNull(state);
assertEquals("step1", state.getName());
}
@Test(expected = IllegalArgumentException.class)
public void testGetStateDoesNotExist() throws Exception {
flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState(
"step1"))));
flow.afterPropertiesSet();
flow.getState("bar");
}
private List<StateTransition> collect(StateTransition s1, StateTransition s2) {
List<StateTransition> list = new ArrayList<StateTransition>();