ConversionService is able to apply Converters to interface-based array elements (SPR-7150); a context ConversionService is able to override an ApplicationContext's resource editors (SPR-7079)

This commit is contained in:
Juergen Hoeller
2010-05-26 13:58:37 +00:00
parent 6c6004a93b
commit 1532119787
8 changed files with 158 additions and 36 deletions

View File

@@ -16,13 +16,13 @@
package org.springframework.context.support;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.ResourceTestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.ConversionService;
@@ -31,6 +31,7 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
/**
* @author Keith Donald
@@ -92,16 +93,25 @@ public class ConversionServiceFactoryBeanTests {
@Test
public void conversionServiceInApplicationContext() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("conversionService.xml", getClass());
doTestConversionServiceInApplicationContext("conversionService.xml", ClassPathResource.class);
}
@Test
public void conversionServiceInApplicationContextWithResourceOverriding() {
doTestConversionServiceInApplicationContext("conversionServiceWithResourceOverriding.xml", FileSystemResource.class);
}
private void doTestConversionServiceInApplicationContext(String fileName, Class resourceClass) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(fileName, getClass());
ResourceTestBean tb = ctx.getBean("resourceTestBean", ResourceTestBean.class);
assertTrue(tb.getResource() instanceof ClassPathResource);
assertTrue(resourceClass.isInstance(tb.getResource()));
assertTrue(tb.getResourceArray().length > 0);
assertTrue(tb.getResourceArray()[0] instanceof ClassPathResource);
assertTrue(resourceClass.isInstance(tb.getResourceArray()[0]));
assertTrue(tb.getResourceMap().size() == 1);
assertTrue(tb.getResourceMap().get("key1") instanceof ClassPathResource);
assertTrue(resourceClass.isInstance(tb.getResourceMap().get("key1")));
assertTrue(tb.getResourceArrayMap().size() == 1);
assertTrue(tb.getResourceArrayMap().get("key1").length > 0);
assertTrue(tb.getResourceArrayMap().get("key1")[0] instanceof ClassPathResource);
assertTrue(resourceClass.isInstance(tb.getResourceArrayMap().get("key1")[0]));
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2002-2010 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.context.support;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
*/
public class ResourceConverter implements Converter<String, Resource> {
public Resource convert(String source) {
return new FileSystemResource(source + ".xml");
}
}

View File

@@ -3,14 +3,6 @@
<beans>
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map key-type="java.lang.String" value-type="java.lang.Class">
<entry key="org.springframework.core.io.Resource[]" value="org.springframework.core.io.support.ResourceArrayPropertyEditor"/>
</map>
</property>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
<bean id="resourceTestBean" class="org.springframework.beans.ResourceTestBean">

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<bean class="org.springframework.context.support.ResourceConverter"/>
</property>
</bean>
<bean id="resourceTestBean" class="org.springframework.beans.ResourceTestBean">
<property name="resource" value="org/springframework/context/support/conversionService.xml"/>
<property name="resourceArray" value="org/springframework/context/support/conversionService.xml"/>
<property name="resourceMap">
<map>
<entry key="key1" value="org/springframework/context/support/conversionService.xml"/>
</map>
</property>
<property name="resourceArrayMap">
<map>
<entry key="key1" value="org/springframework/context/support/conversionService.xml"/>
</map>
</property>
</bean>
</beans>