1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 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.
44
44
/**
45
45
* @author Keith Donald
46
46
* @author Juergen Hoeller
47
+ * @author Stephane Nicoll
47
48
*/
48
49
public class CollectionToCollectionConverterTests {
49
50
50
51
private GenericConversionService conversionService = new GenericConversionService ();
51
52
53
+
52
54
@ Before
53
55
public void setUp () {
54
56
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
55
57
}
56
58
59
+
57
60
@ Test
58
61
public void scalarList () throws Exception {
59
62
List <String > list = new ArrayList <String >();
@@ -77,8 +80,6 @@ public void scalarList() throws Exception {
77
80
assertEquals (37 , result .get (1 ));
78
81
}
79
82
80
- public ArrayList <Integer > scalarListTarget ;
81
-
82
83
@ Test
83
84
public void emptyListToList () throws Exception {
84
85
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
@@ -90,8 +91,6 @@ public void emptyListToList() throws Exception {
90
91
assertEquals (list , conversionService .convert (list , sourceType , targetType ));
91
92
}
92
93
93
- public List <Integer > emptyListTarget ;
94
-
95
94
@ Test
96
95
public void emptyListToListDifferentTargetType () throws Exception {
97
96
conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
@@ -106,8 +105,6 @@ public void emptyListToListDifferentTargetType() throws Exception {
106
105
assertTrue (result .isEmpty ());
107
106
}
108
107
109
- public LinkedList <Integer > emptyListDifferentTarget ;
110
-
111
108
@ Test
112
109
public void collectionToObjectInteraction () throws Exception {
113
110
List <List <String >> list = new ArrayList <List <String >>();
@@ -149,8 +146,6 @@ public void objectToCollection() throws Exception {
149
146
assertEquals ((Integer ) 23 , result .get (1 ).get (1 ).get (0 ));
150
147
}
151
148
152
- public List <List <List <Integer >>> objectToCollection ;
153
-
154
149
@ Test
155
150
@ SuppressWarnings ("unchecked" )
156
151
public void stringToCollection () throws Exception {
@@ -165,10 +160,46 @@ public void stringToCollection() throws Exception {
165
160
TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("objectToCollection" ));
166
161
assertTrue (conversionService .canConvert (sourceType , targetType ));
167
162
List <List <List <Integer >>> result = (List <List <List <Integer >>>) conversionService .convert (list , sourceType , targetType );
168
- assertEquals ((Integer )9 , result .get (0 ).get (0 ).get (0 ));
169
- assertEquals ((Integer )12 , result .get (0 ).get (0 ).get (1 ));
170
- assertEquals ((Integer )37 , result .get (1 ).get (0 ).get (0 ));
171
- assertEquals ((Integer )23 , result .get (1 ).get (0 ).get (1 ));
163
+ assertEquals ((Integer ) 9 , result .get (0 ).get (0 ).get (0 ));
164
+ assertEquals ((Integer ) 12 , result .get (0 ).get (0 ).get (1 ));
165
+ assertEquals ((Integer ) 37 , result .get (1 ).get (0 ).get (0 ));
166
+ assertEquals ((Integer ) 23 , result .get (1 ).get (0 ).get (1 ));
167
+ }
168
+
169
+ @ Test
170
+ public void convertEmptyVector_shouldReturnEmptyArrayList () {
171
+ Vector <String > vector = new Vector <String >();
172
+ vector .add ("Element" );
173
+ testCollectionConversionToArrayList (vector );
174
+ }
175
+
176
+ @ Test
177
+ public void convertNonEmptyVector_shouldReturnNonEmptyArrayList () {
178
+ Vector <String > vector = new Vector <String >();
179
+ vector .add ("Element" );
180
+ testCollectionConversionToArrayList (vector );
181
+ }
182
+
183
+ @ Test
184
+ public void testCollectionsEmptyList () throws Exception {
185
+ CollectionToCollectionConverter converter = new CollectionToCollectionConverter (new GenericConversionService ());
186
+ TypeDescriptor type = new TypeDescriptor (getClass ().getField ("list" ));
187
+ converter .convert (list , type , TypeDescriptor .valueOf (Class .forName ("java.util.Collections$EmptyList" )));
188
+ }
189
+
190
+ @ SuppressWarnings ("rawtypes" )
191
+ private void testCollectionConversionToArrayList (Collection <String > aSource ) {
192
+ Object myConverted = (new CollectionToCollectionConverter (new GenericConversionService ())).convert (
193
+ aSource , TypeDescriptor .forObject (aSource ), TypeDescriptor .forObject (new ArrayList ()));
194
+ assertTrue (myConverted instanceof ArrayList <?>);
195
+ assertEquals (aSource .size (), ((ArrayList <?>) myConverted ).size ());
196
+ }
197
+
198
+ @ Test
199
+ public void listToCollectionNoCopyRequired () throws NoSuchFieldException {
200
+ List <?> input = new ArrayList <String >(Arrays .asList ("foo" , "bar" ));
201
+ assertSame (input , conversionService .convert (input , TypeDescriptor .forObject (input ),
202
+ new TypeDescriptor (getClass ().getField ("wildCardCollection" ))));
172
203
}
173
204
174
205
@ Test
@@ -210,8 +241,6 @@ public void elementTypesNotConvertible() throws Exception {
210
241
assertEquals (resources , conversionService .convert (resources , sourceType , new TypeDescriptor (getClass ().getField ("resources" ))));
211
242
}
212
243
213
- public List <String > strings ;
214
-
215
244
@ Test (expected =ConversionFailedException .class )
216
245
public void nothingInCommon () throws Exception {
217
246
List <Object > resources = new ArrayList <Object >();
@@ -221,8 +250,24 @@ public void nothingInCommon() throws Exception {
221
250
assertEquals (resources , conversionService .convert (resources , sourceType , new TypeDescriptor (getClass ().getField ("resources" ))));
222
251
}
223
252
253
+
254
+ public ArrayList <Integer > scalarListTarget ;
255
+
256
+ public List <Integer > emptyListTarget ;
257
+
258
+ public LinkedList <Integer > emptyListDifferentTarget ;
259
+
260
+ public List <List <List <Integer >>> objectToCollection ;
261
+
262
+ public List <String > strings ;
263
+
264
+ public List list = Collections .emptyList ();
265
+
266
+ public Collection <?> wildCardCollection = Collections .emptyList ();
267
+
224
268
public List <Resource > resources ;
225
269
270
+
226
271
public static abstract class BaseResource implements Resource {
227
272
228
273
@ Override
@@ -286,39 +331,8 @@ public String getDescription() {
286
331
}
287
332
}
288
333
289
- public static class TestResource extends BaseResource {
290
-
291
- }
292
-
293
- @ Test
294
- public void convertEmptyVector_shouldReturnEmptyArrayList () {
295
- Vector <String > vector = new Vector <String >();
296
- vector .add ("Element" );
297
- testCollectionConversionToArrayList (vector );
298
- }
299
334
300
- @ Test
301
- public void convertNonEmptyVector_shouldReturnNonEmptyArrayList () {
302
- Vector <String > vector = new Vector <String >();
303
- vector .add ("Element" );
304
- testCollectionConversionToArrayList (vector );
305
- }
306
-
307
- @ Test
308
- public void testCollectionsEmptyList () throws Exception {
309
- CollectionToCollectionConverter converter = new CollectionToCollectionConverter (new GenericConversionService ());
310
- TypeDescriptor type = new TypeDescriptor (getClass ().getField ("list" ));
311
- converter .convert (list , type , TypeDescriptor .valueOf (Class .forName ("java.util.Collections$EmptyList" )));
312
- }
313
-
314
- public List list = Collections .emptyList ();
315
-
316
- @ SuppressWarnings ("rawtypes" )
317
- private void testCollectionConversionToArrayList (Collection <String > aSource ) {
318
- Object myConverted = (new CollectionToCollectionConverter (new GenericConversionService ())).convert (
319
- aSource , TypeDescriptor .forObject (aSource ), TypeDescriptor .forObject (new ArrayList ()));
320
- assertTrue (myConverted instanceof ArrayList <?>);
321
- assertEquals (aSource .size (), ((ArrayList <?>) myConverted ).size ());
335
+ public static class TestResource extends BaseResource {
322
336
}
323
337
324
338
}
0 commit comments