Search in sources :

Example 1 with ConversionFailedException

use of cn.taketoday.core.conversion.ConversionFailedException in project today-infrastructure by TAKETODAY.

the class AbstractPropertyAccessorTests method setPropertyIntermediateListIsNullWithBadConversionService.

@Test
void setPropertyIntermediateListIsNullWithBadConversionService() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new GenericConversionService() {

        @Override
        public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    accessor.setAutoGrowNestedPaths(true);
    accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertThat(target.listOfMaps.get(0).get("luckyNumber")).isEqualTo("9");
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with ConversionFailedException

use of cn.taketoday.core.conversion.ConversionFailedException in project today-infrastructure by TAKETODAY.

the class MapToMapConverterTests method scalarMap.

@Test
void scalarMap() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("1", "9");
    map.put("2", "37");
    TypeDescriptor sourceType = TypeDescriptor.fromObject(map);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
    assertThat(map.equals(result)).isFalse();
    assertThat((int) result.get(1)).isEqualTo(9);
    assertThat((int) result.get(2)).isEqualTo(37);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 3 with ConversionFailedException

use of cn.taketoday.core.conversion.ConversionFailedException in project today-infrastructure by TAKETODAY.

the class MapToMapConverterTests method scalarMapNotGenericSourceField.

@Test
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"));
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
    assertThat(map.equals(result)).isFalse();
    assertThat((int) result.get(1)).isEqualTo(9);
    assertThat((int) result.get(2)).isEqualTo(37);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 4 with ConversionFailedException

use of cn.taketoday.core.conversion.ConversionFailedException in project today-framework by TAKETODAY.

the class BindFailureAnalyzer method getMessage.

private String getMessage(BindException cause) {
    Throwable rootCause = getRootCause(cause.getCause());
    ConversionFailedException conversionFailure = findCause(cause, ConversionFailedException.class);
    if (conversionFailure != null) {
        String message = "failed to convert " + conversionFailure.getSourceType() + " to " + conversionFailure.getTargetType();
        if (rootCause != null) {
            message += " (caused by " + getExceptionTypeAndMessage(rootCause) + ")";
        }
        return message;
    }
    if (rootCause != null && StringUtils.hasText(rootCause.getMessage())) {
        return getExceptionTypeAndMessage(rootCause);
    }
    return getExceptionTypeAndMessage(cause);
}
Also used : ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException)

Example 5 with ConversionFailedException

use of cn.taketoday.core.conversion.ConversionFailedException in project today-framework by TAKETODAY.

the class MapToMapConverterTests method scalarMapNotGenericSourceField.

@Test
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"));
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
    assertThat(map.equals(result)).isFalse();
    assertThat((int) result.get(1)).isEqualTo(9);
    assertThat((int) result.get(2)).isEqualTo(37);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)12 TypeDescriptor (cn.taketoday.core.TypeDescriptor)10 Test (org.junit.jupiter.api.Test)10 ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)8 MultiValueMap (cn.taketoday.core.MultiValueMap)6 EnumMap (java.util.EnumMap)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 List (java.util.List)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 ArrayList (java.util.ArrayList)2