|
16 | 16 |
|
17 | 17 | package org.springframework.context.support;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Field; |
19 | 20 | import java.util.ArrayList;
|
20 | 21 | import java.util.Collections;
|
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.Optional;
|
23 | 24 | import java.util.Properties;
|
24 | 25 |
|
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
25 | 28 | import org.junit.jupiter.api.Nested;
|
26 | 29 | import org.junit.jupiter.api.Test;
|
27 | 30 | import org.junit.jupiter.params.ParameterizedTest;
|
|
37 | 40 | import org.springframework.context.annotation.Bean;
|
38 | 41 | import org.springframework.context.annotation.Configuration;
|
39 | 42 | import org.springframework.context.annotation.Scope;
|
| 43 | +import org.springframework.core.SpringProperties; |
40 | 44 | import org.springframework.core.convert.support.DefaultConversionService;
|
| 45 | +import org.springframework.core.env.AbstractPropertyResolver; |
41 | 46 | import org.springframework.core.env.EnumerablePropertySource;
|
42 | 47 | import org.springframework.core.env.MutablePropertySources;
|
43 | 48 | import org.springframework.core.env.PropertySource;
|
|
47 | 52 | import org.springframework.core.testfixture.env.MockPropertySource;
|
48 | 53 | import org.springframework.mock.env.MockEnvironment;
|
49 | 54 | import org.springframework.util.PlaceholderResolutionException;
|
| 55 | +import org.springframework.util.ReflectionUtils; |
50 | 56 |
|
51 | 57 | import static org.assertj.core.api.Assertions.assertThat;
|
52 | 58 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 59 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
53 | 60 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
54 | 61 | import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
|
55 | 62 | import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
|
| 63 | +import static org.springframework.core.env.AbstractPropertyResolver.DEFAULT_PLACEHOLDER_ESCAPE_CHARACTER_PROPERTY_NAME; |
56 | 64 |
|
57 | 65 | /**
|
58 | 66 | * Tests for {@link PropertySourcesPlaceholderConfigurer}.
|
@@ -667,6 +675,108 @@ private static DefaultListableBeanFactory createBeanFactory() {
|
667 | 675 | }
|
668 | 676 |
|
669 | 677 |
|
| 678 | + /** |
| 679 | + * Tests that globally set the default escape character (or disable it) and |
| 680 | + * rely on nested placeholder resolution. |
| 681 | + */ |
| 682 | + @Nested |
| 683 | + class GlobalDefaultEscapeCharacterTests { |
| 684 | + |
| 685 | + private static final Field defaultEscapeCharacterField = |
| 686 | + ReflectionUtils.findField(AbstractPropertyResolver.class, "defaultEscapeCharacter"); |
| 687 | + |
| 688 | + static { |
| 689 | + ReflectionUtils.makeAccessible(defaultEscapeCharacterField); |
| 690 | + } |
| 691 | + |
| 692 | + |
| 693 | + @BeforeEach |
| 694 | + void resetStateBeforeEachTest() { |
| 695 | + resetState(); |
| 696 | + } |
| 697 | + |
| 698 | + @AfterAll |
| 699 | + static void resetState() { |
| 700 | + ReflectionUtils.setField(defaultEscapeCharacterField, null, Character.MIN_VALUE); |
| 701 | + setSpringProperty(null); |
| 702 | + } |
| 703 | + |
| 704 | + |
| 705 | + @Test // gh-34865 |
| 706 | + void defaultEscapeCharacterSetToXyz() { |
| 707 | + setSpringProperty("XYZ"); |
| 708 | + |
| 709 | + assertThatIllegalArgumentException() |
| 710 | + .isThrownBy(PropertySourcesPlaceholderConfigurer::new) |
| 711 | + .withMessage("Value [XYZ] for property [%s] must be a single character or an empty string", |
| 712 | + DEFAULT_PLACEHOLDER_ESCAPE_CHARACTER_PROPERTY_NAME); |
| 713 | + } |
| 714 | + |
| 715 | + @Test // gh-34865 |
| 716 | + void defaultEscapeCharacterDisabled() { |
| 717 | + setSpringProperty(""); |
| 718 | + |
| 719 | + MockEnvironment env = new MockEnvironment() |
| 720 | + .withProperty("user.home", "admin") |
| 721 | + .withProperty("my.property", "\\DOMAIN\\${user.home}"); |
| 722 | + |
| 723 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 724 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 725 | + ppc.setEnvironment(env); |
| 726 | + ppc.postProcessBeanFactory(bf); |
| 727 | + |
| 728 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("\\DOMAIN\\admin"); |
| 729 | + } |
| 730 | + |
| 731 | + @Test // gh-34865 |
| 732 | + void defaultEscapeCharacterSetToBackslash() { |
| 733 | + setSpringProperty("\\"); |
| 734 | + |
| 735 | + MockEnvironment env = new MockEnvironment() |
| 736 | + .withProperty("user.home", "admin") |
| 737 | + .withProperty("my.property", "\\DOMAIN\\${user.home}"); |
| 738 | + |
| 739 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 740 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 741 | + ppc.setEnvironment(env); |
| 742 | + ppc.postProcessBeanFactory(bf); |
| 743 | + |
| 744 | + // \DOMAIN\${user.home} resolves to \DOMAIN${user.home} instead of \DOMAIN\admin |
| 745 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("\\DOMAIN${user.home}"); |
| 746 | + } |
| 747 | + |
| 748 | + @Test // gh-34865 |
| 749 | + void defaultEscapeCharacterSetToTilde() { |
| 750 | + setSpringProperty("~"); |
| 751 | + |
| 752 | + MockEnvironment env = new MockEnvironment() |
| 753 | + .withProperty("user.home", "admin\\~${nested}") |
| 754 | + .withProperty("my.property", "DOMAIN\\${user.home}\\~${enigma}"); |
| 755 | + |
| 756 | + DefaultListableBeanFactory bf = createBeanFactory(); |
| 757 | + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); |
| 758 | + ppc.setEnvironment(env); |
| 759 | + ppc.postProcessBeanFactory(bf); |
| 760 | + |
| 761 | + assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("DOMAIN\\admin\\${nested}\\${enigma}"); |
| 762 | + } |
| 763 | + |
| 764 | + private static void setSpringProperty(String value) { |
| 765 | + SpringProperties.setProperty(DEFAULT_PLACEHOLDER_ESCAPE_CHARACTER_PROPERTY_NAME, value); |
| 766 | + } |
| 767 | + |
| 768 | + private static DefaultListableBeanFactory createBeanFactory() { |
| 769 | + BeanDefinition beanDefinition = genericBeanDefinition(TestBean.class) |
| 770 | + .addPropertyValue("name", "${my.property}") |
| 771 | + .getBeanDefinition(); |
| 772 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 773 | + bf.registerBeanDefinition("testBean",beanDefinition); |
| 774 | + return bf; |
| 775 | + } |
| 776 | + |
| 777 | + } |
| 778 | + |
| 779 | + |
670 | 780 | private static class OptionalTestBean {
|
671 | 781 |
|
672 | 782 | private Optional<String> name;
|
|
0 commit comments