From 205e681295fcc3e8a2eac77203c19b0f2667b680 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 18 Sep 2014 23:48:35 +0200 Subject: [PATCH] Backported tests for property source ordering Issue: SPR-12198 (cherry picked from commit 90b93ff) --- .../PropertySourceAnnotationTests.java | 70 +++++++++++++++---- .../context/annotation/p3.properties | 1 + .../context/annotation/p4.properties | 1 + 3 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/p3.properties create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/p4.properties diff --git a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java index a6492e970c7..549d124ca72 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java @@ -142,16 +142,6 @@ public class PropertySourceAnnotationTests { ctx.refresh(); } - // SPR-10820 - @Test - public void orderingWithAndWithoutNameAndMultipleResourceLocations() { - // p2 should 'win' as it was registered last - AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class); - AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class); - assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); - assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); - } - @Test public void withNameAndMultipleResourceLocations() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class); @@ -202,6 +192,30 @@ public class PropertySourceAnnotationTests { assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); } + @Test + public void withSameSourceImportedInDifferentOrder() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithSameSourceImportedInDifferentOrder.class); + assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); + assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); + assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); + } + + @Test + public void orderingWithAndWithoutNameAndMultipleResourceLocations() { + // SPR-10820: p2 should 'win' as it was registered last + AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class); + AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class); + assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); + assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); + } + + @Test + public void orderingWithAndWithoutNameAndFourResourceLocations() { + // SPR-12198: p4 should 'win' as it was registered last + AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class); + assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean")); + } + @Configuration @PropertySource(value="classpath:${unresolvable}/p1.properties") @@ -326,8 +340,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(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"), + @PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"), }) static class ConfigWithNamedPropertySources { } @@ -359,4 +373,36 @@ public class PropertySourceAnnotationTests { static class ConfigWithEmptyResourceLocations { } + + @Import(ConfigImportedWithSameSourceImportedInDifferentOrder.class) + @PropertySources({ + @PropertySource("classpath:org/springframework/context/annotation/p1.properties"), + @PropertySource("classpath:org/springframework/context/annotation/p2.properties") + }) + @Configuration + public static class ConfigWithSameSourceImportedInDifferentOrder { + + } + + + @Configuration + @PropertySources({ + @PropertySource("classpath:org/springframework/context/annotation/p2.properties"), + @PropertySource("classpath:org/springframework/context/annotation/p1.properties") + }) + public static class ConfigImportedWithSameSourceImportedInDifferentOrder { + } + + + @Configuration + @PropertySource( + value = { + "classpath:org/springframework/context/annotation/p1.properties", + "classpath:org/springframework/context/annotation/p2.properties", + "classpath:org/springframework/context/annotation/p3.properties", + "classpath:org/springframework/context/annotation/p4.properties" + }) + static class ConfigWithFourResourceLocations { + } + } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/p3.properties b/spring-context/src/test/java/org/springframework/context/annotation/p3.properties new file mode 100644 index 00000000000..5165c44d452 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/p3.properties @@ -0,0 +1 @@ +testbean.name=p3TestBean diff --git a/spring-context/src/test/java/org/springframework/context/annotation/p4.properties b/spring-context/src/test/java/org/springframework/context/annotation/p4.properties new file mode 100644 index 00000000000..a9fbccd9826 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/p4.properties @@ -0,0 +1 @@ +testbean.name=p4TestBean