| 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 | * @param jobParameters comma or new line separated name=value pairs |
| 68 | * @return a {@link JobInstance} |
| 69 | */ |
| 70 | public static JobInstance createJobInstance(String jobName, Long instanceId, String jobParameters) { |
| 71 | JobParameters params = new DefaultJobParametersConverter().getJobParameters(PropertiesConverter |
| 72 | .stringToProperties(jobParameters)); |
| 73 | return createJobInstance(jobName, instanceId, params); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create a {@link JobInstance} with the parameters provided. |
| 78 | * |
| 79 | * @param jobName the name of the job |
| 80 | * @param instanceId the Id of the {@link JobInstance} |
| 81 | * @param jobParameters an instance of {@link JobParameters} |
| 82 | * @return a {@link JobInstance} |
| 83 | */ |
| 84 | public static JobInstance createJobInstance(String jobName, Long instanceId, JobParameters jobParameters) { |
| 85 | return new JobInstance(instanceId, jobParameters, jobName); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create a {@link JobInstance} with the parameters provided. |
| 90 | * |
| 91 | * @param jobName the name of the job |
| 92 | * @param instanceId the Id of the {@link JobInstance} |
| 93 | * @return a {@link JobInstance} with empty {@link JobParameters} |
| 94 | */ |
| 95 | public static JobInstance createJobInstance(String jobName, Long instanceId) { |
| 96 | return new JobInstance(instanceId, new JobParameters(), jobName); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Create a {@link JobInstance} with default parameters. |
| 101 | * |
| 102 | * @return a {@link JobInstance} with name=DEFAULT_JOB_NAME, |
| 103 | * id=DEFAULT_JOB_INSTANCE_ID and empty parameters |
| 104 | */ |
| 105 | public static JobInstance createJobInstance() { |
| 106 | return new JobInstance(DEFAULT_JOB_INSTANCE_ID, new JobParameters(), DEFAULT_JOB_NAME); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Create a {@link JobExecution} with default parameters. |
| 111 | * |
| 112 | * @return a {@link JobExecution} with id=DEFAULT_JOB_EXECUTION_ID |
| 113 | */ |
| 114 | public static JobExecution createJobExecution() { |
| 115 | return createJobExecution(DEFAULT_JOB_EXECUTION_ID); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Create a {@link JobExecution} with the parameters provided. |
| 120 | * |
| 121 | * @param executionId the id for the {@link JobExecution} |
| 122 | * @return a {@link JobExecution} with valid {@link JobInstance} |
| 123 | */ |
| 124 | public static JobExecution createJobExecution(Long executionId) { |
| 125 | return createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Create a {@link JobExecution} with the parameters provided. |
| 130 | * |
| 131 | * @param jobName the name of the job |
| 132 | * @param instanceId the id for the {@link JobInstance} |
| 133 | * @param executionId the id for the {@link JobExecution} |
| 134 | * @return a {@link JobExecution} with empty {@link JobParameters} |
| 135 | */ |
| 136 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId) { |
| 137 | return createJobExecution(jobName, instanceId, executionId, new JobParameters()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Create a {@link JobExecution} with the parameters provided. |
| 142 | * |
| 143 | * @param jobName the name of the job |
| 144 | * @param instanceId the Id of the {@link JobInstance} |
| 145 | * @param executionId the id for the {@link JobExecution} |
| 146 | * @param jobParameters comma or new line separated name=value pairs |
| 147 | * @return a {@link JobExecution} |
| 148 | */ |
| 149 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId, |
| 150 | String jobParameters) { |
| 151 | JobParameters params = new DefaultJobParametersConverter().getJobParameters(PropertiesConverter |
| 152 | .stringToProperties(jobParameters)); |
| 153 | return createJobExecution(jobName, instanceId, executionId, params); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Create a {@link JobExecution} with the parameters provided. |
| 158 | * |
| 159 | * @param jobName the name of the job |
| 160 | * @param instanceId the Id of the {@link JobInstance} |
| 161 | * @param executionId the id for the {@link JobExecution} |
| 162 | * @param jobParameters an instance of {@link JobParameters} |
| 163 | * @return a {@link JobExecution} |
| 164 | */ |
| 165 | public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId, |
| 166 | JobParameters jobParameters) { |
| 167 | return new JobExecution(createJobInstance(jobName, instanceId, jobParameters), executionId); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Create a {@link StepExecution} with default parameters. |
| 172 | * |
| 173 | * @return a {@link StepExecution} with stepName="step" and |
| 174 | * id=DEFAULT_STEP_EXECUTION_ID |
| 175 | */ |
| 176 | public static StepExecution createStepExecution() { |
| 177 | return createStepExecution(DEFAULT_STEP_NAME, DEFAULT_STEP_EXECUTION_ID); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Create a {@link StepExecution} with the parameters provided. |
| 182 | * |
| 183 | * @param stepName the stepName for the {@link StepExecution} |
| 184 | * @param executionId the id for the {@link StepExecution} |
| 185 | * @return a {@link StepExecution} with a {@link JobExecution} having |
| 186 | * default properties |
| 187 | */ |
| 188 | public static StepExecution createStepExecution(String stepName, Long executionId) { |
| 189 | return createStepExecution(createJobExecution(), stepName, executionId); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Create a {@link StepExecution} with the parameters provided. |
| 194 | * |
| 195 | * @param stepName the stepName for the {@link StepExecution} |
| 196 | * @param executionId the id for the {@link StepExecution} |
| 197 | * @return a {@link StepExecution} with the given {@link JobExecution} |
| 198 | */ |
| 199 | public static StepExecution createStepExecution(JobExecution jobExecution, String stepName, Long executionId) { |
| 200 | StepExecution stepExecution = jobExecution.createStepExecution(stepName); |
| 201 | stepExecution.setId(executionId); |
| 202 | return stepExecution; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Create a {@link JobExecution} with the parameters provided with attached |
| 207 | * step executions. |
| 208 | * |
| 209 | * @param executionId the {@link JobExecution} id |
| 210 | * @param stepNames the names of the step executions |
| 211 | * @return a {@link JobExecution} with step executions as specified, each |
| 212 | * with a unique id |
| 213 | */ |
| 214 | public static JobExecution createJobExecutionWithStepExecutions(Long executionId, Collection<String> stepNames) { |
| 215 | JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId); |
| 216 | Long stepExecutionId = DEFAULT_STEP_EXECUTION_ID; |
| 217 | for (String stepName : stepNames) { |
| 218 | createStepExecution(jobExecution, stepName, stepExecutionId); |
| 219 | stepExecutionId++; |
| 220 | } |
| 221 | return jobExecution; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Create a {@link StepExecution} and all its parent entities with default |
| 226 | * values, but using the {@link ExecutionContext} and {@link JobParameters} |
| 227 | * provided. |
| 228 | * |
| 229 | * @param jobParameters come {@link JobParameters} |
| 230 | * @param executionContext some {@link ExecutionContext} |
| 231 | * |
| 232 | * @return a {@link StepExecution} with the execution context provided |
| 233 | */ |
| 234 | public static StepExecution createStepExecution(JobParameters jobParameters, ExecutionContext executionContext) { |
| 235 | StepExecution stepExecution = createStepExecution(jobParameters); |
| 236 | stepExecution.setExecutionContext(executionContext); |
| 237 | return stepExecution; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Create a {@link StepExecution} and all its parent entities with default |
| 242 | * values, but using the {@link JobParameters} provided. |
| 243 | * |
| 244 | * @param jobParameters some {@link JobParameters} |
| 245 | * @return a {@link StepExecution} with the job parameters provided |
| 246 | */ |
| 247 | public static StepExecution createStepExecution(JobParameters jobParameters) { |
| 248 | JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, |
| 249 | DEFAULT_JOB_EXECUTION_ID, jobParameters); |
| 250 | return jobExecution.createStepExecution(DEFAULT_STEP_NAME); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Create a {@link StepExecution} and all its parent entities with default |
| 255 | * values, but using the {@link ExecutionContext} provided. |
| 256 | * |
| 257 | * @param executionContext some {@link ExecutionContext} |
| 258 | * @return a {@link StepExecution} with the execution context provided |
| 259 | */ |
| 260 | public static StepExecution createStepExecution(ExecutionContext executionContext) { |
| 261 | StepExecution stepExecution = createStepExecution(); |
| 262 | stepExecution.setExecutionContext(executionContext); |
| 263 | return stepExecution; |
| 264 | } |
| 265 | |
| 266 | } |