1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.test;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.springframework.batch.core.JobExecution;
23 import org.springframework.batch.core.StepExecution;
24 import org.springframework.batch.item.ExecutionContext;
25
26
27
28
29
30
31
32
33
34 public class ExecutionContextTestUtils {
35
36 @SuppressWarnings("unchecked")
37 public static <T> T getValueFromJob(JobExecution jobExecution, String key) {
38 return (T) jobExecution.getExecutionContext().get(key);
39 }
40
41 public static <T> T getValueFromStepInJob(JobExecution jobExecution, String stepName, String key) {
42 StepExecution stepExecution = null;
43 List<String> stepNames = new ArrayList<String>();
44 for (StepExecution candidate : jobExecution.getStepExecutions()) {
45 String name = candidate.getStepName();
46 stepNames.add(name);
47 if (name.equals(stepName)) {
48 stepExecution = candidate;
49 }
50 }
51 if (stepExecution == null) {
52 throw new IllegalArgumentException("No such step in this job execution: " + stepName + " not in "
53 + stepNames);
54 }
55 @SuppressWarnings("unchecked")
56 T result = (T) stepExecution.getExecutionContext().get(key);
57 return result;
58 }
59
60 @SuppressWarnings("unchecked")
61 public static <T> T getValueFromStep(StepExecution stepExecution, String key) {
62 return (T) stepExecution.getExecutionContext().get(key);
63 }
64
65 }