Search in sources :

Example 46 with TypeDescriptor

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

the class CollectionToCollectionConverterTests method scalarList.

@Test
public void scalarList() throws Exception {
    List<String> list = new ArrayList<>();
    list.add("9");
    list.add("37");
    TypeDescriptor sourceType = TypeDescriptor.forObject(list);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(list, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof ConverterNotFoundException);
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
    assertFalse(list.equals(result));
    assertEquals(9, result.get(0).intValue());
    assertEquals(37, result.get(1).intValue());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 47 with TypeDescriptor

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

the class CollectionToCollectionConverterTests method differentImpls.

@Test
public void differentImpls() throws Exception {
    List<Resource> resources = new ArrayList<>();
    resources.add(new ClassPathResource("test"));
    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 48 with TypeDescriptor

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

the class CollectionToCollectionConverterTests method nothingInCommon.

@Test(expected = ConversionFailedException.class)
public void nothingInCommon() throws Exception {
    List<Object> resources = new ArrayList<>();
    resources.add(new ClassPathResource("test"));
    resources.add(3);
    TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
    assertEquals(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) Test(org.junit.Test)

Example 49 with TypeDescriptor

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

the class MapToMapConverterTests method noDefaultConstructorCopyNotRequired.

@Test
public void noDefaultConstructorCopyNotRequired() throws Exception {
    // SPR-9284
    NoDefaultConstructorMap<String, Integer> map = new NoDefaultConstructorMap<>(Collections.<String, Integer>singletonMap("1", 1));
    TypeDescriptor sourceType = TypeDescriptor.map(NoDefaultConstructorMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
    TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType);
    assertEquals(map, result);
    assertEquals(NoDefaultConstructorMap.class, result.getClass());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) 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)

Example 50 with TypeDescriptor

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

the class StreamConverterTests method convertFromStreamToList.

@Test
public void convertFromStreamToList() throws NoSuchFieldException {
    this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
    Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
    TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("listOfStrings"));
    ;
    Object result = this.conversionService.convert(stream, listOfStrings);
    assertNotNull("Converted object must not be null", result);
    assertTrue("Converted object must be a list", result instanceof List);
    @SuppressWarnings("unchecked") List<String> content = (List<String>) result;
    assertEquals("1", content.get(0));
    assertEquals("2", content.get(1));
    assertEquals("3", content.get(2));
    assertEquals("Wrong number of elements", 3, content.size());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) List(java.util.List) 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