-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
SpringApplicationJsonEnvironmentPostProcessorTests.java
220 lines (192 loc) · 9.4 KB
/
SpringApplicationJsonEnvironmentPostProcessorTests.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
/*
* Copyright 2012-2020 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;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.json.JsonParseException;
import org.springframework.boot.origin.PropertySourceOrigin;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.web.context.support.StandardServletEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link SpringApplicationJsonEnvironmentPostProcessor}.
*
* @author Dave Syer
* @author Madhura Bhave
* @author Phillip Webb
* @author Artsiom Yudovin
*/
class SpringApplicationJsonEnvironmentPostProcessorTests {
private SpringApplicationJsonEnvironmentPostProcessor processor = new SpringApplicationJsonEnvironmentPostProcessor();
private ConfigurableEnvironment environment = new StandardEnvironment();
@Test
void error() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.application.json=foo:bar");
assertThatExceptionOfType(JsonParseException.class)
.isThrownBy(() -> this.processor.postProcessEnvironment(this.environment, null))
.withMessageContaining("Cannot parse JSON");
}
@Test
void missing() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
}
@Test
void empty() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.application.json={}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
}
@Test
void periodSeparated() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
}
@Test
void envVar() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
}
@Test
void nested() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":{\"bar\":\"spam\",\"rab\":\"maps\"}}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
assertThat(this.environment.resolvePlaceholders("${foo.rab:}")).isEqualTo("maps");
}
@Test
void prefixed() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo.bar\":\"spam\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
}
@Test
void list() {
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[\"bar\",\"spam\"]}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEqualTo("spam");
}
@Test
void listOfObject() {
assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[{\"bar\":\"spam\"}]}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")).isEqualTo("spam");
}
@Test
void propertySourceShouldTrackOrigin() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
PropertySource<?> propertySource = this.environment.getPropertySources().get("spring.application.json");
PropertySourceOrigin origin = (PropertySourceOrigin) PropertySourceOrigin.get(propertySource, "foo");
assertThat(origin.getPropertySource().getName()).isEqualTo("Inlined Test Properties");
assertThat(origin.getPropertyName()).isEqualTo("spring.application.json");
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
}
@Test
void propertySourceShouldBeOrderedBeforeJndiPropertySource() {
testServletPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME);
}
@Test
void propertySourceShouldBeOrderedBeforeServletContextPropertySource() {
testServletPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
}
@Test
void propertySourceShouldBeOrderedBeforeServletConfigPropertySource() {
testServletPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
}
@Test
void propertySourceOrderingWhenMultipleServletSpecificPropertySources() {
MapPropertySource jndi = getPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME, "jndi");
this.environment.getPropertySources().addFirst(jndi);
MapPropertySource servlet = getPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
"servlet");
this.environment.getPropertySources().addFirst(servlet);
MapPropertySource custom = getPropertySource("custom", "custom");
this.environment.getPropertySources().addFirst(custom);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
PropertySource<?> json = this.environment.getPropertySources().get("spring.application.json");
assertThat(this.environment.getProperty("foo")).isEqualTo("custom");
assertThat(this.environment.getPropertySources()).containsSequence(custom, json, servlet, jndi);
}
@Test
void nullValuesShouldBeAddedToPropertySource() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":null}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.containsProperty("foo")).isTrue();
}
@Test
void emptyValuesForCollectionShouldNotBeIgnored() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[]}");
MockPropertySource source = new MockPropertySource();
source.setProperty("foo", "bar");
this.environment.getPropertySources().addLast(source);
assertThat(this.environment.resolvePlaceholders("${foo}")).isEqualTo("bar");
this.environment.getPropertySources().addLast(source);
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo}")).isEmpty();
}
@Test
@SuppressWarnings("unchecked")
void emptyMapValuesShouldNotBeIgnored() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":{}}");
MockPropertySource source = new MockPropertySource();
source.setProperty("foo.baz", "bar");
this.environment.getPropertySources().addLast(source);
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("foo", Map.class)).isEmpty();
}
private void testServletPropertySource(String servletPropertySourceName) {
this.environment.getPropertySources().addFirst(getPropertySource(servletPropertySourceName, "servlet"));
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("foo")).isEqualTo("bar");
}
private MapPropertySource getPropertySource(String name, String value) {
return new MapPropertySource(name, Collections.singletonMap("foo", value));
}
}