1 | /* |
2 | * Copyright 2006-2007 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 | package org.springframework.batch.test; |
17 | |
18 | import java.util.Collection; |
19 | |
20 | import org.springframework.batch.core.JobExecution; |
21 | import org.springframework.batch.core.JobInstance; |
22 | import org.springframework.batch.core.JobParameters; |
23 | import org.springframework.batch.core.StepExecution; |
24 | import org.springframework.batch.core.converter.DefaultJobParametersConverter; |
25 | import org.springframework.batch.item.ExecutionContext; |
26 | import org.springframework.batch.support.PropertiesConverter; |
27 | |
28 | /** |
29 | * Convenience methods for creating test instances of {@link JobExecution}, |
30 | * {@link JobInstance} and {@link StepExecution}. |
31 | * |
32 | * @author Dave Syer |
33 | * |
34 | */ |
35 | public class MetaDataInstanceFactory { |
36 | |
37 | /** |
38 | * The default name for a job ("job") |
39 | */ |
40 | public static final String DEFAULT_JOB_NAME = "job"; |
41 | |
42 | /** |
43 | * The default id for a job instance (12L) |
44 | */ |
45 | public static final long DEFAULT_JOB_INSTANCE_ID = 12L; |
46 | |
47 | /** |
48 | * The default id for a job execution (123L) |
49 | */ |
50 | public static final long DEFAULT_JOB_EXECUTION_ID = 123L; |
51 | |
52 | /** |
53 | * The default name for a step ("step") |
54 | */ |
55 | public static final String DEFAULT_STEP_NAME = "step"; |
56 | |
57 | /** |
58 | * The default id for a step execution (1234L) |
59 | */ |
60 | public static final long DEFAULT_STEP_EXECUTION_ID = 1234L; |
61 | |
62 | /** |
63 | * Create a {@link JobInstance} with the parameters provided. |
64 | * |
65 | * @param jobName the name of the job |
66 | * @param instanceId the Id of the {@link JobInstance} |
67 | * @return a {@link JobInstance} with empty {@link JobParameters} |
68 | */ |
69 | public static JobInstance createJobInstance(String jobName, Long instanceId) { |
70 | return new JobInstance(instanceId, jobName); |
71 | } |
72 | |
73 | /** |
74 | * Create a {@link JobInstance} with default parameters. |
75 | * |
76 | * @return a {@link JobInstance} with name=DEFAULT_JOB_NAME, |
77 | * id=DEFAULT_JOB_INSTANCE_ID and empty parameters |
78 | */ |
79 | public static JobInstance createJobInstance() { |
80 | return new JobInstance(DEFAULT_JOB_INSTANCE_ID, DEFAULT_JOB_NAME); |
81 | } |
82 | |
83 | /** |
84 | * Create a {@link JobExecution} with default parameters. |
85 | * |
86 | * @return a {@link JobExecution} with id=DEFAULT_JOB_EXECUTION_ID |
87 | */ |
88 | public static JobExecution createJobExecution() { |
89 | return createJobExecution(DEFAULT_JOB_EXECUTION_ID); |
90 | } |
91 | |
92 | /** |
93 | * Create a {@link JobExecution} with the parameters provided. |
94 | * |
95 | * @param executionId the id for the {@link JobExecution} |
96 | * @return a {@link JobExecution} with valid {@link JobInstance} |
97 | */ |
98 | public static JobExecution createJobExecution(Long executionId) { |
99 | return createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId); |
100 | } |
101 | |
102 | /** |
103 | * Create a {@link JobExecution} with the parameters provided. |
104 | * |
105 | * @param jobName the name of the job |
106 | * @param instanceId the id for the {@link JobInstance} |
107 | * @param executionId the id for the {@link JobExecution} |
108 | * @return a {@link JobExecution} with empty {@link JobParameters} |
109 | */ |
110 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId) { |
111 | return createJobExecution(jobName, instanceId, executionId, new JobParameters()); |
112 | } |
113 | |
114 | /** |
115 | * Create a {@link JobExecution} with the parameters provided. |
116 | * |
117 | * @param jobName the name of the job |
118 | * @param instanceId the Id of the {@link JobInstance} |
119 | * @param executionId the id for the {@link JobExecution} |
120 | * @param jobParameters comma or new line separated name=value pairs |
121 | * @return a {@link JobExecution} |
122 | */ |
123 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId, |
124 | String jobParameters) { |
125 | JobParameters params = new DefaultJobParametersConverter().getJobParameters(PropertiesConverter |
126 | .stringToProperties(jobParameters)); |
127 | return createJobExecution(jobName, instanceId, executionId, params); |
128 | } |
129 | |
130 | /** |
131 | * Create a {@link JobExecution} with the parameters provided. |
132 | * |
133 | * @param jobName the name of the job |
134 | * @param instanceId the Id of the {@link JobInstance} |
135 | * @param executionId the id for the {@link JobExecution} |
136 | * @param jobParameters an instance of {@link JobParameters} |
137 | * @return a {@link JobExecution} |
138 | */ |
139 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId, |
140 | JobParameters jobParameters) { |
141 | return new JobExecution(createJobInstance(jobName, instanceId), executionId, jobParameters); |
142 | } |
143 | |
144 | /** |
145 | * Create a {@link StepExecution} with default parameters. |
146 | * |
147 | * @return a {@link StepExecution} with stepName="step" and |
148 | * id=DEFAULT_STEP_EXECUTION_ID |
149 | */ |
150 | public static StepExecution createStepExecution() { |
151 | return createStepExecution(DEFAULT_STEP_NAME, DEFAULT_STEP_EXECUTION_ID); |
152 | } |
153 | |
154 | /** |
155 | * Create a {@link StepExecution} with the parameters provided. |
156 | * |
157 | * @param stepName the stepName for the {@link StepExecution} |
158 | * @param executionId the id for the {@link StepExecution} |
159 | * @return a {@link StepExecution} with a {@link JobExecution} having |
160 | * default properties |
161 | */ |
162 | public static StepExecution createStepExecution(String stepName, Long executionId) { |
163 | return createStepExecution(createJobExecution(), stepName, executionId); |
164 | } |
165 | |
166 | /** |
167 | * Create a {@link StepExecution} with the parameters provided. |
168 | * |
169 | * @param stepName the stepName for the {@link StepExecution} |
170 | * @param executionId the id for the {@link StepExecution} |
171 | * @return a {@link StepExecution} with the given {@link JobExecution} |
172 | */ |
173 | public static StepExecution createStepExecution(JobExecution jobExecution, String stepName, Long executionId) { |
174 | StepExecution stepExecution = jobExecution.createStepExecution(stepName); |
175 | stepExecution.setId(executionId); |
176 | return stepExecution; |
177 | } |
178 | |
179 | /** |
180 | * Create a {@link JobExecution} with the parameters provided with attached |
181 | * step executions. |
182 | * |
183 | * @param executionId the {@link JobExecution} id |
184 | * @param stepNames the names of the step executions |
185 | * @return a {@link JobExecution} with step executions as specified, each |
186 | * with a unique id |
187 | */ |
188 | public static JobExecution createJobExecutionWithStepExecutions(Long executionId, Collection<String> stepNames) { |
189 | JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId); |
190 | Long stepExecutionId = DEFAULT_STEP_EXECUTION_ID; |
191 | for (String stepName : stepNames) { |
192 | createStepExecution(jobExecution, stepName, stepExecutionId); |
193 | stepExecutionId++; |
194 | } |
195 | return jobExecution; |
196 | } |
197 | |
198 | /** |
199 | * Create a {@link StepExecution} and all its parent entities with default |
200 | * values, but using the {@link ExecutionContext} and {@link JobParameters} |
201 | * provided. |
202 | * |
203 | * @param jobParameters come {@link JobParameters} |
204 | * @param executionContext some {@link ExecutionContext} |
205 | * |
206 | * @return a {@link StepExecution} with the execution context provided |
207 | */ |
208 | public static StepExecution createStepExecution(JobParameters jobParameters, ExecutionContext executionContext) { |
209 | StepExecution stepExecution = createStepExecution(jobParameters); |
210 | stepExecution.setExecutionContext(executionContext); |
211 | return stepExecution; |
212 | } |
213 | |
214 | /** |
215 | * Create a {@link StepExecution} and all its parent entities with default |
216 | * values, but using the {@link JobParameters} provided. |
217 | * |
218 | * @param jobParameters some {@link JobParameters} |
219 | * @return a {@link StepExecution} with the job parameters provided |
220 | */ |
221 | public static StepExecution createStepExecution(JobParameters jobParameters) { |
222 | JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, |
223 | DEFAULT_JOB_EXECUTION_ID, jobParameters); |
224 | return jobExecution.createStepExecution(DEFAULT_STEP_NAME); |
225 | } |
226 | |
227 | /** |
228 | * Create a {@link StepExecution} and all its parent entities with default |
229 | * values, but using the {@link ExecutionContext} provided. |
230 | * |
231 | * @param executionContext some {@link ExecutionContext} |
232 | * @return a {@link StepExecution} with the execution context provided |
233 | */ |
234 | public static StepExecution createStepExecution(ExecutionContext executionContext) { |
235 | StepExecution stepExecution = createStepExecution(); |
236 | stepExecution.setExecutionContext(executionContext); |
237 | return stepExecution; |
238 | } |
239 | |
240 | } |