11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4444/**
4545 * @author Keith Donald
4646 * @author Juergen Hoeller
47+ * @author Stephane Nicoll
4748 */
4849public class CollectionToCollectionConverterTests {
4950
5051 private GenericConversionService conversionService = new GenericConversionService ();
5152
53+
5254 @ Before
5355 public void setUp () {
5456 conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
5557 }
5658
59+
5760 @ Test
5861 public void scalarList () throws Exception {
5962 List <String > list = new ArrayList <String >();
@@ -77,8 +80,6 @@ public void scalarList() throws Exception {
7780 assertEquals (37 , result .get (1 ));
7881 }
7982
80- public ArrayList <Integer > scalarListTarget ;
81-
8283 @ Test
8384 public void emptyListToList () throws Exception {
8485 conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
@@ -90,8 +91,6 @@ public void emptyListToList() throws Exception {
9091 assertEquals (list , conversionService .convert (list , sourceType , targetType ));
9192 }
9293
93- public List <Integer > emptyListTarget ;
94-
9594 @ Test
9695 public void emptyListToListDifferentTargetType () throws Exception {
9796 conversionService .addConverter (new CollectionToCollectionConverter (conversionService ));
@@ -106,8 +105,6 @@ public void emptyListToListDifferentTargetType() throws Exception {
106105 assertTrue (result .isEmpty ());
107106 }
108107
109- public LinkedList <Integer > emptyListDifferentTarget ;
110-
111108 @ Test
112109 public void collectionToObjectInteraction () throws Exception {
113110 List <List <String >> list = new ArrayList <List <String >>();
@@ -149,8 +146,6 @@ public void objectToCollection() throws Exception {
149146 assertEquals ((Integer ) 23 , result .get (1 ).get (1 ).get (0 ));
150147 }
151148
152- public List <List <List <Integer >>> objectToCollection ;
153-
154149 @ Test
155150 @ SuppressWarnings ("unchecked" )
156151 public void stringToCollection () throws Exception {
@@ -165,10 +160,46 @@ public void stringToCollection() throws Exception {
165160 TypeDescriptor targetType = new TypeDescriptor (getClass ().getField ("objectToCollection" ));
166161 assertTrue (conversionService .canConvert (sourceType , targetType ));
167162 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" ))));
172203 }
173204
174205 @ Test
@@ -210,8 +241,6 @@ public void elementTypesNotConvertible() throws Exception {
210241 assertEquals (resources , conversionService .convert (resources , sourceType , new TypeDescriptor (getClass ().getField ("resources" ))));
211242 }
212243
213- public List <String > strings ;
214-
215244 @ Test (expected =ConversionFailedException .class )
216245 public void nothingInCommon () throws Exception {
217246 List <Object > resources = new ArrayList <Object >();
@@ -221,8 +250,24 @@ public void nothingInCommon() throws Exception {
221250 assertEquals (resources , conversionService .convert (resources , sourceType , new TypeDescriptor (getClass ().getField ("resources" ))));
222251 }
223252
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+
224268 public List <Resource > resources ;
225269
270+
226271 public static abstract class BaseResource implements Resource {
227272
228273 @ Override
@@ -286,39 +331,8 @@ public String getDescription() {
286331 }
287332 }
288333
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- }
299334
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 {
322336 }
323337
324338}
0 commit comments