|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2011 the original author or authors. |
| 2 | + * Copyright 2002-2012 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.core.env;
|
18 | 18 |
|
19 |
| -import static org.hamcrest.CoreMatchers.equalTo; |
20 |
| -import static org.hamcrest.CoreMatchers.is; |
21 |
| -import static org.hamcrest.CoreMatchers.nullValue; |
22 |
| -import static org.junit.Assert.assertThat; |
23 |
| -import static org.junit.Assert.assertTrue; |
24 |
| -import static org.junit.Assert.fail; |
25 |
| - |
26 | 19 | import java.util.HashMap;
|
27 | 20 | import java.util.Map;
|
28 | 21 | import java.util.Properties;
|
29 | 22 |
|
| 23 | +import org.hamcrest.Matchers; |
30 | 24 | import org.junit.Before;
|
31 | 25 | import org.junit.Test;
|
| 26 | + |
32 | 27 | import org.springframework.core.convert.ConversionException;
|
33 | 28 | import org.springframework.mock.env.MockPropertySource;
|
34 | 29 |
|
| 30 | +import static org.hamcrest.CoreMatchers.*; |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
35 | 33 | /**
|
36 | 34 | * Unit tests for {@link PropertySourcesPropertyResolver}.
|
37 | 35 | *
|
@@ -352,6 +350,39 @@ public void setRequiredProperties_andValidateRequiredProperties() {
|
352 | 350 | propertyResolver.validateRequiredProperties();
|
353 | 351 | }
|
354 | 352 |
|
| 353 | + @Test |
| 354 | + public void resolveNestedPropertyPlaceholders() { |
| 355 | + MutablePropertySources ps = new MutablePropertySources(); |
| 356 | + ps.addFirst(new MockPropertySource() |
| 357 | + .withProperty("p1", "v1") |
| 358 | + .withProperty("p2", "v2") |
| 359 | + .withProperty("p3", "${p1}:${p2}") // nested placeholders |
| 360 | + .withProperty("p4", "${p3}") // deeply nested placeholders |
| 361 | + .withProperty("p5", "${p1}:${p2}:${bogus}") // unresolvable placeholder |
| 362 | + .withProperty("p6", "${p1}:${p2}:${bogus:def}") // unresolvable w/ default |
| 363 | + .withProperty("pL", "${pR}") // cyclic reference left |
| 364 | + .withProperty("pR", "${pL}") // cyclic reference right |
| 365 | + ); |
| 366 | + PropertySourcesPropertyResolver pr = new PropertySourcesPropertyResolver(ps); |
| 367 | + assertThat(pr.getProperty("p1"), equalTo("v1")); |
| 368 | + assertThat(pr.getProperty("p2"), equalTo("v2")); |
| 369 | + assertThat(pr.getProperty("p3"), equalTo("v1:v2")); |
| 370 | + assertThat(pr.getProperty("p4"), equalTo("v1:v2")); |
| 371 | + try { |
| 372 | + pr.getProperty("p5"); |
| 373 | + } catch (IllegalArgumentException ex) { |
| 374 | + assertThat(ex.getMessage(), Matchers.containsString( |
| 375 | + "Could not resolve placeholder 'bogus' in string value [${p1}:${p2}:${bogus}]")); |
| 376 | + } |
| 377 | + assertThat(pr.getProperty("p6"), equalTo("v1:v2:def")); |
| 378 | + try { |
| 379 | + pr.getProperty("pL"); |
| 380 | + } catch (StackOverflowError ex) { |
| 381 | + // no explicit handling for cyclic references for now |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + |
355 | 386 | static interface SomeType { }
|
356 | 387 | static class SpecificType implements SomeType { }
|
357 | 388 | }
|
0 commit comments