use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method objectToCollection.
@Test
@SuppressWarnings("unchecked")
public void objectToCollection() throws Exception {
List<List<String>> list = new ArrayList<>();
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));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method allNulls.
@Test
public void allNulls() throws Exception {
List<Resource> resources = new ArrayList<>();
resources.add(null);
resources.add(null);
TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method mixedInNulls.
@Test
public void mixedInNulls() throws Exception {
List<Resource> resources = new ArrayList<>();
resources.add(new ClassPathResource("test"));
resources.add(null);
resources.add(new FileSystemResource("test"));
resources.add(new TestResource());
TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method testCollectionsEmptyList.
@Test
public void testCollectionsEmptyList() throws Exception {
CollectionToCollectionConverter converter = new CollectionToCollectionConverter(new GenericConversionService());
TypeDescriptor type = new TypeDescriptor(getClass().getField("list"));
converter.convert(list, type, TypeDescriptor.valueOf(Class.forName("java.util.Collections$EmptyList")));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class GenericConversionServiceTests method testPerformance2.
@Test
public void testPerformance2() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
List<String> source = new LinkedList<>();
source.add("1");
source.add("2");
source.add("3");
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
for (int i = 0; i < 1000000; i++) {
conversionService.convert(source, TypeDescriptor.forObject(source), td);
}
watch.stop();
watch.start("convert 4,000,000 manually");
for (int i = 0; i < 4000000; i++) {
List<Integer> target = new ArrayList<>(source.size());
for (String element : source) {
target.add(Integer.valueOf(element));
}
}
watch.stop();
// System.out.println(watch.prettyPrint());
}
Aggregations