Search in sources :

Example 31 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithGlobalErrorsAndNoDefaultMessage.

@Test
void bindTagWithGlobalErrorsAndNoDefaultMessage() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat(status.getExpression() == null).as("Correct expression").isTrue();
    assertThat(status.getValue() == null).as("Correct value").isTrue();
    assertThat("".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.*");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("*".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat(status.getValue() == null).as("Correct value").isTrue();
    assertThat("".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
}
Also used : Errors(org.springframework.validation.Errors) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)31 Test (org.junit.jupiter.api.Test)25 TestBean (org.springframework.beans.testfixture.beans.TestBean)24 PageContext (jakarta.servlet.jsp.PageContext)22 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)22 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)21 Errors (org.springframework.validation.Errors)16 BindStatus (org.springframework.web.servlet.support.BindStatus)14 SimpleDateFormat (java.text.SimpleDateFormat)7 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)7 DateFormat (java.text.DateFormat)6 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 HashMap (java.util.HashMap)2 ServletRequest (jakarta.servlet.ServletRequest)1 PropertyEditorSupport (java.beans.PropertyEditorSupport)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 lombok.val (lombok.val)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1