use of java.beans.PropertyEditorSupport 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 java.beans.PropertyEditorSupport 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 java.beans.PropertyEditorSupport 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());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class CheckboxesTagTests method collectionOfPetsWithEditor.
@Test
public void collectionOfPetsWithEditor() throws Exception {
this.tag.setPath("pets");
List allPets = new ArrayList();
allPets.add(new ItemPet("Rudiger"));
allPets.add(new ItemPet("Spot"));
allPets.add(new ItemPet("Checkers"));
allPets.add(new ItemPet("Fluffy"));
allPets.add(new ItemPet("Mufty"));
this.tag.setItems(allPets);
this.tag.setItemLabel("label");
this.tag.setId("myId");
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 spanElement1 = (Element) document.getRootElement().elements().get(0);
Element checkboxElement1 = (Element) spanElement1.elements().get(0);
assertEquals("input", checkboxElement1.getName());
assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
assertEquals("pets", checkboxElement1.attribute("name").getValue());
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals("Rudiger", checkboxElement1.attribute("value").getValue());
assertEquals("RUDIGER", spanElement1.getStringValue());
Element spanElement2 = (Element) document.getRootElement().elements().get(1);
Element checkboxElement2 = (Element) spanElement2.elements().get(0);
assertEquals("input", checkboxElement2.getName());
assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
assertEquals("pets", checkboxElement2.attribute("name").getValue());
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals("Spot", checkboxElement2.attribute("value").getValue());
assertEquals("SPOT", spanElement2.getStringValue());
Element spanElement3 = (Element) document.getRootElement().elements().get(2);
Element checkboxElement3 = (Element) spanElement3.elements().get(0);
assertEquals("input", checkboxElement3.getName());
assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
assertEquals("pets", checkboxElement3.attribute("name").getValue());
assertNull("not checked", checkboxElement3.attribute("checked"));
assertEquals("Checkers", checkboxElement3.attribute("value").getValue());
assertEquals("CHECKERS", spanElement3.getStringValue());
Element spanElement4 = (Element) document.getRootElement().elements().get(3);
Element checkboxElement4 = (Element) spanElement4.elements().get(0);
assertEquals("input", checkboxElement4.getName());
assertEquals("checkbox", checkboxElement4.attribute("type").getValue());
assertEquals("pets", checkboxElement4.attribute("name").getValue());
assertEquals("checked", checkboxElement4.attribute("checked").getValue());
assertEquals("Fluffy", checkboxElement4.attribute("value").getValue());
assertEquals("FLUFFY", spanElement4.getStringValue());
Element spanElement5 = (Element) document.getRootElement().elements().get(4);
Element checkboxElement5 = (Element) spanElement5.elements().get(0);
assertEquals("input", checkboxElement5.getName());
assertEquals("checkbox", checkboxElement5.attribute("type").getValue());
assertEquals("pets", checkboxElement5.attribute("name").getValue());
assertEquals("checked", checkboxElement5.attribute("checked").getValue());
assertEquals("Mufty", checkboxElement5.attribute("value").getValue());
assertEquals("MUFTY", spanElement5.getStringValue());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class SelectTagTests method withListAndEditorAndNullValue.
@Test
public void withListAndEditorAndNullValue() throws Exception {
this.tag.setPath("realCountry");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setItemLabel("name");
TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean();
testBean.setRealCountry(null);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return "";
}
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\""));
}
Aggregations