diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Step.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Step.java index 6366dbc4b..bff532be8 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Step.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/Step.java @@ -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. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepSupport.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepSupport.java index 2bf8ee0b6..ac79c6696 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepSupport.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepSupport.java @@ -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) * diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java index a675c3ed4..afffd978e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/executor/StepExecutor.java @@ -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); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/StepSupportTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/StepSupportTests.java index b4cf1c847..fbb1497d7 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/StepSupportTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/StepSupportTests.java @@ -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); + } + } + } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/AbstractStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/AbstractStep.java index 10b9d9ca6..af5764f81 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/AbstractStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/AbstractStep.java @@ -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; + } + } \ No newline at end of file diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/RepeatOperationsStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/RepeatOperationsStep.java index 530af6915..aa98c2311 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/RepeatOperationsStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/RepeatOperationsStep.java @@ -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); diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java index 1a9e899d3..c3ea237a0 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java @@ -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); } } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/RepeatOperationsStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/RepeatOperationsStepTests.java index 73cd22c2d..eb8fa03d2 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/RepeatOperationsStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/RepeatOperationsStepTests.java @@ -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()); + } + } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java index feb3c3aa6..576089530 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepConfigurationTests.java @@ -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()); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java deleted file mode 100644 index 3127f6579..000000000 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java +++ /dev/null @@ -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; - } - } - -} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepTests.java new file mode 100644 index 000000000..1c9a0e391 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepTests.java @@ -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 + } + } + +}