IN PROGRESS - issue BATCH-282: Make input parameters easier to access from ItemReaders, etc.

Implemented late binding to StepContext properties using a homegrown proxy TargetSource.  Can be upgraded or dynamically switched to Spring 3.0 EL features when and if they are available.
This commit is contained in:
dsyer
2008-11-14 16:50:49 +00:00
parent 9b415455a0
commit 0ab5464e4d
26 changed files with 1715 additions and 34 deletions

View File

@@ -0,0 +1,106 @@
package org.springframework.batch.core.scope;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AsyncStepScopeIntegrationTests implements BeanFactoryAware {
private Log logger = LogFactory.getLog(getClass());
@Autowired
@Qualifier("simple")
private Collaborator simple;
private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
private ListableBeanFactory beanFactory;
private int beanCount;
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ListableBeanFactory) beanFactory;
}
@Before
public void countBeans() {
beanCount = beanFactory.getBeanDefinitionCount();
}
@After
public void cleanUp() {
StepSynchronizationManager.close();
// Check that all temporary bean definitions are cleaned up
assertEquals(beanCount, beanFactory.getBeanDefinitionCount());
}
@Test
public void testSimpleProperty() throws Exception {
StepExecution stepExecution = new StepExecution("step", new JobExecution(0L), 123L);
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("foo", "bar");
StepSynchronizationManager.register(stepExecution);
assertEquals("bar", simple.getName());
}
@Test
public void testGetMultiple() throws Exception {
List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
for (int i = 0; i < 12; i++) {
final String value = "foo" + i;
final Long id = 123L+i;
FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
public String call() throws Exception {
StepExecution stepExecution = new StepExecution(value, new JobExecution(0L), id);
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("foo", value);
StepContext context = StepSynchronizationManager.register(stepExecution);
logger.debug("Registered: "+context.getStepExecutionContext());
try {
return simple.getName();
}
finally {
StepSynchronizationManager.close();
}
}
});
tasks.add(task);
taskExecutor.execute(task);
}
int i = 0;
for (FutureTask<String> task : tasks) {
assertEquals("foo" + i, task.get());
i++;
}
}
}

View File

@@ -2,6 +2,8 @@ package org.springframework.batch.core.scope;
public interface Collaborator {
public abstract String getName();
String getName();
Collaborator getParent();
}

View File

