This commit is contained in:
Keith Donald
2011-05-23 07:38:27 +00:00
parent 5c67dbf424
commit 7430fcd904
8 changed files with 168 additions and 56 deletions

View File

@@ -1,7 +1,8 @@
package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
@@ -9,6 +10,8 @@ import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
@@ -28,6 +31,114 @@ public class CollectionToCollectionConverterTests {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
}
@Test
public void scalarList() throws Exception {
List<String> list = new ArrayList<String>();
list.add("9");
list.add("37");
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
assertFalse(conversionService.canConvert(sourceType, targetType));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
assertFalse(list.equals(result));
assertEquals((Integer) 9, result.get(0));
assertEquals((Integer) 37, result.get(1));
}
public List<Integer> scalarListTarget;
@Test
public void emptyListToList() throws Exception {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
assertEquals(list, conversionService.convert(list, sourceType, targetType));
}
public List<Integer> emptyListTarget;
@Test
public void emptyListToListDifferentTargetType() throws Exception {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType);
assertEquals(LinkedList.class, result.getClass());
assertTrue(result.isEmpty());
}
public LinkedList<Integer> emptyListDifferentTarget;
@Test
public void collectionToObjectInteraction() throws Exception {
List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList("9", "12"));
list.add(Arrays.asList("37", "23"));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
assertTrue(conversionService.canConvert(List.class, List.class));
assertEquals(list, conversionService.convert(list, List.class));
}
@Test
public void arrayCollectionToObjectInteraction() throws Exception {
List<String>[] array = new List[2];
array[0] = Arrays.asList("9", "12");
array[1] = Arrays.asList("37", "23");
conversionService.addConverter(new ArrayToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
assertTrue(conversionService.canConvert(String[].class, List.class));
assertEquals(Arrays.asList(array), conversionService.convert(array, List.class));
}
@Test
public void objectToCollection() throws Exception {
List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList("9", "12"));
list.add(Arrays.asList("37", "23"));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
assertTrue(conversionService.canConvert(sourceType, targetType));
List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
assertEquals((Integer)9, result.get(0).get(0).get(0));
assertEquals((Integer)12, result.get(0).get(1).get(0));
assertEquals((Integer)37, result.get(1).get(0).get(0));
assertEquals((Integer)23, result.get(1).get(1).get(0));
}
public List<List<List<Integer>>> objectToCollection;
@Test
public void stringToCollection() throws Exception {
List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList("9,12"));
list.add(Arrays.asList("37,23"));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverter(new StringToCollectionConverter(conversionService));
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
assertTrue(conversionService.canConvert(sourceType, targetType));
List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
assertEquals((Integer)9, result.get(0).get(0).get(0));
assertEquals((Integer)12, result.get(0).get(0).get(1));
assertEquals((Integer)37, result.get(1).get(0).get(0));
assertEquals((Integer)23, result.get(1).get(0).get(1));
}
@Test
public void differentImpls() throws Exception {
List<Resource> resources = new ArrayList<Resource>();
@@ -69,7 +180,7 @@ public class CollectionToCollectionConverterTests {
public List<Resource> resources;
public static abstract class BaseResource implements Resource {
public static abstract class BaseResource implements Resource {
public InputStream getInputStream() throws IOException {
// TODO Auto-generated method stub

View File

@@ -16,7 +16,16 @@
package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -24,9 +33,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
@@ -261,8 +268,13 @@ public class GenericConversionServiceTests {
@Test
public void testListOfList() {
GenericConversionService service = new DefaultConversionService();
List<List<String>> list = Collections.singletonList(Collections.singletonList("Foo"));
assertNotNull(service.convert(list, String.class));
List<String> list1 = Arrays.asList("Foo", "Bar");
List<String> list2 = Arrays.asList("Baz", "Boop");
@SuppressWarnings("unchecked")
List<List<String>> list = Arrays.asList(list1, list2);
String result = service.convert(list, String.class);
assertNotNull(result);
assertEquals("Foo,Bar,Baz,Boop", result);
}
@Test
@@ -360,36 +372,6 @@ public class GenericConversionServiceTests {
public static Map<String, Integer> map;
@Test
public void emptyListToList() throws Exception {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
assertEquals(list, conversionService.convert(list, sourceType, targetType));
}
public List<Integer> emptyListTarget;
@Test
public void emptyListToListDifferentTargetType() throws Exception {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType);
assertEquals(LinkedList.class, result.getClass());
assertTrue(result.isEmpty());
}
public LinkedList<Integer> emptyListDifferentTarget;
@Test
public void emptyListToArray() throws Exception {
conversionService.addConverter(new CollectionToArrayConverter(conversionService));

View File

@@ -52,7 +52,7 @@ public class MapToMapConverterTests {
}
@Test
public void scalarMapNotGenericSource() throws Exception {
public void scalarMapNotGenericSourceField() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "9");
map.put("2", "37");
@@ -117,6 +117,7 @@ public class MapToMapConverterTests {
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("1", Arrays.asList("9", "12"));
map.put("2", Arrays.asList("37", "23"));
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
assertTrue(conversionService.canConvert(Map.class, Map.class));
assertEquals(map, conversionService.convert(map, Map.class));
@@ -127,7 +128,7 @@ public class MapToMapConverterTests {
Map<String, String> map = new HashMap<String, String>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget"));
//assertTrue(conversionService.canConvert(sourceType, targetType));
assertTrue(conversionService.canConvert(sourceType, targetType));
assertEquals(map, conversionService.convert(map, sourceType, targetType));
}

View File

@@ -11,7 +11,7 @@
</layout>
</appender>
<logger name="org.springframework.core">
<logger name="org.springframework.core.convert">
<level value="warn" />
</logger>