Commit acaef9c9 authored by Madhura Bhave's avatar Madhura Bhave

Make origin aware system property source

Closes gh-9252
parent 7275c879
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.env;
import java.util.Map;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.core.env.SystemEnvironmentPropertySource;
/**
* {@link SystemEnvironmentPropertySource} and {@link OriginLookup} backed by a map
* containing {@link OriginTrackedValue OriginTrackedValues}.
*
* @author Madhura Bhave
* @since 2.0.0
*/
public class OriginTrackedSystemPropertySource extends SystemEnvironmentPropertySource
implements OriginLookup<String> {
/**
* Create a new {@code OriginTrackedSystemPropertySource} with the given name and
* delegating to the given {@code MapPropertySource}.
* @param name the property name
* @param source the underlying map
*/
public OriginTrackedSystemPropertySource(String name, Map<String, Object> source) {
super(name, source);
}
@Override
public Object getProperty(String name) {
Object value = super.getProperty(name);
if (value instanceof OriginTrackedValue) {
return ((OriginTrackedValue) value).getValue();
}
return value;
}
@Override
public Origin getOrigin(String key) {
Object value = super.getProperty(key);
if (value instanceof OriginTrackedValue) {
return ((OriginTrackedValue) value).getOrigin();
}
return null;
}
}
......@@ -16,12 +16,11 @@
package org.springframework.boot.env;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.boot.origin.SystemEnvironmentOrigin;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
......@@ -32,7 +31,7 @@ import org.springframework.core.env.SystemEnvironmentPropertySource;
/**
* An {@link EnvironmentPostProcessor} that replaces the systemEnvironment
* {@link SystemEnvironmentPropertySource} with an
* {@link OriginTrackedSystemPropertySource} that can track the
* {@link OriginAwareSystemEnvironmentPropertySource} that can track the
* {@link SystemEnvironmentOrigin} for every system environment property.
*
* @author Madhura Bhave
......@@ -63,17 +62,11 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor
@SuppressWarnings("unchecked")
private void replacePropertySource(ConfigurableEnvironment environment,
String sourceName, PropertySource<?> propertySource) {
if (propertySource.getSource() instanceof Map) {
Map<String, Object> originalSource = (Map<String, Object>) propertySource
.getSource();
Map<String, Object> originTrackedSource = new LinkedHashMap<>(originalSource);
originTrackedSource.entrySet().forEach(e -> e.setValue(OriginTrackedValue
.of(e.getValue(), new SystemEnvironmentOrigin(e.getKey()))));
OriginTrackedSystemPropertySource source = new OriginTrackedSystemPropertySource(
sourceName, Collections.unmodifiableMap(originTrackedSource));
SystemEnvironmentPropertySource source = new OriginAwareSystemEnvironmentPropertySource(sourceName, originalSource);
environment.getPropertySources().replace(sourceName, source);
}
}
@Override
public int getOrder() {
......@@ -84,4 +77,24 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor
this.order = order;
}
/**
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
*/
protected static class OriginAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
implements OriginLookup<String> {
OriginAwareSystemEnvironmentPropertySource(String name, Map<String, Object> source) {
super(name, source);
}
@Override
public Origin getOrigin(String key) {
String property = resolvePropertyName(key);
if (super.containsProperty(property)) {
return new SystemEnvironmentOrigin(property);
}
return null;
}
}
}
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.env;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedValue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link OriginTrackedSystemPropertySource}.
*
* @author Madhura Bhave
*/
public class OriginTrackedSystemPropertySourceTests {
private Map<String, Object> map = new LinkedHashMap<>();
private OriginTrackedSystemPropertySource source = new OriginTrackedSystemPropertySource(
"test", this.map);
private Origin origin = mock(Origin.class);
@Test
public void getPropertyWhenMissingShouldReturnNull() throws Exception {
assertThat(this.source.getProperty("test")).isNull();
}
@Test
public void getPropertyWhenNonTrackedShouldReturnValue() throws Exception {
this.map.put("test", "foo");
assertThat(this.source.getProperty("test")).isEqualTo("foo");
}
@Test
public void getPropertyWhenTrackedShouldReturnValue() throws Exception {
this.map.put("test", OriginTrackedValue.of("foo", this.origin));
assertThat(this.source.getProperty("test")).isEqualTo("foo");
}
@Test
public void getPropertyOriginWhenMissingShouldReturnNull() throws Exception {
assertThat(this.source.getOrigin("test")).isNull();
}
@Test
public void getPropertyOriginWhenNonTrackedShouldReturnNull() throws Exception {
this.map.put("test", "foo");
assertThat(this.source.getOrigin("test")).isNull();
}
@Test
public void getPropertyOriginWhenTrackedShouldReturnOrigin() throws Exception {
this.map.put("test", OriginTrackedValue.of("foo", this.origin));
assertThat(this.source.getOrigin("test")).isEqualTo(this.origin);
}
}
......@@ -16,16 +16,18 @@
package org.springframework.boot.env;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor.OriginAwareSystemEnvironmentPropertySource;
import org.springframework.boot.origin.SystemEnvironmentOrigin;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -50,27 +52,49 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
postProcessor.postProcessEnvironment(this.environment, null);
PropertySource<?> replaced = this.environment.getPropertySources()
.get("systemEnvironment");
assertThat(replaced).isInstanceOf(OriginTrackedSystemPropertySource.class);
assertThat(replaced).isInstanceOf(OriginAwareSystemEnvironmentPropertySource.class);
}
@Test
@SuppressWarnings("unchecked")
public void replacedPropertySourceShouldHaveOriginTrackedValues() throws Exception {
public void replacedPropertySourceShouldBeOriginAware() throws Exception {
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
PropertySource<?> original = this.environment.getPropertySources()
.get("systemEnvironment");
postProcessor.postProcessEnvironment(this.environment, null);
PropertySource<?> replaced = this.environment.getPropertySources()
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
.get("systemEnvironment");
Map<String, Object> originalMap = (Map<String, Object>) original.getSource();
Map<String, OriginTrackedValue> replacedMap = (Map<String, OriginTrackedValue>) replaced
Map<String, Object> replacedMap = replaced
.getSource();
for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
OriginTrackedValue actual = replacedMap.get(entry.getKey());
assertThat(actual.getValue()).isEqualTo(entry.getValue());
assertThat(actual.getOrigin())
.isEqualTo(new SystemEnvironmentOrigin(entry.getKey()));
Object actual = replacedMap.get(entry.getKey());
assertThat(actual).isEqualTo(entry.getValue());
assertThat(replaced.getOrigin(entry.getKey())).isInstanceOf(SystemEnvironmentOrigin.class);
}
}
@Test
public void replacedPropertySourceWhenPropertyAbsentShouldReturnNullOrigin() throws Exception {
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
postProcessor.postProcessEnvironment(this.environment, null);
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
.get("systemEnvironment");
assertThat(replaced.getOrigin("NON_EXISTENT")).isNull();
}
@Test
@SuppressWarnings("unchecked")
public void replacedPropertySourceShouldResolveProperty() throws Exception {
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
Map<String, Object> source = Collections.singletonMap("FOO_BAR_BAZ", "hello");
this.environment.getPropertySources().replace("systemEnvironment", new SystemEnvironmentPropertySource("systemEnvironment", source));
postProcessor.postProcessEnvironment(this.environment, null);
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment.getPropertySources()
.get("systemEnvironment");
SystemEnvironmentOrigin origin = (SystemEnvironmentOrigin) replaced.getOrigin("foo.bar.baz");
assertThat(origin.getProperty()).isEqualTo("FOO_BAR_BAZ");
assertThat(replaced.getProperty("foo.bar.baz")).isEqualTo("hello");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment