1
1
/*
2
- * Copyright 2002-2018 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 ;
21
+ import java .util .List ;
20
22
import java .util .Map ;
21
23
22
24
import org .junit .Rule ;
23
25
import org .junit .Test ;
24
26
import org .junit .rules .ExpectedException ;
27
+ import org .yaml .snakeyaml .constructor .ConstructorException ;
25
28
import org .yaml .snakeyaml .parser .ParserException ;
26
29
import org .yaml .snakeyaml .scanner .ScannerException ;
27
30
34
37
*
35
38
* @author Dave Syer
36
39
* @author Juergen Hoeller
40
+ * @author Sam Brannen
37
41
*/
38
42
public class YamlProcessorTests {
39
43
@@ -45,7 +49,7 @@ public class YamlProcessorTests {
45
49
46
50
@ Test
47
51
public void arrayConvertedToIndexedBeanReference () {
48
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n bar: [1,2,3]" . getBytes ()) );
52
+ setYaml ( "foo: bar\n bar: [1,2,3]" );
49
53
this .processor .process ((properties , map ) -> {
50
54
assertEquals (4 , properties .size ());
51
55
assertEquals ("bar" , properties .get ("foo" ));
@@ -61,29 +65,29 @@ public void arrayConvertedToIndexedBeanReference() {
61
65
62
66
@ Test
63
67
public void testStringResource () {
64
- this . processor . setResources ( new ByteArrayResource ( "foo # a document that is a literal" . getBytes ()) );
68
+ setYaml ( "foo # a document that is a literal" );
65
69
this .processor .process ((properties , map ) -> assertEquals ("foo" , map .get ("document" )));
66
70
}
67
71
68
72
@ Test
69
73
public void testBadDocumentStart () {
70
- this . processor . setResources ( new ByteArrayResource ( "foo # a document\n bar: baz" . getBytes ()) );
74
+ setYaml ( "foo # a document\n bar: baz" );
71
75
this .exception .expect (ParserException .class );
72
76
this .exception .expectMessage ("line 2, column 1" );
73
77
this .processor .process ((properties , map ) -> {});
74
78
}
75
79
76
80
@ Test
77
81
public void testBadResource () {
78
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n cd\n spam:\n foo: baz" . getBytes ()) );
82
+ setYaml ( "foo: bar\n cd\n spam:\n foo: baz" );
79
83
this .exception .expect (ScannerException .class );
80
84
this .exception .expectMessage ("line 3, column 1" );
81
85
this .processor .process ((properties , map ) -> {});
82
86
}
83
87
84
88
@ Test
85
89
public void mapConvertedToIndexedBeanReference () {
86
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n bar:\n spam: bucket" . getBytes ()) );
90
+ setYaml ( "foo: bar\n bar:\n spam: bucket" );
87
91
this .processor .process ((properties , map ) -> {
88
92
assertEquals ("bucket" , properties .get ("bar.spam" ));
89
93
assertEquals (2 , properties .size ());
@@ -92,7 +96,7 @@ public void mapConvertedToIndexedBeanReference() {
92
96
93
97
@ Test
94
98
public void integerKeyBehaves () {
95
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n 1: bar" . getBytes ()) );
99
+ setYaml ( "foo: bar\n 1: bar" );
96
100
this .processor .process ((properties , map ) -> {
97
101
assertEquals ("bar" , properties .get ("[1]" ));
98
102
assertEquals (2 , properties .size ());
@@ -101,7 +105,7 @@ public void integerKeyBehaves() {
101
105
102
106
@ Test
103
107
public void integerDeepKeyBehaves () {
104
- this . processor . setResources ( new ByteArrayResource ( "foo:\n 1: bar" . getBytes ()) );
108
+ setYaml ( "foo:\n 1: bar" );
105
109
this .processor .process ((properties , map ) -> {
106
110
assertEquals ("bar" , properties .get ("foo[1]" ));
107
111
assertEquals (1 , properties .size ());
@@ -111,7 +115,7 @@ public void integerDeepKeyBehaves() {
111
115
@ Test
112
116
@ SuppressWarnings ("unchecked" )
113
117
public void flattenedMapIsSameAsPropertiesButOrdered () {
114
- this . processor . setResources ( new ByteArrayResource ( "foo: bar\n bar:\n spam: bucket" . getBytes ()) );
118
+ setYaml ( "foo: bar\n bar:\n spam: bucket" );
115
119
this .processor .process ((properties , map ) -> {
116
120
assertEquals ("bucket" , properties .get ("bar.spam" ));
117
121
assertEquals (2 , properties .size ());
@@ -124,4 +128,47 @@ public void flattenedMapIsSameAsPropertiesButOrdered() {
124
128
});
125
129
}
126
130
131
+ @ Test
132
+ public void customTypeSupportedByDefault () throws Exception {
133
+ URL url = new URL ("https://localhost:9000/" );
134
+ setYaml ("value: !!java.net.URL [\" " + url + "\" ]" );
135
+
136
+ this .processor .process ((properties , map ) -> {
137
+ assertEquals (1 , properties .size ());
138
+ assertEquals (1 , map .size ());
139
+ assertEquals (url , properties .get ("value" ));
140
+ assertEquals (url , map .get ("value" ));
141
+ });
142
+ }
143
+
144
+ @ Test
145
+ public void customTypesSupportedDueToExplicitConfiguration () throws Exception {
146
+ this .processor .setSupportedTypes (URL .class , String .class );
147
+
148
+ URL url = new URL ("https://localhost:9000/" );
149
+ setYaml ("value: !!java.net.URL [!!java.lang.String [\" " + url + "\" ]]" );
150
+
151
+ this .processor .process ((properties , map ) -> {
152
+ assertEquals (1 , properties .size ());
153
+ assertEquals (1 , map .size ());
154
+ assertEquals (url , properties .get ("value" ));
155
+ assertEquals (url , map .get ("value" ));
156
+ });
157
+ }
158
+
159
+ @ Test
160
+ public void customTypeNotSupportedDueToExplicitConfiguration () {
161
+ this .processor .setSupportedTypes (List .class );
162
+
163
+ setYaml ("value: !!java.net.URL [\" https://localhost:9000/\" ]" );
164
+
165
+ this .exception .expect (ConstructorException .class );
166
+ this .exception .expectMessage ("Unsupported type encountered in YAML document: java.net.URL" );
167
+ this .processor .process ((properties , map ) -> {});
168
+ }
169
+
170
+ private void setYaml (String yaml ) {
171
+ this .processor .setResources (new ByteArrayResource (yaml .getBytes ()));
172
+ }
173
+
127
174
}
0 commit comments