36
36
import static org .hamcrest .Matchers .*;
37
37
import static org .junit .Assert .*;
38
38
39
+ /**
40
+ * @author Keith Donald
41
+ * @author Phil Webb
42
+ * @author Juergen Hoeller
43
+ */
39
44
public class MapToMapConverterTests {
40
45
41
- private GenericConversionService conversionService = new GenericConversionService ();
46
+ private final GenericConversionService conversionService = new GenericConversionService ();
42
47
43
48
44
49
@ Before
@@ -54,12 +59,15 @@ public void scalarMap() throws Exception {
54
59
map .put ("2" , "37" );
55
60
TypeDescriptor sourceType = TypeDescriptor .forObject (map );
56
61
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("scalarMapTarget" ));
62
+
57
63
assertTrue (conversionService .canConvert (sourceType , targetType ));
58
64
try {
59
65
conversionService .convert (map , sourceType , targetType );
60
- } catch (ConversionFailedException e ) {
61
- assertTrue (e .getCause () instanceof ConverterNotFoundException );
62
66
}
67
+ catch (ConversionFailedException ex ) {
68
+ assertTrue (ex .getCause () instanceof ConverterNotFoundException );
69
+ }
70
+
63
71
conversionService .addConverterFactory (new StringToNumberConverterFactory ());
64
72
assertTrue (conversionService .canConvert (sourceType , targetType ));
65
73
@ SuppressWarnings ("unchecked" )
@@ -74,6 +82,7 @@ public void scalarMapNotGenericTarget() throws Exception {
74
82
Map <String , String > map = new HashMap <String , String >();
75
83
map .put ("1" , "9" );
76
84
map .put ("2" , "37" );
85
+
77
86
assertTrue (conversionService .canConvert (Map .class , Map .class ));
78
87
assertSame (map , conversionService .convert (map , Map .class ));
79
88
}
@@ -85,12 +94,15 @@ public void scalarMapNotGenericSourceField() throws Exception {
85
94
map .put ("2" , "37" );
86
95
TypeDescriptor sourceType = new TypeDescriptor (getClass ().getField ("notGenericMapSource" ));
87
96
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("scalarMapTarget" ));
97
+
88
98
assertTrue (conversionService .canConvert (sourceType , targetType ));
89
99
try {
90
100
conversionService .convert (map , sourceType , targetType );
91
- } catch (ConversionFailedException e ) {
92
- assertTrue (e .getCause () instanceof ConverterNotFoundException );
93
101
}
102
+ catch (ConversionFailedException ex ) {
103
+ assertTrue (ex .getCause () instanceof ConverterNotFoundException );
104
+ }
105
+
94
106
conversionService .addConverterFactory (new StringToNumberConverterFactory ());
95
107
assertTrue (conversionService .canConvert (sourceType , targetType ));
96
108
@ SuppressWarnings ("unchecked" )
@@ -107,12 +119,15 @@ public void collectionMap() throws Exception {
107
119
map .put ("2" , Arrays .asList ("37" , "23" ));
108
120
TypeDescriptor sourceType = TypeDescriptor .forObject (map );
109
121
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("collectionMapTarget" ));
122
+
110
123
assertTrue (conversionService .canConvert (sourceType , targetType ));
111
124
try {
112
125
conversionService .convert (map , sourceType , targetType );
113
- } catch (ConversionFailedException e ) {
114
- assertTrue (e .getCause () instanceof ConverterNotFoundException );
115
126
}
127
+ catch (ConversionFailedException ex ) {
128
+ assertTrue (ex .getCause () instanceof ConverterNotFoundException );
129
+ }
130
+
116
131
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
117
132
conversionService .addConverterFactory (new StringToNumberConverterFactory ());
118
133
assertTrue (conversionService .canConvert (sourceType , targetType ));
@@ -130,6 +145,7 @@ public void collectionMapSourceTarget() throws Exception {
130
145
map .put ("2" , Arrays .asList ("37" , "23" ));
131
146
TypeDescriptor sourceType = new TypeDescriptor (getClass ().getField ("sourceCollectionMapTarget" ));
132
147
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("collectionMapTarget" ));
148
+
133
149
assertFalse (conversionService .canConvert (sourceType , targetType ));
134
150
try {
135
151
conversionService .convert (map , sourceType , targetType );
@@ -138,6 +154,7 @@ public void collectionMapSourceTarget() throws Exception {
138
154
catch (ConverterNotFoundException ex ) {
139
155
// expected
140
156
}
157
+
141
158
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
142
159
conversionService .addConverterFactory (new StringToNumberConverterFactory ());
143
160
assertTrue (conversionService .canConvert (sourceType , targetType ));
@@ -153,6 +170,7 @@ public void collectionMapNotGenericTarget() throws Exception {
153
170
Map <String , List <String >> map = new HashMap <String , List <String >>();
154
171
map .put ("1" , Arrays .asList ("9" , "12" ));
155
172
map .put ("2" , Arrays .asList ("37" , "23" ));
173
+
156
174
assertTrue (conversionService .canConvert (Map .class , Map .class ));
157
175
assertSame (map , conversionService .convert (map , Map .class ));
158
176
}
@@ -164,6 +182,7 @@ public void collectionMapNotGenericTargetCollectionToObjectInteraction() throws
164
182
map .put ("2" , Arrays .asList ("37" , "23" ));
165
183
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
166
184
conversionService .addConverter (new CollectionToObjectConverter (conversionService ));
185
+
167
186
assertTrue (conversionService .canConvert (Map .class , Map .class ));
168
187
assertSame (map , conversionService .convert (map , Map .class ));
169
188
}
@@ -173,13 +192,15 @@ public void emptyMap() throws Exception {
173
192
Map <String , String > map = new HashMap <String , String >();
174
193
TypeDescriptor sourceType = TypeDescriptor .forObject (map );
175
194
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("emptyMapTarget" ));
195
+
176
196
assertTrue (conversionService .canConvert (sourceType , targetType ));
177
197
assertSame (map , conversionService .convert (map , sourceType , targetType ));
178
198
}
179
199
180
200
@ Test
181
201
public void emptyMapNoTargetGenericInfo () throws Exception {
182
202
Map <String , String > map = new HashMap <String , String >();
203
+
183
204
assertTrue (conversionService .canConvert (Map .class , Map .class ));
184
205
assertSame (map , conversionService .convert (map , Map .class ));
185
206
}
@@ -189,6 +210,7 @@ public void emptyMapDifferentTargetImplType() throws Exception {
189
210
Map <String , String > map = new HashMap <String , String >();
190
211
TypeDescriptor sourceType = TypeDescriptor .forObject (map );
191
212
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("emptyMapDifferentTarget" ));
213
+
192
214
assertTrue (conversionService .canConvert (sourceType , targetType ));
193
215
@ SuppressWarnings ("unchecked" )
194
216
LinkedHashMap <String , String > result = (LinkedHashMap <String , String >) conversionService .convert (map , sourceType , targetType );
@@ -205,6 +227,7 @@ public void noDefaultConstructorCopyNotRequired() throws Exception {
205
227
TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class ));
206
228
TypeDescriptor targetType = TypeDescriptor .map (NoDefaultConstructorMap .class ,
207
229
TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class ));
230
+
208
231
assertTrue (conversionService .canConvert (sourceType , targetType ));
209
232
@ SuppressWarnings ("unchecked" )
210
233
Map <String , Integer > result = (Map <String , Integer >) conversionService .convert (map , sourceType , targetType );
@@ -220,6 +243,7 @@ public void multiValueMapToMultiValueMap() throws Exception {
220
243
source .put ("a" , Arrays .asList (1 , 2 , 3 ));
221
244
source .put ("b" , Arrays .asList (4 , 5 , 6 ));
222
245
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("multiValueMapTarget" ));
246
+
223
247
MultiValueMap <String , String > converted = (MultiValueMap <String , String >) conversionService .convert (source , targetType );
224
248
assertThat (converted .size (), equalTo (2 ));
225
249
assertThat (converted .get ("a" ), equalTo (Arrays .asList ("1" , "2" , "3" )));
@@ -234,6 +258,7 @@ public void mapToMultiValueMap() throws Exception {
234
258
source .put ("a" , 1 );
235
259
source .put ("b" , 2 );
236
260
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("multiValueMapTarget" ));
261
+
237
262
MultiValueMap <String , String > converted = (MultiValueMap <String , String >) conversionService .convert (source , targetType );
238
263
assertThat (converted .size (), equalTo (2 ));
239
264
assertThat (converted .get ("a" ), equalTo (Arrays .asList ("1" )));
@@ -249,23 +274,12 @@ public void testStringToEnumMap() throws Exception {
249
274
EnumMap <MyEnum , Integer > result = new EnumMap <MyEnum , Integer >(MyEnum .class );
250
275
result .put (MyEnum .A , 1 );
251
276
result .put (MyEnum .C , 2 );
252
- assertEquals (result ,
253
- conversionService .convert (source , TypeDescriptor .forObject (source ), new TypeDescriptor (getClass ().getField ("enumMap" ))));
254
- }
255
-
256
-
257
- @ SuppressWarnings ("serial" )
258
- public static class NoDefaultConstructorMap <K , V > extends HashMap <K , V > {
259
277
260
- public NoDefaultConstructorMap (Map <? extends K , ? extends V > map ) {
261
- super (map );
262
- }
278
+ assertEquals (result , conversionService .convert (source ,
279
+ TypeDescriptor .forObject (source ), new TypeDescriptor (getClass ().getField ("enumMap" ))));
263
280
}
264
281
265
282
266
- public static enum MyEnum {A , B , C }
267
-
268
-
269
283
public Map <Integer , Integer > scalarMapTarget ;
270
284
271
285
public Map <Integer , List <Integer >> collectionMapTarget ;
@@ -283,4 +297,16 @@ public static enum MyEnum {A, B, C}
283
297
284
298
public EnumMap <MyEnum , Integer > enumMap ;
285
299
300
+
301
+ @ SuppressWarnings ("serial" )
302
+ public static class NoDefaultConstructorMap <K , V > extends HashMap <K , V > {
303
+
304
+ public NoDefaultConstructorMap (Map <? extends K , ? extends V > map ) {
305
+ super (map );
306
+ }
307
+ }
308
+
309
+
310
+ public enum MyEnum {A , B , C }
311
+
286
312
}
0 commit comments