From d09691aad2bdf04731f519fbdced7b798be78540 Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 26 Jan 2009 11:11:59 +0000 Subject: [PATCH] RESOLVED - BATCH-1016: Create listener to promote items from Step ExecutionContext to Job ExecutionContext applied patch and added unit test in core --- .../ExecutionContextPromotionListener.java | 64 +++++++++++++++++++ ...xecutionContextPromotionListenerTests.java | 47 ++++++++++++++ .../src/main/resources/jobs/skipSampleJob.xml | 12 +++- .../sample/common/SkipCheckingListener.java | 2 +- 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java new file mode 100644 index 000000000..515892678 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java @@ -0,0 +1,64 @@ +/* + * Copyright 2006-2007 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.core.listener; + +import java.util.List; + +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +import com.sun.org.apache.xerces.internal.impl.xpath.XPath.Step; + +/** + * This class can be used to automatically promote items from the {@link Step} + * {@link ExecutionContext} to the {@link Job} {@link ExecutionContext} at the + * end of a step. A list of keys should be provided that correspond to the items + * in the {@link Step} {@link ExecutionContext} that should be promoted. + * + * @author Dan Garrette + * @since 2.0 + */ +public class ExecutionContextPromotionListener extends StepExecutionListenerSupport implements InitializingBean { + + private List keys = null; + + /* + * (non-Javadoc) + * + * @see org.springframework.batch.core.domain.StepListener#afterStep(StepExecution + * stepExecution) + */ + public ExitStatus afterStep(StepExecution stepExecution) { + ExecutionContext stepContext = stepExecution.getExecutionContext(); + ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext(); + for (String key : keys) { + jobContext.put(key, stepContext.get(key)); + } + return null; + } + + public void afterPropertiesSet() throws Exception { + Assert.notNull(this.keys, "The 'keys' property must be provided"); + } + + public void setKeys(List keys) { + this.keys = keys; + } +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java new file mode 100644 index 000000000..8802625cd --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java @@ -0,0 +1,47 @@ +package org.springframework.batch.core.listener; + +import static org.junit.Assert.*; + +import java.util.Collections; + +import org.junit.Test; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; +import org.springframework.util.Assert; + +/** + * Tests for {@link ExecutionContextPromotionListener}. + */ +public class ExecutionContextPromotionListenerTests { + + private ExecutionContextPromotionListener tested = new ExecutionContextPromotionListener(); + + private static final String key = "testKey"; + + private static final String value = "testValue"; + + @Test + public void promoteEntryFromStepToJobExectutionContext() throws Exception { + + JobExecution jobExecution = new JobExecution(1L); + StepExecution stepExecution = jobExecution.createStepExecution("step1"); + + Assert.state(jobExecution.getExecutionContext().isEmpty()); + Assert.state(stepExecution.getExecutionContext().isEmpty()); + + stepExecution.getExecutionContext().putString(key, value); + + tested.setKeys(Collections.singletonList(key)); + tested.afterPropertiesSet(); + + tested.afterStep(stepExecution); + + assertEquals(value, jobExecution.getExecutionContext().getString(key)); + } + + @Test(expected = IllegalArgumentException.class) + public void keysMustBeSet() throws Exception { + // didn't set the keys, same as tested.setKeys(null); + tested.afterPropertiesSet(); + } +} diff --git a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml index 2f6496544..39ad3574c 100644 --- a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml @@ -24,6 +24,7 @@ + @@ -33,12 +34,13 @@ - org.springframework.batch.item.validator.ValidationException, + org.springframework.batch.item.validator.ValidationException java.lang.RuntimeException + @@ -58,6 +60,14 @@ + + + + stepName + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/SkipCheckingListener.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/SkipCheckingListener.java index 0dd0aea03..520eff7e3 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/SkipCheckingListener.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/SkipCheckingListener.java @@ -16,6 +16,6 @@ public class SkipCheckingListener implements StepExecutionListener { } public void beforeStep(StepExecution stepExecution) { - stepExecution.getJobExecution().getExecutionContext().put("stepName", stepExecution.getStepName()); + stepExecution.getExecutionContext().put("stepName", stepExecution.getStepName()); } }