use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreation.
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
TestBean tb = new TestBean();
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new TestBean());
}
});
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("spouse", "someValue");
request.addParameter("spouse.name", "test");
binder.bind(request);
assertNotNull(tb.getSpouse());
assertEquals("test", tb.getSpouse().getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class RadioButtonsTagTests 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 radioButtonElement1 = (Element) spanElement1.elements().get(0);
assertEquals("input", radioButtonElement1.getName());
assertEquals("radio", radioButtonElement1.attribute("type").getValue());
assertEquals("pets", radioButtonElement1.attribute("name").getValue());
assertEquals("checked", radioButtonElement1.attribute("checked").getValue());
assertEquals("Rudiger", radioButtonElement1.attribute("value").getValue());
assertEquals("RUDIGER", spanElement1.getStringValue());
Element spanElement2 = (Element) document.getRootElement().elements().get(1);
Element radioButtonElement2 = (Element) spanElement2.elements().get(0);
assertEquals("input", radioButtonElement2.getName());
assertEquals("radio", radioButtonElement2.attribute("type").getValue());
assertEquals("pets", radioButtonElement2.attribute("name").getValue());
assertEquals("checked", radioButtonElement2.attribute("checked").getValue());
assertEquals("Spot", radioButtonElement2.attribute("value").getValue());
assertEquals("SPOT", spanElement2.getStringValue());
Element spanElement3 = (Element) document.getRootElement().elements().get(2);
Element radioButtonElement3 = (Element) spanElement3.elements().get(0);
assertEquals("input", radioButtonElement3.getName());
assertEquals("radio", radioButtonElement3.attribute("type").getValue());
assertEquals("pets", radioButtonElement3.attribute("name").getValue());
assertNull("not checked", radioButtonElement3.attribute("checked"));
assertEquals("Checkers", radioButtonElement3.attribute("value").getValue());
assertEquals("CHECKERS", spanElement3.getStringValue());
Element spanElement4 = (Element) document.getRootElement().elements().get(3);
Element radioButtonElement4 = (Element) spanElement4.elements().get(0);
assertEquals("input", radioButtonElement4.getName());
assertEquals("radio", radioButtonElement4.attribute("type").getValue());
assertEquals("pets", radioButtonElement4.attribute("name").getValue());
assertEquals("checked", radioButtonElement4.attribute("checked").getValue());
assertEquals("Fluffy", radioButtonElement4.attribute("value").getValue());
assertEquals("FLUFFY", spanElement4.getStringValue());
Element spanElement5 = (Element) document.getRootElement().elements().get(4);
Element radioButtonElement5 = (Element) spanElement5.elements().get(0);
assertEquals("input", radioButtonElement5.getName());
assertEquals("radio", radioButtonElement5.attribute("type").getValue());
assertEquals("pets", radioButtonElement5.attribute("name").getValue());
assertEquals("checked", radioButtonElement5.attribute("checked").getValue());
assertEquals("Mufty", radioButtonElement5.attribute("value").getValue());
assertEquals("MUFTY", spanElement5.getStringValue());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class SelectTagTests method nestedPathWithListAndEditor.
@Test
public void nestedPathWithListAndEditor() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setItemLabel("name");
TestBeanWrapper testBean = new TestBeanWrapper();
testBean.setBean(getTestBean());
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() {
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 CheckboxTagTests 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("checkbox", 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 SelectTagTests method withMultiMapWithItemValueAndItemLabel.
/**
* Tests new support added as a result of <a
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-2660"
* target="_blank">SPR-2660</a>.
* <p>
* Specifically, if the {@code items} attribute is supplied a
* {@link Map}, and {@code itemValue} and {@code itemLabel}
* are supplied non-null values, then:
* </p>
* <ul>
* <li>{@code itemValue} will be used as the property name of the
* map's <em>key</em>, and</li>
* <li>{@code itemLabel} will be used as the property name of the
* map's <em>value</em>.</li>
* </ul>
*/
@Test
public void withMultiMapWithItemValueAndItemLabel() throws Exception {
// Save original default locale.
final Locale defaultLocale = Locale.getDefault();
// Use a locale that doesn't result in the generation of HTML entities
// (e.g., not German, where รค becomes ä)
Locale.setDefault(Locale.US);
try {
final Country austria = Country.COUNTRY_AT;
final Country usa = Country.COUNTRY_US;
final Map someMap = new HashMap();
someMap.put(austria, LOCALE_AT);
someMap.put(usa, Locale.US);
this.bean.setSomeMap(someMap);
// see: TestBean
this.tag.setPath("someMap");
this.tag.setItems(getCountryToLocaleMap());
// Map key: Country
this.tag.setItemValue("isoCode");
// Map value: Locale
this.tag.setItemLabel("displayLanguage");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
@Override
public void setAsText(final String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getIsoCode();
}
});
exposeBindingResult(bindingResult);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertEquals(2, rootElement.elements().size());
Element selectElement = rootElement.element("select");
assertEquals("select", selectElement.getName());
assertEquals("someMap", selectElement.attribute("name").getValue());
List children = selectElement.elements();
assertEquals("Incorrect number of children", 3, children.size());
Element e;
e = (Element) selectElement.selectSingleNode("option[@value = '" + austria.getIsoCode() + "']");
assertNotNull("Option node not found with Country ISO code value [" + austria.getIsoCode() + "].", e);
assertEquals("AT node not selected.", "selected", e.attribute("selected").getValue());
assertEquals("AT Locale displayLanguage property not used for option label.", LOCALE_AT.getDisplayLanguage(), e.getData());
e = (Element) selectElement.selectSingleNode("option[@value = '" + usa.getIsoCode() + "']");
assertNotNull("Option node not found with Country ISO code value [" + usa.getIsoCode() + "].", e);
assertEquals("US node not selected.", "selected", e.attribute("selected").getValue());
assertEquals("US Locale displayLanguage property not used for option label.", Locale.US.getDisplayLanguage(), e.getData());
} finally {
// Restore original default locale.
Locale.setDefault(defaultLocale);
}
}
Aggregations