-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
ConfigurationPropertySourcesTests.java
240 lines (209 loc) · 9.49 KB
/
ConfigurationPropertySourcesTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2012-2022 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.context.properties.source;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigurationPropertySources}.
*
* @author Phillip Webb
*/
class ConfigurationPropertySourcesTests {
@Test
void attachShouldAddAdapterAtBeginning() {
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources sources = environment.getPropertySources();
sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
int size = sources.size();
ConfigurationPropertySources.attach(environment);
assertThat(sources.size()).isEqualTo(size + 1);
PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
assertThat(resolver.getProperty("server.port")).isEqualTo("1234");
}
@Test
void attachShouldReattachInMergedSetup() {
ConfigurableEnvironment parent = new StandardEnvironment();
ConfigurationPropertySources.attach(parent);
ConfigurableEnvironment child = new StandardEnvironment();
child.merge(parent);
child.getPropertySources()
.addLast(new MapPropertySource("config", Collections.singletonMap("my.example_property", "1234")));
ConfigurationPropertySources.attach(child);
assertThat(child.getProperty("my.example-property")).isEqualTo("1234");
}
@Test
void attachWhenAlreadyAttachedWithSameSourcesShouldReturnExistingInstance() {
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources sources = environment.getPropertySources();
sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
ConfigurationPropertySources.attach(environment);
Iterable<ConfigurationPropertySource> first = ConfigurationPropertySources.get(environment);
ConfigurationPropertySources.attach(environment);
Iterable<ConfigurationPropertySource> second = ConfigurationPropertySources.get(environment);
assertThat(first).isSameAs(second);
}
@Test
void getWhenNotAttachedShouldReturnAdapted() {
ConfigurableEnvironment environment = new StandardEnvironment();
assertThat(ConfigurationPropertySources.get(environment)).isNotEmpty();
}
@Test
void getWhenAttachedShouldReturnAttached() {
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources sources = environment.getPropertySources();
sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
int expectedSize = sources.size();
ConfigurationPropertySources.attach(environment);
assertThat(ConfigurationPropertySources.get(environment)).hasSize(expectedSize);
}
@Test
void environmentPropertyExpansionShouldWorkWhenAttached() {
ConfigurableEnvironment environment = new StandardEnvironment();
Map<String, Object> source = new LinkedHashMap<>();
source.put("fooBar", "Spring ${barBaz} ${bar-baz}");
source.put("barBaz", "Boot");
PropertySource<?> propertySource = new MapPropertySource("test", source);
environment.getPropertySources().addFirst(propertySource);
ConfigurationPropertySources.attach(environment);
assertThat(environment.getProperty("foo-bar")).isEqualTo("Spring Boot Boot");
}
@Test
void fromPropertySourceShouldReturnSpringConfigurationPropertySource() {
PropertySource<?> source = new MapPropertySource("foo", Collections.singletonMap("foo", "bar"));
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySources.from(source).iterator()
.next();
assertThat(configurationPropertySource).isInstanceOf(SpringConfigurationPropertySource.class);
}
@Test
void fromPropertySourceShouldFlattenPropertySources() {
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
MutablePropertySources sources = new MutablePropertySources();
sources.addFirst(new PropertySource<Environment>("env", environment) {
@Override
public String getProperty(String key) {
return this.source.getProperty(key);
}
});
sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
assertThat(configurationSources.iterator()).toIterable().hasSize(5);
}
@Test // gh-20625
void environmentPropertyAccessWhenImmutableShouldBePerformant() {
long baseline = testPropertySourcePerformance(false);
long immutable = testPropertySourcePerformance(true);
assertThat(immutable).isLessThan(baseline / 2);
}
@Test // gh-20625
void environmentPropertyAccessWhenMutableWithCacheShouldBePerformant() {
StandardEnvironment environment = createPerformanceTestEnvironment(false);
long uncached = testPropertySourcePerformance(environment);
ConfigurationPropertyCaching.get(environment).enable();
long cached = testPropertySourcePerformance(environment);
assertThat(cached).isLessThan(uncached / 2);
}
@Test // gh-20625
@Disabled("for manual testing")
void environmentPropertyAccessWhenMutableShouldBeTolerable() {
assertThat(testPropertySourcePerformance(false)).isLessThan(5000);
}
@Test // gh-21416
void descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant() {
Function<StandardEnvironment, Long> descendantOfPerformance = (environment) -> {
Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
ConfigurationPropertyName missing = ConfigurationPropertyName.of("missing");
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
for (ConfigurationPropertySource source : sources) {
assertThat(source.containsDescendantOf(missing)).isEqualTo(ConfigurationPropertyState.ABSENT);
}
}
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
};
StandardEnvironment environment = createPerformanceTestEnvironment(false);
long baseline = descendantOfPerformance.apply(environment);
ConfigurationPropertyCaching.get(environment).enable();
long cached = descendantOfPerformance.apply(environment);
assertThat(cached).isLessThan(baseline / 2);
}
private long testPropertySourcePerformance(boolean immutable) {
StandardEnvironment environment = createPerformanceTestEnvironment(immutable);
return testPropertySourcePerformance(environment);
}
private StandardEnvironment createPerformanceTestEnvironment(boolean immutable) {
StandardEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
for (int i = 0; i < 100; i++) {
propertySources.addLast(new TestPropertySource(i, immutable));
}
ConfigurationPropertySources.attach(environment);
return environment;
}
private long testPropertySourcePerformance(StandardEnvironment environment) {
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
environment.getProperty("missing" + i);
}
long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
assertThat(environment.getProperty("test-10-property-80")).isEqualTo("test-10-property-80-value");
return total;
}
static class TestPropertySource extends MapPropertySource implements OriginLookup<String> {
private final boolean immutable;
TestPropertySource(int index, boolean immutable) {
super("test-" + index, createProperties(index));
this.immutable = immutable;
}
private static Map<String, Object> createProperties(int index) {
Map<String, Object> map = new LinkedHashMap<>();
for (int i = 0; i < 1000; i++) {
String name = "test-" + index + "-property-" + i;
String value = name + "-value";
map.put(name, value);
}
return map;
}
@Override
public Origin getOrigin(String key) {
return null;
}
@Override
public boolean isImmutable() {
return this.immutable;
}
}
}