diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java new file mode 100644 index 00000000000..108a38b2a59 --- /dev/null +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java @@ -0,0 +1,44 @@ +package org.springframework.beans.factory.config; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; + +import org.junit.Test; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.core.io.ByteArrayResource; + +import test.beans.TestBean; + +/** + * Tests cornering SPR-7547. + * + * @author Chris Beams + */ +public class PropertyPlaceholderConfigurerTests { + + /** + * Prior to the fix for SPR-7547, the following would throw + * IllegalStateException because the PropertiesLoaderSupport base class + * assumed ByteArrayResource implements Resource.getFilename(). It does + * not, and AbstractResource.getFilename() is called instead, raising the + * exception. The following now works, as getFilename() is called in a + * try/catch to check whether the resource is actually file-based or not. + * + * See SPR-7552, which suggests paths to address the root issue rather than + * just patching the problem. + */ + @Test + public void repro() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + bf.registerBeanDefinition("testBean", + rootBeanDefinition(TestBean.class) + .addPropertyValue("name", "${my.name}").getBeanDefinition()); + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + ppc.setLocation(new ByteArrayResource("my.name=Inigo Montoya".getBytes())); + ppc.postProcessBeanFactory(bf); + + TestBean testBean = bf.getBean(TestBean.class); + assertThat(testBean.getName(), equalTo("Inigo Montoya")); + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java index 640780e4bce..65f80cec55c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java @@ -179,7 +179,15 @@ public abstract class PropertiesLoaderSupport { InputStream is = null; try { is = location.getInputStream(); - if (location.getFilename().endsWith(XML_FILE_EXTENSION)) { + + String filename = null; + try { + filename = location.getFilename(); + } catch (IllegalStateException ex) { + // resource is not file-based. See SPR-7552. + } + + if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { this.propertiesPersister.loadFromXml(props, is); } else { diff --git a/spring-framework-reference/src/images b/spring-framework-reference/src/images new file mode 120000 index 00000000000..5e67573196d --- /dev/null +++ b/spring-framework-reference/src/images @@ -0,0 +1 @@ +../images \ No newline at end of file