use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SelectTagTests method withFloatCustom.
@Test
public void withFloatCustom() throws Exception {
PropertyEditor propertyEditor = new SimpleFloatEditor();
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
exposeBindingResult(errors);
this.tag.setPath("myFloat");
Float[] array = new Float[] { new Float("12.30"), new Float("12.32"), new Float("12.34"), new Float("12.36"), new Float("12.38"), new Float("12.40"), new Float("12.42"), new Float("12.44"), new Float("12.46"), new Float("12.48") };
this.tag.setItems(array);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>"));
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertEquals("select", rootElement.getName());
assertEquals("myFloat", rootElement.attribute("name").getValue());
List children = rootElement.elements();
assertEquals("Incorrect number of children", array.length, children.size());
Element e = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
assertEquals("'12.34' node not selected", "selected", e.attribute("selected").getValue());
e = (Element) rootElement.selectSingleNode("option[text() = '12.32f']");
assertNull("'12.32' node incorrectly selected", e.attribute("selected"));
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SelectTagTests method nestedPathWithListAndEditorAndNullValue.
@Test
public void nestedPathWithListAndEditorAndNullValue() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setItemLabel("name");
this.tag.setMultiple("false");
TestBeanWrapper testBean = new TestBeanWrapper();
TestBeanWithRealCountry withCountry = (TestBeanWithRealCountry) getTestBean();
withCountry.setRealCountry(null);
testBean.setBean(withCountry);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.length() == 0) {
setValue(null);
return;
}
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return null;
}
return value.getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>"));
assertFalse(output.contains("selected=\"selected\""));
assertFalse(output.contains("multiple=\"multiple\""));
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SelectTagTests method withListAndEditor.
@Test
public void withListAndEditor() throws Exception {
this.tag.setPath("realCountry");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setItemLabel("name");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>"));
assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class RadioButtonTagTests method withCheckedObjectValueAndEditor.
@Test
public void withCheckedObjectValueAndEditor() throws Exception {
this.tag.setPath("myFloat");
this.tag.setValue("F12.99");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
MyFloatEditor editor = new MyFloatEditor();
bindingResult.getPropertyEditorRegistry().registerCustomEditor(Float.class, editor);
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
assertTagOpened(output);
assertTagClosed(output);
assertContainsAttribute(output, "name", "myFloat");
assertContainsAttribute(output, "type", "radio");
assertContainsAttribute(output, "value", "F" + getFloat().toString());
assertContainsAttribute(output, "checked", "checked");
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class RadioButtonTagTests method collectionOfPetsWithEditor.
@Test
public void collectionOfPetsWithEditor() throws Exception {
this.tag.setPath("pets");
this.tag.setValue(new ItemPet("Rudiger"));
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
PropertyEditorSupport editor = new ItemPet.CustomEditor();
bindingResult.getPropertyEditorRegistry().registerCustomEditor(ItemPet.class, editor);
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
// wrap the output so it is valid XML
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element checkboxElement = (Element) document.getRootElement().elements().get(0);
assertEquals("input", checkboxElement.getName());
assertEquals("radio", checkboxElement.attribute("type").getValue());
assertEquals("pets", checkboxElement.attribute("name").getValue());
assertEquals("Rudiger", checkboxElement.attribute("value").getValue());
assertEquals("checked", checkboxElement.attribute("checked").getValue());
}
Aggregations