001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.test;
017
018import java.util.Collection;
019
020import org.springframework.batch.core.JobExecution;
021import org.springframework.batch.core.JobInstance;
022import org.springframework.batch.core.JobParameters;
023import org.springframework.batch.core.StepExecution;
024import org.springframework.batch.core.converter.DefaultJobParametersConverter;
025import org.springframework.batch.item.ExecutionContext;
026import org.springframework.batch.support.PropertiesConverter;
027
028/**
029 * Convenience methods for creating test instances of {@link JobExecution},
030 * {@link JobInstance} and {@link StepExecution}.
031 *
032 * @author Dave Syer
033 *
034 */
035public class MetaDataInstanceFactory {
036
037        /**
038         * The default name for a job ("job")
039         */
040        public static final String DEFAULT_JOB_NAME = "job";
041
042        /**
043         * The default id for a job instance (12L)
044         */
045        public static final long DEFAULT_JOB_INSTANCE_ID = 12L;
046
047        /**
048         * The default id for a job execution (123L)
049         */
050        public static final long DEFAULT_JOB_EXECUTION_ID = 123L;
051
052        /**
053         * The default name for a step ("step")
054         */
055        public static final String DEFAULT_STEP_NAME = "step";
056
057        /**
058         * The default id for a step execution (1234L)
059         */
060        public static final long DEFAULT_STEP_EXECUTION_ID = 1234L;
061
062        /**
063         * Create a {@link JobInstance} with the parameters provided.
064         *
065         * @param jobName the name of the job
066         * @param instanceId the Id of the {@link JobInstance}
067         * @return a {@link JobInstance} with empty {@link JobParameters}
068         */
069        public static JobInstance createJobInstance(String jobName, Long instanceId) {
070                return new JobInstance(instanceId, jobName);
071        }
072
073        /**
074         * Create a {@link JobInstance} with default parameters.
075         *
076         * @return a {@link JobInstance} with name=DEFAULT_JOB_NAME,
077         * id=DEFAULT_JOB_INSTANCE_ID and empty parameters
078         */
079        public static JobInstance createJobInstance() {
080                return new JobInstance(DEFAULT_JOB_INSTANCE_ID, DEFAULT_JOB_NAME);
081        }
082
083        /**
084         * Create a {@link JobExecution} with default parameters.
085         *
086         * @return a {@link JobExecution} with id=DEFAULT_JOB_EXECUTION_ID
087         */
088        public static JobExecution createJobExecution() {
089                return createJobExecution(DEFAULT_JOB_EXECUTION_ID);
090        }
091
092        /**
093         * Create a {@link JobExecution} with the parameters provided.
094         *
095         * @param executionId the id for the {@link JobExecution}
096         * @return a {@link JobExecution} with valid {@link JobInstance}
097         */
098        public static JobExecution createJobExecution(Long executionId) {
099                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, null);
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 jobExecution instance of {@link JobExecution}.
170         * @param stepName the name for the {@link StepExecution}.
171         * @param executionId the id for the {@link StepExecution}.
172         * @return a {@link StepExecution} with the given {@link JobExecution}.
173         */
174        public static StepExecution createStepExecution(JobExecution jobExecution, String stepName, Long executionId) {
175                StepExecution stepExecution = jobExecution.createStepExecution(stepName);
176                stepExecution.setId(executionId);
177                return stepExecution;
178        }
179
180        /**
181         * Create a {@link JobExecution} with the parameters provided with attached
182         * step executions.
183         *
184         * @param executionId the {@link JobExecution} id
185         * @param stepNames the names of the step executions
186         * @return a {@link JobExecution} with step executions as specified, each
187         * with a unique id
188         */
189        public static JobExecution createJobExecutionWithStepExecutions(Long executionId, Collection<String> stepNames) {
190                JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId);
191                Long stepExecutionId = DEFAULT_STEP_EXECUTION_ID;
192                for (String stepName : stepNames) {
193                        createStepExecution(jobExecution, stepName, stepExecutionId);
194                        stepExecutionId++;
195                }
196                return jobExecution;
197        }
198
199        /**
200         * Create a {@link StepExecution} and all its parent entities with default
201         * values, but using the {@link ExecutionContext} and {@link JobParameters}
202         * provided.
203         *
204         * @param jobParameters come {@link JobParameters}
205         * @param executionContext some {@link ExecutionContext}
206         *
207         * @return a {@link StepExecution} with the execution context provided
208         */
209        public static StepExecution createStepExecution(JobParameters jobParameters, ExecutionContext executionContext) {
210                StepExecution stepExecution = createStepExecution(jobParameters);
211                stepExecution.setExecutionContext(executionContext);
212                return stepExecution;
213        }
214
215        /**
216         * Create a {@link StepExecution} and all its parent entities with default
217         * values, but using the {@link JobParameters} provided.
218         *
219         * @param jobParameters some {@link JobParameters}
220         * @return a {@link StepExecution} with the job parameters provided
221         */
222        public static StepExecution createStepExecution(JobParameters jobParameters) {
223                JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID,
224                                DEFAULT_JOB_EXECUTION_ID, jobParameters);
225                return jobExecution.createStepExecution(DEFAULT_STEP_NAME);
226        }
227
228        /**
229         * Create a {@link StepExecution} and all its parent entities with default
230         * values, but using the {@link ExecutionContext} provided.
231         *
232         * @param executionContext some {@link ExecutionContext}
233         * @return a {@link StepExecution} with the execution context provided
234         */
235        public static StepExecution createStepExecution(ExecutionContext executionContext) {
236                StepExecution stepExecution = createStepExecution();
237                stepExecution.setExecutionContext(executionContext);
238                return stepExecution;
239        }
240
241}