Copying and pasting StepLocatorStepFactoryBean from spring-batch-admin and adding a-probably-overkill unit test for it.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package org.springframework.batch.core.step;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
/**
|
||||
* Convenience factory for {@link Step} instances given a {@link StepLocator}.
|
||||
* Most implementations of {@link Job} implement StepLocator, so that can be a
|
||||
* good starting point.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class StepLocatorStepFactoryBean implements FactoryBean<Step> {
|
||||
|
||||
public StepLocator stepLocator;
|
||||
|
||||
public String stepName;
|
||||
|
||||
/**
|
||||
* @param stepLocator
|
||||
*/
|
||||
public void setStepLocator(StepLocator stepLocator) {
|
||||
this.stepLocator = stepLocator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stepName
|
||||
*/
|
||||
public void setStepName(String stepName) {
|
||||
this.stepName = stepName;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see FactoryBean#getObject()
|
||||
*/
|
||||
public Step getObject() throws Exception {
|
||||
return stepLocator.getStep(stepName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell clients that we are a factory for {@link Step} instances.
|
||||
*
|
||||
* @see FactoryBean#getObjectType()
|
||||
*/
|
||||
public Class<? extends Step> getObjectType() {
|
||||
return Step.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always return true as optimization for bean factory.
|
||||
*
|
||||
* @see FactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.springframework.batch.core.step;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobInterruptedException;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for StepLocatorStepFactoryBean.
|
||||
*
|
||||
* @author tvaughan
|
||||
*/
|
||||
public class StepLocatorStepFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testFoo() throws Exception {
|
||||
Step testStep1 = buildTestStep("foo");
|
||||
Step testStep2 = buildTestStep("bar");
|
||||
Step testStep3 = buildTestStep("baz");
|
||||
|
||||
SimpleJob simpleJob = new SimpleJob(); // is a StepLocator
|
||||
simpleJob.addStep(testStep1);
|
||||
simpleJob.addStep(testStep2);
|
||||
simpleJob.addStep(testStep3);
|
||||
|
||||
StepLocatorStepFactoryBean stepLocatorStepFactoryBean = new StepLocatorStepFactoryBean();
|
||||
stepLocatorStepFactoryBean.setStepLocator(simpleJob);
|
||||
stepLocatorStepFactoryBean.setStepName("bar");
|
||||
assertEquals(testStep2, stepLocatorStepFactoryBean.getObject());
|
||||
}
|
||||
|
||||
private Step buildTestStep(final String stepName) {
|
||||
return new Step() {
|
||||
public String getName() {
|
||||
return stepName;
|
||||
}
|
||||
|
||||
public boolean isAllowStartIfComplete() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public int getStartLimit() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public void execute(StepExecution stepExecution) throws JobInterruptedException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetObjectType() {
|
||||
assertTrue((new StepLocatorStepFactoryBean()).getObjectType().isAssignableFrom(Step.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user