Revised @PropertySource parsing for consistent PropertySource naming, avoiding accidental overriding by name

Issue: SPR-11637
This commit is contained in:
Juergen Hoeller
2014-04-28 23:24:57 +02:00
parent ce4912b627
commit ab24dda4ff
4 changed files with 74 additions and 38 deletions

View File

@@ -124,9 +124,14 @@ public class PropertySourceAnnotationTests {
System.clearProperty("path.to.properties");
}
/**
* SPR-10820
*/
@Test(expected = IllegalArgumentException.class)
public void withEmptyResourceLocations() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithEmptyResourceLocations.class);
ctx.refresh();
}
// SPR-10820
@Test
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
// p2 should 'win' as it was registered last
@@ -136,13 +141,6 @@ public class PropertySourceAnnotationTests {
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}
@Test(expected=IllegalArgumentException.class)
public void withEmptyResourceLocations() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithEmptyResourceLocations.class);
ctx.refresh();
}
@Test
public void withNameAndMultipleResourceLocations() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
@@ -170,6 +168,15 @@ public class PropertySourceAnnotationTests {
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}
@Test
public void withNamedPropertySources() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
// p2 should 'win' as it was registered last
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}
@Test
public void withMissingPropertySource() {
thrown.expect(BeanDefinitionStoreException.class);
@@ -269,8 +276,8 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
@PropertySource("classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource("classpath:org/springframework/context/annotation/p2.properties"),
})
static class ConfigWithPropertySources {
}
@@ -278,9 +285,18 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/missing.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"),
})
static class ConfigWithNamedPropertySources {
}
@Configuration
@PropertySources({
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/missing.properties"),
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties")
})
static class ConfigWithMissingPropertySource {
}
@@ -288,10 +304,10 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/missing.properties", ignoreResourceNotFound=true),
@PropertySource(name = "psName", value="classpath:${myPath}/missing.properties", ignoreResourceNotFound=true),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/missing.properties", ignoreResourceNotFound=true),
@PropertySource(name = "psName", value = "classpath:${myPath}/missing.properties", ignoreResourceNotFound=true),
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties")
})
static class ConfigWithIgnoredPropertySource {
}