Skip to content

Commit 4301186

Browse files
committed
Fix SystemEnvironmentPropertyMapper tests and remove duplicate names
See gh-45741
1 parent bb13eaa commit 4301186

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

There

Whitespace-only changes.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.context.properties.source;
1818

19+
import java.util.ArrayList;
1920
import java.util.List;
2021
import java.util.Locale;
2122
import java.util.function.BiPredicate;
@@ -40,10 +41,18 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
4041

4142
@Override
4243
public List<String> map(ConfigurationPropertyName configurationPropertyName) {
43-
return List.of(configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, true),
44-
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, true),
45-
configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, false),
46-
configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, false));
44+
List<String> mapped = new ArrayList<>(4);
45+
addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, true));
46+
addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, true));
47+
addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.SYSTEM_ENVIRONMENT, false));
48+
addIfMissing(mapped, configurationPropertyName.toString(ToStringFormat.LEGACY_SYSTEM_ENVIRONMENT, false));
49+
return mapped;
50+
}
51+
52+
private void addIfMissing(List<String> list, String value) {
53+
if (!list.contains(value)) {
54+
list.add(value);
55+
}
4756
}
4857

4958
@Override

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapperTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ void mapFromStringShouldReturnBestGuess() {
4848

4949
@Test
5050
void mapFromConfigurationShouldReturnBestGuess() {
51-
assertThat(mapConfigurationPropertyName("server")).containsExactly("SERVER");
52-
assertThat(mapConfigurationPropertyName("server.port")).containsExactly("SERVER_PORT");
53-
assertThat(mapConfigurationPropertyName("host[0]")).containsExactly("HOST_0");
54-
assertThat(mapConfigurationPropertyName("host[0][1]")).containsExactly("HOST_0_1");
55-
assertThat(mapConfigurationPropertyName("host[0].name")).containsExactly("HOST_0_NAME");
56-
assertThat(mapConfigurationPropertyName("host.f00.name")).containsExactly("HOST_F00_NAME");
57-
assertThat(mapConfigurationPropertyName("foo.the-bar")).containsExactly("FOO_THEBAR", "FOO_THE_BAR");
51+
assertThat(mapConfigurationPropertyName("server")).containsExactly("SERVER", "server");
52+
assertThat(mapConfigurationPropertyName("server.port")).containsExactly("SERVER_PORT", "server_port");
53+
assertThat(mapConfigurationPropertyName("host[0]")).containsExactly("HOST_0", "host_0");
54+
assertThat(mapConfigurationPropertyName("host[0][1]")).containsExactly("HOST_0_1", "host_0_1");
55+
assertThat(mapConfigurationPropertyName("host[0].name")).containsExactly("HOST_0_NAME", "host_0_name");
56+
assertThat(mapConfigurationPropertyName("host.f00.name")).containsExactly("HOST_F00_NAME", "host_f00_name");
57+
assertThat(mapConfigurationPropertyName("foo.the-bar")).containsExactly("FOO_THEBAR", "FOO_THE_BAR",
58+
"foo_thebar", "foo_the_bar");
5859
}
5960

6061
@Test

0 commit comments

Comments
 (0)