23
23
import static java .time .temporal .ChronoUnit .MINUTES ;
24
24
import static java .time .temporal .ChronoUnit .NANOS ;
25
25
import static java .time .temporal .ChronoUnit .SECONDS ;
26
+ import static org .assertj .core .api .Assertions .assertThat ;
26
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
28
import static org .junit .jupiter .api .Assertions .assertNotNull ;
28
29
import static org .junit .jupiter .api .Assertions .assertNull ;
29
30
import static org .junit .jupiter .api .Assertions .assertThrows ;
31
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
30
32
31
33
import java .nio .charset .Charset ;
32
34
import java .nio .charset .StandardCharsets ;
36
38
import java .util .Map ;
37
39
import java .util .Properties ;
38
40
import java .util .stream .Stream ;
39
- import org .assertj .core .api .Assertions ;
40
41
import org .junit .jupiter .api .BeforeEach ;
41
42
import org .junit .jupiter .api .Test ;
42
43
import org .junit .jupiter .api .parallel .ResourceAccessMode ;
45
46
import org .junit .jupiter .params .ParameterizedTest ;
46
47
import org .junit .jupiter .params .provider .Arguments ;
47
48
import org .junit .jupiter .params .provider .MethodSource ;
49
+ import org .junitpioneer .jupiter .Issue ;
48
50
import org .junitpioneer .jupiter .ReadsSystemProperty ;
49
51
50
- public class PropertiesUtilTest {
52
+ class PropertiesUtilTest {
51
53
52
54
private final Properties properties = new Properties ();
53
55
54
56
@ BeforeEach
55
- public void setUp () throws Exception {
57
+ void setUp () throws Exception {
56
58
properties .load (ClassLoader .getSystemResourceAsStream ("PropertiesUtilTest.properties" ));
57
59
}
58
60
59
61
@ Test
60
- public void testExtractSubset () {
62
+ void testExtractSubset () {
61
63
assertHasAllProperties (PropertiesUtil .extractSubset (properties , "a" ));
62
64
assertHasAllProperties (PropertiesUtil .extractSubset (properties , "b." ));
63
65
assertHasAllProperties (PropertiesUtil .extractSubset (properties , "c.1" ));
@@ -67,7 +69,7 @@ public void testExtractSubset() {
67
69
}
68
70
69
71
@ Test
70
- public void testPartitionOnCommonPrefix () {
72
+ void testPartitionOnCommonPrefix () {
71
73
final Map <String , Properties > parts = PropertiesUtil .partitionOnCommonPrefixes (properties );
72
74
assertEquals (4 , parts .size ());
73
75
assertHasAllProperties (parts .get ("a" ));
@@ -85,7 +87,7 @@ private static void assertHasAllProperties(final Properties properties) {
85
87
}
86
88
87
89
@ Test
88
- public void testGetCharsetProperty () {
90
+ void testGetCharsetProperty () {
89
91
final Properties p = new Properties ();
90
92
p .setProperty ("e.1" , StandardCharsets .US_ASCII .name ());
91
93
p .setProperty ("e.2" , "wrong-charset-name" );
@@ -130,8 +132,8 @@ static Stream<Arguments> should_properly_parse_duration() {
130
132
131
133
@ ParameterizedTest
132
134
@ MethodSource
133
- void should_properly_parse_duration (final Duration expected , final String value ) {
134
- Assertions . assertThat (PropertiesUtil .parseDuration (value )).isEqualTo (expected );
135
+ void should_properly_parse_duration (final Duration expected , final CharSequence value ) {
136
+ assertThat (PropertiesUtil .parseDuration (value )).isEqualTo (expected );
135
137
}
136
138
137
139
static List <String > should_throw_on_invalid_duration () {
@@ -142,29 +144,29 @@ static List<String> should_throw_on_invalid_duration() {
142
144
143
145
@ ParameterizedTest
144
146
@ MethodSource
145
- void should_throw_on_invalid_duration (final String value ) {
147
+ void should_throw_on_invalid_duration (final CharSequence value ) {
146
148
assertThrows (IllegalArgumentException .class , () -> PropertiesUtil .parseDuration (value ));
147
149
}
148
150
149
151
@ Test
150
152
@ ResourceLock (value = Resources .SYSTEM_PROPERTIES , mode = ResourceAccessMode .READ )
151
- public void testGetMappedProperty_sun_stdout_encoding () {
153
+ void testGetMappedProperty_sun_stdout_encoding () {
152
154
final PropertiesUtil pu = new PropertiesUtil (System .getProperties ());
153
155
final Charset expected = System .console () == null ? Charset .defaultCharset () : StandardCharsets .UTF_8 ;
154
156
assertEquals (expected , pu .getCharsetProperty ("sun.stdout.encoding" ));
155
157
}
156
158
157
159
@ Test
158
160
@ ResourceLock (value = Resources .SYSTEM_PROPERTIES , mode = ResourceAccessMode .READ )
159
- public void testGetMappedProperty_sun_stderr_encoding () {
161
+ void testGetMappedProperty_sun_stderr_encoding () {
160
162
final PropertiesUtil pu = new PropertiesUtil (System .getProperties ());
161
163
final Charset expected = System .console () == null ? Charset .defaultCharset () : StandardCharsets .UTF_8 ;
162
164
assertEquals (expected , pu .getCharsetProperty ("sun.err.encoding" ));
163
165
}
164
166
165
167
@ Test
166
168
@ ResourceLock (Resources .SYSTEM_PROPERTIES )
167
- public void testNonStringSystemProperties () {
169
+ void testNonStringSystemProperties () {
168
170
final Object key1 = "1" ;
169
171
final Object key2 = new Object ();
170
172
System .getProperties ().put (key1 , new Object ());
@@ -180,14 +182,32 @@ public void testNonStringSystemProperties() {
180
182
181
183
@ Test
182
184
@ ResourceLock (value = Resources .SYSTEM_PROPERTIES , mode = ResourceAccessMode .READ )
183
- public void testPublish () {
185
+ void testPublish () {
184
186
final Properties props = new Properties ();
185
- final PropertiesUtil util = new PropertiesUtil (props );
187
+ new PropertiesUtil (props );
186
188
final String value = System .getProperty ("Application" );
187
189
assertNotNull (value , "System property was not published" );
188
190
assertEquals ("Log4j" , value );
189
191
}
190
192
193
+ @ Test
194
+ @ ResourceLock (value = Resources .SYSTEM_PROPERTIES , mode = ResourceAccessMode .READ )
195
+ @ Issue ("https://github.com/spring-projects/spring-boot/issues/33450" )
196
+ void testBadPropertySource () {
197
+ final String key = "testKey" ;
198
+ final Properties props = new Properties ();
199
+ props .put (key , "test" );
200
+ final PropertiesUtil util = new PropertiesUtil (props );
201
+ final ErrorPropertySource source = new ErrorPropertySource ();
202
+ util .addPropertySource (source );
203
+ try {
204
+ assertEquals ("test" , util .getStringProperty (key ));
205
+ assertTrue (source .exceptionThrown );
206
+ } finally {
207
+ util .removePropertySource (source );
208
+ }
209
+ }
210
+
191
211
private static final String [][] data = {
192
212
{null , "org.apache.logging.log4j.level" },
193
213
{null , "Log4jAnotherProperty" },
@@ -209,7 +229,7 @@ public void testPublish() {
209
229
*/
210
230
@ Test
211
231
@ ResourceLock (value = Resources .SYSTEM_PROPERTIES , mode = ResourceAccessMode .READ )
212
- public void testResolvesOnlyLog4jProperties () {
232
+ void testResolvesOnlyLog4jProperties () {
213
233
final PropertiesUtil util = new PropertiesUtil ("Jira3413Test.properties" );
214
234
for (final String [] pair : data ) {
215
235
assertEquals (pair [0 ], util .getStringProperty (pair [1 ]));
@@ -222,7 +242,7 @@ public void testResolvesOnlyLog4jProperties() {
222
242
*/
223
243
@ Test
224
244
@ ReadsSystemProperty
225
- public void testLog4jProperty () {
245
+ void testLog4jProperty () {
226
246
final Properties props = new Properties ();
227
247
final String incorrect = "log4j2." ;
228
248
final String correct = "not.starting.with.log4j" ;
@@ -231,4 +251,40 @@ public void testLog4jProperty() {
231
251
final PropertiesUtil util = new PropertiesUtil (props );
232
252
assertEquals (correct , util .getStringProperty (correct ));
233
253
}
254
+
255
+ @ Test
256
+ void should_support_multiple_sources_with_same_priority () {
257
+ final int priority = 2003 ;
258
+ final String key1 = "propertySource1" ;
259
+ final Properties props1 = new Properties ();
260
+ props1 .put (key1 , "props1" );
261
+ final String key2 = "propertySource2" ;
262
+ final Properties props2 = new Properties ();
263
+ props2 .put (key2 , "props2" );
264
+ final PropertiesUtil util = new PropertiesUtil (new PropertiesPropertySource (props1 , priority ));
265
+ util .addPropertySource (new PropertiesPropertySource (props2 , priority ));
266
+ assertThat (util .getStringProperty (key1 )).isEqualTo ("props1" );
267
+ assertThat (util .getStringProperty (key2 )).isEqualTo ("props2" );
268
+ }
269
+
270
+ private static class ErrorPropertySource implements PropertySource {
271
+ public boolean exceptionThrown = false ;
272
+
273
+ @ Override
274
+ public int getPriority () {
275
+ return Integer .MIN_VALUE ;
276
+ }
277
+
278
+ @ Override
279
+ public String getProperty (final String key ) {
280
+ exceptionThrown = true ;
281
+ throw new IllegalStateException ("Test" );
282
+ }
283
+
284
+ @ Override
285
+ public boolean containsProperty (final String key ) {
286
+ exceptionThrown = true ;
287
+ throw new IllegalStateException ("Test" );
288
+ }
289
+ }
234
290
}
0 commit comments