Search in sources :

Example 56 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.

@Test
public void createDefaultConversionServiceWithSupplements() {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    Set<Object> converters = new HashSet<>();
    converters.add(new Converter<String, Foo>() {

        @Override
        public Foo convert(String source) {
            return new Foo();
        }
    });
    converters.add(new ConverterFactory<String, Bar>() {

        @Override
        public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
            return new Converter<String, T>() {

                @SuppressWarnings("unchecked")
                @Override
                public T convert(String source) {
                    return (T) new Bar();
                }
            };
        }
    });
    converters.add(new GenericConverter() {

        @Override
        public Set<ConvertiblePair> getConvertibleTypes() {
            return Collections.singleton(new ConvertiblePair(String.class, Baz.class));
        }

        @Override
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return new Baz();
        }
    });
    factory.setConverters(converters);
    factory.afterPropertiesSet();
    ConversionService service = factory.getObject();
    assertTrue(service.canConvert(String.class, Integer.class));
    assertTrue(service.canConvert(String.class, Foo.class));
    assertTrue(service.canConvert(String.class, Bar.class));
    assertTrue(service.canConvert(String.class, Baz.class));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(org.springframework.core.convert.converter.GenericConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionService(org.springframework.core.convert.ConversionService) Converter(org.springframework.core.convert.converter.Converter) GenericConverter(org.springframework.core.convert.converter.GenericConverter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 57 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class CollectionToCollectionConverterTests method elementTypesNotConvertible.

@Test(expected = ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
    List<String> resources = new ArrayList<>();
    resources.add(null);
    resources.add(null);
    TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
    assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 58 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class CollectionToCollectionConverterTests method emptyListToListDifferentTargetType.

@Test
public void emptyListToListDifferentTargetType() throws Exception {
    conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    List<String> list = new ArrayList<>();
    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());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 59 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class CollectionToCollectionConverterTests method stringToCollection.

@Test
@SuppressWarnings("unchecked")
public void stringToCollection() 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 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));
}
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 60 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class CollectionToCollectionConverterTests method emptyListToList.

@Test
public void emptyListToList() throws Exception {
    conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    List<String> list = new ArrayList<>();
    TypeDescriptor sourceType = TypeDescriptor.forObject(list);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    assertEquals(list, conversionService.convert(list, sourceType, targetType));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) 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