|
26 | 26 | import org.mockito.Mock;
|
27 | 27 | import org.mockito.junit.jupiter.MockitoExtension;
|
28 | 28 |
|
| 29 | +import org.springframework.core.env.CompositePropertySource; |
29 | 30 | import org.springframework.core.env.MutablePropertySources;
|
30 | 31 | import org.springframework.core.env.PropertySource;
|
31 | 32 | import org.springframework.mock.env.MockEnvironment;
|
|
39 | 40 | * Tests for {@link DefaultPropertiesPropertySource}.
|
40 | 41 | *
|
41 | 42 | * @author Phillip Webb
|
| 43 | + * @author Madhura Bhave |
42 | 44 | */
|
43 | 45 | @ExtendWith(MockitoExtension.class)
|
44 | 46 | class DefaultPropertiesPropertySourceTests {
|
@@ -104,6 +106,39 @@ void moveToEndWhenNotPresentDoesNothing() {
|
104 | 106 | DefaultPropertiesPropertySource.moveToEnd(environment);
|
105 | 107 | }
|
106 | 108 |
|
| 109 | + @Test |
| 110 | + void addOrMergeWhenExistingNotFoundShouldAdd() { |
| 111 | + MockEnvironment environment = new MockEnvironment(); |
| 112 | + MutablePropertySources propertySources = environment.getPropertySources(); |
| 113 | + DefaultPropertiesPropertySource.addOrMerge(Collections.singletonMap("spring", "boot"), propertySources); |
| 114 | + assertThat(propertySources.contains(DefaultPropertiesPropertySource.NAME)).isTrue(); |
| 115 | + assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("spring")).isEqualTo("boot"); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void addOrMergeWhenExistingFoundShouldMerge() { |
| 120 | + MockEnvironment environment = new MockEnvironment(); |
| 121 | + MutablePropertySources propertySources = environment.getPropertySources(); |
| 122 | + propertySources.addLast(new DefaultPropertiesPropertySource(Collections.singletonMap("spring", "boot"))); |
| 123 | + DefaultPropertiesPropertySource.addOrMerge(Collections.singletonMap("hello", "world"), propertySources); |
| 124 | + assertThat(propertySources.contains(DefaultPropertiesPropertySource.NAME)).isTrue(); |
| 125 | + assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("spring")).isEqualTo("boot"); |
| 126 | + assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("hello")).isEqualTo("world"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void addOrMergeWhenExistingNotMapPropertySourceShouldNotMerge() { |
| 131 | + MockEnvironment environment = new MockEnvironment(); |
| 132 | + MutablePropertySources propertySources = environment.getPropertySources(); |
| 133 | + CompositePropertySource composite = new CompositePropertySource(DefaultPropertiesPropertySource.NAME); |
| 134 | + composite.addPropertySource(new DefaultPropertiesPropertySource(Collections.singletonMap("spring", "boot"))); |
| 135 | + propertySources.addFirst(composite); |
| 136 | + DefaultPropertiesPropertySource.addOrMerge(Collections.singletonMap("hello", "world"), propertySources); |
| 137 | + assertThat(propertySources.contains(DefaultPropertiesPropertySource.NAME)).isTrue(); |
| 138 | + assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("spring")).isNull(); |
| 139 | + assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("hello")).isEqualTo("world"); |
| 140 | + } |
| 141 | + |
107 | 142 | @Test
|
108 | 143 | void moveToEndWhenPresentMovesToEnd() {
|
109 | 144 | MockEnvironment environment = new MockEnvironment();
|
|
0 commit comments