use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class WebRequestDataBinderTests method testBindingWithNestedObjectCreation.
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
TestBean tb = new TestBean();
WebRequestDataBinder binder = new WebRequestDataBinder(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(new ServletWebRequest(request));
assertNotNull(tb.getSpouse());
assertEquals("test", tb.getSpouse().getName());
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreationAndWrongOrder.
@Test
public void testBindingWithNestedObjectCreationAndWrongOrder() 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.name", "test");
request.addParameter("spouse", "someValue");
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 BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.
@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
PageContext pc = createPageContext();
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new ServletRequestDataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {
@Override
public String getAsText() {
return "something";
}
});
Errors errors = binder.getBindingResult();
errors.rejectValue("array[0]", "code1", "message1");
errors.rejectValue("array[0]", "code2", "message2");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.array[0]");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
// because of the custom editor getValue() should return a String
assertTrue("Value is TestBean", status.getValue() instanceof String);
assertTrue("Correct value", "something".equals(status.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);
}
}
use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.
the class SelectTagTests method withListAndTransformTagAndEditor.
@Test
public void withListAndTransformTagAndEditor() throws Exception {
this.tag.setPath("realCountry");
this.tag.setItems(Country.getCountries());
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();
TransformTag transformTag = new TransformTag();
transformTag.setValue(Country.getCountries().get(0));
transformTag.setVar("key");
transformTag.setParent(this.tag);
transformTag.setPageContext(getPageContext());
transformTag.doStartTag();
assertEquals("Austria", getPageContext().findAttribute("key"));
}
Aggregations