Search in sources :

Example 26 with FormattingConversionService

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();
    }
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 27 with FormattingConversionService

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();
    }
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 28 with FormattingConversionService

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();
    }
}
Also used : NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 29 with FormattingConversionService

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");
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document) FormattingConversionService(org.springframework.format.support.FormattingConversionService) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) Test(org.junit.jupiter.api.Test)

Example 30 with FormattingConversionService

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");
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) FormattingConversionService(org.springframework.format.support.FormattingConversionService) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) Test(org.junit.jupiter.api.Test)

Aggregations

FormattingConversionService (org.springframework.format.support.FormattingConversionService)38 Test (org.junit.jupiter.api.Test)27 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)13 Date (java.util.Date)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)8 Locale (java.util.Locale)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 NumberStyleFormatter (org.springframework.format.number.NumberStyleFormatter)6 ParseException (java.text.ParseException)5 LocalDateTime (java.time.LocalDateTime)4 LocalTime (java.time.LocalTime)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 StringReader (java.io.StringReader)3 Document (org.dom4j.Document)3 Element (org.dom4j.Element)3