Search in sources :

Example 1 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method withPropertyEditorStringComparison.

@Test
public void withPropertyEditorStringComparison() throws Exception {
    final PropertyEditor testBeanEditor = new TestBeanPropertyEditor();
    testBeanEditor.setValue(new TestBean("Sally"));
    String selectName = "testBean.spouse";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {

        @Override
        public PropertyEditor getEditor() {
            return testBeanEditor;
        }
    };
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    this.tag.setValue("Sally");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", "Sally");
    assertContainsAttribute(output, "selected", "selected");
    assertBlockTagContains(output, "Sally");
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) PropertyEditor(java.beans.PropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 2 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method asBodyTagWithEditor.

@Test
public void asBodyTagWithEditor() throws Exception {
    String selectName = "testBean.stringArray";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {

        @Override
        public PropertyEditor getEditor() {
            return new RulesVariantEditor();
        }
    };
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    RulesVariant rulesVariant = new RulesVariant("someRules", "someVariant");
    this.tag.setValue(rulesVariant);
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    assertEquals(rulesVariant, getPageContext().getAttribute("value"));
    assertEquals(rulesVariant.toId(), getPageContext().getAttribute("displayValue"));
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
}
Also used : BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 3 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method withNoLabel.

@Test
public void withNoLabel() throws Exception {
    String selectName = "testBean.name";
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
    this.tag.setValue("bar");
    this.tag.setCssClass("myClass");
    this.tag.setOnclick("CLICK");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", "bar");
    assertContainsAttribute(output, "class", "myClass");
    assertContainsAttribute(output, "onclick", "CLICK");
    assertBlockTagContains(output, "bar");
}
Also used : BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 4 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method withCustomObjectAndEditorNotSelected.

@Test
public void withCustomObjectAndEditorNotSelected() throws Exception {
    final PropertyEditor floatEditor = new SimpleFloatEditor();
    String selectName = "testBean.someNumber";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {

        @Override
        public PropertyEditor getEditor() {
            return floatEditor;
        }
    };
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    this.tag.setValue(new Float(12.35));
    this.tag.setLabel("12.35f");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertAttributeNotPresent(output, "selected");
    assertBlockTagContains(output, "12.35f");
}
Also used : PropertyEditor(java.beans.PropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 5 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method withCustomObjectNotSelected.

@Test
public void withCustomObjectNotSelected() throws Exception {
    String selectName = "testBean.someNumber";
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
    this.tag.setValue(new Float(12.35));
    this.tag.setLabel("GBP 12.35");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", "12.35");
    assertAttributeNotPresent(output, "selected");
    assertBlockTagContains(output, "GBP 12.35");
}
Also used : BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Aggregations

BindStatus (org.springframework.web.servlet.support.BindStatus)48 Test (org.junit.jupiter.api.Test)28 TestBean (org.springframework.beans.testfixture.beans.TestBean)20 PageContext (jakarta.servlet.jsp.PageContext)19 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)19 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)18 Test (org.junit.Test)16 Errors (org.springframework.validation.Errors)14 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)14 ArrayList (java.util.ArrayList)5 List (java.util.List)5 PropertyEditor (java.beans.PropertyEditor)4 StringReader (java.io.StringReader)4 Document (org.dom4j.Document)4 Element (org.dom4j.Element)4 SAXReader (org.dom4j.io.SAXReader)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2