Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add repro case (and workaround) for SPR-6428

Successfully reproduces reported error:

    java.io.FileNotFoundException: class path resource
    [${resourceDirPlaceHolder}/props.properties] cannot be opened
    because it does not exist

And provides a workaround @Test that demonstrates using a
PropertySource to avoid the lifecycle issue that causes the exception
above.

Issue: SPR-6428
  • Loading branch information...
commit 09aea36859bb0d806eb4085a2c154cb781bd8345 1 parent c583a82
@cbeams cbeams authored
View
54 SPR-6428/pom.xml
@@ -0,0 +1,54 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.springframework.issues</groupId>
+ <artifactId>SPR-6428</artifactId>
+ <packaging>war</packaging>
+ <name>Issue with multiple PPCs</name>
+ <version>1.0.0-SNAPSHOT</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>3.1.0.BUILD-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>2.4.3</version>
+ <configuration>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.7.2</version>
+ <configuration>
+ <includes>
+ <include>**/*Tests.java</include>
+ </includes>
+ <excludes>
+ <exclude>**/*Abstract*.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
View
0  SPR-6428/src/main/java/.gitignore
No changes.
View
7 SPR-6428/src/main/resources/log4j.properties
@@ -0,0 +1,7 @@
+log4j.rootCategory=INFO, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
+
+log4j.category.org.springframework.web=DEBUG
View
1  SPR-6428/src/main/resources/myResourceDir/props.properties
@@ -0,0 +1 @@
+my.message=Greetings!
View
62 SPR-6428/src/test/java/ReproTests.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2002-2011 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.
+ */
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.springframework.context.support.GenericXmlApplicationContext;
+import org.springframework.core.env.MapPropertySource;
+
+/**
+ * Unit tests demonstrating the failure reported by SPR-6428, and the workaround using
+ * Spring's Environment and PropertySource APIs that fixes it.
+ *
+ * @author Chris Beams
+ */
+public class ReproTests {
+
+ /**
+ * Test will fail as two PropertyPlaceholderConfigurer beans are defined, with one
+ * depending on the other for placeholder resolution. This is a lifecycle issue with
+ * the way BeanFactoryPostProcessors are processed. See {@link #workaround()} below
+ * for a solution to this.
+ */
+ @Test
+ public void repro() {
+ GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
+ ctx.load("ReproTests-repro.xml");
+ ctx.refresh();
+ }
+
+ /**
+ * Work around the lifecycle issues above by introducing a PropertySource containing
+ * the value for the "resourceDirPlaceHolder"
+ */
+ @Test
+ public void workaround() {
+ Map<String,Object> localProps = new HashMap<String,Object>();
+ localProps.put("resourceDirPlaceHolder", "myResourceDir");
+
+ GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
+ ctx.getEnvironment()
+ .getPropertySources()
+ .addFirst(new MapPropertySource("localProps", localProps));
+ ctx.load("ReproTests-workaround.xml");
+ ctx.refresh();
+ }
+
+}
View
20 SPR-6428/src/test/resources/ReproTests-repro.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
+
+ <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="properties">
+ <props>
+ <prop key="resourceDirPlaceHolder">myResourceDir</prop>
+ </props>
+ </property>
+ <property name="order" value="1"/>
+ </bean>
+
+ <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="location" value="classpath:${resourceDirPlaceHolder}/props.properties"/>
+ </bean>
+
+</beans>
View
19 SPR-6428/src/test/resources/ReproTests-workaround.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:jee="http://www.springframework.org/schema/jee"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
+ http://www.springframework.org/schema/jee
+ http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
+
+ <!-- only one PPC is necessary. The "resourceDirPlaceHolder" property is resolved from
+ the Environment and the "localProps" PropertySource configured in the test -->
+ <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="locations">
+ <list>
+ <value>classpath:${resourceDirPlaceHolder}/props.properties</value>
+ </list>
+ </property>
+ </bean>
+</beans>
View
7 SPR-6428/src/test/resources/log4j.properties
@@ -0,0 +1,7 @@
+log4j.rootCategory=INFO, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
+
+log4j.category.org.springframework.web=DEBUG
Please sign in to comment.
Something went wrong with that request. Please try again.