From fcb82be654cf77e1f45b1f3de5d5d6b5ec2228d1 Mon Sep 17 00:00:00 2001 From: dsyer Date: Thu, 28 Oct 2010 15:47:52 +0000 Subject: [PATCH] RESOLVED - issue BATCH-1642: test utility class to retrieve value from execution context --- .../batch/test/ExecutionContextTestUtils.java | 65 ++++++++++++++++++ .../test/ExecutionContextTestUtilsTests.java | 66 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 spring-batch-test/src/main/java/org/springframework/batch/test/ExecutionContextTestUtils.java create mode 100644 spring-batch-test/src/test/java/org/springframework/batch/test/ExecutionContextTestUtilsTests.java diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/ExecutionContextTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/ExecutionContextTestUtils.java new file mode 100644 index 000000000..cf7d7f43b --- /dev/null +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/ExecutionContextTestUtils.java @@ -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 getFromJob(JobExecution jobExecution, String key) { + return (T) jobExecution.getExecutionContext().get(key); + } + + static T getFromStep(JobExecution jobExecution, String stepName, String key) { + StepExecution stepExecution = null; + List stepNames = new ArrayList(); + 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 getFromStep(StepExecution stepExecution, String key) { + return (T) stepExecution.getExecutionContext().get(key); + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/ExecutionContextTestUtilsTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/ExecutionContextTestUtilsTests.java new file mode 100644 index 000000000..c2a436613 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/ExecutionContextTestUtilsTests.java @@ -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); + } + +}