Fix bug in PlaceholderTargetSource for null values

This commit is contained in:
dsyer
2008-12-11 19:05:59 +00:00
parent e8ef2ae31d
commit a6b6af9327
3 changed files with 28 additions and 1 deletions

View File

@@ -177,7 +177,7 @@ public class PlaceholderTargetSource extends SimpleBeanTargetSource implements I
BeanWrapper wrapper = new BeanWrapperImpl(contextFactory.getContext());
if (wrapper.isReadableProperty(key)) {
Object property = wrapper.getPropertyValue(key);
if (requiredType.isAssignableFrom(property.getClass())) {
if (property==null || requiredType.isAssignableFrom(property.getClass())) {
result = property;
}
}

View File

@@ -38,6 +38,10 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
@Qualifier("withDate")
private PlaceholderTargetSource withDate;
@Autowired
@Qualifier("withNull")
private PlaceholderTargetSource withNull;
@Autowired
@Qualifier("compound")
private PlaceholderTargetSource compound;
@@ -82,6 +86,10 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
return date;
}
public String getGarbage() {
return null;
}
@Test
public void testAfterPropertiesSet() throws Exception {
PlaceholderTargetSource targetSource = new PlaceholderTargetSource();
@@ -143,6 +151,13 @@ public class PlaceholderTargetSourceTests extends ContextFactorySupport {
assertEquals("bar-#{date}", target.getName());
}
@Test
public void testGetNull() {
Node target = (Node) withNull.getTarget();
// Remains unconverted because null is explicitly excluded
assertEquals("bar-#{garbage}", target.getName());
}
public static interface Node {
String getName();

View File

@@ -104,6 +104,18 @@
<property name="name" value="bar-#{date}" />
</bean>
<bean id="withNull"
class="org.springframework.batch.core.scope.util.PlaceholderTargetSource">
<property name="contextFactory" ref="context" />
<property name="targetBeanName" value="withNullTarget" />
</bean>
<bean id="withNullTarget"
class="org.springframework.batch.core.scope.util.PlaceholderTargetSourceTests$Foo"
lazy-init="true">
<property name="name" value="bar-#{garbage}" />
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>