RESOLVED - issue BATCH-1370: Bind to non-scalar map entry values in step scope

Also fixed: no need to use custom PropertyEditor to bind to Dates from step context
This commit is contained in:
dsyer
2009-08-14 09:35:50 +00:00
parent 0bf64b4a80
commit 7c487a7abc
7 changed files with 330 additions and 104 deletions

View File

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

View File

@@ -10,6 +10,7 @@ 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;
@@ -74,8 +75,15 @@ public class PlaceholderTargetSourceErrorTests extends ContextFactorySupport {
@Test
public void testPartialReplaceMissingProperty() throws Exception {
Node target = (Node) createValue("name", "%{garbage}-bar").getTarget();
assertEquals("%{garbage}-bar", target.getName());
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
@@ -91,7 +99,7 @@ public class PlaceholderTargetSourceErrorTests extends ContextFactorySupport {
assertEquals("bar", target.getName());
fail("Expected IllegalStateException");
}
catch (Exception e) {
catch (BeanCreationException e) {
String message = e.getMessage();
assertTrue("Wrong message: " + message, message.toLowerCase().contains("cannot bind"));
}
@@ -99,8 +107,8 @@ public class PlaceholderTargetSourceErrorTests extends ContextFactorySupport {
@Test
public void testPartialReplaceIntegerToString() throws Exception {
Node target = (Node) createValue("name", "foo-%{integer}").getTarget();
assertEquals("foo-4321", target.getName());
Node target = (Node) createValue("name", "foo-%{integer}").getTarget();
assertEquals("foo-4321", target.getName());
}
@Test

View File

@@ -3,7 +3,7 @@ package org.springframework.batch.core.scope.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -40,6 +40,14 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
@Qualifier("withList")
private PlaceholderTargetSource withList;
@Autowired
@Qualifier("withLiteralList")
private PlaceholderTargetSource withLiteralList;
@Autowired
@Qualifier("withMap")
private PlaceholderTargetSource withMap;
@Autowired
@Qualifier("withMultiple")
private PlaceholderTargetSource withMultiple;
@@ -84,6 +92,10 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
return map;
}
public List<String> getList() {
return Arrays.asList("bar", "spam");
}
public Node getParent() {
return new Foo("spam");
}
@@ -165,6 +177,20 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
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();
@@ -180,16 +206,14 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
@Test
public void testGetEmbeddedDate() {
Node target = (Node) withEmbeddedDate.getTarget();
String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date(1L));
String date = new Date(1L).toString();
assertEquals("bar-"+date, target.getName());
}
@Test
public void testGetDate() {
Node target = (Node) withDate.getTarget();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String date = sdf.format(new Date(1L));
assertEquals(date, sdf.format(target.getDate()));
assertEquals(new Date(1L), target.getDate());
}
public static interface Node {
@@ -200,6 +224,8 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
Node getParent();
List<String> getList();
Map<String, Object> getMap();
}
public static class Foo implements Node {
@@ -212,6 +238,8 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
private List<String> list;
private Map<String, Object> map;
public Foo() {
}
@@ -251,6 +279,14 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
return list;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
}