Browse Source
Add `OriginTrackedMapPropertySource` that can be used for any `Map` backed `PropertySource` that is also aware property origins. Rather than directly storing the value in the `Map`, an `OriginTrackedValue` can be used. See gh-8142 See gh-8517pull/8526/head
3 changed files with 246 additions and 0 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* 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.core.env.MapPropertySource; |
||||
|
||||
/** |
||||
* {@link OriginCapablePropertySource} backed by a {@link Map} containing |
||||
* {@link OriginTrackedValue OriginTrackedValues}. |
||||
* |
||||
* @author Madhura Bhave |
||||
* @author Phillip Webb |
||||
* @see OriginTrackedValue |
||||
*/ |
||||
class OriginTrackedMapPropertySource extends MapPropertySource |
||||
implements OriginCapablePropertySource { |
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" }) |
||||
OriginTrackedMapPropertySource(String name, Map 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 PropertyOrigin getPropertyOrigin(String name) { |
||||
Object value = super.getProperty(name); |
||||
if (value instanceof OriginTrackedValue) { |
||||
return ((OriginTrackedValue) value).getOrigin(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
/** |
||||
* Wrapper class for an Object {@code value} and {@link PropertyOrigin origin}. |
||||
* |
||||
* @author Madhura Bhave |
||||
* @see OriginTrackedMapPropertySource |
||||
*/ |
||||
class OriginTrackedValue { |
||||
|
||||
private final Object value; |
||||
|
||||
private final PropertyOrigin origin; |
||||
|
||||
OriginTrackedValue(Object value, PropertyOrigin origin) { |
||||
this.value = value; |
||||
this.origin = origin; |
||||
} |
||||
|
||||
public Object getValue() { |
||||
return this.value; |
||||
} |
||||
|
||||
public PropertyOrigin getOrigin() { |
||||
return this.origin; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return this.value.toString(); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return this.value.hashCode(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (obj == null || obj.getClass() != getClass()) { |
||||
return false; |
||||
} |
||||
return this.value.equals(((OriginTrackedValue) obj).value); |
||||
} |
||||
|
||||
/** |
||||
* Create an {@link OriginTrackedValue} containing the specified {@code value} and |
||||
* {@code origin}. If the source value implements {@link CharSequence} then so will |
||||
* the resulting {@link OriginTrackedValue}. |
||||
* @param value the source value |
||||
* @param origin the origin |
||||
* @return a {@link OriginTrackedValue} or {@code null} if the source value was |
||||
* {@code null}. |
||||
*/ |
||||
public static OriginTrackedValue of(Object value, PropertyOrigin origin) { |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
if (value instanceof CharSequence) { |
||||
return new OriginTrackedCharSequence((CharSequence) value, origin); |
||||
} |
||||
return new OriginTrackedValue(value, origin); |
||||
} |
||||
|
||||
/** |
||||
* {@link OriginTrackedValue} for a {@link CharSequence}. |
||||
*/ |
||||
private static class OriginTrackedCharSequence extends OriginTrackedValue |
||||
implements CharSequence { |
||||
|
||||
OriginTrackedCharSequence(CharSequence value, PropertyOrigin origin) { |
||||
super(value, origin); |
||||
} |
||||
|
||||
@Override |
||||
public int length() { |
||||
return getValue().length(); |
||||
} |
||||
|
||||
@Override |
||||
public char charAt(int index) { |
||||
return getValue().charAt(index); |
||||
} |
||||
|
||||
@Override |
||||
public CharSequence subSequence(int start, int end) { |
||||
return getValue().subSequence(start, end); |
||||
} |
||||
|
||||
@Override |
||||
public CharSequence getValue() { |
||||
return (CharSequence) super.getValue(); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* 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 static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link OriginTrackedMapPropertySource}. |
||||
* |
||||
* @author Madhura Bhave |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class OriginTrackedMapPropertySourceTests { |
||||
|
||||
private Map<String, Object> map = new LinkedHashMap<>(); |
||||
|
||||
private OriginTrackedMapPropertySource source = new OriginTrackedMapPropertySource( |
||||
"test", this.map); |
||||
|
||||
private PropertyOrigin origin = mock(PropertyOrigin.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", new OriginTrackedValue("foo", this.origin)); |
||||
assertThat(this.source.getProperty("test")).isEqualTo("foo"); |
||||
} |
||||
|
||||
@Test |
||||
public void getPropertyOriginWhenMissingShouldReturnNull() throws Exception { |
||||
assertThat(this.source.getPropertyOrigin("test")).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void getPropertyOriginWhenNonTrackedShouldReturnNull() throws Exception { |
||||
this.map.put("test", "foo"); |
||||
assertThat(this.source.getPropertyOrigin("test")).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void getPropertyOriginWhenTrackedShouldReturnOrigin() throws Exception { |
||||
this.map.put("test", new OriginTrackedValue("foo", this.origin)); |
||||
assertThat(this.source.getPropertyOrigin("test")).isEqualTo(this.origin); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue