Search in sources :

Example 66 with TypeDescriptor

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

the class GenericConversionServiceTests method adaptedCollectionTypesFromSameSourceType.

@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
    conversionService.addConverter(new MyStringToStringCollectionConverter());
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
    try {
        conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
        fail("Should have thrown ConverterNotFoundException");
    } catch (ConverterNotFoundException ex) {
    // expected
    }
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Test(org.junit.Test)

Example 67 with TypeDescriptor

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

the class GenericConversionServiceTests method emptyListToObject.

@Test
public void emptyListToObject() {
    conversionService.addConverter(new CollectionToObjectConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    List<String> list = new ArrayList<>();
    TypeDescriptor sourceType = TypeDescriptor.forObject(list);
    TypeDescriptor targetType = TypeDescriptor.valueOf(Integer.class);
    assertTrue(conversionService.canConvert(sourceType, targetType));
    assertNull(conversionService.convert(list, sourceType, targetType));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 68 with TypeDescriptor

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

the class GenericConversionServiceTests method testPerformance3.

@Test
public void testPerformance3() throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
    watch.start("convert 4,000,000 with conversion service");
    Map<String, String> source = new HashMap<>();
    source.put("1", "1");
    source.put("2", "2");
    source.put("3", "3");
    TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
    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++) {
        Map<String, Integer> target = new HashMap<>(source.size());
        for (Map.Entry<String, String> entry : source.entrySet()) {
            target.put(entry.getKey(), Integer.valueOf(entry.getValue()));
        }
    }
    watch.stop();
// System.out.println(watch.prettyPrint());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 69 with TypeDescriptor

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

the class GenericConversionServiceTests method testWildcardMap.

@Test
public void testWildcardMap() throws Exception {
    Map<String, String> input = new LinkedHashMap<>();
    input.put("key", "value");
    Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
    assertEquals(input, converted);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 70 with TypeDescriptor

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

the class MapToMapConverterTests method scalarMapNotGenericSourceField.

@Test
public void scalarMapNotGenericSourceField() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("1", "9");
    map.put("2", "37");
    TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof ConverterNotFoundException);
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
    assertFalse(map.equals(result));
    assertEquals((Integer) 9, result.get(1));
    assertEquals((Integer) 37, result.get(2));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) EnumMap(java.util.EnumMap) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) 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