Search in sources :

Example 11 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DataBinderFieldAccessTests method bindingWithErrorsAndCustomEditors.

@Test
public void bindingWithErrorsAndCustomEditors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.initDirectFieldAccess();
    binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 0));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("name", "Rod"));
    pvs.addPropertyValue(new PropertyValue("age", "32x"));
    pvs.addPropertyValue(new PropertyValue("spouse", "Kerry"));
    binder.bind(pvs);
    try {
        binder.close();
        fail("Should have thrown BindException");
    } catch (BindException ex) {
        assertTrue("changed name correctly", rod.getName().equals("Rod"));
        //assertTrue("changed age correctly", rod.getAge() == 32);
        Map<?, ?> model = binder.getBindingResult().getModel();
        //assertTrue("There are 3 element in map", m.size() == 1);
        FieldAccessBean tb = (FieldAccessBean) model.get("person");
        assertTrue("Same object", tb.equals(rod));
        BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
        assertTrue("Added itself to map", br == binder.getBindingResult());
        assertTrue(br.hasErrors());
        assertTrue("Correct number of errors", br.getErrorCount() == 1);
        assertTrue("Has age errors", br.hasFieldErrors("age"));
        assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
        assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
        assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
        assertEquals(0, tb.getAge());
        assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
        assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));
        assertNotNull(tb.getSpouse());
    }
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FieldAccessBean(org.springframework.tests.sample.beans.FieldAccessBean) PropertyValue(org.springframework.beans.PropertyValue) Map(java.util.Map) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 12 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DataBinderFieldAccessTests method nestedBindingWithDisabledAutoGrow.

@Test
public void nestedBindingWithDisabledAutoGrow() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.setAutoGrowNestedPaths(false);
    binder.initDirectFieldAccess();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
    thrown.expect(NullValueInNestedPathException.class);
    binder.bind(pvs);
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FieldAccessBean(org.springframework.tests.sample.beans.FieldAccessBean) PropertyValue(org.springframework.beans.PropertyValue) Test(org.junit.Test)

Example 13 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DataBinderFieldAccessTests method bindingNoErrors.

@Test
public void bindingNoErrors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertTrue(binder.isIgnoreUnknownFields());
    binder.initDirectFieldAccess();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("name", "Rod"));
    pvs.addPropertyValue(new PropertyValue("age", new Integer(32)));
    pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue"));
    binder.bind(pvs);
    binder.close();
    assertTrue("changed name correctly", rod.getName().equals("Rod"));
    assertTrue("changed age correctly", rod.getAge() == 32);
    Map<?, ?> m = binder.getBindingResult().getModel();
    assertTrue("There is one element in map", m.size() == 2);
    FieldAccessBean tb = (FieldAccessBean) m.get("person");
    assertTrue("Same object", tb.equals(rod));
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FieldAccessBean(org.springframework.tests.sample.beans.FieldAccessBean) PropertyValue(org.springframework.beans.PropertyValue) Test(org.junit.Test)

Example 14 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class JdbcNamespaceIntegrationTests method assertBeanPropertyValueOf.

private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
    BeanDefinition bean = factory.getBeanDefinition(expected);
    PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
    assertThat(value, is(notNullValue()));
    assertThat(value.getValue().toString(), is(expected));
}
Also used : PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 15 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class AbstractListenerContainerParser method parseListener.

private void parseListener(Element containerEle, Element listenerEle, ParserContext parserContext, PropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
    RootBeanDefinition listenerDef = new RootBeanDefinition();
    listenerDef.setSource(parserContext.extractSource(listenerEle));
    listenerDef.setBeanClassName("org.springframework.jms.listener.adapter.MessageListenerAdapter");
    String ref = listenerEle.getAttribute(REF_ATTRIBUTE);
    if (!StringUtils.hasText(ref)) {
        parserContext.getReaderContext().error("Listener 'ref' attribute contains empty value.", listenerEle);
    } else {
        listenerDef.getPropertyValues().add("delegate", new RuntimeBeanReference(ref));
    }
    String method = null;
    if (listenerEle.hasAttribute(METHOD_ATTRIBUTE)) {
        method = listenerEle.getAttribute(METHOD_ATTRIBUTE);
        if (!StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Listener 'method' attribute contains empty value.", listenerEle);
        }
    }
    listenerDef.getPropertyValues().add("defaultListenerMethod", method);
    PropertyValue messageConverterPv = commonContainerProperties.getPropertyValue("messageConverter");
    if (messageConverterPv != null) {
        listenerDef.getPropertyValues().addPropertyValue(messageConverterPv);
    }
    BeanDefinition containerDef = createContainer(containerEle, listenerEle, parserContext, commonContainerProperties, specificContainerProperties);
    containerDef.getPropertyValues().add("messageListener", listenerDef);
    if (listenerEle.hasAttribute(RESPONSE_DESTINATION_ATTRIBUTE)) {
        String responseDestination = listenerEle.getAttribute(RESPONSE_DESTINATION_ATTRIBUTE);
        Boolean pubSubDomain = (Boolean) commonContainerProperties.getPropertyValue("replyPubSubDomain").getValue();
        listenerDef.getPropertyValues().add(pubSubDomain ? "defaultResponseTopicName" : "defaultResponseQueueName", responseDestination);
        if (containerDef.getPropertyValues().contains("destinationResolver")) {
            listenerDef.getPropertyValues().add("destinationResolver", containerDef.getPropertyValues().getPropertyValue("destinationResolver").getValue());
        }
    }
    String containerBeanName = listenerEle.getAttribute(ID_ATTRIBUTE);
    // If no bean id is given auto generate one using the ReaderContext's BeanNameGenerator
    if (!StringUtils.hasText(containerBeanName)) {
        containerBeanName = parserContext.getReaderContext().generateBeanName(containerDef);
    }
    // Register the listener and fire event
    parserContext.registerBeanComponent(new BeanComponentDefinition(containerDef, containerBeanName));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)41 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 Test (org.junit.Test)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 FieldAccessBean (org.springframework.tests.sample.beans.FieldAccessBean)6 ArrayList (java.util.ArrayList)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2