Skip to content

Commit acaef9c

Browse files
committed
Make origin aware system property source
Closes gh-9252
1 parent 7275c87 commit acaef9c

File tree

4 files changed

+57
-162
lines changed

4 files changed

+57
-162
lines changed

spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedSystemPropertySource.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

spring-boot/src/main/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessor.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
package org.springframework.boot.env;
1818

19-
import java.util.Collections;
20-
import java.util.LinkedHashMap;
2119
import java.util.Map;
2220

2321
import org.springframework.boot.SpringApplication;
24-
import org.springframework.boot.origin.OriginTrackedValue;
22+
import org.springframework.boot.origin.Origin;
23+
import org.springframework.boot.origin.OriginLookup;
2524
import org.springframework.boot.origin.SystemEnvironmentOrigin;
2625
import org.springframework.core.Ordered;
2726
import org.springframework.core.env.ConfigurableEnvironment;
@@ -32,7 +31,7 @@
3231
/**
3332
* An {@link EnvironmentPostProcessor} that replaces the systemEnvironment
3433
* {@link SystemEnvironmentPropertySource} with an
35-
* {@link OriginTrackedSystemPropertySource} that can track the
34+
* {@link OriginAwareSystemEnvironmentPropertySource} that can track the
3635
* {@link SystemEnvironmentOrigin} for every system environment property.
3736
*
3837
* @author Madhura Bhave
@@ -63,17 +62,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
6362
@SuppressWarnings("unchecked")
6463
private void replacePropertySource(ConfigurableEnvironment environment,
6564
String sourceName, PropertySource<?> propertySource) {
66-
if (propertySource.getSource() instanceof Map) {
6765
Map<String, Object> originalSource = (Map<String, Object>) propertySource
6866
.getSource();
69-
Map<String, Object> originTrackedSource = new LinkedHashMap<>(originalSource);
70-
originTrackedSource.entrySet().forEach(e -> e.setValue(OriginTrackedValue
71-
.of(e.getValue(), new SystemEnvironmentOrigin(e.getKey()))));
72-
OriginTrackedSystemPropertySource source = new OriginTrackedSystemPropertySource(
73-
sourceName, Collections.unmodifiableMap(originTrackedSource));
67+
SystemEnvironmentPropertySource source = new OriginAwareSystemEnvironmentPropertySource(sourceName, originalSource);
7468
environment.getPropertySources().replace(sourceName, source);
7569
}
76-
}
7770

7871
@Override
7972
public int getOrder() {
@@ -84,4 +77,24 @@ public void setOrder(int order) {
8477
this.order = order;
8578
}
8679

80+
/**
81+
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
82+
*/
83+
protected static class OriginAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
84+
implements OriginLookup<String> {
85+
86+
OriginAwareSystemEnvironmentPropertySource(String name, Map<String, Object> source) {
87+
super(name, source);
88+
}
89+
90+
@Override
91+
public Origin getOrigin(String key) {
92+
String property = resolvePropertyName(key);
93+
if (super.containsProperty(property)) {
94+
return new SystemEnvironmentOrigin(property);
95+
}
96+
return null;
97+
}
98+
}
99+
87100
}

spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedSystemPropertySourceTests.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

spring-boot/src/test/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessorTests.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.springframework.boot.env;
1818

19+
import java.util.Collections;
1920
import java.util.Map;
2021

2122
import org.junit.Before;
2223
import org.junit.Test;
2324

24-
import org.springframework.boot.origin.OriginTrackedValue;
25+
import org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor.OriginAwareSystemEnvironmentPropertySource;
2526
import org.springframework.boot.origin.SystemEnvironmentOrigin;
2627
import org.springframework.core.env.ConfigurableEnvironment;
2728
import org.springframework.core.env.PropertySource;
2829
import org.springframework.core.env.StandardEnvironment;
30+
import org.springframework.core.env.SystemEnvironmentPropertySource;
2931

3032
import static org.assertj.core.api.Assertions.assertThat;
3133

@@ -50,27 +52,49 @@ public void postProcessShouldReplaceSystemEnvironmentPropertySource()
5052
postProcessor.postProcessEnvironment(this.environment, null);
5153
PropertySource<?> replaced = this.environment.getPropertySources()
5254
.get("systemEnvironment");
53-
assertThat(replaced).isInstanceOf(OriginTrackedSystemPropertySource.class);
55+
assertThat(replaced).isInstanceOf(OriginAwareSystemEnvironmentPropertySource.class);
5456
}
5557

5658
@Test
5759
@SuppressWarnings("unchecked")
58-
public void replacedPropertySourceShouldHaveOriginTrackedValues() throws Exception {
60+
public void replacedPropertySourceShouldBeOriginAware() throws Exception {
5961
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
6062
PropertySource<?> original = this.environment.getPropertySources()
6163
.get("systemEnvironment");
6264
postProcessor.postProcessEnvironment(this.environment, null);
63-
PropertySource<?> replaced = this.environment.getPropertySources()
65+
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
6466
.get("systemEnvironment");
6567
Map<String, Object> originalMap = (Map<String, Object>) original.getSource();
66-
Map<String, OriginTrackedValue> replacedMap = (Map<String, OriginTrackedValue>) replaced
68+
Map<String, Object> replacedMap = replaced
6769
.getSource();
6870
for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
69-
OriginTrackedValue actual = replacedMap.get(entry.getKey());
70-
assertThat(actual.getValue()).isEqualTo(entry.getValue());
71-
assertThat(actual.getOrigin())
72-
.isEqualTo(new SystemEnvironmentOrigin(entry.getKey()));
71+
Object actual = replacedMap.get(entry.getKey());
72+
assertThat(actual).isEqualTo(entry.getValue());
73+
assertThat(replaced.getOrigin(entry.getKey())).isInstanceOf(SystemEnvironmentOrigin.class);
7374
}
7475
}
7576

77+
@Test
78+
public void replacedPropertySourceWhenPropertyAbsentShouldReturnNullOrigin() throws Exception {
79+
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
80+
postProcessor.postProcessEnvironment(this.environment, null);
81+
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
82+
.get("systemEnvironment");
83+
assertThat(replaced.getOrigin("NON_EXISTENT")).isNull();
84+
}
85+
86+
@Test
87+
@SuppressWarnings("unchecked")
88+
public void replacedPropertySourceShouldResolveProperty() throws Exception {
89+
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
90+
Map<String, Object> source = Collections.singletonMap("FOO_BAR_BAZ", "hello");
91+
this.environment.getPropertySources().replace("systemEnvironment", new SystemEnvironmentPropertySource("systemEnvironment", source));
92+
postProcessor.postProcessEnvironment(this.environment, null);
93+
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
94+
.get("systemEnvironment");
95+
SystemEnvironmentOrigin origin = (SystemEnvironmentOrigin) replaced.getOrigin("foo.bar.baz");
96+
assertThat(origin.getProperty()).isEqualTo("FOO_BAR_BAZ");
97+
assertThat(replaced.getProperty("foo.bar.baz")).isEqualTo("hello");
98+
}
99+
76100
}

0 commit comments

Comments
 (0)