1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .beans .factory .config ;
18
18
19
19
import java .net .URL ;
20
+ import java .util .ArrayList ;
20
21
import java .util .LinkedHashMap ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
24
+ import java .util .Set ;
23
25
24
26
import org .junit .jupiter .api .Test ;
25
27
import org .yaml .snakeyaml .constructor .ConstructorException ;
28
30
29
31
import org .springframework .core .io .ByteArrayResource ;
30
32
31
- import static java .util .stream .Collectors .toList ;
32
33
import static org .assertj .core .api .Assertions .assertThat ;
33
34
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
34
35
import static org .assertj .core .api .Assertions .entry ;
39
40
* @author Dave Syer
40
41
* @author Juergen Hoeller
41
42
* @author Sam Brannen
43
+ * @author Brian Clozel
42
44
*/
43
45
class YamlProcessorTests {
44
46
45
- private final YamlProcessor processor = new YamlProcessor () {};
47
+ private final YamlProcessor processor = new YamlProcessor () {
48
+ };
46
49
47
50
48
51
@ Test
@@ -79,8 +82,8 @@ void badDocumentStart() {
79
82
void badResource () {
80
83
setYaml ("foo: bar\n cd\n spam:\n foo: baz" );
81
84
assertThatExceptionOfType (ScannerException .class )
82
- .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
83
- .withMessageContaining ("line 3, column 1" );
85
+ .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
86
+ .withMessageContaining ("line 3, column 1" );
84
87
}
85
88
86
89
@ Test
@@ -127,8 +130,8 @@ void flattenedMapIsSameAsPropertiesButOrdered() {
127
130
Map <String , Object > bar = (Map <String , Object >) map .get ("bar" );
128
131
assertThat (bar .get ("spam" )).isEqualTo ("bucket" );
129
132
130
- List <Object > keysFromProperties = properties .keySet (). stream (). collect ( toList ());
131
- List <String > keysFromFlattenedMap = flattenedMap .keySet (). stream (). collect ( toList ());
133
+ List <Object > keysFromProperties = new ArrayList <>( properties .keySet ());
134
+ List <String > keysFromFlattenedMap = new ArrayList <>( flattenedMap .keySet ());
132
135
assertThat (keysFromProperties ).containsExactlyInAnyOrderElementsOf (keysFromFlattenedMap );
133
136
// Keys in the Properties object are sorted.
134
137
assertThat (keysFromProperties ).containsExactly ("bar.spam" , "cat" , "foo" );
@@ -138,16 +141,25 @@ void flattenedMapIsSameAsPropertiesButOrdered() {
138
141
}
139
142
140
143
@ Test
141
- void customTypeSupportedByDefault () throws Exception {
142
- URL url = new URL ("https://localhost:9000/" );
143
- setYaml ("value: !!java.net.URL [\" " + url + "\" ]" );
144
-
144
+ void standardTypesSupportedByDefault () throws Exception {
145
+ setYaml ("value: !!set\n ? first\n ? second" );
145
146
this .processor .process ((properties , map ) -> {
146
- assertThat (properties ).containsExactly (entry ("value" , url ));
147
- assertThat (map ).containsExactly (entry ("value" , url ));
147
+ assertThat (properties ).containsExactly (entry ("value[0]" , "first" ), entry ("value[1]" , "second" ));
148
+ assertThat (map .get ("value" )).isInstanceOf (Set .class );
149
+ Set <String > set = (Set <String >) map .get ("value" );
150
+ assertThat (set ).containsExactly ("first" , "second" );
148
151
});
149
152
}
150
153
154
+ @ Test
155
+ void customTypeNotSupportedByDefault () throws Exception {
156
+ URL url = new URL ("https://localhost:9000/" );
157
+ setYaml ("value: !!java.net.URL [\" " + url + "\" ]" );
158
+ assertThatExceptionOfType (ConstructorException .class )
159
+ .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
160
+ .withMessageContaining ("Unsupported type encountered in YAML document: java.net.URL" );
161
+ }
162
+
151
163
@ Test
152
164
void customTypesSupportedDueToExplicitConfiguration () throws Exception {
153
165
this .processor .setSupportedTypes (URL .class , String .class );
@@ -168,8 +180,8 @@ void customTypeNotSupportedDueToExplicitConfiguration() {
168
180
setYaml ("value: !!java.net.URL [\" https://localhost:9000/\" ]" );
169
181
170
182
assertThatExceptionOfType (ConstructorException .class )
171
- .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
172
- .withMessageContaining ("Unsupported type encountered in YAML document: java.net.URL" );
183
+ .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
184
+ .withMessageContaining ("Unsupported type encountered in YAML document: java.net.URL" );
173
185
}
174
186
175
187
private void setYaml (String yaml ) {
0 commit comments