RESOLVED - issue BATCH-1481: Support injection of step-scoped dependencies into unit tests
Added StepScopeTestUtils. Revised a few test cases.
This commit is contained in:
@@ -22,6 +22,7 @@ import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
|
||||
/**
|
||||
@@ -33,6 +34,31 @@ import org.springframework.batch.support.PropertiesConverter;
|
||||
*/
|
||||
public class MetaDataInstanceFactory {
|
||||
|
||||
/**
|
||||
* The default name for a job ("job")
|
||||
*/
|
||||
public static final String DEFAULT_JOB_NAME = "job";
|
||||
|
||||
/**
|
||||
* The default id for a job instance (12L)
|
||||
*/
|
||||
public static final long DEFAULT_JOB_INSTANCE_ID = 12L;
|
||||
|
||||
/**
|
||||
* The default id for a job execution (123L)
|
||||
*/
|
||||
public static final long DEFAULT_JOB_EXECUTION_ID = 123L;
|
||||
|
||||
/**
|
||||
* The default name for a step ("step")
|
||||
*/
|
||||
public static final String DEFAULT_STEP_NAME = "step";
|
||||
|
||||
/**
|
||||
* The default id for a step execution (1234L)
|
||||
*/
|
||||
public static final long DEFAULT_STEP_EXECUTION_ID = 1234L;
|
||||
|
||||
/**
|
||||
* Create a {@link JobInstance} with the parameters provided.
|
||||
*
|
||||
@@ -73,20 +99,20 @@ public class MetaDataInstanceFactory {
|
||||
/**
|
||||
* Create a {@link JobInstance} with default parameters.
|
||||
*
|
||||
* @return a {@link JobInstance} with name="job", id=12L and empty
|
||||
* parameters
|
||||
* @return a {@link JobInstance} with name=DEFAULT_JOB_NAME,
|
||||
* id=DEFAULT_JOB_INSTANCE_ID and empty parameters
|
||||
*/
|
||||
public static JobInstance createJobInstance() {
|
||||
return new JobInstance(12L, new JobParameters(), "job");
|
||||
return new JobInstance(DEFAULT_JOB_INSTANCE_ID, new JobParameters(), DEFAULT_JOB_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link JobExecution} with default parameters.
|
||||
*
|
||||
* @return a {@link JobExecution} with id=123L
|
||||
* @return a {@link JobExecution} with id=DEFAULT_JOB_EXECUTION_ID
|
||||
*/
|
||||
public static JobExecution createJobExecution() {
|
||||
return createJobExecution(123L);
|
||||
return createJobExecution(DEFAULT_JOB_EXECUTION_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +122,7 @@ public class MetaDataInstanceFactory {
|
||||
* @return a {@link JobExecution} with valid {@link JobInstance}
|
||||
*/
|
||||
public static JobExecution createJobExecution(Long executionId) {
|
||||
return createJobExecution("job", 12L, executionId);
|
||||
return createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,10 +170,11 @@ public class MetaDataInstanceFactory {
|
||||
/**
|
||||
* Create a {@link StepExecution} with default parameters.
|
||||
*
|
||||
* @return a {@link StepExecution} with stepName="step" and id=1234L
|
||||
* @return a {@link StepExecution} with stepName="step" and
|
||||
* id=DEFAULT_STEP_EXECUTION_ID
|
||||
*/
|
||||
public static StepExecution createStepExecution() {
|
||||
return createStepExecution("step", 1234L);
|
||||
return createStepExecution(DEFAULT_STEP_NAME, DEFAULT_STEP_EXECUTION_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,8 +212,8 @@ public class MetaDataInstanceFactory {
|
||||
* with a unique id
|
||||
*/
|
||||
public static JobExecution createJobExecutionWithStepExecutions(Long executionId, Collection<String> stepNames) {
|
||||
JobExecution jobExecution = createJobExecution("job", 12L, executionId);
|
||||
Long stepExecutionId = 1234L;
|
||||
JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, executionId);
|
||||
Long stepExecutionId = DEFAULT_STEP_EXECUTION_ID;
|
||||
for (String stepName : stepNames) {
|
||||
createStepExecution(jobExecution, stepName, stepExecutionId);
|
||||
stepExecutionId++;
|
||||
@@ -194,4 +221,46 @@ public class MetaDataInstanceFactory {
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StepExecution} and all its parent entities with default
|
||||
* values, but using the {@link ExecutionContext} and {@link JobParameters}
|
||||
* provided.
|
||||
*
|
||||
* @param jobParameters come {@link JobParameters}
|
||||
* @param executionContext some {@link ExecutionContext}
|
||||
*
|
||||
* @return a {@link StepExecution} with the execution context provided
|
||||
*/
|
||||
public static StepExecution createStepExecution(JobParameters jobParameters, ExecutionContext executionContext) {
|
||||
StepExecution stepExecution = createStepExecution(jobParameters);
|
||||
stepExecution.setExecutionContext(executionContext);
|
||||
return stepExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StepExecution} and all its parent entities with default
|
||||
* values, but using the {@link JobParameters} provided.
|
||||
*
|
||||
* @param jobParameters some {@link JobParameters}
|
||||
* @return a {@link StepExecution} with the job parameters provided
|
||||
*/
|
||||
public static StepExecution createStepExecution(JobParameters jobParameters) {
|
||||
JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID,
|
||||
DEFAULT_JOB_EXECUTION_ID, jobParameters);
|
||||
return jobExecution.createStepExecution(DEFAULT_STEP_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StepExecution} and all its parent entities with default
|
||||
* values, but using the {@link ExecutionContext} provided.
|
||||
*
|
||||
* @param executionContext some {@link ExecutionContext}
|
||||
* @return a {@link StepExecution} with the execution context provided
|
||||
*/
|
||||
public static StepExecution createStepExecution(ExecutionContext executionContext) {
|
||||
StepExecution stepExecution = createStepExecution();
|
||||
stepExecution.setExecutionContext(executionContext);
|
||||
return stepExecution;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2006-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.StepScope;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
|
||||
/**
|
||||
* Utility class for creating and manipulating {@link StepScope} in unit tests.
|
||||
* This is useful when you want to use the Spring test support and inject
|
||||
* dependencies into your test case that happen to be step scoped in the
|
||||
* application context.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class StepScopeTestUtils {
|
||||
|
||||
public static <T> T doInStepScope(StepExecution stepExecution, Callable<T> callable) throws Exception {
|
||||
try {
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
return callable.call();
|
||||
}
|
||||
finally {
|
||||
StepSynchronizationManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user