Search in sources :

Example 41 with BindStatus

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

the class OptionTagTests method withCustomObjectAndEditorSelected.

@Test
public void withCustomObjectAndEditorSelected() throws Exception {
    final PropertyEditor floatEditor = new SimpleFloatEditor();
    floatEditor.setValue(new Float("12.34"));
    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.34));
    this.tag.setLabel("12.34f");
    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, "selected", "selected");
    assertBlockTagContains(output, "12.34f");
}
Also used : PropertyEditor(java.beans.PropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 42 with BindStatus

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

the class OptionTagTests method asBodyTagCollapsed.

@Test
public void asBodyTagCollapsed() throws Exception {
    String selectName = "testBean.name";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    String bodyContent = "some content";
    this.tag.setValue(bodyContent);
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", bodyContent);
    assertBlockTagContains(output, bodyContent);
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 43 with BindStatus

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

the class OptionTagTests method renderNotSelected.

@Test
public void renderNotSelected() throws Exception {
    String selectName = "testBean.name";
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
    this.tag.setValue("bar");
    this.tag.setLabel("Bar");
    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");
    assertBlockTagContains(output, "Bar");
}
Also used : BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 44 with BindStatus

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

the class OptionsTagTests method withItemsNullReference.

@Test
public void withItemsNullReference() throws Exception {
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false));
    this.tag.setItems(Collections.emptyList());
    this.tag.setItemValue("isoCode");
    this.tag.setItemLabel("name");
    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();
    List children = rootElement.elements();
    assertEquals("Incorrect number of children", 0, children.size());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) BindStatus(org.springframework.web.servlet.support.BindStatus) Document(org.dom4j.Document) Test(org.junit.Test)

Example 45 with BindStatus

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

the class OptionsTagTests method withCollection.

@Test
public void withCollection() throws Exception {
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false));
    this.tag.setItems(Country.getCountries());
    this.tag.setItemValue("isoCode");
    this.tag.setItemLabel("name");
    this.tag.setId("myOption");
    this.tag.setCssClass("myClass");
    this.tag.setOnclick("CLICK");
    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();
    List children = rootElement.elements();
    assertEquals("Incorrect number of children", 4, children.size());
    Element element = (Element) rootElement.selectSingleNode("option[@value = 'UK']");
    assertEquals("UK node not selected", "selected", element.attribute("selected").getValue());
    assertEquals("myOption3", element.attribute("id").getValue());
    assertEquals("myClass", element.attribute("class").getValue());
    assertEquals("CLICK", element.attribute("onclick").getValue());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) BindStatus(org.springframework.web.servlet.support.BindStatus) Document(org.dom4j.Document) Test(org.junit.Test)

Aggregations

BindStatus (org.springframework.web.servlet.support.BindStatus)47 Test (org.junit.Test)44 TestBean (org.springframework.tests.sample.beans.TestBean)22 PageContext (javax.servlet.jsp.PageContext)19 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)19 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)18 Errors (org.springframework.validation.Errors)14 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)14 PropertyEditor (java.beans.PropertyEditor)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 List (java.util.List)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 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)2 BindTag (org.springframework.web.servlet.tags.BindTag)2 SimpleHash (freemarker.template.SimpleHash)1