@@ -26,7 +26,11 @@ import java.util.List;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
/**
* @author Dave Syer
@@ -36,7 +40,7 @@ public class StepContextTests {
private List<String> list = new ArrayList<String>();
private StepExecution stepExecution = new StepExecution("step", new JobExecution(0L));
private StepExecution stepExecution = new StepExecution("step", new JobExecution(0L), 1L);
private StepContext context = new StepContext(stepExecution);
@@ -128,4 +132,37 @@ public class StepContextTests {
assertTrue(list.contains("spam"));
}
@Test
public void testStepExecutionContext() throws Exception {
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("foo", "bar");
assertEquals("bar", context.getStepExecutionContext().get("foo"));
}
@Test
public void testJobExecutionContext() throws Exception {
ExecutionContext executionContext = stepExecution.getJobExecution().getExecutionContext();
executionContext.put("foo", "bar");
assertEquals("bar", context.getJobExecutionContext().get("foo"));
}
@Test
public void testJobParameters() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
JobInstance jobInstance = new JobInstance(0L, jobParameters, "foo");
stepExecution.getJobExecution().setJobInstance(jobInstance);
assertEquals("bar", context.getJobParameters().get("foo"));
}
@Test
public void testContextId() throws Exception {
assertEquals("execution#1", context.getId());
}
@Test(expected = IllegalStateException.class)
public void testIllegalContextId() throws Exception {
context = new StepContext(new StepExecution("foo", new JobExecution(0L)));
context.getId();
}
}

View File

@@ -48,14 +48,14 @@ public class StepScopeIntegrationTests {
@Test
public void testScopeCreation() throws Exception {
vanilla.execute(new StepExecution("foo",new JobExecution(11L)));
vanilla.execute(new StepExecution("foo",new JobExecution(11L),12L));
assertNotNull(TestStep.getContext());
assertNull(StepSynchronizationManager.getContext());
}
@Test
public void testScopedProxy() throws Exception {
proxied.execute(new StepExecution("foo",new JobExecution(11L)));
proxied.execute(new StepExecution("foo",new JobExecution(11L),31L));
assertTrue(TestStep.getContext().attributeNames().length>0);
String collaborator = (String) TestStep.getContext().getAttribute("collaborator");
assertNotNull(collaborator);
@@ -64,7 +64,7 @@ public class StepScopeIntegrationTests {
@Test
public void testExecutionContext() throws Exception {
StepExecution stepExecution = new StepExecution("foo",new JobExecution(11L));
StepExecution stepExecution = new StepExecution("foo",new JobExecution(11L), 1L);
ExecutionContext executionContext = new ExecutionContext();
executionContext.put("name", "spam");
stepExecution.setExecutionContext(executionContext);
@@ -77,7 +77,7 @@ public class StepScopeIntegrationTests {
@Test
public void testScopedProxyForReference() throws Exception {
enhanced.execute(new StepExecution("foo",new JobExecution(11L)));
enhanced.execute(new StepExecution("foo",new JobExecution(11L),123L));
assertTrue(TestStep.getContext().attributeNames().length>0);
String collaborator = (String) TestStep.getContext().getAttribute("collaborator");
assertNotNull(collaborator);
@@ -86,7 +86,7 @@ public class StepScopeIntegrationTests {
@Test
public void testScopedProxyForSecondReference() throws Exception {
doubleEnhanced.execute(new StepExecution("foo",new JobExecution(11L)));
doubleEnhanced.execute(new StepExecution("foo",new JobExecution(11L),321L));
assertTrue(TestStep.getContext().attributeNames().length>0);
String collaborator = (String) TestStep.getContext().getAttribute("collaborator");
assertNotNull(collaborator);

View File

@@ -0,0 +1,99 @@
package org.springframework.batch.core.scope;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopePlaceholderIntegrationTests implements BeanFactoryAware {
@Autowired
@Qualifier("simple")
private Collaborator simple;
@Autowired
@Qualifier("compound")
private Collaborator compound;
@Autowired
@Qualifier("value")
private Collaborator value;
@Autowired
@Qualifier("ref")
private Collaborator ref;
@Autowired
@Qualifier("bar")
private Collaborator bar;
private StepExecution stepExecution;
private ListableBeanFactory beanFactory;
private int beanCount;
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ListableBeanFactory) beanFactory;
}
@Before
public void start() {
StepSynchronizationManager.close();
stepExecution = new StepExecution("foo", new JobExecution(11L), 123L);
ExecutionContext executionContext = new ExecutionContext();
executionContext.put("foo", "bar");
executionContext.put("parent", bar);
stepExecution.setExecutionContext(executionContext);
StepSynchronizationManager.register(stepExecution);
beanCount = beanFactory.getBeanDefinitionCount();
}
@After
public void cleanUp() {
StepSynchronizationManager.close();
// Check that all temporary bean definitions are cleaned up
assertEquals(beanCount, beanFactory.getBeanDefinitionCount());
}
@Test
public void testSimpleProperty() throws Exception {
assertEquals("bar", simple.getName());
}
@Test
public void testCompoundProperty() throws Exception {
assertEquals("bar-bar", compound.getName());
}
@Test
public void testParentByRef() throws Exception {
assertEquals("bar", ref.getParent().getName());
}
@Test
public void testParentByValue() throws Exception {
assertEquals("bar", value.getParent().getName());
}
}

View File

@@ -42,7 +42,7 @@ public class StepScopeTests {
private StepScope scope = new StepScope();
private StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L));
private StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L), 123L);
private StepContext context;
@@ -126,13 +126,6 @@ public class StepScopeTests {
assertNotNull(id);
}
@Test
public void testGetConversationIdFromAttribute() {
context.setAttribute(StepScope.ID_KEY, "foo");
String id = scope.getConversationId();
assertEquals("foo", id);
}
@Test
public void testRegisterDestructionCallback() {
final List<String> list = new ArrayList<String>();

View File

@@ -1,9 +1,21 @@
package org.springframework.batch.core.scope;
import java.io.Serializable;
public class TestCollaborator implements Collaborator {
public class TestCollaborator implements Collaborator, Serializable {
private String name;
private Collaborator parent;
public Collaborator getParent() {
return parent;
}
public void setParent(Collaborator parent) {
this.parent = parent;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.scope.Collaborator#getName()

View File

@@ -0,0 +1,143 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AsyncPlaceholderTargetSourceTests implements BeanFactoryAware {
private ThreadLocal<Map<String, String>> attributes = new ThreadLocal<Map<String, String>>();
public Map<String, String> getAttributes() {
return attributes.get();
}
@Autowired
private Node simple;
@Autowired
private SimpleContextFactory contextFactory;
private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
private ListableBeanFactory beanFactory;
private int beanCount;
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ListableBeanFactory) beanFactory;
}
@After
public void removeContext() {
contextFactory.clearContext();
attributes.set(null);
// Check that all temporary bean definitions are cleaned up
assertEquals(beanCount, beanFactory.getBeanDefinitionCount());
}
@Before
public void setUpContext() {
contextFactory.setContext(this);
beanCount = beanFactory.getBeanDefinitionCount();
}
@Test
public void testGetSimple() {
attributes.set(Collections.singletonMap("foo", "bar"));
assertEquals("bar", simple.getName());
}
@Test
public void testGetMultiple() throws Exception {
List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
for (int i = 0; i < 12; i++) {
final String value = "foo" + i;
FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
public String call() throws Exception {
attributes.set(Collections.singletonMap("foo", value));
try {
return simple.getName();
}
finally {
attributes.set(null);
}
}
});
tasks.add(task);
taskExecutor.execute(task);
}
int i = 0;
for (FutureTask<String> task : tasks) {
assertEquals("foo" + i, task.get());
i++;
}
}
public static class SimpleContextFactory extends ContextFactorySupport {
private Object root;
public Object getContext() {
return root;
}
public void setContext(Object root) {
this.root = root;
}
public void clearContext() {
root = null;
}
}
public static interface Node {
String getName();
}
public static class Foo implements Node {
private String name;
private Log logger = LogFactory.getLog(getClass());
public String getName() {
return name;
}
public void setName(String name) {
logger.debug("Setting name: " + name);
this.name = name;
}
}
}

View File

@@ -0,0 +1,25 @@
package org.springframework.batch.core.scope.util;
public class ContextFactorySupport implements ContextFactory {
private int count = 0;
/**
* Returns this. Override for more sensible behaviour.
*
* @see org.springframework.batch.core.scope.util.ContextFactory#getContext()
*/
public Object getContext() {
return this;
}
/**
* Returns the context plus a counter, so each call is unique.
*
* @see org.springframework.batch.core.scope.util.ContextFactory#getContextId()
*/
public String getContextId() {
return getContext()+"#"+(count ++);
}
}

