use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithFormatter.
@Test
void testBindingErrorWithFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getMyFloat()).isEqualTo(Float.valueOf(0.0f));
assertThat(binder.getBindingResult().getFieldValue("myFloat")).isEqualTo("1x2");
assertThat(binder.getBindingResult().hasFieldErrors("myFloat")).isTrue();
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithFormatterAgainstList.
@Test
void testBindingWithFormatterAgainstList() {
BeanWithIntegerList tb = new BeanWithIntegerList();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("integerList[0]", "1");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getIntegerList().get(0)).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1");
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class DataBinderTests method testBindingErrorWithFormatterAgainstList.
@Test
void testBindingErrorWithFormatterAgainstList() {
BeanWithIntegerList tb = new BeanWithIntegerList();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("integerList[0]", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getIntegerList().isEmpty()).isTrue();
assertThat(binder.getBindingResult().getFieldValue("integerList[0]")).isEqualTo("1x2");
assertThat(binder.getBindingResult().hasFieldErrors("integerList[0]")).isTrue();
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of org.springframework.format.support.FormattingConversionService 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();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.elements().size()).isEqualTo(1);
Element selectElement = rootElement.element("select");
assertThat(selectElement.getName()).isEqualTo("select");
assertThat(selectElement.attribute("name").getValue()).isEqualTo("realCountry");
List children = selectElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
assertThat(e.attribute("selected").getValue()).as("UK node not selected").isEqualTo("selected");
assertThat(e.getText()).isEqualTo("United Kingdom");
}
use of org.springframework.format.support.FormattingConversionService 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();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.elements().size()).isEqualTo(2);
Element selectElement = rootElement.element("select");
assertThat(selectElement.getName()).isEqualTo("select");
assertThat(selectElement.attribute("name").getValue()).isEqualTo("someList");
List children = selectElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
assertThat(e.attribute("selected").getValue()).as("UK node not selected").isEqualTo("selected");
assertThat(e.getText()).isEqualTo("United Kingdom");
e = (Element) selectElement.selectSingleNode("option[@value = 'AT']");
assertThat(e.attribute("selected").getValue()).as("AT node not selected").isEqualTo("selected");
assertThat(e.getText()).isEqualTo("Austria");
}
Aggregations