diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/InterruptibleTasklet.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/InterruptibleTasklet.java new file mode 100644 index 000000000..65be1b903 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/InterruptibleTasklet.java @@ -0,0 +1,43 @@ +/* + * 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.core.configuration.xml; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.repeat.RepeatStatus; + +/** + * This tasklet will call + * {@link NameStoringTasklet#execute(StepContribution, ChunkContext)} and then + * return CONTINUABLE, so it needs to be interrupted for it to stop. + * + * @author Dave Syer + * @since 2.0 + */ +public class InterruptibleTasklet extends NameStoringTasklet { + + private volatile boolean started = false; + + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { + if (!started) { + super.execute(contribution, chunkContext); + started = true; + } + Thread.sleep(50L); + return RepeatStatus.CONTINUABLE; + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests.java new file mode 100644 index 000000000..84be5fa94 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests.java @@ -0,0 +1,65 @@ +/* + * 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.core.configuration.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Dave Syer + * @since 2.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class SplitInterruptedJobParserTests extends AbstractJobParserTests { + + @Test + public void testSplitInterrupted() throws Exception { + + final JobExecution jobExecution = createJobExecution(); + new Thread(new Runnable() { + public void run() { + job.execute(jobExecution); + } + }).start(); + + Thread.sleep(100L); + jobExecution.setStatus(BatchStatus.STOPPING); + Thread.sleep(200L); + + assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); + assertEquals(ExitStatus.STOPPED, jobExecution.getExitStatus()); + + assertTrue(stepNamesList.contains("stop")); + + StepExecution stepExecution = getStepExecution(jobExecution, "stop"); + assertEquals(BatchStatus.STOPPED, stepExecution.getStatus()); + assertEquals(ExitStatus.STOPPED.getExitCode(), stepExecution.getExitStatus().getExitCode()); + + assertEquals(1, stepNamesList.size()); + + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java index 4f2b3235b..e815b136d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java @@ -252,6 +252,62 @@ public class FlowJobTests { assertEquals(JobInterruptedException.class, jobExecution.getFailureExceptions().get(0).getClass()); } + @Test + public void testInterruptedException() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1") { + @Override + public void execute(StepExecution stepExecution) throws JobInterruptedException { + throw new JobInterruptedException("Stopped"); + } + }), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + job.setFlow(flow); + job.afterPropertiesSet(); + job.execute(jobExecution); + assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); + checkRepository(BatchStatus.STOPPED, ExitStatus.STOPPED); + assertEquals(1, jobExecution.getAllFailureExceptions().size()); + assertEquals(JobInterruptedException.class, jobExecution.getFailureExceptions().get(0).getClass()); + } + + @Test + public void testInterruptedSplitException() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + SimpleFlow flow1 = new SimpleFlow("flow1"); + SimpleFlow flow2 = new SimpleFlow("flow2"); + + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1") { + @Override + public void execute(StepExecution stepExecution) throws JobInterruptedException { + throw new JobInterruptedException("Stopped"); + } + }), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); + flow1.setStateTransitions(new ArrayList(transitions)); + flow1.afterPropertiesSet(); + flow2.setStateTransitions(new ArrayList(transitions)); + flow2.afterPropertiesSet(); + + transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new SplitState(Arrays.asList(flow1, flow2), "split"), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + + job.setFlow(flow); + job.afterPropertiesSet(); + job.execute(jobExecution); + assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); + checkRepository(BatchStatus.STOPPED, ExitStatus.STOPPED); + assertEquals(1, jobExecution.getAllFailureExceptions().size()); + assertEquals(JobInterruptedException.class, jobExecution.getFailureExceptions().get(0).getClass()); + } + @Test public void testEndStateStopped() throws Exception { SimpleFlow flow = new SimpleFlow("job"); diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests-context.xml new file mode 100644 index 000000000..198e2a519 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/SplitInterruptedJobParserTests-context.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +