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");
}
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);
}
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);
}
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);
}
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);
}
Aggregations