|
| 1 | +/* |
| 2 | + * Copyright 2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.batch.core.repository.dao; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.springframework.batch.core.BatchStatus; |
| 22 | +import org.springframework.batch.core.Job; |
| 23 | +import org.springframework.batch.core.JobExecution; |
| 24 | +import org.springframework.batch.core.JobParametersBuilder; |
| 25 | +import org.springframework.batch.core.StepExecution; |
| 26 | +import org.springframework.batch.core.launch.JobLauncher; |
| 27 | +import org.springframework.batch.core.launch.JobOperator; |
| 28 | +import org.springframework.batch.item.ItemWriter; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.test.context.ContextConfiguration; |
| 31 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertTrue; |
| 36 | + |
| 37 | +@ContextConfiguration |
| 38 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 39 | +public class OptimisticLockingFailureTests { |
| 40 | + @Autowired |
| 41 | + private Job job; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private JobLauncher jobLauncher; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private JobOperator jobOperator; |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testAsyncStopOfStartingJob() throws Exception { |
| 51 | + JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder() |
| 52 | + .addLong("test", 1L) |
| 53 | + .toJobParameters()); |
| 54 | + |
| 55 | + jobOperator.stop(jobExecution.getId()); |
| 56 | + |
| 57 | + while(jobExecution.isRunning()) { |
| 58 | + // wait for async launched job to complete execution |
| 59 | + } |
| 60 | + |
| 61 | + int numStepExecutions = jobExecution.getStepExecutions().size(); |
| 62 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 63 | + String stepName = stepExecution.getStepName(); |
| 64 | + BatchStatus stepExecutionStatus = stepExecution.getStatus(); |
| 65 | + BatchStatus jobExecutionStatus = jobExecution.getStatus(); |
| 66 | + |
| 67 | + assertTrue("Should only be one StepExecution but got: " + numStepExecutions, numStepExecutions == 1); |
| 68 | + assertTrue("Step name for execution should be step1 but got: " + stepName, "step1".equals(stepName)); |
| 69 | + assertTrue("Step execution status should be STOPPED but got: " + stepExecutionStatus, stepExecutionStatus.equals(BatchStatus.STOPPED)); |
| 70 | + assertTrue("Job execution status should be STOPPED but got:" + jobExecutionStatus, jobExecutionStatus.equals(BatchStatus.STOPPED)); |
| 71 | + |
| 72 | + JobExecution restartJobExecution = jobLauncher.run(job, new JobParametersBuilder() |
| 73 | + .addLong("test", 1L) |
| 74 | + .toJobParameters()); |
| 75 | + |
| 76 | + while(restartJobExecution.isRunning()) { |
| 77 | + // wait for async launched job to complete execution |
| 78 | + } |
| 79 | + |
| 80 | + int restartNumStepExecutions = restartJobExecution.getStepExecutions().size(); |
| 81 | + assertTrue("Should be two StepExecution's on restart but got: " + restartNumStepExecutions, restartNumStepExecutions == 2); |
| 82 | + |
| 83 | + for(StepExecution restartStepExecution : restartJobExecution.getStepExecutions()) { |
| 84 | + BatchStatus restartStepExecutionStatus = restartStepExecution.getStatus(); |
| 85 | + |
| 86 | + assertTrue("Step execution status should be COMPLETED but got: " + restartStepExecutionStatus, |
| 87 | + restartStepExecutionStatus.equals(BatchStatus.COMPLETED)); |
| 88 | + } |
| 89 | + |
| 90 | + BatchStatus restartJobExecutionStatus = restartJobExecution.getStatus(); |
| 91 | + assertTrue("Job execution status should be COMPLETED but got:" + restartJobExecutionStatus, |
| 92 | + restartJobExecutionStatus.equals(BatchStatus.COMPLETED)); |
| 93 | + } |
| 94 | + |
| 95 | + public static class Writer implements ItemWriter<String> { |
| 96 | + @Override |
| 97 | + public void write(List<? extends String> items) throws Exception { |
| 98 | + for(String item : items) { |
| 99 | + System.out.println(item); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments