Search in sources :

Example 36 with TypeDescriptor

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

the class GenericConversionServiceTests method stringToCollectionCanConvert.

@Test
public void stringToCollectionCanConvert() throws Exception {
    conversionService.addConverter(new StringToCollectionConverter(conversionService));
    assertTrue(conversionService.canConvert(String.class, Collection.class));
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("integerCollection"));
    assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Collection(java.util.Collection) Test(org.junit.Test)

Example 37 with TypeDescriptor

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

the class GenericConversionServiceTests method emptyListToArray.

@Test
public void emptyListToArray() {
    conversionService.addConverter(new CollectionToArrayConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    List<String> list = new ArrayList<>();
    TypeDescriptor sourceType = TypeDescriptor.forObject(list);
    TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class);
    assertTrue(conversionService.canConvert(sourceType, targetType));
    assertEquals(0, ((String[]) conversionService.convert(list, sourceType, targetType)).length);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 38 with TypeDescriptor

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

the class GenericConversionServiceTests method convertNullAnnotatedStringToString.

@Test
public void convertNullAnnotatedStringToString() throws Exception {
    String source = null;
    TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString"));
    TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
    conversionService.convert(source, sourceType, targetType);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Test(org.junit.Test)

Example 39 with TypeDescriptor

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

the class MapToMapConverterTests method emptyMap.

@Test
public void emptyMap() throws Exception {
    Map<String, String> map = new HashMap<>();
    TypeDescriptor sourceType = TypeDescriptor.forObject(map);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    assertSame(map, conversionService.convert(map, sourceType, targetType));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 40 with TypeDescriptor

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

the class MapToMapConverterTests method mapToMultiValueMap.

@Test
@SuppressWarnings("unchecked")
public void mapToMultiValueMap() throws Exception {
    DefaultConversionService.addDefaultConverters(conversionService);
    Map<String, Integer> source = new HashMap<>();
    source.put("a", 1);
    source.put("b", 2);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
    MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
    assertThat(converted.size(), equalTo(2));
    assertThat(converted.get("a"), equalTo(Arrays.asList("1")));
    assertThat(converted.get("b"), equalTo(Arrays.asList("2")));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) 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