View File

@@ -0,0 +1,148 @@
/*
* 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.scope.util;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class MultipleContextPlaceholderTargetSourceTests {
private Map<String, String> attributes;
public Map<String, String> getAttributes() {
return attributes;
}
@Autowired
private SimpleContextFactory contextFactory;
@Autowired
@Qualifier("simple")
private TestBean simple;
@Autowired
@Qualifier("list")
private TestBean list;
@After
public void removeContext() {
contextFactory.clearContext();
}
@Before
public void setUpContext() {
contextFactory.setContext(this);
}
@Test
public void testValueFromProperties() throws Exception {
attributes = Collections.singletonMap("foo", "bar");
assertEquals("bar", simple.getName());
}
@Test
public void testMultipleValueFromProperties() throws Exception {
for (int i = 0; i < 4; i++) {
final String value = "foo" + i;
attributes = Collections.singletonMap("foo", value);
assertEquals("foo" + i, simple.getName());
}
}
@Test
public void testMultipleValueInList() throws Exception {
for (int i = 0; i < 4; i++) {
final String value = "foo" + i;
contextFactory.setContext(this);
attributes = Collections.singletonMap("foo", value);
try {
assertEquals("foo" + i, list.getNames().get(0));
}
finally {
contextFactory.clearContext();
}
}
}
@Override
public String toString() {
return "Test context: attributes=" + attributes;
}
public static class TestBean {
private String name;
private List<String> names = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getNames() {
return new ArrayList<String>(names);
}
public void setNames(List<String> names) {
this.names.addAll(names);
}
}
public static class SimpleContextFactory extends ContextFactorySupport {
private Object root;
public Object getContext() {
return root;
}
public void setContext(Object root) {
this.root = root;
}
public void clearContext() {
root = null;
}
}
}

View File

@@ -0,0 +1,183 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PlaceholderTargetSourceTests extends ContextFactorySupport {
@Autowired
@Qualifier("vanilla")
private PlaceholderTargetSource vanilla;
@Autowired
@Qualifier("simple")
private PlaceholderTargetSource simple;
@Autowired
@Qualifier("withLong")
private PlaceholderTargetSource withLong;
@Autowired
@Qualifier("withInteger")
private PlaceholderTargetSource withInteger;
@Autowired
@Qualifier("withDate")
private PlaceholderTargetSource withDate;
@Autowired
@Qualifier("compound")
private PlaceholderTargetSource compound;
@Autowired
@Qualifier("ref")
private PlaceholderTargetSource ref;
@Autowired
@Qualifier("value")
private PlaceholderTargetSource value;
private Map<String, Object> map = Collections.singletonMap("foo.foo", (Object) "bar");
private Date date = new Date();
public Object getContext() {
return this;
}
public String getFoo() {
return "bar";
}
public Map<String, Object> getMap() {
return map;
}
public Node getParent() {
return new Foo("spam");
}
public Long getLong() {
return 12345678912345L;
}
public Integer getInteger() {
return 4321;
}
public Date getDate() {
return date;
}
@Test
public void testAfterPropertiesSet() throws Exception {
PlaceholderTargetSource targetSource = new PlaceholderTargetSource();
try {
targetSource.afterPropertiesSet();
fail("Axpected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
// expected
}
}
@Test
public void testGetVanilla() {
Node target = (Node) vanilla.getTarget();
assertEquals("foo", target.getName());
}
@Test
public void testGetSimple() {
Node target = (Node) simple.getTarget();
assertEquals("bar", target.getName());
}
@Test
public void testGetCompound() {
Node target = (Node) compound.getTarget();
assertEquals("bar-bar", target.getName());
}
@Test
public void testGetRef() {
Node target = (Node) ref.getTarget();
assertEquals("foo", target.getParent().getName());
}
@Test
public void testGetValue() {
Node target = (Node) value.getTarget();
assertEquals("spam", target.getParent().getName());
}
@Test
public void testGetLong() {
Node target = (Node) withLong.getTarget();
assertEquals("bar-12345678912345", target.getName());
}
@Test
public void testGetInteger() {
Node target = (Node) withInteger.getTarget();
assertEquals("bar-4321", target.getName());
}
@Test
public void testGetDate() {
Node target = (Node) withDate.getTarget();
// Remains unconverted because Spring cannot convert from Date to String
assertEquals("bar-#{date}", target.getName());
}
public static interface Node {
String getName();
Node getParent();
}
public static class Foo implements Node {
private String name;
private Node parent;
public Foo() {
}
public Foo(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
}
}

View File

@@ -0,0 +1,70 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class SimplePlaceholderTargetSourceTests {
@Autowired
@Qualifier("simple")
private Node simple;
@Autowired
private SimpleContextFactory contextFactory;
@Test
public void testGetSimple() {
contextFactory.set("bar");
assertEquals("bar", simple.getName());
contextFactory.clear();
}
public static class SimpleContextFactory extends ContextFactorySupport {
private ThreadLocal<String> fooHolder = new ThreadLocal<String>();
public Object getContext() {
return this;
}
public void set(String value) {
fooHolder.set(value);
}
public void clear() {
fooHolder.set(null);
}
public String getFoo() {
return fooHolder.get();
}
}
public static interface Node {
String getName();
}
public static class Foo implements Node {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,39 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.After;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.StepContext;
import org.springframework.batch.core.scope.StepSynchronizationManager;
public class StepContextFactoryTests {
private StepContextFactory factory = new StepContextFactory();
@After
public void cleanUp() {
StepSynchronizationManager.close();
StepSynchronizationManager.close();
}
@Test
public void testGetContext() {
StepExecution stepExecution = new StepExecution("foo", new JobExecution(11L));
StepContext context = StepSynchronizationManager.register(stepExecution);
assertEquals(context, factory.getContext());
}
@Test
public void testGetContextId() {
StepSynchronizationManager.register(new StepExecution("foo", new JobExecution(11L), 0L));
Object id1 = factory.getContextId();
StepSynchronizationManager.register(new StepExecution("foo", new JobExecution(12L), 1L));
Object id2 = factory.getContextId();
assertFalse(id2.equals(id1));
}
}