use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method webBindingInitializer.
@Test
public void webBindingInitializer() throws Exception {
RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter();
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
assertNotNull(initializer);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, "");
initializer.getValidator().validate(null, bindingResult);
assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode());
String[] codes = initializer.getMessageCodesResolver().resolveMessageCodes("invalid", null);
assertEquals("custom.invalid", codes[0]);
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SelectTagTests method withElementFormatter.
@Test
public void withElementFormatter() throws Exception {
this.bean.setRealCountry(Country.COUNTRY_UK);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
FormattingConversionService cs = new FormattingConversionService();
cs.addFormatterForFieldType(Country.class, new Formatter<Country>() {
@Override
public String print(Country object, Locale locale) {
return object.getName();
}
@Override
public Country parse(String text, Locale locale) throws ParseException {
return new Country(text, text);
}
});
errors.initConversion(cs);
exposeBindingResult(errors);
this.tag.setPath("realCountry");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
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(1, rootElement.elements().size());
Element selectElement = rootElement.element("select");
assertEquals("select", selectElement.getName());
assertEquals("realCountry", selectElement.attribute("name").getValue());
List children = selectElement.elements();
assertEquals("Incorrect number of children", 4, children.size());
Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
assertEquals("UK node not selected", "selected", e.attribute("selected").getValue());
assertEquals("United Kingdom", e.getText());
}
use of org.springframework.validation.BeanPropertyBindingResult 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 org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SelectTagTests method withMultiListAndElementFormatter.
@Test
public void withMultiListAndElementFormatter() throws Exception {
List list = new ArrayList();
list.add(Country.COUNTRY_UK);
list.add(Country.COUNTRY_AT);
this.bean.setSomeList(list);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
FormattingConversionService cs = new FormattingConversionService();
cs.addFormatterForFieldType(Country.class, new Formatter<Country>() {
@Override
public String print(Country object, Locale locale) {
return object.getName();
}
@Override
public Country parse(String text, Locale locale) throws ParseException {
return new Country(text, text);
}
});
errors.initConversion(cs);
exposeBindingResult(errors);
this.tag.setPath("someList");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
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("someList", selectElement.attribute("name").getValue());
List children = selectElement.elements();
assertEquals("Incorrect number of children", 4, children.size());
Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
assertEquals("UK node not selected", "selected", e.attribute("selected").getValue());
assertEquals("United Kingdom", e.getText());
e = (Element) selectElement.selectSingleNode("option[@value = 'AT']");
assertEquals("AT node not selected", "selected", e.attribute("selected").getValue());
assertEquals("Austria", e.getText());
}
use of org.springframework.validation.BeanPropertyBindingResult 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