Search in sources :

Example 46 with BeanPropertyBindingResult

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]);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 47 with BeanPropertyBindingResult

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());
}
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) LinkedList(java.util.LinkedList) List(java.util.List) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 48 with BeanPropertyBindingResult

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 &auml;)
    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);
    }
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 49 with BeanPropertyBindingResult

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());
}
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) LinkedList(java.util.LinkedList) List(java.util.List) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 50 with BeanPropertyBindingResult

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"));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TransformTag(org.springframework.web.servlet.tags.TransformTag) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)73 Test (org.junit.Test)60 TestBean (org.springframework.tests.sample.beans.TestBean)19 Errors (org.springframework.validation.Errors)18 StringReader (java.io.StringReader)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 BindingResult (org.springframework.validation.BindingResult)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 LinkedList (java.util.LinkedList)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 Model (org.springframework.ui.Model)5 FieldError (org.springframework.validation.FieldError)5 ObjectError (org.springframework.validation.ObjectError)5