1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .context .annotation ;
18
18
19
- import static org .hamcrest .CoreMatchers .equalTo ;
20
- import static org .hamcrest .CoreMatchers .is ;
21
- import static org .junit .Assert .assertThat ;
22
- import static org .junit .Assert .assertTrue ;
23
-
19
+ import java .io .FileNotFoundException ;
24
20
import java .util .Iterator ;
25
-
26
21
import javax .inject .Inject ;
27
22
23
+ import org .junit .Rule ;
28
24
import org .junit .Test ;
25
+ import org .junit .rules .ExpectedException ;
26
+
27
+ import org .springframework .beans .factory .BeanDefinitionStoreException ;
28
+ import org .springframework .beans .factory .FactoryBean ;
29
29
import org .springframework .core .env .Environment ;
30
30
import org .springframework .core .env .MutablePropertySources ;
31
-
32
31
import org .springframework .tests .sample .beans .TestBean ;
33
32
33
+ import static org .hamcrest .CoreMatchers .*;
34
+ import static org .junit .Assert .*;
35
+
34
36
/**
35
37
* Tests the processing of @PropertySource annotations on @Configuration classes.
36
38
*
37
39
* @author Chris Beams
40
+ * @author Phillip Webb
38
41
* @since 3.1
39
42
*/
40
43
public class PropertySourceAnnotationTests {
41
44
45
+ @ Rule
46
+ public ExpectedException thrown = ExpectedException .none ();
47
+
48
+
42
49
@ Test
43
50
public void withExplicitName () {
44
51
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
@@ -93,7 +100,7 @@ public void orderingIsLifo() {
93
100
}
94
101
}
95
102
96
- @ Test (expected = IllegalArgumentException .class )
103
+ @ Test (expected = IllegalArgumentException .class )
97
104
public void withUnresolvablePlaceholder () {
98
105
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
99
106
ctx .register (ConfigWithUnresolvablePlaceholder .class );
@@ -118,21 +125,24 @@ public void withResolvablePlaceholder() {
118
125
System .clearProperty ("path.to.properties" );
119
126
}
120
127
121
- /**
122
- * Corner bug reported in SPR-9127.
123
- */
124
128
@ Test
125
- public void withNameAndMultipleResourceLocations () {
129
+ public void withResolvablePlaceholderAndFactoryBean () {
126
130
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
127
- ctx .register (ConfigWithNameAndMultipleResourceLocations .class );
131
+ ctx .register (ConfigWithResolvablePlaceholderAndFactoryBean .class );
132
+ System .setProperty ("path.to.properties" , "org/springframework/context/annotation" );
128
133
ctx .refresh ();
129
- assertThat (ctx .getEnvironment (). containsProperty ( "from.p1" ), is ( true ));
130
- assertThat ( ctx . getEnvironment (). containsProperty ( "from.p2" ), is ( true ) );
134
+ assertThat (ctx .getBean ( TestBean . class ). getName ( ), equalTo ( "p1TestBean" ));
135
+ System . clearProperty ( "path.to.properties" );
131
136
}
132
137
133
- /**
134
- * SPR-10820
135
- */
138
+ @ Test (expected = IllegalArgumentException .class )
139
+ public void withEmptyResourceLocations () {
140
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
141
+ ctx .register (ConfigWithEmptyResourceLocations .class );
142
+ ctx .refresh ();
143
+ }
144
+
145
+ // SPR-10820
136
146
@ Test
137
147
public void orderingWithAndWithoutNameAndMultipleResourceLocations () {
138
148
// p2 should 'win' as it was registered last
@@ -142,11 +152,22 @@ public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
142
152
assertThat (ctxWithName .getEnvironment ().getProperty ("testbean.name" ), equalTo ("p2TestBean" ));
143
153
}
144
154
145
- @ Test (expected =IllegalArgumentException .class )
146
- public void withEmptyResourceLocations () {
147
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
148
- ctx .register (ConfigWithEmptyResourceLocations .class );
149
- ctx .refresh ();
155
+ @ Test
156
+ public void withNameAndMultipleResourceLocations () {
157
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithNameAndMultipleResourceLocations .class );
158
+ assertThat (ctx .getEnvironment ().containsProperty ("from.p1" ), is (true ));
159
+ assertThat (ctx .getEnvironment ().containsProperty ("from.p2" ), is (true ));
160
+ // p2 should 'win' as it was registered last
161
+ assertThat (ctx .getEnvironment ().getProperty ("testbean.name" ), equalTo ("p2TestBean" ));
162
+ }
163
+
164
+ @ Test
165
+ public void withMultipleResourceLocations () {
166
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithMultipleResourceLocations .class );
167
+ assertThat (ctx .getEnvironment ().containsProperty ("from.p1" ), is (true ));
168
+ assertThat (ctx .getEnvironment ().containsProperty ("from.p2" ), is (true ));
169
+ // p2 should 'win' as it was registered last
170
+ assertThat (ctx .getEnvironment ().getProperty ("testbean.name" ), equalTo ("p2TestBean" ));
150
171
}
151
172
152
173
@@ -159,6 +180,7 @@ static class ConfigWithUnresolvablePlaceholder {
159
180
@ Configuration
160
181
@ PropertySource (value ="classpath:${unresolvable:org/springframework/context/annotation}/p1.properties" )
161
182
static class ConfigWithUnresolvablePlaceholderAndDefault {
183
+
162
184
@ Inject Environment env ;
163
185
164
186
@ Bean
@@ -171,6 +193,7 @@ public TestBean testBean() {
171
193
@ Configuration
172
194
@ PropertySource (value ="classpath:${path.to.properties}/p1.properties" )
173
195
static class ConfigWithResolvablePlaceholder {
196
+
174
197
@ Inject Environment env ;
175
198
176
199
@ Bean
@@ -180,10 +203,37 @@ public TestBean testBean() {
180
203
}
181
204
182
205
206
+ @ Configuration
207
+ @ PropertySource (value ="classpath:${path.to.properties}/p1.properties" )
208
+ static class ConfigWithResolvablePlaceholderAndFactoryBean {
209
+
210
+ @ Inject Environment env ;
211
+
212
+ @ Bean
213
+ public FactoryBean testBean () {
214
+ final String name = env .getProperty ("testbean.name" );
215
+ return new FactoryBean () {
216
+ @ Override
217
+ public Object getObject () {
218
+ return new TestBean (name );
219
+ }
220
+ @ Override
221
+ public Class <?> getObjectType () {
222
+ return TestBean .class ;
223
+ }
224
+ @ Override
225
+ public boolean isSingleton () {
226
+ return false ;
227
+ }
228
+ };
229
+ }
230
+ }
231
+
183
232
184
233
@ Configuration
185
234
@ PropertySource (name ="p1" , value ="classpath:org/springframework/context/annotation/p1.properties" )
186
235
static class ConfigWithExplicitName {
236
+
187
237
@ Inject Environment env ;
188
238
189
239
@ Bean
@@ -196,6 +246,7 @@ public TestBean testBean() {
196
246
@ Configuration
197
247
@ PropertySource ("classpath:org/springframework/context/annotation/p1.properties" )
198
248
static class ConfigWithImplicitName {
249
+
199
250
@ Inject Environment env ;
200
251
201
252
@ Bean
@@ -221,6 +272,7 @@ static class P2Config {
221
272
static class ConfigWithNameAndMultipleResourceLocations {
222
273
}
223
274
275
+
224
276
@ Configuration
225
277
@ PropertySource (
226
278
value = {
@@ -235,4 +287,5 @@ static class ConfigWithMultipleResourceLocations {
235
287
@ PropertySource (value = {})
236
288
static class ConfigWithEmptyResourceLocations {
237
289
}
290
+
238
291
}
0 commit comments