Skip to content

Commit 9b52cfd

Browse files
iifawzisbrannen
authored andcommitted
Test escape character support in PropertySourcesPropertyResolver
See gh-34326 See gh-34720 See gh-34861 Signed-off-by: Fawzi Essam <[email protected]>
1 parent dd6eede commit 9b52cfd

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

+52
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,56 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
366366
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
367367
}
368368

369+
@Test
370+
void escapedPlaceholders_areNotEvaluated() {
371+
testProperties.put("prop1", "value1");
372+
testProperties.put("prop2", "value2\\${prop1}");
373+
374+
assertThat(propertyResolver.getProperty("prop2")).isEqualTo("value2${prop1}");
375+
}
376+
377+
@Test
378+
void multipleEscapedPlaceholders_arePreserved() {
379+
testProperties.put("prop1", "value1");
380+
testProperties.put("prop2", "value2");
381+
testProperties.put("complex", "start\\${prop1}middle\\${prop2}end");
382+
383+
assertThat(propertyResolver.getProperty("complex")).isEqualTo("start${prop1}middle${prop2}end");
384+
}
385+
386+
@Test
387+
void doubleBackslashes_areProcessedCorrectly() {
388+
testProperties.put("prop1", "value1");
389+
testProperties.put("doubleEscaped", "value2\\\\${prop1}");
390+
391+
assertThat(propertyResolver.getProperty("doubleEscaped")).isEqualTo("value2\\${prop1}");
392+
}
393+
394+
@Test
395+
void escapedPlaceholdersInNestedProperties() {
396+
MutablePropertySources ps = new MutablePropertySources();
397+
ps.addFirst(new MockPropertySource()
398+
.withProperty("p1", "v1")
399+
.withProperty("p2", "v2")
400+
.withProperty("escaped", "prefix-\\${p1}")
401+
.withProperty("nested", "${escaped}-${p2}")
402+
);
403+
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
404+
405+
assertThat(pr.getProperty("nested")).isEqualTo("prefix-${p1}-v2");
406+
}
407+
408+
409+
@Test
410+
void escapedPlaceholders_withCharSequenceValues() {
411+
MutablePropertySources ps = new MutablePropertySources();
412+
ps.addFirst(new MockPropertySource()
413+
.withProperty("p1", "v1")
414+
.withProperty("charseq", new StringBuilder("prefix-\\${p1}"))
415+
);
416+
PropertyResolver resolver = new PropertySourcesPropertyResolver(ps);
417+
418+
assertThat(resolver.getProperty("charseq")).isEqualTo("prefix-${p1}");
419+
}
420+
369421
}

0 commit comments

Comments
 (0)