Search in sources :

Example 11 with BindStatus

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

the class BindTagTests method bindTagWithoutErrors.

@Test
public void bindTagWithoutErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", status.getExpression() == null);
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", !status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 0);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 0);
    assertTrue("Correct errorCode", "".equals(status.getErrorCode()));
    assertTrue("Correct errorMessage", "".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(",")));
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 12 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 13 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 14 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 15 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)

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