/
OriginTrackedPropertiesLoaderTests.java
317 lines (270 loc) · 10.2 KB
/
OriginTrackedPropertiesLoaderTests.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* 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.io.IOException;
import java.util.List;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.boot.origin.TextResourceOrigin;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link OriginTrackedPropertiesLoader}.
*
* @author Madhura Bhave
* @author Phillip Webb
*/
class OriginTrackedPropertiesLoaderTests {
private ClassPathResource resource;
private List<Document> documents;
@BeforeEach
void setUp() throws Exception {
String path = "test-properties.properties";
this.resource = new ClassPathResource(path, getClass());
this.documents = new OriginTrackedPropertiesLoader(this.resource).load();
}
@Test
void compareToJavaProperties() throws Exception {
Properties java = PropertiesLoaderUtils.loadProperties(this.resource);
Properties ours = new Properties();
new OriginTrackedPropertiesLoader(this.resource).load(false).get(0).asMap()
.forEach((k, v) -> ours.put(k, v.getValue()));
assertThat(ours).isEqualTo(java);
}
@Test
void getSimpleProperty() {
OriginTrackedValue value = getFromFirst("test");
assertThat(getValue(value)).isEqualTo("properties");
assertThat(getLocation(value)).isEqualTo("11:6");
}
@Test
void getSimplePropertyWithColonSeparator() {
OriginTrackedValue value = getFromFirst("test-colon-separator");
assertThat(getValue(value)).isEqualTo("my-property");
assertThat(getLocation(value)).isEqualTo("15:23");
}
@Test
void getPropertyWithSeparatorSurroundedBySpaces() {
OriginTrackedValue value = getFromFirst("blah");
assertThat(getValue(value)).isEqualTo("hello world");
assertThat(getLocation(value)).isEqualTo("2:12");
}
@Test
void getUnicodeProperty() {
OriginTrackedValue value = getFromFirst("test-unicode");
assertThat(getValue(value)).isEqualTo("properties&test");
assertThat(getLocation(value)).isEqualTo("12:14");
}
@Test
void getMalformedUnicodeProperty() throws Exception {
// gh-12716
ClassPathResource resource = new ClassPathResource("test-properties-malformed-unicode.properties", getClass());
assertThatIllegalStateException().isThrownBy(() -> new OriginTrackedPropertiesLoader(resource).load())
.withMessageContaining("Malformed \\uxxxx encoding");
}
@Test
void getEscapedProperty() {
OriginTrackedValue value = getFromFirst("test=property");
assertThat(getValue(value)).isEqualTo("helloworld");
assertThat(getLocation(value)).isEqualTo("14:15");
}
@Test
void getPropertyWithTab() {
OriginTrackedValue value = getFromFirst("test-tab-property");
assertThat(getValue(value)).isEqualTo("foo\tbar");
assertThat(getLocation(value)).isEqualTo("16:19");
}
@Test
void getPropertyWithBang() {
OriginTrackedValue value = getFromFirst("test-bang-property");
assertThat(getValue(value)).isEqualTo("foo!");
assertThat(getLocation(value)).isEqualTo("34:20");
}
@Test
void getPropertyWithValueComment() {
OriginTrackedValue value = getFromFirst("test-property-value-comment");
assertThat(getValue(value)).isEqualTo("foo !bar #foo");
assertThat(getLocation(value)).isEqualTo("36:29");
}
@Test
void getPropertyWithMultilineImmediateBang() {
OriginTrackedValue value = getFromFirst("test-multiline-immediate-bang");
assertThat(getValue(value)).isEqualTo("!foo");
assertThat(getLocation(value)).isEqualTo("39:1");
}
@Test
void getPropertyWithCarriageReturn() {
OriginTrackedValue value = getFromFirst("test-return-property");
assertThat(getValue(value)).isEqualTo("foo\rbar");
assertThat(getLocation(value)).isEqualTo("17:22");
}
@Test
void getPropertyWithNewLine() {
OriginTrackedValue value = getFromFirst("test-newline-property");
assertThat(getValue(value)).isEqualTo("foo\nbar");
assertThat(getLocation(value)).isEqualTo("18:23");
}
@Test
void getPropertyWithFormFeed() {
OriginTrackedValue value = getFromFirst("test-form-feed-property");
assertThat(getValue(value)).isEqualTo("foo\fbar");
assertThat(getLocation(value)).isEqualTo("19:25");
}
@Test
void getPropertyWithWhiteSpace() {
OriginTrackedValue value = getFromFirst("test-whitespace-property");
assertThat(getValue(value)).isEqualTo("foo bar");
assertThat(getLocation(value)).isEqualTo("20:32");
}
@Test
void getCommentedOutPropertyShouldBeNull() {
assertThat(getFromFirst("commented-property")).isNull();
assertThat(getFromFirst("#commented-property")).isNull();
assertThat(getFromFirst("commented-two")).isNull();
assertThat(getFromFirst("!commented-two")).isNull();
}
@Test
void getMultiline() {
OriginTrackedValue value = getFromFirst("test-multiline");
assertThat(getValue(value)).isEqualTo("ab\\c");
assertThat(getLocation(value)).isEqualTo("21:17");
}
@Test
void getImmediateMultiline() {
OriginTrackedValue value = getFromFirst("test-multiline-immediate");
assertThat(getValue(value)).isEqualTo("foo");
assertThat(getLocation(value)).isEqualTo("32:1");
}
@Test
void loadWhenMultiDocumentWithoutWhitespaceLoadsMultiDoc() throws IOException {
String content = "a=a\n#---\nb=b";
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
assertThat(loaded).hasSize(2);
}
@Test
void loadWhenMultiDocumentWithLeadingWhitespaceLoadsSingleDoc() throws IOException {
String content = "a=a\n \t#---\nb=b";
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
assertThat(loaded).hasSize(1);
}
@Test
void loadWhenMultiDocumentWithTrailingWhitespaceLoadsMultiDoc() throws IOException {
String content = "a=a\n#--- \t \nb=b";
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
assertThat(loaded).hasSize(2);
}
@Test
void loadWhenMultiDocumentWithTrailingCharsLoadsSingleDoc() throws IOException {
String content = "a=a\n#--- \tcomment\nb=b";
List<Document> loaded = new OriginTrackedPropertiesLoader(new ByteArrayResource(content.getBytes())).load();
assertThat(loaded).hasSize(1);
}
@Test
void getPropertyWithWhitespaceAfterKey() {
OriginTrackedValue value = getFromFirst("bar");
assertThat(getValue(value)).isEqualTo("foo=baz");
assertThat(getLocation(value)).isEqualTo("3:7");
}
@Test
void getPropertyWithSpaceSeparator() {
OriginTrackedValue value = getFromFirst("hello");
assertThat(getValue(value)).isEqualTo("world");
assertThat(getLocation(value)).isEqualTo("4:9");
}
@Test
void getPropertyWithBackslashEscaped() {
OriginTrackedValue value = getFromFirst("proper\\ty");
assertThat(getValue(value)).isEqualTo("test");
assertThat(getLocation(value)).isEqualTo("5:11");
}
@Test
void getPropertyWithEmptyValue() {
OriginTrackedValue value = getFromFirst("foo");
assertThat(getValue(value)).isEqualTo("");
assertThat(getLocation(value)).isEqualTo("7:0");
}
@Test
void getPropertyWithBackslashEscapedInValue() {
OriginTrackedValue value = getFromFirst("bat");
assertThat(getValue(value)).isEqualTo("a\\");
assertThat(getLocation(value)).isEqualTo("7:7");
}
@Test
void getPropertyWithSeparatorInValue() {
OriginTrackedValue value = getFromFirst("bling");
assertThat(getValue(value)).isEqualTo("a=b");
assertThat(getLocation(value)).isEqualTo("8:9");
}
@Test
void getListProperty() {
OriginTrackedValue apple = getFromFirst("foods[0]");
assertThat(getValue(apple)).isEqualTo("Apple");
assertThat(getLocation(apple)).isEqualTo("24:9");
OriginTrackedValue orange = getFromFirst("foods[1]");
assertThat(getValue(orange)).isEqualTo("Orange");
assertThat(getLocation(orange)).isEqualTo("25:1");
OriginTrackedValue strawberry = getFromFirst("foods[2]");
assertThat(getValue(strawberry)).isEqualTo("Strawberry");
assertThat(getLocation(strawberry)).isEqualTo("26:1");
OriginTrackedValue mango = getFromFirst("foods[3]");
assertThat(getValue(mango)).isEqualTo("Mango");
assertThat(getLocation(mango)).isEqualTo("27:1");
}
@Test
void getPropertyWithISO88591Character() {
OriginTrackedValue value = getFromFirst("test-iso8859-1-chars");
assertThat(getValue(value)).isEqualTo("æ×ÈÅÞßáñÀÿ");
}
@Test
void getPropertyWithTrailingSpace() {
OriginTrackedValue value = getFromFirst("test-with-trailing-space");
assertThat(getValue(value)).isEqualTo("trailing ");
}
@Test
void getPropertyWithEscapedTrailingSpace() {
OriginTrackedValue value = getFromFirst("test-with-escaped-trailing-space");
assertThat(getValue(value)).isEqualTo("trailing ");
}
@Test
void existingCommentsAreNotTreatedAsMultiDoc() throws Exception {
this.resource = new ClassPathResource("existing-non-multi-document.properties", getClass());
this.documents = new OriginTrackedPropertiesLoader(this.resource).load();
assertThat(this.documents.size()).isEqualTo(1);
}
@Test
void getPropertyAfterPoundCharacter() {
OriginTrackedValue value = getFromFirst("test-line-after-empty-pound");
assertThat(getValue(value)).isEqualTo("abc");
}
private OriginTrackedValue getFromFirst(String key) {
return this.documents.get(0).asMap().get(key);
}
private Object getValue(OriginTrackedValue value) {
return (value != null) ? value.getValue() : null;
}
private String getLocation(OriginTrackedValue value) {
if (value == null) {
return null;
}
return ((TextResourceOrigin) value.getOrigin()).getLocation().toString();
}
}