Skip to content

Commit 205e681

Browse files
committed
Backported tests for property source ordering
Issue: SPR-12198 (cherry picked from commit 90b93ff)
1 parent 29abca5 commit 205e681

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,6 @@ public void withEmptyResourceLocations() {
142142
ctx.refresh();
143143
}
144144

145-
// SPR-10820
146-
@Test
147-
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
148-
// p2 should 'win' as it was registered last
149-
AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
150-
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
151-
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
152-
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
153-
}
154-
155145
@Test
156146
public void withNameAndMultipleResourceLocations() {
157147
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
@@ -202,6 +192,30 @@ public void withIgnoredPropertySource() {
202192
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
203193
}
204194

195+
@Test
196+
public void withSameSourceImportedInDifferentOrder() {
197+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithSameSourceImportedInDifferentOrder.class);
198+
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
199+
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
200+
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
201+
}
202+
203+
@Test
204+
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
205+
// SPR-10820: p2 should 'win' as it was registered last
206+
AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
207+
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
208+
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
209+
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
210+
}
211+
212+
@Test
213+
public void orderingWithAndWithoutNameAndFourResourceLocations() {
214+
// SPR-12198: p4 should 'win' as it was registered last
215+
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
216+
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean"));
217+
}
218+
205219

206220
@Configuration
207221
@PropertySource(value="classpath:${unresolvable}/p1.properties")
@@ -326,8 +340,8 @@ static class ConfigWithPropertySources {
326340

327341
@Configuration
328342
@PropertySources({
329-
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
330-
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"),
343+
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
344+
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"),
331345
})
332346
static class ConfigWithNamedPropertySources {
333347
}
@@ -359,4 +373,36 @@ static class ConfigWithIgnoredPropertySource {
359373
static class ConfigWithEmptyResourceLocations {
360374
}
361375

376+
377+
@Import(ConfigImportedWithSameSourceImportedInDifferentOrder.class)
378+
@PropertySources({
379+
@PropertySource("classpath:org/springframework/context/annotation/p1.properties"),
380+
@PropertySource("classpath:org/springframework/context/annotation/p2.properties")
381+
})
382+
@Configuration
383+
public static class ConfigWithSameSourceImportedInDifferentOrder {
384+
385+
}
386+
387+
388+
@Configuration
389+
@PropertySources({
390+
@PropertySource("classpath:org/springframework/context/annotation/p2.properties"),
391+
@PropertySource("classpath:org/springframework/context/annotation/p1.properties")
392+
})
393+
public static class ConfigImportedWithSameSourceImportedInDifferentOrder {
394+
}
395+
396+
397+
@Configuration
398+
@PropertySource(
399+
value = {
400+
"classpath:org/springframework/context/annotation/p1.properties",
401+
"classpath:org/springframework/context/annotation/p2.properties",
402+
"classpath:org/springframework/context/annotation/p3.properties",
403+
"classpath:org/springframework/context/annotation/p4.properties"
404+
})
405+
static class ConfigWithFourResourceLocations {
406+
}
407+
362408
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testbean.name=p3TestBean
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testbean.name=p4TestBean

0 commit comments

Comments
 (0)