1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 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.
16
16
17
17
package org .springframework .beans .factory .config ;
18
18
19
+ import java .net .URL ;
19
20
import java .util .LinkedHashMap ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
23
24
import org .junit .jupiter .api .Test ;
25
+ import org .yaml .snakeyaml .constructor .ConstructorException ;
24
26
import org .yaml .snakeyaml .parser .ParserException ;
25
27
import org .yaml .snakeyaml .scanner .ScannerException ;
26
28
29
31
import static java .util .stream .Collectors .toList ;
30
32
import static org .assertj .core .api .Assertions .assertThat ;
31
33
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
34
+ import static org .assertj .core .api .Assertions .entry ;
32
35
33
36
/**
34
37
* Tests for {@link YamlProcessor}.
37
40
* @author Juergen Hoeller
38
41
* @author Sam Brannen
39
42
*/
40
- public class YamlProcessorTests {
43
+ class YamlProcessorTests {
41
44
42
45
private final YamlProcessor processor = new YamlProcessor () {};
43
46
44
47
45
48
@ Test
46
- public void arrayConvertedToIndexedBeanReference () {
47
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n bar: [1,2,3]" . getBytes ()) );
49
+ void arrayConvertedToIndexedBeanReference () {
50
+ setYaml ( "foo: bar\n bar: [1,2,3]" );
48
51
this .processor .process ((properties , map ) -> {
49
52
assertThat (properties .size ()).isEqualTo (4 );
50
53
assertThat (properties .get ("foo" )).isEqualTo ("bar" );
@@ -59,48 +62,48 @@ public void arrayConvertedToIndexedBeanReference() {
59
62
}
60
63
61
64
@ Test
62
- public void stringResource () {
63
- this . processor . setResources ( new ByteArrayResource ( "foo # a document that is a literal" . getBytes ()) );
65
+ void stringResource () {
66
+ setYaml ( "foo # a document that is a literal" );
64
67
this .processor .process ((properties , map ) -> assertThat (map .get ("document" )).isEqualTo ("foo" ));
65
68
}
66
69
67
70
@ Test
68
- public void badDocumentStart () {
69
- this . processor . setResources ( new ByteArrayResource ( "foo # a document\n bar: baz" . getBytes ()) );
71
+ void badDocumentStart () {
72
+ setYaml ( "foo # a document\n bar: baz" );
70
73
assertThatExceptionOfType (ParserException .class )
71
74
.isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
72
75
.withMessageContaining ("line 2, column 1" );
73
76
}
74
77
75
78
@ Test
76
- public void badResource () {
77
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n cd\n spam:\n foo: baz" . getBytes ()) );
79
+ void badResource () {
80
+ setYaml ( "foo: bar\n cd\n spam:\n foo: baz" );
78
81
assertThatExceptionOfType (ScannerException .class )
79
82
.isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
80
83
.withMessageContaining ("line 3, column 1" );
81
84
}
82
85
83
86
@ Test
84
- public void mapConvertedToIndexedBeanReference () {
85
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n bar:\n spam: bucket" . getBytes ()) );
87
+ void mapConvertedToIndexedBeanReference () {
88
+ setYaml ( "foo: bar\n bar:\n spam: bucket" );
86
89
this .processor .process ((properties , map ) -> {
87
90
assertThat (properties .get ("bar.spam" )).isEqualTo ("bucket" );
88
91
assertThat (properties ).hasSize (2 );
89
92
});
90
93
}
91
94
92
95
@ Test
93
- public void integerKeyBehaves () {
94
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n 1: bar" . getBytes ()) );
96
+ void integerKeyBehaves () {
97
+ setYaml ( "foo: bar\n 1: bar" );
95
98
this .processor .process ((properties , map ) -> {
96
99
assertThat (properties .get ("[1]" )).isEqualTo ("bar" );
97
100
assertThat (properties ).hasSize (2 );
98
101
});
99
102
}
100
103
101
104
@ Test
102
- public void integerDeepKeyBehaves () {
103
- this . processor . setResources ( new ByteArrayResource ( "foo:\n 1: bar" . getBytes ()) );
105
+ void integerDeepKeyBehaves () {
106
+ setYaml ( "foo:\n 1: bar" );
104
107
this .processor .process ((properties , map ) -> {
105
108
assertThat (properties .get ("foo[1]" )).isEqualTo ("bar" );
106
109
assertThat (properties ).hasSize (1 );
@@ -109,8 +112,8 @@ public void integerDeepKeyBehaves() {
109
112
110
113
@ Test
111
114
@ SuppressWarnings ("unchecked" )
112
- public void flattenedMapIsSameAsPropertiesButOrdered () {
113
- this . processor . setResources ( new ByteArrayResource ( "cat: dog\n foo: bar\n bar:\n spam: bucket" . getBytes ()) );
115
+ void flattenedMapIsSameAsPropertiesButOrdered () {
116
+ setYaml ( "cat: dog\n foo: bar\n bar:\n spam: bucket" );
114
117
this .processor .process ((properties , map ) -> {
115
118
Map <String , Object > flattenedMap = processor .getFlattenedMap (map );
116
119
assertThat (flattenedMap ).isInstanceOf (LinkedHashMap .class );
@@ -134,4 +137,43 @@ public void flattenedMapIsSameAsPropertiesButOrdered() {
134
137
});
135
138
}
136
139
140
+ @ Test
141
+ void customTypeSupportedByDefault () throws Exception {
142
+ URL url = new URL ("https://localhost:9000/" );
143
+ setYaml ("value: !!java.net.URL [\" " + url + "\" ]" );
144
+
145
+ this .processor .process ((properties , map ) -> {
146
+ assertThat (properties ).containsExactly (entry ("value" , url ));
147
+ assertThat (map ).containsExactly (entry ("value" , url ));
148
+ });
149
+ }
150
+
151
+ @ Test
152
+ void customTypesSupportedDueToExplicitConfiguration () throws Exception {
153
+ this .processor .setSupportedTypes (URL .class , String .class );
154
+
155
+ URL url = new URL ("https://localhost:9000/" );
156
+ setYaml ("value: !!java.net.URL [!!java.lang.String [\" " + url + "\" ]]" );
157
+
158
+ this .processor .process ((properties , map ) -> {
159
+ assertThat (properties ).containsExactly (entry ("value" , url ));
160
+ assertThat (map ).containsExactly (entry ("value" , url ));
161
+ });
162
+ }
163
+
164
+ @ Test
165
+ void customTypeNotSupportedDueToExplicitConfiguration () {
166
+ this .processor .setSupportedTypes (List .class );
167
+
168
+ setYaml ("value: !!java.net.URL [\" https://localhost:9000/\" ]" );
169
+
170
+ assertThatExceptionOfType (ConstructorException .class )
171
+ .isThrownBy (() -> this .processor .process ((properties , map ) -> {}))
172
+ .withMessageContaining ("Unsupported type encountered in YAML document: java.net.URL" );
173
+ }
174
+
175
+ private void setYaml (String yaml ) {
176
+ this .processor .setResources (new ByteArrayResource (yaml .getBytes ()));
177
+ }
178
+
137
179
}
0 commit comments