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