OPEN - issue BATCH-324: Step as factory for StepExecutor
http://jira.springframework.org/browse/BATCH-324 Consolidate changes and remove unnecessary methods from interfaces
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
package org.springframework.batch.core.domain;
|
||||
|
||||
import org.springframework.batch.core.executor.StepExecutor;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
|
||||
/**
|
||||
* Batch domain interface representing the configuration of a step. As with the
|
||||
@@ -35,11 +34,6 @@ public interface Step {
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* @return the {@link Tasklet} instance to execute for each item processed.
|
||||
*/
|
||||
Tasklet getTasklet();
|
||||
|
||||
/**
|
||||
* @return true if a step that is already marked as complete can be started
|
||||
* again.
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package org.springframework.batch.core.domain;
|
||||
|
||||
import org.springframework.batch.core.executor.StepExecutor;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
|
||||
/**
|
||||
@@ -33,8 +32,6 @@ public class StepSupport implements Step, BeanNameAware {
|
||||
|
||||
private int startLimit = Integer.MAX_VALUE;
|
||||
|
||||
private Tasklet tasklet;
|
||||
|
||||
private boolean allowStartIfComplete;
|
||||
|
||||
private boolean saveRestartData = false;
|
||||
@@ -106,24 +103,6 @@ public class StepSupport implements Step, BeanNameAware {
|
||||
this.startLimit = startLimit;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.core.configuration.StepConfiguration#getTasklet()
|
||||
*/
|
||||
public Tasklet getTasklet() {
|
||||
return this.tasklet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the tasklet.
|
||||
*
|
||||
* @param tasklet the tasklet to set
|
||||
*/
|
||||
public void setTasklet(Tasklet tasklet) {
|
||||
this.tasklet = tasklet;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
||||
@@ -63,11 +63,4 @@ public interface StepExecutor {
|
||||
StepExecution stepExecution) throws StepInterruptedException,
|
||||
BatchCriticalException;
|
||||
|
||||
/**
|
||||
* Apply the configuration by inspecting it to see if it has any relevant
|
||||
* policy information. Should be called before any calls to process.
|
||||
*
|
||||
* @param configuration
|
||||
*/
|
||||
void applyConfiguration(Step configuration);
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.batch.core.configuration;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.domain.StepSupport;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -53,20 +51,6 @@ public class StepSupportTests extends TestCase {
|
||||
assertEquals(10, configuration.getStartLimit());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.StepSupport#getTasklet()}.
|
||||
*/
|
||||
public void testGetTasklet() {
|
||||
assertEquals(null, configuration.getTasklet());
|
||||
Tasklet tasklet = new Tasklet() {
|
||||
public ExitStatus execute() throws Exception {
|
||||
return ExitStatus.FINISHED;
|
||||
}
|
||||
};
|
||||
configuration.setTasklet(tasklet);
|
||||
assertEquals(tasklet, configuration.getTasklet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.domain.StepSupport#isAllowStartIfComplete()}.
|
||||
*/
|
||||
@@ -76,4 +60,17 @@ public class StepSupportTests extends TestCase {
|
||||
assertEquals(true, configuration.isAllowStartIfComplete());
|
||||
}
|
||||
|
||||
public void testUnsuccessfulWrongConfiguration() throws Exception {
|
||||
try {
|
||||
new StepSupport().createStepExecutor();
|
||||
fail("Expected UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
assertTrue(
|
||||
"Error message does not contain SimpleStep: "
|
||||
+ e.getMessage(), e.getMessage().indexOf(
|
||||
"SimpleStep") >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.springframework.batch.core.domain.Step;
|
||||
import org.springframework.batch.core.domain.StepSupport;
|
||||
import org.springframework.batch.core.executor.StepExecutor;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.exception.handler.ExceptionHandler;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -36,9 +37,11 @@ public abstract class AbstractStep extends StepSupport {
|
||||
|
||||
private ExceptionHandler exceptionHandler;
|
||||
|
||||
protected JobRepository jobRepository;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
protected PlatformTransactionManager transactionManager;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
private Tasklet tasklet;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
@@ -113,9 +116,18 @@ public abstract class AbstractStep extends StepSupport {
|
||||
SimpleStepExecutor executor = new SimpleStepExecutor();
|
||||
executor.setRepository(jobRepository);
|
||||
executor.applyConfiguration(this);
|
||||
executor.setTasklet(getTasklet());
|
||||
executor.setTasklet(tasklet);
|
||||
executor.setTransactionManager(transactionManager);
|
||||
return executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the tasklet.
|
||||
*
|
||||
* @param tasklet the tasklet to set
|
||||
*/
|
||||
public void setTasklet(Tasklet tasklet) {
|
||||
this.tasklet = tasklet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,6 @@ package org.springframework.batch.execution.step.simple;
|
||||
import org.springframework.batch.core.domain.Step;
|
||||
import org.springframework.batch.core.executor.StepExecutor;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link Step} implementation that allows full step of the
|
||||
@@ -80,7 +79,7 @@ public class RepeatOperationsStep extends AbstractStep implements RepeatOperatio
|
||||
* @see org.springframework.batch.core.domain.StepSupport#createStepExecutor()
|
||||
*/
|
||||
public StepExecutor createStepExecutor() {
|
||||
Assert.notNull(jobRepository, "JobRepository is mandatory");
|
||||
assertMandatoryProperties();
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) super.createStepExecutor();
|
||||
if (stepOperations != null) {
|
||||
executor.setStepOperations(stepOperations);
|
||||
|
||||
@@ -438,43 +438,27 @@ public class SimpleStepExecutor implements StepExecutor {
|
||||
*
|
||||
* @param step a step
|
||||
*/
|
||||
public void applyConfiguration(Step step) {
|
||||
void applyConfiguration(AbstractStep step) {
|
||||
|
||||
if (step instanceof RepeatOperationsHolder) {
|
||||
|
||||
RepeatOperationsHolder holder = (RepeatOperationsHolder) step;
|
||||
RepeatOperations chunkOperations = holder.getChunkOperations();
|
||||
RepeatOperations stepOperations = holder.getStepOperations();
|
||||
|
||||
if (chunkOperations != null) {
|
||||
setChunkOperations(chunkOperations);
|
||||
}
|
||||
if (stepOperations != null) {
|
||||
setStepOperations(stepOperations);
|
||||
}
|
||||
|
||||
}
|
||||
else if (step instanceof AbstractStep) {
|
||||
|
||||
SimpleStep simpleConfiguation = (SimpleStep) step;
|
||||
if (step instanceof SimpleStep) {
|
||||
SimpleStep simple = (SimpleStep) step;
|
||||
if (this.chunkOperations instanceof RepeatTemplate) {
|
||||
RepeatTemplate template = (RepeatTemplate) this.chunkOperations;
|
||||
template.setCompletionPolicy(new SimpleCompletionPolicy(simpleConfiguation.getCommitInterval()));
|
||||
template.setCompletionPolicy(new SimpleCompletionPolicy(simple.getCommitInterval()));
|
||||
}
|
||||
}
|
||||
|
||||
ExceptionHandler exceptionHandler = simpleConfiguation.getExceptionHandler();
|
||||
ExceptionHandler exceptionHandler = step.getExceptionHandler();
|
||||
|
||||
if (simpleConfiguation.getSkipLimit() > 0 && exceptionHandler == null) {
|
||||
SimpleLimitExceptionHandler handler = new SimpleLimitExceptionHandler();
|
||||
handler.setLimit(simpleConfiguation.getSkipLimit());
|
||||
exceptionHandler = handler;
|
||||
}
|
||||
|
||||
if (this.stepOperations instanceof RepeatTemplate && exceptionHandler != null) {
|
||||
RepeatTemplate template = (RepeatTemplate) this.stepOperations;
|
||||
template.setExceptionHandler(exceptionHandler);
|
||||
}
|
||||
if (step.getSkipLimit() > 0 && exceptionHandler == null) {
|
||||
SimpleLimitExceptionHandler handler = new SimpleLimitExceptionHandler();
|
||||
handler.setLimit(step.getSkipLimit());
|
||||
exceptionHandler = handler;
|
||||
}
|
||||
|
||||
if (this.stepOperations instanceof RepeatTemplate && exceptionHandler != null) {
|
||||
RepeatTemplate template = (RepeatTemplate) this.stepOperations;
|
||||
template.setExceptionHandler(exceptionHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,9 +15,23 @@
|
||||
*/
|
||||
package org.springframework.batch.execution.step.simple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepInstance;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -48,4 +62,69 @@ public class RepeatOperationsStepTests extends TestCase {
|
||||
assertEquals(executor, configuration.getStepOperations());
|
||||
|
||||
}
|
||||
|
||||
public void testSuccessfulRepeatOperationsHolder() throws Exception {
|
||||
RepeatTemplate repeatTemplate = new RepeatTemplate();
|
||||
final List list = new ArrayList();
|
||||
repeatTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void onError(RepeatContext context, Throwable e) {
|
||||
list.add(e);
|
||||
}
|
||||
});
|
||||
repeatTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
RepeatOperationsStep configuration = new RepeatOperationsStep();
|
||||
configuration.setChunkOperations(repeatTemplate);
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
try {
|
||||
executor.process(configuration, stepExecution);
|
||||
fail("Expected RuntimeException");
|
||||
} catch (NullPointerException e) {
|
||||
// expected
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
public void testSuccessfulRepeatOperationsHolderWithStepOperations() throws Exception {
|
||||
RepeatTemplate chunkTemplate = new RepeatTemplate();
|
||||
final List list = new ArrayList();
|
||||
chunkTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void before(RepeatContext context) {
|
||||
list.add(context);
|
||||
}
|
||||
});
|
||||
chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
RepeatTemplate stepTemplate = new RepeatTemplate();
|
||||
final List steps = new ArrayList();
|
||||
stepTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void before(RepeatContext context) {
|
||||
steps.add(context);
|
||||
}
|
||||
});
|
||||
stepTemplate.setCompletionPolicy(new SimpleCompletionPolicy(1));
|
||||
RepeatOperationsStep configuration = new RepeatOperationsStep();
|
||||
configuration.setChunkOperations(chunkTemplate);
|
||||
configuration.setStepOperations(stepTemplate);
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
configuration.setTasklet(new Tasklet() {
|
||||
public ExitStatus execute() throws Exception {
|
||||
return ExitStatus.CONTINUABLE;
|
||||
}
|
||||
});
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
executor.process(configuration, stepExecution);
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, steps.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,17 +20,19 @@ import junit.framework.TestCase;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.exception.handler.DefaultExceptionHandler;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class SimpleStepConfigurationTests extends TestCase {
|
||||
|
||||
SimpleStep configuration = new SimpleStep("foo");
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.SimpleStep#SimpleStepConfiguration()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.SimpleStep#SimpleStepConfiguration()}.
|
||||
*/
|
||||
public void testSimpleStepConfiguration() {
|
||||
assertNotNull(configuration.getName());
|
||||
@@ -39,20 +41,25 @@ public class SimpleStepConfigurationTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.SimpleStep#SimpleStepConfiguration(org.springframework.batch.core.tasklet.Tasklet)}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.SimpleStep#SimpleStepConfiguration(org.springframework.batch.core.tasklet.Tasklet)}.
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testSimpleStepConfigurationTasklet() {
|
||||
public void testSimpleStepConfigurationTasklet() throws Exception {
|
||||
Tasklet tasklet = new Tasklet() {
|
||||
public ExitStatus execute() throws Exception {
|
||||
return ExitStatus.FINISHED;
|
||||
}
|
||||
};
|
||||
configuration = new SimpleStep(tasklet);
|
||||
assertEquals(tasklet, configuration.getTasklet());
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
configuration.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.SimpleStep#getCommitInterval()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.SimpleStep#getCommitInterval()}.
|
||||
*/
|
||||
public void testGetCommitInterval() {
|
||||
assertEquals(1, configuration.getCommitInterval());
|
||||
@@ -61,7 +68,8 @@ public class SimpleStepConfigurationTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.AbstractStep#getExceptionHandler()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.AbstractStep#getExceptionHandler()}.
|
||||
*/
|
||||
public void testGetExceptionHandler() {
|
||||
assertNull(configuration.getExceptionHandler());
|
||||
@@ -70,7 +78,8 @@ public class SimpleStepConfigurationTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.AbstractStep#getExceptionHandler()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.AbstractStep#getExceptionHandler()}.
|
||||
*/
|
||||
public void testSkipLimit() {
|
||||
assertEquals(0, configuration.getSkipLimit());
|
||||
@@ -79,7 +88,8 @@ public class SimpleStepConfigurationTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.AbstractStep#getSkipLimit()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.AbstractStep#getSkipLimit()}.
|
||||
*/
|
||||
public void testGetSkipLimit() {
|
||||
assertEquals(0, configuration.getSkipLimit());
|
||||
@@ -88,7 +98,8 @@ public class SimpleStepConfigurationTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.execution.step.simple.AbstractStep#isSaveRestartData()}.
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.execution.step.simple.AbstractStep#isSaveRestartData()}.
|
||||
*/
|
||||
public void testIsSaveRestartData() {
|
||||
assertEquals(false, configuration.isSaveRestartData());
|
||||
|
||||
@@ -1,204 +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.execution.step.simple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepInstance;
|
||||
import org.springframework.batch.core.domain.StepSupport;
|
||||
import org.springframework.batch.core.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.batch.repeat.exception.handler.ExceptionHandler;
|
||||
import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleStepExecutorFactoryTests extends TestCase {
|
||||
|
||||
public void testSuccessfulStepExecutor() throws Exception {
|
||||
AbstractStep step = new SimpleStep();
|
||||
step.setJobRepository(new JobRepositorySupport());
|
||||
step.setTransactionManager(new ResourcelessTransactionManager());
|
||||
assertNotNull(step.createStepExecutor());
|
||||
}
|
||||
|
||||
public void testSuccessfulExceptionHandler() throws Exception {
|
||||
AbstractStep configuration = new SimpleStep("foo");
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
final List list = new ArrayList();
|
||||
configuration.setExceptionHandler(new ExceptionHandler() {
|
||||
public void handleException(RepeatContext context,
|
||||
Throwable throwable) throws RuntimeException {
|
||||
list.add(throwable);
|
||||
throw new RuntimeException("Oops");
|
||||
}
|
||||
});
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
try {
|
||||
executor.process(configuration, stepExecution);
|
||||
fail("Expected RuntimeException");
|
||||
} catch (NullPointerException e) {
|
||||
throw e;
|
||||
}catch (RuntimeException e) {
|
||||
assertEquals("Oops", e.getMessage());
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
public void testSuccessfulRepeatOperationsHolder() throws Exception {
|
||||
RepeatTemplate repeatTemplate = new RepeatTemplate();
|
||||
final List list = new ArrayList();
|
||||
repeatTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void onError(RepeatContext context, Throwable e) {
|
||||
list.add(e);
|
||||
}
|
||||
});
|
||||
repeatTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
SimpleHolderStepConfiguration configuration = new SimpleHolderStepConfiguration(
|
||||
repeatTemplate);
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
try {
|
||||
executor.process(configuration, stepExecution);
|
||||
fail("Expected RuntimeException");
|
||||
} catch (NullPointerException e) {
|
||||
// expected
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
public void testSuccessfulRepeatOperationsHolderWithStepOperations() throws Exception {
|
||||
RepeatTemplate chunkTemplate = new RepeatTemplate();
|
||||
final List list = new ArrayList();
|
||||
chunkTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void before(RepeatContext context) {
|
||||
list.add(context);
|
||||
}
|
||||
});
|
||||
chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
RepeatTemplate stepTemplate = new RepeatTemplate();
|
||||
final List steps = new ArrayList();
|
||||
stepTemplate.setInterceptor(new RepeatInterceptorAdapter() {
|
||||
public void before(RepeatContext context) {
|
||||
steps.add(context);
|
||||
}
|
||||
});
|
||||
stepTemplate.setCompletionPolicy(new SimpleCompletionPolicy(1));
|
||||
SimpleHolderStepConfiguration configuration = new SimpleHolderStepConfiguration(
|
||||
chunkTemplate, stepTemplate);
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
configuration.setTasklet(new Tasklet() {
|
||||
public ExitStatus execute() throws Exception {
|
||||
return ExitStatus.CONTINUABLE;
|
||||
}
|
||||
});
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
executor.process(configuration, stepExecution);
|
||||
assertEquals(2, list.size());
|
||||
assertEquals(1, steps.size());
|
||||
}
|
||||
|
||||
public void testUnsuccessfulWrongConfiguration() throws Exception {
|
||||
try {
|
||||
new StepSupport().createStepExecutor();
|
||||
fail("Expected UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
assertTrue(
|
||||
"Error message does not contain SimpleStep: "
|
||||
+ e.getMessage(), e.getMessage().indexOf(
|
||||
"SimpleStep") >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void testUnsuccessfulNoJobRepository() throws Exception {
|
||||
try {
|
||||
new SimpleStep().createStepExecutor();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
assertTrue("Error message does not contain JobRepository: "
|
||||
+ e.getMessage(),
|
||||
e.getMessage().indexOf("JobRepository") >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void testMandatoryProperties() throws Exception {
|
||||
try {
|
||||
new SimpleStep().afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleHolderStepConfiguration extends SimpleStep
|
||||
implements RepeatOperationsHolder {
|
||||
private RepeatOperations chunkOperations;
|
||||
private RepeatOperations stepOperations;
|
||||
|
||||
public SimpleHolderStepConfiguration(RepeatOperations operations) {
|
||||
this.chunkOperations = operations;
|
||||
}
|
||||
|
||||
public SimpleHolderStepConfiguration(RepeatOperations chunkOperations, RepeatOperations stepOperations) {
|
||||
this.chunkOperations = chunkOperations;
|
||||
this.stepOperations = stepOperations;
|
||||
}
|
||||
|
||||
public RepeatOperations getChunkOperations() {
|
||||
return chunkOperations;
|
||||
}
|
||||
|
||||
public RepeatOperations getStepOperations() {
|
||||
return stepOperations;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.execution.step.simple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepInstance;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.exception.handler.ExceptionHandler;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleStepTests extends TestCase {
|
||||
|
||||
public void testSuccessfulStepExecutor() throws Exception {
|
||||
SimpleStep step = new SimpleStep();
|
||||
step.setJobRepository(new JobRepositorySupport());
|
||||
step.setTransactionManager(new ResourcelessTransactionManager());
|
||||
assertNotNull(step.createStepExecutor());
|
||||
}
|
||||
|
||||
public void testSuccessfulExceptionHandler() throws Exception {
|
||||
SimpleStep configuration = new SimpleStep("foo");
|
||||
configuration.setJobRepository(new JobRepositorySupport());
|
||||
configuration.setTransactionManager(new ResourcelessTransactionManager());
|
||||
final List list = new ArrayList();
|
||||
configuration.setExceptionHandler(new ExceptionHandler() {
|
||||
public void handleException(RepeatContext context,
|
||||
Throwable throwable) throws RuntimeException {
|
||||
list.add(throwable);
|
||||
throw new RuntimeException("Oops");
|
||||
}
|
||||
});
|
||||
SimpleStepExecutor executor = (SimpleStepExecutor) configuration
|
||||
.createStepExecutor();
|
||||
StepExecution stepExecution = new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(0L), new JobParameters()),
|
||||
new Long(12)));
|
||||
try {
|
||||
executor.process(configuration, stepExecution);
|
||||
fail("Expected RuntimeException");
|
||||
} catch (NullPointerException e) {
|
||||
throw e;
|
||||
}catch (RuntimeException e) {
|
||||
assertEquals("Oops", e.getMessage());
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
public void testUnsuccessfulNoJobRepository() throws Exception {
|
||||
try {
|
||||
new SimpleStep().createStepExecutor();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
assertTrue("Error message does not contain JobRepository: "
|
||||
+ e.getMessage(),
|
||||
e.getMessage().indexOf("JobRepository") >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void testMandatoryProperties() throws Exception {
|
||||
try {
|
||||
new SimpleStep().afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user