Search in sources :

Example 61 with TypeDescriptor

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));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 62 with TypeDescriptor

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"))));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) Test(org.junit.Test)

Example 63 with TypeDescriptor

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"))));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 64 with TypeDescriptor

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")));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Test(org.junit.Test)

Example 65 with TypeDescriptor

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());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Aggregations

TypeDescriptor (org.springframework.core.convert.TypeDescriptor)115 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)35 List (java.util.List)20 Map (java.util.Map)16 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 LinkedList (java.util.LinkedList)12 MethodParameter (org.springframework.core.MethodParameter)12 Collection (java.util.Collection)11 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)10 Method (java.lang.reflect.Method)9 AccessException (org.springframework.expression.AccessException)9 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)8 TypedValue (org.springframework.expression.TypedValue)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)8 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)8 MultiValueMap (org.springframework.util.MultiValueMap)8 AbstractList (java.util.AbstractList)7 Field (java.lang.reflect.Field)6