|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2014 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.batch.core.step.builder;
|
17 | 17 |
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
18 | 21 | import org.junit.Test;
|
19 |
| -import org.springframework.batch.core.*; |
20 |
| -import org.springframework.batch.core.annotation.*; |
| 22 | + |
| 23 | +import org.springframework.batch.core.BatchStatus; |
| 24 | +import org.springframework.batch.core.ExitStatus; |
| 25 | +import org.springframework.batch.core.JobParameters; |
| 26 | +import org.springframework.batch.core.StepContribution; |
| 27 | +import org.springframework.batch.core.StepExecution; |
| 28 | +import org.springframework.batch.core.StepExecutionListener; |
| 29 | +import org.springframework.batch.core.annotation.AfterChunk; |
| 30 | +import org.springframework.batch.core.annotation.AfterChunkError; |
| 31 | +import org.springframework.batch.core.annotation.AfterProcess; |
| 32 | +import org.springframework.batch.core.annotation.AfterRead; |
| 33 | +import org.springframework.batch.core.annotation.AfterStep; |
| 34 | +import org.springframework.batch.core.annotation.AfterWrite; |
| 35 | +import org.springframework.batch.core.annotation.BeforeChunk; |
| 36 | +import org.springframework.batch.core.annotation.BeforeProcess; |
| 37 | +import org.springframework.batch.core.annotation.BeforeRead; |
| 38 | +import org.springframework.batch.core.annotation.BeforeStep; |
| 39 | +import org.springframework.batch.core.annotation.BeforeWrite; |
| 40 | +import org.springframework.batch.core.configuration.xml.DummyItemReader; |
21 | 41 | import org.springframework.batch.core.configuration.xml.DummyItemWriter;
|
| 42 | +import org.springframework.batch.core.job.SimpleJob; |
22 | 43 | import org.springframework.batch.core.repository.JobRepository;
|
23 | 44 | import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
|
24 | 45 | import org.springframework.batch.core.scope.context.ChunkContext;
|
|
30 | 51 | import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
31 | 52 | import org.springframework.transaction.PlatformTransactionManager;
|
32 | 53 |
|
33 |
| -import java.util.ArrayList; |
34 |
| -import java.util.List; |
35 |
| - |
36 | 54 | import static org.junit.Assert.assertEquals;
|
37 | 55 |
|
38 | 56 | /**
|
39 | 57 | * @author Dave Syer
|
40 | 58 | * @author Michael Minella
|
| 59 | + * @author Mahmoud Ben Hassine |
41 | 60 | *
|
42 | 61 | */
|
43 | 62 | public class StepBuilderTests {
|
@@ -85,6 +104,90 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
|
85 | 104 | assertEquals(1, InterfaceBasedStepExecutionListener.afterStepCount);
|
86 | 105 | assertEquals(1, AnnotationBasedStepExecutionListener.beforeStepCount);
|
87 | 106 | assertEquals(1, AnnotationBasedStepExecutionListener.afterStepCount);
|
| 107 | + assertEquals(1, AnnotationBasedStepExecutionListener.beforeChunkCount); |
| 108 | + assertEquals(1, AnnotationBasedStepExecutionListener.afterChunkCount); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testAnnotationBasedChunkListenerForTaskletStep() throws Exception { |
| 113 | + JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject(); |
| 114 | + StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step"); |
| 115 | + jobRepository.add(execution); |
| 116 | + PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); |
| 117 | + TaskletStepBuilder builder = new StepBuilder("step") |
| 118 | + .repository(jobRepository) |
| 119 | + .transactionManager(transactionManager) |
| 120 | + .tasklet(new Tasklet() { |
| 121 | + @Override |
| 122 | + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { |
| 123 | + return null; |
| 124 | + } |
| 125 | + }) |
| 126 | + .listener(new AnnotationBasedChunkListener()); |
| 127 | + builder.build().execute(execution); |
| 128 | + assertEquals(BatchStatus.COMPLETED, execution.getStatus()); |
| 129 | + assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount); |
| 130 | + assertEquals(1, AnnotationBasedChunkListener.afterChunkCount); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testAnnotationBasedChunkListenerForSimpleTaskletStep() throws Exception { |
| 135 | + JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject(); |
| 136 | + StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step"); |
| 137 | + jobRepository.add(execution); |
| 138 | + PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); |
| 139 | + SimpleStepBuilder builder = new StepBuilder("step") |
| 140 | + .repository(jobRepository) |
| 141 | + .transactionManager(transactionManager) |
| 142 | + .chunk(5) |
| 143 | + .reader(new DummyItemReader()) |
| 144 | + .writer(new DummyItemWriter()) |
| 145 | + .listener(new AnnotationBasedChunkListener()); |
| 146 | + builder.build().execute(execution); |
| 147 | + assertEquals(BatchStatus.COMPLETED, execution.getStatus()); |
| 148 | + assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount); |
| 149 | + assertEquals(1, AnnotationBasedChunkListener.afterChunkCount); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testAnnotationBasedChunkListenerForFaultTolerantTaskletStep() throws Exception { |
| 154 | + JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject(); |
| 155 | + StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step"); |
| 156 | + jobRepository.add(execution); |
| 157 | + PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); |
| 158 | + SimpleStepBuilder builder = new StepBuilder("step") |
| 159 | + .repository(jobRepository) |
| 160 | + .transactionManager(transactionManager) |
| 161 | + .chunk(5) |
| 162 | + .reader(new DummyItemReader()) |
| 163 | + .writer(new DummyItemWriter()) |
| 164 | + .faultTolerant() |
| 165 | + .listener(new AnnotationBasedChunkListener()); // TODO should this return FaultTolerantStepBuilder? |
| 166 | + builder.build().execute(execution); |
| 167 | + assertEquals(BatchStatus.COMPLETED, execution.getStatus()); |
| 168 | + assertEquals(1, AnnotationBasedChunkListener.beforeChunkCount); |
| 169 | + assertEquals(1, AnnotationBasedChunkListener.afterChunkCount); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testAnnotationBasedChunkListenerForJobStepBuilder() throws Exception { |
| 174 | + JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject(); |
| 175 | + StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution("step"); |
| 176 | + jobRepository.add(execution); |
| 177 | + PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); |
| 178 | + SimpleJob job = new SimpleJob("job"); |
| 179 | + job.setJobRepository(jobRepository); |
| 180 | + JobStepBuilder builder = new StepBuilder("step") |
| 181 | + .repository(jobRepository) |
| 182 | + .transactionManager(transactionManager) |
| 183 | + .job(job) |
| 184 | + .listener(new AnnotationBasedChunkListener()); |
| 185 | + builder.build().execute(execution); |
| 186 | + assertEquals(BatchStatus.COMPLETED, execution.getStatus()); |
| 187 | + |
| 188 | + // it makes no sense to register a ChunkListener on a step which is not of type tasklet, so it should not be invoked |
| 189 | + assertEquals(0, AnnotationBasedChunkListener.beforeChunkCount); |
| 190 | + assertEquals(0, AnnotationBasedChunkListener.afterChunkCount); |
88 | 191 | }
|
89 | 192 |
|
90 | 193 | @Test
|
@@ -221,4 +324,32 @@ public void afterChunk() {
|
221 | 324 | afterChunkCount++;
|
222 | 325 | }
|
223 | 326 | }
|
| 327 | + |
| 328 | + public static class AnnotationBasedChunkListener { |
| 329 | + |
| 330 | + static int beforeChunkCount = 0; |
| 331 | + static int afterChunkCount = 0; |
| 332 | + static int afterChunkErrorCount = 0; |
| 333 | + |
| 334 | + public AnnotationBasedChunkListener() { |
| 335 | + beforeChunkCount = 0; |
| 336 | + afterChunkCount = 0; |
| 337 | + afterChunkErrorCount = 0; |
| 338 | + } |
| 339 | + |
| 340 | + @BeforeChunk |
| 341 | + public void beforeChunk() { |
| 342 | + beforeChunkCount++; |
| 343 | + } |
| 344 | + |
| 345 | + @AfterChunk |
| 346 | + public void afterChunk() { |
| 347 | + afterChunkCount++; |
| 348 | + } |
| 349 | + |
| 350 | + @AfterChunkError |
| 351 | + public void afterChunkError() { |
| 352 | + afterChunkErrorCount++; |
| 353 | + } |
| 354 | + } |
224 | 355 | }
|
0 commit comments