Search in sources :

Example 1 with GenericConversionService

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

the class BooleanExpressionTests method testConvertAndHandleNull.

@Test
public void testConvertAndHandleNull() {
    // SPR-9445
    // without null conversion
    evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
    evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    // with null conversion (null -> false)
    GenericConversionService conversionService = new GenericConversionService() {

        @Override
        protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
            return targetType.getType() == Boolean.class ? false : null;
        }
    };
    context.setTypeConverter(new StandardTypeConverter(conversionService));
    evaluate("null or true", Boolean.TRUE, Boolean.class, false);
    evaluate("null and true", Boolean.FALSE, Boolean.class, false);
    evaluate("!null", Boolean.TRUE, Boolean.class, false);
    evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with GenericConversionService

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

the class BindConverterTests method getBindConverter.

private BindConverter getBindConverter(Converter<?, ?> converter) {
    GenericConversionService conversionService = new GenericConversionService();
    conversionService.addConverter(converter);
    return BindConverter.get(Collections.singletonList(conversionService), null);
}
Also used : GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService)

Example 3 with GenericConversionService

use of cn.taketoday.core.conversion.support.GenericConversionService 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 4 with GenericConversionService

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

the class BooleanExpressionTests method testConvertAndHandleNull.

@Test
public void testConvertAndHandleNull() {
    // SPR-9445
    // without null conversion
    evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
    evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
    // with null conversion (null -> false)
    GenericConversionService conversionService = new GenericConversionService() {

        @Override
        protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
            return targetType.getType() == Boolean.class ? false : null;
        }
    };
    context.setTypeConverter(new StandardTypeConverter(conversionService));
    evaluate("null or true", Boolean.TRUE, Boolean.class, false);
    evaluate("null and true", Boolean.FALSE, Boolean.class, false);
    evaluate("!null", Boolean.TRUE, Boolean.class, false);
    evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 5 with GenericConversionService

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

the class BindConverterTests method fallsBackToApplicationConversionService.

@Test
void fallsBackToApplicationConversionService() {
    BindConverter bindConverter = BindConverter.get(Collections.singletonList(new GenericConversionService()), null);
    Duration result = bindConverter.convert("10s", ResolvableType.fromClass(Duration.class));
    assertThat(result.getSeconds()).isEqualTo(10);
}
Also used : Duration(java.time.Duration) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)12 Test (org.junit.jupiter.api.Test)10 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4 StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)4 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)2 TypedValue (cn.taketoday.expression.TypedValue)2 ExpressionState (cn.taketoday.expression.spel.ExpressionState)2 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)2 File (java.io.File)2 Time (java.sql.Time)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Duration (java.time.Duration)2 Date (java.util.Date)2