3939import org .springframework .batch .item .ItemProvider ;
4040import org .springframework .batch .item .provider .ListItemProvider ;
4141import org .springframework .batch .repeat .ExitStatus ;
42+ import org .springframework .batch .repeat .RepeatContext ;
43+ import org .springframework .batch .repeat .interceptor .RepeatInterceptorAdapter ;
4244import org .springframework .batch .repeat .policy .SimpleCompletionPolicy ;
4345import org .springframework .batch .repeat .support .RepeatTemplate ;
4446import org .springframework .batch .restart .RestartData ;
@@ -58,6 +60,8 @@ public void process(Object data) throws Exception {
5860
5961 private StepConfigurationSupport stepConfiguration ;
6062
63+ private RepeatTemplate template ;
64+
6165 private ItemProvider getProvider (String [] args ) {
6266 return new ListItemProvider (Arrays .asList (args ));
6367 }
@@ -85,8 +89,7 @@ protected void setUp() throws Exception {
8589 stepConfiguration = new SimpleStepConfiguration ();
8690 stepConfiguration .setTasklet (getTasklet (new String [] { "foo" , "bar" ,
8791 "spam" }));
88- // Only process one chunk:
89- RepeatTemplate template = new RepeatTemplate ();
92+ template = new RepeatTemplate ();
9093 template .setCompletionPolicy (new SimpleCompletionPolicy (1 ));
9194 stepExecutor .setStepOperations (template );
9295 // Only process one item:
@@ -110,7 +113,7 @@ public void testStepExecutor() throws Exception {
110113
111114 public void testChunkExecutor () throws Exception {
112115
113- RepeatTemplate template = new RepeatTemplate ();
116+ template = new RepeatTemplate ();
114117
115118 // Only process one item:
116119 template .setCompletionPolicy (new SimpleCompletionPolicy (1 ));
@@ -129,7 +132,7 @@ public void testChunkExecutor() throws Exception {
129132
130133 public void testStepContextInitialized () throws Exception {
131134
132- RepeatTemplate template = new RepeatTemplate ();
135+ template = new RepeatTemplate ();
133136
134137 // Only process one item:
135138 template .setCompletionPolicy (new SimpleCompletionPolicy (1 ));
@@ -159,6 +162,35 @@ public ExitStatus execute() throws Exception {
159162
160163 }
161164
165+ public void testStepContextInitializedBeforeTasklet () throws Exception {
166+
167+ template = new RepeatTemplate ();
168+
169+ // Only process one chunk:
170+ template .setCompletionPolicy (new SimpleCompletionPolicy (1 ));
171+ stepExecutor .setStepOperations (template );
172+
173+ final StepInstance step = new StepInstance (new Long (1 ));
174+ SimpleJobIdentifier jobIdentifier = new SimpleJobIdentifier ("FOO" );
175+ final JobExecution jobExecution = new JobExecution (new JobInstance (
176+ jobIdentifier , new Long (3 )));
177+ final StepExecution stepExecution = new StepExecution (step ,
178+ jobExecution );
179+
180+ template .setInterceptor (new RepeatInterceptorAdapter () {
181+ public void open (RepeatContext context ) {
182+ assertNotNull (StepSynchronizationManager .getContext ()
183+ .getStepExecution ());
184+ assertEquals (stepExecution , StepSynchronizationManager .getContext ()
185+ .getStepExecution ());
186+ }
187+ });
188+
189+ stepExecutor .process (stepConfiguration , stepExecution );
190+ assertEquals (1 , processed .size ());
191+
192+ }
193+
162194 public void testRepository () throws Exception {
163195
164196 SimpleJobRepository repository = new SimpleJobRepository (
0 commit comments