BATCH-1949: Updated for autoproxy check

This commit is contained in:
Michael Minella
2013-01-11 15:01:51 -06:00
parent fcede7245e
commit aadc5ee39e
13 changed files with 16 additions and 1843 deletions

View File

@@ -1,147 +0,0 @@
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;
@Override
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>() {
@Override
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;
@Override
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());
@Override
public String getName() {
return name;
}
public void setName(String name) {
logger.debug("Setting name: " + name);
this.name = name;
}
}
}

View File

@@ -1,27 +0,0 @@
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()
*/
@Override
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()
*/
@Override
public String getContextId() {
return getContext()+"#"+(count ++);
}
}

View File

@@ -1,228 +0,0 @@
/*
* Copyright 2006-2013 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.HashMap;
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("nested")
private TestBean nested;
@Autowired
@Qualifier("list")
private TestBean list;
@Autowired
@Qualifier("nestedList")
private TestBean nestedList;
@Autowired
@Qualifier("map")
private TestBean map;
@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 testMultipleNestedValueFromProperties() throws Exception {
for (int i = 0; i < 4; i++) {
final String value = "bar" + i;
attributes = Collections.singletonMap("foo", value);
assertEquals("foo-bar" + i, nested.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();
}
}
}
@Test
public void testMultipleValueInNestedList() 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, nestedList.getParent().getNames().get(0));
}
finally {
contextFactory.clearContext();
}
}
}
@Test
public void testMultipleValueInMap() 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, map.getMap().get("foo"));
}
finally {
contextFactory.clearContext();
}
}
}
@Override
public String toString() {
return "Test context: attributes=" + attributes;
}
public static class TestBean {
private String name;
private TestBean parent;
private List<String> names = new ArrayList<String>();
private Map<String, String> map = new HashMap<String, String>();
public TestBean getParent() {
return parent;
}
public void setParent(TestBean parent) {
this.parent = parent;
}
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 Map<String, String> getMap() {
return new HashMap<String, String>(map);
}
public void setMap(Map<String, String> map) {
this.map.putAll(map);
}
}
public static class SimpleContextFactory extends ContextFactorySupport {
private Object root;
@Override
public Object getContext() {
return root;
}
public void setContext(Object root) {
this.root = root;
}
public void clearContext() {
root = null;
}
}
}

View File

@@ -1,63 +0,0 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat;
import java.util.Date;
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 PlaceholderTargetSourceCustomEditorTests extends ContextFactorySupport {
@Autowired
@Qualifier("withEmbeddedDate")
private PlaceholderTargetSource withEmbeddedDate;
private Date date = new Date(1L);
@Override
public Object getContext() {
return this;
}
public Date getDate() {
return date;
}
@Test
public void testGetEmbeddedDate() {
Node target = (Node) withEmbeddedDate.getTarget();
String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date(1L));
assertEquals("bar-"+date, target.getName());
}
public static interface Node {
String getName();
}
public static class Foo implements Node {
private String name;
public Foo() {
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -1,205 +0,0 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class PlaceholderTargetSourceErrorTests extends ContextFactorySupport {
private Map<String, Object> map = Collections.singletonMap("foo.foo", (Object) "bar");
private Date date = new Date(1L);
@Override
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;
}
public String getGarbage() {
return null;
}
private PlaceholderTargetSource createValue(String name, String value) throws Exception {
String input = IOUtils.toString(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass())
.getInputStream());
input = input.replace("<!-- INSERT -->", String.format("<property name=\"%s\" value=\"%s\" />", name, value));
Resource resource = new ByteArrayResource(input.getBytes());
GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions(resource);
context.refresh();
// XmlBeanFactory context = new XmlBeanFactory(resource);
return (PlaceholderTargetSource) context.getBean("value");
}
@Test
public void testPartialReplaceSunnyDay() throws Exception {
Node target = (Node) createValue("name", "%{foo}-bar").getTarget();
assertEquals("bar-bar", target.getName());
}
@Test
public void testPartialReplaceMissingProperty() throws Exception {
try {
Node target = (Node) createValue("name", "%{garbage}-bar").getTarget();
assertEquals("%{garbage}-bar", target.getName());
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.toLowerCase().contains("cannot bind"));
}
}
@Test
public void testFullReplaceSunnyDay() throws Exception {
Node target = (Node) createValue("name", "%{foo}").getTarget();
assertEquals("bar", target.getName());
}
@Test
public void testFullReplaceMissingProperty() throws Exception {
try {
Node target = (Node) createValue("name", "%{garbage}").getTarget();
assertEquals("bar", target.getName());
fail("Expected IllegalStateException");
}
catch (BeanCreationException e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.toLowerCase().contains("cannot bind"));
}
}
@Test
public void testPartialReplaceIntegerToString() throws Exception {
Node target = (Node) createValue("name", "foo-%{integer}").getTarget();
assertEquals("foo-4321", target.getName());
}
@Test
public void testFullReplaceIntegerToString() throws Exception {
Node target = (Node) createValue("name", "%{integer}").getTarget();
assertEquals("4321", target.getName());
}
@Test
public void testFullReplaceIntegerToLong() throws Exception {
Node target = (Node) createValue("value", "%{integer}").getTarget();
assertEquals(4321L, target.getValue());
}
@Test
public void testFullReplaceIntegerToNode() throws Exception {
try {
Node target = (Node) createValue("parent", "%{integer}").getTarget();
assertEquals("4321", target.getParent());
fail("Expected IllegalArgumentException");
}
catch (Exception e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.toLowerCase().contains("cannot convert"));
}
}
public static interface Node {
String getName();
Date getDate();
Node getParent();
long getValue();
}
public static class Foo implements Node {
private String name;
private Date date;
private Node parent;
private long value;
public Foo() {
}
@Override
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public Foo(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setName(String name) {
this.name = name;
}
@Override
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
}
}

View File

@@ -1,298 +0,0 @@
package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
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("withList")
private PlaceholderTargetSource withList;
@Autowired
@Qualifier("withLiteralList")
private PlaceholderTargetSource withLiteralList;
@Autowired
@Qualifier("withMap")
private PlaceholderTargetSource withMap;
@Autowired
@Qualifier("withMultiple")
private PlaceholderTargetSource withMultiple;
@Autowired
@Qualifier("withMultipleStartAndEnd")
private PlaceholderTargetSource withMultipleStartAndEnd;
@Autowired
@Qualifier("withEmbeddedDate")
private PlaceholderTargetSource withEmbeddedDate;
@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(1L);
@Override
public Object getContext() {
return this;
}
public String getFoo() {
return "bar";
}
public Map<String, Object> getMap() {
return map;
}
public List<String> getList() {
return Arrays.asList("bar", "spam");
}
public Node getParent() {
return new Foo("spam");
}
public Long getLong() {
return 12345678912345L;
}
public Integer getInteger() {
return 4321;
}
public Date getDate() {
return date;
}
public String getGarbage() {
return null;
}
@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 testGetList() {
Node target = (Node) withList.getTarget();
assertEquals(3, target.getList().size());
assertEquals("[bar, foo-4321, bar-4321]", target.getList().toString());
}
@Test
public void testGetLiteralList() {
Node target = (Node) withLiteralList.getTarget();
assertEquals(2, target.getList().size());
assertEquals("[bar, spam]", target.getList().toString());
}
@Test
public void testGetMap() {
Node target = (Node) withMap.getTarget();
assertEquals(3, target.getMap().size());
assertEquals("{foo=bar, bar=foo-4321, spam=[bar, spam]}", target.getMap().toString());
}
@Test
public void testGetMultiple() {
Node target = (Node) withMultiple.getTarget();
assertEquals("bar-4321-4321", target.getName());
}
@Test
public void testGetMultipleStartAndEnd() {
Node target = (Node) withMultipleStartAndEnd.getTarget();
assertEquals("4321-4321", target.getName());
}
@Test
public void testGetEmbeddedDate() {
Node target = (Node) withEmbeddedDate.getTarget();
String date = new Date(1L).toString();
assertEquals("bar-"+date, target.getName());
}
@Test
public void testGetDate() {
Node target = (Node) withDate.getTarget();
assertEquals(new Date(1L), target.getDate());
}
public static interface Node {
String getName();
Date getDate();
Node getParent();
List<String> getList();
Map<String, Object> getMap();
}
public static class Foo implements Node {
private String name;
private Date date;
private Node parent;
private List<String> list;
private Map<String, Object> map;
public Foo() {
}
public Foo(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setName(String name) {
this.name = name;
}
@Override
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public List<String> getList() {
return list;
}
@Override
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
}

View File

@@ -1,72 +0,0 @@
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>();
@Override
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;
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -1,39 +0,0 @@
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.context.StepContext;
import org.springframework.batch.core.scope.context.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));
}
}