Browse Source
Move the `Origin` and related classes from `o.s.boot.env` to `o.s.boot.orgin` and extend support. The concept of an origin can now be used outside of just the Spring Environment abstraction. Closes gh-9001pull/8802/merge
21 changed files with 718 additions and 109 deletions
@ -1,39 +0,0 @@ |
|||||||
/* |
|
||||||
* 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 org.springframework.core.env.PropertySource; |
|
||||||
|
|
||||||
/** |
|
||||||
* An additional interface that may be implemented by a {@link PropertySource} that can |
|
||||||
* return origin information. For example a property source that's backed by a file may |
|
||||||
* return origin information for line and column numbers. |
|
||||||
* |
|
||||||
* @author Phillip Webb |
|
||||||
* @since 2.0.0 |
|
||||||
*/ |
|
||||||
public interface OriginCapablePropertySource { |
|
||||||
|
|
||||||
/** |
|
||||||
* Return the origin of the given property name or {@code null} if the origin cannot |
|
||||||
* be determined. |
|
||||||
* @param name the property name |
|
||||||
* @return the origin of the property or {@code null} |
|
||||||
*/ |
|
||||||
PropertyOrigin getPropertyOrigin(String name); |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,56 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* Interface that uniquely represents the origin of an item. For example, a item loaded |
||||||
|
* from a {@link File} may have an origin made up of the file name along with line/column |
||||||
|
* numbers. |
||||||
|
* <p> |
||||||
|
* Implementations must provide sensible {@code hashCode()}, {@code equals(...)} and |
||||||
|
* {@code #toString()} implementations. |
||||||
|
* |
||||||
|
* @author Madhura Bhave |
||||||
|
* @author Phillip Webb |
||||||
|
* @since 2.0.0 |
||||||
|
* @see OriginProvider |
||||||
|
*/ |
||||||
|
public interface Origin { |
||||||
|
|
||||||
|
/** |
||||||
|
* Find the {@link Origin} that an object originated from. Checks if the source object |
||||||
|
* is a {@link OriginProvider} and also searches exception stacks. |
||||||
|
* @param source the source object or {@code null} |
||||||
|
* @return an optional {@link Origin} |
||||||
|
*/ |
||||||
|
static Origin from(Object source) { |
||||||
|
if (source instanceof Origin) { |
||||||
|
return (Origin) source; |
||||||
|
} |
||||||
|
Origin origin = null; |
||||||
|
if (source != null && source instanceof OriginProvider) { |
||||||
|
origin = ((OriginProvider) source).getOrigin(); |
||||||
|
} |
||||||
|
if (origin == null && source != null && source instanceof Throwable) { |
||||||
|
return from(((Throwable) source).getCause()); |
||||||
|
} |
||||||
|
return origin; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
/** |
||||||
|
* An interface that may be implemented by an object that can lookup {@link Origin} |
||||||
|
* information from a given key. Can be used to add origin support to existing classes. |
||||||
|
* |
||||||
|
* @param <K> The lookup key type |
||||||
|
* @author Phillip Webb |
||||||
|
* @since 2.0.0 |
||||||
|
*/ |
||||||
|
public interface OriginLookup<K> { |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the origin of the given key or {@code null} if the origin cannot be |
||||||
|
* determined. |
||||||
|
* @param key the key to lookup |
||||||
|
* @return the origin of the key or {@code null} |
||||||
|
*/ |
||||||
|
Origin getOrigin(K key); |
||||||
|
|
||||||
|
/** |
||||||
|
* Attempt to lookup the origin from the given source. If the source is not a |
||||||
|
* {@link OriginLookup} or if an exception occurs during lookup then {@code null} is |
||||||
|
* returned. |
||||||
|
* @param source the source object |
||||||
|
* @param key the key to lookup |
||||||
|
* @param <K> the key type |
||||||
|
* @return an {@link Origin} or {@code null} |
||||||
|
*/ |
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
static <K> Origin getOrigin(Object source, K key) { |
||||||
|
if (!(source instanceof OriginLookup)) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
try { |
||||||
|
return ((OriginLookup<K>) source).getOrigin(key); |
||||||
|
} |
||||||
|
catch (Throwable ex) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import org.springframework.core.env.PropertySource; |
||||||
|
import org.springframework.util.Assert; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link Origin} from a {@link PropertySource}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
* @since 2.0.0 |
||||||
|
*/ |
||||||
|
public class PropertySourceOrigin implements Origin { |
||||||
|
|
||||||
|
private final PropertySource<?> propertySource; |
||||||
|
|
||||||
|
private final String propertyName; |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link PropertySourceOrigin} instance. |
||||||
|
* @param propertySource the origin property source |
||||||
|
* @param propertyName the origin property name |
||||||
|
*/ |
||||||
|
public PropertySourceOrigin(PropertySource<?> propertySource, String propertyName) { |
||||||
|
Assert.notNull(propertySource, "PropertySource must not be null"); |
||||||
|
Assert.hasLength(propertyName, "PropertyName must not be empty"); |
||||||
|
this.propertySource = propertySource; |
||||||
|
this.propertyName = propertyName; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the origin {@link PropertySource}. |
||||||
|
* @return the origin property source |
||||||
|
*/ |
||||||
|
public PropertySource<?> getPropertySource() { |
||||||
|
return this.propertySource; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the origin property name. |
||||||
|
* @return the origin property name |
||||||
|
*/ |
||||||
|
public String getPropertyName() { |
||||||
|
return this.propertyName; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "\"" + this.propertyName + "\" from property source \"" |
||||||
|
+ this.propertySource.getName() + "\""; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get a {@link Origin} for the given {@link PropertySource} and {@code propertyName}. |
||||||
|
* Will either return an {@link OriginLookup} result or a |
||||||
|
* {@link PropertySourceOrigin}. |
||||||
|
* @param propertySource the origin property source |
||||||
|
* @param name the property name |
||||||
|
* @return the property origin |
||||||
|
*/ |
||||||
|
public static Origin get(PropertySource<?> propertySource, String name) { |
||||||
|
Origin origin = OriginLookup.getOrigin(propertySource, name); |
||||||
|
return (origin != null ? origin : new PropertySourceOrigin(propertySource, name)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
/* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Support for item origin tracking. |
||||||
|
* @see org.springframework.boot.origin.Origin |
||||||
|
*/ |
||||||
|
package org.springframework.boot.origin; |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import org.springframework.util.Assert; |
||||||
|
|
||||||
|
/** |
||||||
|
* Mock {@link Origin} implementation used for testing. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public final class MockOrigin implements Origin { |
||||||
|
|
||||||
|
private final String value; |
||||||
|
|
||||||
|
private MockOrigin(String value) { |
||||||
|
Assert.notNull(value, "Value must not be null"); |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
return this.value.hashCode(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object obj) { |
||||||
|
if (this == obj) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
if (obj == null || getClass() != obj.getClass()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return this.value.equals(((MockOrigin) obj).value); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return this.value; |
||||||
|
} |
||||||
|
|
||||||
|
public static Origin of(String value) { |
||||||
|
return (value == null ? null : new MockOrigin(value)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,63 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.mockito.BDDMockito.given; |
||||||
|
import static org.mockito.BDDMockito.willThrow; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link OriginLookup}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public class OriginLookupTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getOriginWhenSourceIsNullShouldReturnNull() throws Exception { |
||||||
|
assertThat(OriginLookup.getOrigin(null, "foo")).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getOriginWhenSourceIsNotLookupShouldReturnLookupOrigin() |
||||||
|
throws Exception { |
||||||
|
Object source = new Object(); |
||||||
|
assertThat(OriginLookup.getOrigin(source, "foo")).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public void getOriginWhenSourceIsLookupShouldReturnLookupOrigin() throws Exception { |
||||||
|
OriginLookup<String> source = mock(OriginLookup.class); |
||||||
|
Origin origin = MockOrigin.of("bar"); |
||||||
|
given(source.getOrigin("foo")).willReturn(origin); |
||||||
|
assertThat(OriginLookup.getOrigin(source, "foo")).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public void getOriginWhenSourceLookupThrowsAndErrorShouldReturnNull() |
||||||
|
throws Exception { |
||||||
|
OriginLookup<String> source = mock(OriginLookup.class); |
||||||
|
willThrow(RuntimeException.class).given(source).getOrigin("foo"); |
||||||
|
assertThat(OriginLookup.getOrigin(source, "foo")).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,90 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.mockito.BDDMockito.given; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link Origin}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public class OriginTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsNullShouldReturnNull() throws Exception { |
||||||
|
assertThat(Origin.from(null)).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsRegularObjectShouldReturnNull() throws Exception { |
||||||
|
Object source = new Object(); |
||||||
|
assertThat(Origin.from(source)).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsOriginShouldReturnSource() throws Exception { |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
assertThat(Origin.from(origin)).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsOriginProviderShouldReturnProvidedOrigin() |
||||||
|
throws Exception { |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
OriginProvider originProvider = mock(OriginProvider.class); |
||||||
|
given(originProvider.getOrigin()).willReturn(origin); |
||||||
|
assertThat(Origin.from(origin)).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsThrowableShouldUseCause() throws Exception { |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
Exception exception = new RuntimeException(new TestException(origin, null)); |
||||||
|
assertThat(Origin.from(exception)).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void fromWhenSourceIsThrowableAndOriginProviderThatReturnsNullShouldUseCause() |
||||||
|
throws Exception { |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
Exception exception = new TestException(null, new TestException(origin, null)); |
||||||
|
assertThat(Origin.from(exception)).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
private static class TestException extends RuntimeException |
||||||
|
implements OriginProvider { |
||||||
|
|
||||||
|
private final Origin origin; |
||||||
|
|
||||||
|
TestException(Origin origin, Throwable cause) { |
||||||
|
super(cause); |
||||||
|
this.origin = origin; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Origin getOrigin() { |
||||||
|
return this.origin; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,78 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link OriginTrackedValue}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public class OriginTrackedValueTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getValueShouldReturnValue() throws Exception { |
||||||
|
Object value = new Object(); |
||||||
|
assertThat(OriginTrackedValue.of(value).getValue()).isEqualTo(value); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getOriginShouldReturnOrigin() throws Exception { |
||||||
|
Object value = new Object(); |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
assertThat(OriginTrackedValue.of(value, origin).getOrigin()).isEqualTo(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void toStringShouldReturnValueToString() throws Exception { |
||||||
|
Object value = new Object(); |
||||||
|
assertThat(OriginTrackedValue.of(value).toString()).isEqualTo(value.toString()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void hashCodeAndEqualsShouldIgnoreOrigin() throws Exception { |
||||||
|
Object value1 = new Object(); |
||||||
|
OriginTrackedValue tracked1 = OriginTrackedValue.of(value1); |
||||||
|
OriginTrackedValue tracked2 = OriginTrackedValue.of(value1, mock(Origin.class)); |
||||||
|
OriginTrackedValue tracked3 = OriginTrackedValue.of(new Object()); |
||||||
|
assertThat(tracked1.hashCode()).isEqualTo(tracked2.hashCode()); |
||||||
|
assertThat(tracked1).isEqualTo(tracked1).isEqualTo(tracked2) |
||||||
|
.isNotEqualTo(tracked3); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void ofWhenValueIsNullShouldReturnNull() throws Exception { |
||||||
|
assertThat(OriginTrackedValue.of(null)).isNull(); |
||||||
|
assertThat(OriginTrackedValue.of(null, mock(Origin.class))).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void ofWhenValueIsCharSequenceShouldReturnCharSequence() throws Exception { |
||||||
|
String value = "foo"; |
||||||
|
OriginTrackedValue tracked = OriginTrackedValue.of(value); |
||||||
|
assertThat(tracked).isInstanceOf(CharSequence.class); |
||||||
|
CharSequence charSequence = (CharSequence) tracked; |
||||||
|
assertThat(charSequence.length()).isEqualTo(value.length()); |
||||||
|
assertThat(charSequence.charAt(0)).isEqualTo(value.charAt(0)); |
||||||
|
assertThat(charSequence.subSequence(0, 1)).isEqualTo(value.subSequence(0, 1)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,114 @@ |
|||||||
|
/* |
||||||
|
* 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.origin; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
|
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.rules.ExpectedException; |
||||||
|
|
||||||
|
import org.springframework.core.env.MapPropertySource; |
||||||
|
import org.springframework.core.env.PropertySource; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.mockito.BDDMockito.given; |
||||||
|
import static org.mockito.Mockito.mock; |
||||||
|
import static org.mockito.Mockito.withSettings; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link PropertySourceOrigin}. |
||||||
|
* |
||||||
|
* @author Phillip Webb |
||||||
|
*/ |
||||||
|
public class PropertySourceOriginTests { |
||||||
|
|
||||||
|
@Rule |
||||||
|
public ExpectedException thrown = ExpectedException.none(); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWhenPropertySourceIsNullShouldThrowException() { |
||||||
|
this.thrown.expect(IllegalArgumentException.class); |
||||||
|
this.thrown.expectMessage("PropertySource must not be null"); |
||||||
|
new PropertySourceOrigin(null, "name"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWhenPropertyNameIsNullShouldThrowException() throws Exception { |
||||||
|
this.thrown.expect(IllegalArgumentException.class); |
||||||
|
this.thrown.expectMessage("PropertyName must not be empty"); |
||||||
|
new PropertySourceOrigin(mock(PropertySource.class), null); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void createWhenPropertyNameIsEmptyShouldThrowException() throws Exception { |
||||||
|
this.thrown.expect(IllegalArgumentException.class); |
||||||
|
this.thrown.expectMessage("PropertyName must not be empty"); |
||||||
|
new PropertySourceOrigin(mock(PropertySource.class), ""); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getPropertySourceShouldReturnPropertySource() throws Exception { |
||||||
|
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>()); |
||||||
|
PropertySourceOrigin origin = new PropertySourceOrigin(propertySource, "foo"); |
||||||
|
assertThat(origin.getPropertySource()).isEqualTo(propertySource); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getPropertyNameShouldReturnPropertyName() throws Exception { |
||||||
|
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>()); |
||||||
|
PropertySourceOrigin origin = new PropertySourceOrigin(propertySource, "foo"); |
||||||
|
assertThat(origin.getPropertyName()).isEqualTo("foo"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void toStringShouldShowDetails() throws Exception { |
||||||
|
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>()); |
||||||
|
PropertySourceOrigin origin = new PropertySourceOrigin(propertySource, "foo"); |
||||||
|
assertThat(origin.toString()).isEqualTo("\"foo\" from property source \"test\""); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public void getWhenPropertySourceSupportsOriginLookupShouldReturnOrigin() |
||||||
|
throws Exception { |
||||||
|
Origin origin = mock(Origin.class); |
||||||
|
PropertySource<?> propertySource = mock(PropertySource.class, |
||||||
|
withSettings().extraInterfaces(OriginLookup.class)); |
||||||
|
OriginLookup<String> originCapablePropertySource = (OriginLookup<String>) propertySource; |
||||||
|
given(originCapablePropertySource.getOrigin("foo")).willReturn(origin); |
||||||
|
assertThat(PropertySourceOrigin.get(propertySource, "foo")).isSameAs(origin); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getWhenPropertySourceSupportsOriginLookupButNoOriginShouldWrap() |
||||||
|
throws Exception { |
||||||
|
PropertySource<?> propertySource = mock(PropertySource.class, |
||||||
|
withSettings().extraInterfaces(OriginLookup.class)); |
||||||
|
assertThat(PropertySourceOrigin.get(propertySource, "foo")) |
||||||
|
.isInstanceOf(PropertySourceOrigin.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getWhenPropertySourceIsNotOriginAwareShouldWrap() throws Exception { |
||||||
|
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>()); |
||||||
|
PropertySourceOrigin origin = new PropertySourceOrigin(propertySource, "foo"); |
||||||
|
assertThat(origin.getPropertySource()).isEqualTo(propertySource); |
||||||
|
assertThat(origin.getPropertyName()).isEqualTo("foo"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue