Search in sources :

Example 1 with GenericConversionService

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

the class DefaultListableBeanFactoryTests method testCustomConverter.

@Test
public void testCustomConverter() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<String, Float>() {

        @Override
        public Float convert(String source) {
            try {
                NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
                return nf.parse(source).floatValue();
            } catch (ParseException ex) {
                throw new IllegalArgumentException(ex);
            }
        }
    });
    lbf.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,1");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("testBean", bd);
    TestBean testBean = (TestBean) lbf.getBean("testBean");
    assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ParseException(java.text.ParseException) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 2 with GenericConversionService

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

the class AbstractPropertyAccessorTests method setPropertyIntermediateListIsNullWithBadConversionService.

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

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

Example 3 with GenericConversionService

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

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;
        }
    };
    eContext.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(org.springframework.expression.spel.support.StandardTypeConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 4 with GenericConversionService

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

the class OpPlusTests method test_binaryPlusWithTimeConverted.

@Test
public void test_binaryPlusWithTimeConverted() {
    final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
    GenericConversionService conversionService = new GenericConversionService();
    conversionService.addConverter(new Converter<Time, String>() {

        @Override
        public String convert(Time source) {
            return format.format(source);
        }
    });
    StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
    evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
    ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertEquals(String.class, value.getTypeDescriptor().getObjectType());
    assertEquals(String.class, value.getTypeDescriptor().getType());
    assertEquals(format.format(time) + " is now", value.getValue());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) StandardTypeConverter(org.springframework.expression.spel.support.StandardTypeConverter) ExpressionState(org.springframework.expression.spel.ExpressionState) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.Test)

Example 5 with GenericConversionService

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

the class DefaultMessageHandlerMethodFactoryTests method customConversionServiceFailure.

@Test
public void customConversionServiceFailure() throws Exception {
    DefaultMessageHandlerMethodFactory instance = createInstance();
    GenericConversionService conversionService = new GenericConversionService();
    assertFalse("conversion service should fail to convert payload", conversionService.canConvert(Integer.class, String.class));
    instance.setConversionService(conversionService);
    instance.afterPropertiesSet();
    InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "simpleString", String.class);
    thrown.expect(MessageConversionException.class);
    invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build());
}
Also used : InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Aggregations

GenericConversionService (org.springframework.core.convert.support.GenericConversionService)10 Test (org.junit.Test)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)2 InitBinder (org.springframework.web.bind.annotation.InitBinder)2 GrailsPlugin (grails.plugins.GrailsPlugin)1 ConfigObject (groovy.util.ConfigObject)1 Time (java.sql.Time)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 NavigableMap (org.grails.config.NavigableMap)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1