RESOLVED - issue BATCH-1642: test utility class to retrieve value from execution context
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2006-2009 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.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
|
||||
/**
|
||||
* Convenience class for accessing {@link ExecutionContext} values from job and
|
||||
* step executions.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @since 2.1.4
|
||||
*
|
||||
*/
|
||||
public class ExecutionContextTestUtils {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T getFromJob(JobExecution jobExecution, String key) {
|
||||
return (T) jobExecution.getExecutionContext().get(key);
|
||||
}
|
||||
|
||||
static <T> T getFromStep(JobExecution jobExecution, String stepName, String key) {
|
||||
StepExecution stepExecution = null;
|
||||
List<String> stepNames = new ArrayList<String>();
|
||||
for (StepExecution candidate : jobExecution.getStepExecutions()) {
|
||||
String name = candidate.getStepName();
|
||||
stepNames.add(name);
|
||||
if (name.equals(stepName)) {
|
||||
stepExecution = candidate;
|
||||
}
|
||||
}
|
||||
if (stepExecution == null) {
|
||||
throw new IllegalArgumentException("No such step in this job execution: " + stepName + " not in "
|
||||
+ stepNames);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) stepExecution.getExecutionContext().get(key);
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T getFromStep(StepExecution stepExecution, String key) {
|
||||
return (T) stepExecution.getExecutionContext().get(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2006-2009 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
|
||||
public class ExecutionContextTestUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testFromJob() throws Exception {
|
||||
Date date = new Date();
|
||||
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecution();
|
||||
jobExecution.getExecutionContext().put("foo", date);
|
||||
Date result = ExecutionContextTestUtils.getFromJob(jobExecution, "foo");
|
||||
assertEquals(date, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromStepInJob() throws Exception {
|
||||
Date date = new Date();
|
||||
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecutionWithStepExecutions(123L, Arrays.asList("foo", "bar"));
|
||||
StepExecution stepExecution = jobExecution.createStepExecution("spam");
|
||||
stepExecution.getExecutionContext().put("foo", date);
|
||||
Date result = ExecutionContextTestUtils.getFromStep(jobExecution, "spam", "foo");
|
||||
assertEquals(date, result);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testFromStepInJobNoSuchStep() throws Exception {
|
||||
Date date = new Date();
|
||||
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecutionWithStepExecutions(123L, Arrays.asList("foo", "bar"));
|
||||
Date result = ExecutionContextTestUtils.getFromStep(jobExecution, "spam", "foo");
|
||||
assertEquals(date, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromStep() throws Exception {
|
||||
Date date = new Date();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution();
|
||||
stepExecution.getExecutionContext().put("foo", date);
|
||||
Date result = ExecutionContextTestUtils.getFromStep(stepExecution, "foo");
|
||||
assertEquals(date, result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user