Fix package tangle and polish prefix support
Polish the prefix support introduced in commit a8592f36d4
and fix a
package tangle between `boot.context.properties.source` and `boot.env`.
The `Prefix` interface has now been moved into a new default method on
`OriginLookup`.
See gh-3450
This commit is contained in:
parent
2c0e0e0662
commit
bb38ee38a6
|
@ -993,6 +993,7 @@ The `YamlPropertiesFactoryBean` loads YAML as `Properties` and the `YamlMapFacto
|
|||
You can also use the `YamlPropertySourceLoader` class if you want to load YAML as a Spring `PropertySource`.
|
||||
|
||||
|
||||
|
||||
[[boot-features-external-config-random-values]]
|
||||
=== Configuring Random Values
|
||||
The `RandomValuePropertySource` is useful for injecting random values (for example, into secrets or test cases).
|
||||
|
@ -1013,13 +1014,15 @@ The `+random.int*+` syntax is `OPEN value (,max) CLOSE` where the `OPEN,CLOSE` a
|
|||
If `max` is provided, then `value` is the minimum value and `max` is the maximum value (exclusive).
|
||||
|
||||
|
||||
|
||||
[[boot-features-external-config-system-environment]]
|
||||
=== Configuring System Environment Properties
|
||||
Spring Boot supports setting a prefix for environment properties.
|
||||
This is useful if the system environment is shared by multiple Spring Boot applications with different configuration requirements.
|
||||
The prefix for system environment properties can be set directly on `SpringApplication`.
|
||||
|
||||
For example, if you set the prefix to `input`, a property such as `foo.bar` will also be resolved as `input.foo.bar` in the system environment.
|
||||
For example, if you set the prefix to `input`, a property such as `remote.timeout` will also be resolved as `input.remote.timeout` in the system environment.
|
||||
|
||||
|
||||
|
||||
[[boot-features-external-config-typesafe-configuration-properties]]
|
||||
|
|
|
@ -364,9 +364,8 @@ public class SpringApplication {
|
|||
listeners.environmentPrepared(bootstrapContext, environment);
|
||||
DefaultPropertiesPropertySource.moveToEnd(environment);
|
||||
configureAdditionalProfiles(environment);
|
||||
if (environment.getProperty("spring.main.environment-prefix") != null) {
|
||||
throw new IllegalStateException("Environment prefix cannot be set via properties.");
|
||||
}
|
||||
Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
|
||||
"Environment prefix cannot be set via properties.");
|
||||
bindToSpringApplication(environment);
|
||||
if (!this.isCustomEnvironment) {
|
||||
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
|
||||
|
@ -1181,10 +1180,22 @@ public class SpringApplication {
|
|||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a prefix that should be applied when obtaining configuration properties from
|
||||
* the system environment.
|
||||
* @return the environment property prefix
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public String getEnvironmentPrefix() {
|
||||
return this.environmentPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that should be applied when obtaining configuration properties from
|
||||
* the system environment.
|
||||
* @param environmentPrefix the environment property prefix to set
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public void setEnvironmentPrefix(String environmentPrefix) {
|
||||
this.environmentPrefix = environmentPrefix;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -24,6 +24,7 @@ import java.util.Map;
|
|||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A configuration property name composed of elements separated by dots. User created
|
||||
|
@ -195,19 +196,32 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link ConfigurationPropertyName} by appending the given elements.
|
||||
* @param elements the elements to append
|
||||
* Create a new {@link ConfigurationPropertyName} by appending the given suffix.
|
||||
* @param suffix the elements to append
|
||||
* @return a new {@link ConfigurationPropertyName}
|
||||
* @throws InvalidConfigurationPropertyNameException if the result is not valid
|
||||
*/
|
||||
public ConfigurationPropertyName append(String elements) {
|
||||
if (elements == null) {
|
||||
public ConfigurationPropertyName append(String suffix) {
|
||||
if (!StringUtils.hasLength(suffix)) {
|
||||
return this;
|
||||
}
|
||||
Elements additionalElements = probablySingleElementOf(elements);
|
||||
Elements additionalElements = probablySingleElementOf(suffix);
|
||||
return new ConfigurationPropertyName(this.elements.append(additionalElements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link ConfigurationPropertyName} by appending the given suffix.
|
||||
* @param suffix the elements to append
|
||||
* @return a new {@link ConfigurationPropertyName}
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public ConfigurationPropertyName append(ConfigurationPropertyName suffix) {
|
||||
if (suffix == null) {
|
||||
return this;
|
||||
}
|
||||
return new ConfigurationPropertyName(this.elements.append(suffix.elements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parent of this {@link ConfigurationPropertyName} or
|
||||
* {@link ConfigurationPropertyName#EMPTY} if there is no parent.
|
||||
|
@ -232,6 +246,27 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
|
|||
return new ConfigurationPropertyName(this.elements.chop(size));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new {@link ConfigurationPropertyName} by based on this name offset by
|
||||
* specific element index. For example, {@code chop(1)} on the name {@code foo.bar}
|
||||
* will return {@code bar}.
|
||||
* @param offset the element offset
|
||||
* @return the sub name
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public ConfigurationPropertyName subName(int offset) {
|
||||
if (offset == 0) {
|
||||
return this;
|
||||
}
|
||||
if (offset == getNumberOfElements()) {
|
||||
return EMPTY;
|
||||
}
|
||||
if (offset < 0 || offset > getNumberOfElements()) {
|
||||
throw new IndexOutOfBoundsException("Offset: " + offset + ", NumberOfElements: " + getNumberOfElements());
|
||||
}
|
||||
return new ConfigurationPropertyName(this.elements.subElements(offset));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this element is an immediate parent of the specified name.
|
||||
* @param name the name to check
|
||||
|
@ -718,7 +753,7 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
|
|||
ElementType[] type = new ElementType[size];
|
||||
System.arraycopy(this.type, 0, type, 0, this.size);
|
||||
System.arraycopy(additional.type, 0, type, this.size, additional.size);
|
||||
CharSequence[] resolved = newResolved(size);
|
||||
CharSequence[] resolved = newResolved(0, size);
|
||||
for (int i = 0; i < additional.size; i++) {
|
||||
resolved[this.size + i] = additional.get(i);
|
||||
}
|
||||
|
@ -726,11 +761,23 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
|
|||
}
|
||||
|
||||
Elements chop(int size) {
|
||||
CharSequence[] resolved = newResolved(size);
|
||||
CharSequence[] resolved = newResolved(0, size);
|
||||
return new Elements(this.source, size, this.start, this.end, this.type, resolved);
|
||||
}
|
||||
|
||||
private CharSequence[] newResolved(int size) {
|
||||
Elements subElements(int offset) {
|
||||
int size = this.size - offset;
|
||||
CharSequence[] resolved = newResolved(offset, size);
|
||||
int[] start = new int[size];
|
||||
System.arraycopy(this.start, offset, start, 0, size);
|
||||
int[] end = new int[size];
|
||||
System.arraycopy(this.end, offset, end, 0, size);
|
||||
ElementType[] type = new ElementType[size];
|
||||
System.arraycopy(this.type, offset, type, 0, size);
|
||||
return new Elements(this.source, size, start, end, type, resolved);
|
||||
}
|
||||
|
||||
private CharSequence[] newResolved(int offset, int size) {
|
||||
CharSequence[] resolved = new CharSequence[size];
|
||||
if (this.resolved != null) {
|
||||
System.arraycopy(this.resolved, 0, resolved, 0, Math.min(size, this.size));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -20,6 +20,7 @@ import java.util.function.Predicate;
|
|||
|
||||
import org.springframework.boot.origin.OriginTrackedValue;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A source of {@link ConfigurationProperty ConfigurationProperties}.
|
||||
|
@ -78,9 +79,10 @@ public interface ConfigurationPropertySource {
|
|||
* Return a variant of this source that supports a prefix.
|
||||
* @param prefix the prefix for properties in the source
|
||||
* @return a {@link ConfigurationPropertySource} instance supporting a prefix
|
||||
* @since 2.5.0
|
||||
*/
|
||||
default ConfigurationPropertySource withPrefix(String prefix) {
|
||||
return new PrefixedConfigurationPropertySource(this, prefix);
|
||||
return (StringUtils.hasText(prefix)) ? new PrefixedConfigurationPropertySource(this, prefix) : this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -21,6 +21,7 @@ import java.util.function.Predicate;
|
|||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.boot.origin.OriginTrackedValue;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link ConfigurationPropertySource} with a fully {@link Iterable} set of entries.
|
||||
|
@ -75,7 +76,7 @@ public interface IterableConfigurationPropertySource
|
|||
|
||||
@Override
|
||||
default IterableConfigurationPropertySource withPrefix(String prefix) {
|
||||
return new PrefixedIterableConfigurationPropertySource(this, prefix);
|
||||
return (StringUtils.hasText(prefix)) ? new PrefixedIterableConfigurationPropertySource(this, prefix) : this;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.boot.context.properties.source;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link ConfigurationPropertySource} supporting a prefix.
|
||||
|
@ -28,13 +27,17 @@ class PrefixedConfigurationPropertySource implements ConfigurationPropertySource
|
|||
|
||||
private final ConfigurationPropertySource source;
|
||||
|
||||
private final String prefix;
|
||||
private final ConfigurationPropertyName prefix;
|
||||
|
||||
PrefixedConfigurationPropertySource(ConfigurationPropertySource source, String prefix) {
|
||||
Assert.notNull(source, "Source must not be null");
|
||||
Assert.notNull(prefix, "Prefix must not be null");
|
||||
Assert.hasText(prefix, "Prefix must not be empty");
|
||||
this.source = source;
|
||||
this.prefix = prefix;
|
||||
this.prefix = ConfigurationPropertyName.of(prefix);
|
||||
}
|
||||
|
||||
protected final ConfigurationPropertyName getPrefix() {
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -47,11 +50,7 @@ class PrefixedConfigurationPropertySource implements ConfigurationPropertySource
|
|||
}
|
||||
|
||||
private ConfigurationPropertyName getPrefixedName(ConfigurationPropertyName name) {
|
||||
if (!StringUtils.hasText(this.prefix)) {
|
||||
return name;
|
||||
}
|
||||
String prefix = this.prefix + ".";
|
||||
return ConfigurationPropertyName.of(prefix + name);
|
||||
return this.prefix.append(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,8 +67,4 @@ class PrefixedConfigurationPropertySource implements ConfigurationPropertySource
|
|||
return this.source;
|
||||
}
|
||||
|
||||
protected String getPrefix() {
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,8 +18,6 @@ package org.springframework.boot.context.properties.source;
|
|||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An iterable {@link PrefixedConfigurationPropertySource}.
|
||||
*
|
||||
|
@ -34,17 +32,11 @@ class PrefixedIterableConfigurationPropertySource extends PrefixedConfigurationP
|
|||
|
||||
@Override
|
||||
public Stream<ConfigurationPropertyName> stream() {
|
||||
if (!StringUtils.hasText(getPrefix())) {
|
||||
return getSource().stream();
|
||||
}
|
||||
ConfigurationPropertyName prefix = ConfigurationPropertyName.of(getPrefix());
|
||||
return getSource().stream().map((propertyName) -> {
|
||||
if (prefix.isAncestorOf(propertyName)) {
|
||||
String name = propertyName.toString();
|
||||
return ConfigurationPropertyName.of(name.substring(getPrefix().length() + 1));
|
||||
}
|
||||
return propertyName;
|
||||
});
|
||||
return getSource().stream().map(this::stripPrefix);
|
||||
}
|
||||
|
||||
private ConfigurationPropertyName stripPrefix(ConfigurationPropertyName name) {
|
||||
return (getPrefix().isAncestorOf(name)) ? name.subName(getPrefix().getNumberOfElements()) : name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -24,7 +24,7 @@ import java.util.NoSuchElementException;
|
|||
import java.util.Random;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.env.Prefixed;
|
||||
import org.springframework.boot.origin.OriginLookup;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
@ -64,8 +64,8 @@ class SpringConfigurationPropertySources implements Iterable<ConfigurationProper
|
|||
return result;
|
||||
}
|
||||
result = SpringConfigurationPropertySource.from(source);
|
||||
if (source instanceof Prefixed) {
|
||||
result = result.withPrefix(((Prefixed) source).getPrefix());
|
||||
if (source instanceof OriginLookup) {
|
||||
result = result.withPrefix(((OriginLookup<?>) source).getPrefix());
|
||||
}
|
||||
this.cache.put(source, result);
|
||||
return result;
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2021 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
/**
|
||||
* Interface that can be implemented by a
|
||||
* {@link org.springframework.core.env.PropertySource} that can be used with a prefix.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @since 2.5.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Prefixed {
|
||||
|
||||
String getPrefix();
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -32,7 +32,7 @@ import org.springframework.util.StringUtils;
|
|||
/**
|
||||
* An {@link EnvironmentPostProcessor} that replaces the systemEnvironment
|
||||
* {@link SystemEnvironmentPropertySource} with an
|
||||
* {@link OriginAndPrefixAwareSystemEnvironmentPropertySource} that can track the
|
||||
* {@link OriginAwareSystemEnvironmentPropertySource} that can track the
|
||||
* {@link SystemEnvironmentOrigin} for every system environment property.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
|
@ -60,7 +60,7 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements
|
|||
private void replacePropertySource(ConfigurableEnvironment environment, String sourceName,
|
||||
PropertySource<?> propertySource, String environmentPrefix) {
|
||||
Map<String, Object> originalSource = (Map<String, Object>) propertySource.getSource();
|
||||
SystemEnvironmentPropertySource source = new OriginAndPrefixAwareSystemEnvironmentPropertySource(sourceName,
|
||||
SystemEnvironmentPropertySource source = new OriginAwareSystemEnvironmentPropertySource(sourceName,
|
||||
originalSource, environmentPrefix);
|
||||
environment.getPropertySources().replace(sourceName, source);
|
||||
}
|
||||
|
@ -77,26 +77,24 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements
|
|||
/**
|
||||
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
|
||||
*/
|
||||
protected static class OriginAndPrefixAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
|
||||
implements OriginLookup<String>, Prefixed {
|
||||
protected static class OriginAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
|
||||
implements OriginLookup<String> {
|
||||
|
||||
private final String environmentPrefix;
|
||||
private final String prefix;
|
||||
|
||||
OriginAndPrefixAwareSystemEnvironmentPropertySource(String name, Map<String, Object> source,
|
||||
String environmentPrefix) {
|
||||
OriginAwareSystemEnvironmentPropertySource(String name, Map<String, Object> source, String environmentPrefix) {
|
||||
super(name, source);
|
||||
this.environmentPrefix = getEnvironmentPrefix(environmentPrefix);
|
||||
this.prefix = determinePrefix(environmentPrefix);
|
||||
}
|
||||
|
||||
private String getEnvironmentPrefix(String environmentPrefix) {
|
||||
String prefix = environmentPrefix;
|
||||
private String determinePrefix(String environmentPrefix) {
|
||||
if (!StringUtils.hasText(environmentPrefix)) {
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
if (environmentPrefix.endsWith(".") || environmentPrefix.endsWith("_") || environmentPrefix.endsWith("-")) {
|
||||
prefix = environmentPrefix.substring(0, environmentPrefix.length() - 1);
|
||||
return environmentPrefix.substring(0, environmentPrefix.length() - 1);
|
||||
}
|
||||
return prefix;
|
||||
return environmentPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -120,7 +118,7 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements
|
|||
|
||||
@Override
|
||||
public String getPrefix() {
|
||||
return this.environmentPrefix;
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -45,6 +45,19 @@ public interface OriginLookup<K> {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the implicit prefix that is applied when performing a lookup or {@code null}
|
||||
* if no prefix is used. Prefixes can be used to disambiguate keys that would
|
||||
* otherwise clash. For example, if multiple applications are running on the same
|
||||
* machine a different prefix can be set on each application to ensure that different
|
||||
* environment variables are used.
|
||||
* @return the prefix applied by the lookup class or {@code null}.
|
||||
* @since 2.5.0
|
||||
*/
|
||||
default String getPrefix() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -423,7 +423,20 @@ class ConfigurationPropertyNameTests {
|
|||
@Test
|
||||
void appendWhenElementNameIsNullShouldReturnName() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
|
||||
assertThat((Object) name.append(null)).isSameAs(name);
|
||||
assertThat((Object) name.append((String) null)).isSameAs(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void appendConfigurationPropertyNameShouldReturnAppendedName() {
|
||||
ConfigurationPropertyName n1 = ConfigurationPropertyName.of("spring.boot");
|
||||
ConfigurationPropertyName n2 = ConfigurationPropertyName.of("tests.code");
|
||||
assertThat(n1.append(n2)).hasToString("spring.boot.tests.code");
|
||||
}
|
||||
|
||||
@Test
|
||||
void appendConfigurationPropertyNameWhenNullShouldReturnName() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
|
||||
assertThat((Object) name.append((ConfigurationPropertyName) null)).isSameAs(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -465,6 +478,37 @@ class ConfigurationPropertyNameTests {
|
|||
assertThat(name.chop(3)).isEqualTo(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subNameWhenOffsetLessThanSizeShouldReturnSubName() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");
|
||||
assertThat(name.subName(1)).hasToString("bar.baz");
|
||||
assertThat(name.subName(2)).hasToString("baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
void subNameWhenOffsetZeroShouldReturnName() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");
|
||||
assertThat(name.subName(0)).isSameAs(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subNameWhenOffsetEqualToSizeShouldReturnEmpty() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");
|
||||
assertThat(name.subName(3)).isSameAs(ConfigurationPropertyName.EMPTY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void subNameWhenOffsetMoreThanSizeShouldReturnEmpty() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");
|
||||
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> name.subName(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void subNameWhenOffsetNegativeShouldThrowException() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");
|
||||
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> name.subName(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isParentOfWhenSameShouldReturnFalse() {
|
||||
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
|
||||
|
|
|
@ -76,6 +76,20 @@ class PrefixedConfigurationPropertySourceTests {
|
|||
.isEqualTo(ConfigurationPropertyState.ABSENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withPrefixWhenPrefixIsNullReturnsOriginalSource() {
|
||||
ConfigurationPropertySource source = new MockConfigurationPropertySource().nonIterable();
|
||||
ConfigurationPropertySource prefixed = source.withPrefix(null);
|
||||
assertThat(prefixed).isSameAs(source);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withPrefixWhenPrefixIsEmptyReturnsOriginalSource() {
|
||||
ConfigurationPropertySource source = new MockConfigurationPropertySource().nonIterable();
|
||||
ConfigurationPropertySource prefixed = source.withPrefix("");
|
||||
assertThat(prefixed).isSameAs(source);
|
||||
}
|
||||
|
||||
private ConfigurationPropertyName getName(ConfigurationPropertySource source, String name) {
|
||||
ConfigurationProperty property = source.getConfigurationProperty(ConfigurationPropertyName.of(name));
|
||||
return (property != null) ? property.getName() : null;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -22,7 +22,7 @@ import java.util.Map;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor.OriginAndPrefixAwareSystemEnvironmentPropertySource;
|
||||
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;
|
||||
|
@ -47,7 +47,7 @@ class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
|
|||
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
|
||||
postProcessor.postProcessEnvironment(this.environment, this.application);
|
||||
PropertySource<?> replaced = this.environment.getPropertySources().get("systemEnvironment");
|
||||
assertThat(replaced).isInstanceOf(OriginAndPrefixAwareSystemEnvironmentPropertySource.class);
|
||||
assertThat(replaced).isInstanceOf(OriginAwareSystemEnvironmentPropertySource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,7 +56,7 @@ class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
|
|||
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
|
||||
PropertySource<?> original = this.environment.getPropertySources().get("systemEnvironment");
|
||||
postProcessor.postProcessEnvironment(this.environment, this.application);
|
||||
OriginAndPrefixAwareSystemEnvironmentPropertySource replaced = (OriginAndPrefixAwareSystemEnvironmentPropertySource) this.environment
|
||||
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment
|
||||
.getPropertySources().get("systemEnvironment");
|
||||
Map<String, Object> originalMap = (Map<String, Object>) original.getSource();
|
||||
Map<String, Object> replacedMap = replaced.getSource();
|
||||
|
@ -71,7 +71,7 @@ class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
|
|||
void replacedPropertySourceWhenPropertyAbsentShouldReturnNullOrigin() {
|
||||
SystemEnvironmentPropertySourceEnvironmentPostProcessor postProcessor = new SystemEnvironmentPropertySourceEnvironmentPostProcessor();
|
||||
postProcessor.postProcessEnvironment(this.environment, this.application);
|
||||
OriginAndPrefixAwareSystemEnvironmentPropertySource replaced = (OriginAndPrefixAwareSystemEnvironmentPropertySource) this.environment
|
||||
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment
|
||||
.getPropertySources().get("systemEnvironment");
|
||||
assertThat(replaced.getOrigin("NON_EXISTENT")).isNull();
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
|
|||
this.environment.getPropertySources().replace("systemEnvironment",
|
||||
new SystemEnvironmentPropertySource("systemEnvironment", source));
|
||||
postProcessor.postProcessEnvironment(this.environment, this.application);
|
||||
OriginAndPrefixAwareSystemEnvironmentPropertySource replaced = (OriginAndPrefixAwareSystemEnvironmentPropertySource) this.environment
|
||||
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment
|
||||
.getPropertySources().get("systemEnvironment");
|
||||
SystemEnvironmentOrigin origin = (SystemEnvironmentOrigin) replaced.getOrigin("foo.bar.baz");
|
||||
assertThat(origin.getProperty()).isEqualTo("FOO_BAR_BAZ");
|
||||
|
@ -96,7 +96,7 @@ class SystemEnvironmentPropertySourceEnvironmentPostProcessorTests {
|
|||
SpringApplication application = new SpringApplication();
|
||||
application.setEnvironmentPrefix("my");
|
||||
postProcessor.postProcessEnvironment(this.environment, application);
|
||||
OriginAndPrefixAwareSystemEnvironmentPropertySource replaced = (OriginAndPrefixAwareSystemEnvironmentPropertySource) this.environment
|
||||
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) this.environment
|
||||
.getPropertySources().get("systemEnvironment");
|
||||
assertThat(replaced.getPrefix()).isEqualTo("my");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue