Search in sources :

Example 46 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldErrorsAndNoDefaultMessage.

@Test
void bindTagWithFieldErrorsAndNoDefaultMessage() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", "code1");
    errors.rejectValue("name", "code2");
    errors.rejectValue("age", "code2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    tag.setHtmlEscape(true);
    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("name".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat("name1".equals(status.getValue())).as("Correct value").isTrue();
    assertThat("name1".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 2).as("Correct errorCodes").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.age");
    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("age".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat(Integer.valueOf(0).equals(status.getValue())).as("Correct value").isTrue();
    assertThat("0".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
    assertThat("code2".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 == 3).as("Correct errorCodes").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
    assertThat("code2".equals(status.getErrorCodes()[2])).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)

Example 47 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithoutBean.

@Test
void bindTagWithoutBean() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertThatExceptionOfType(JspException.class).isThrownBy(tag::doStartTag);
}
Also used : JspException(jakarta.servlet.jsp.JspException) PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 48 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldErrorsAndDefaultMessageOnly.

@Test
void bindTagWithFieldErrorsAndDefaultMessageOnly() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", null, "message & 1");
    errors.rejectValue("name", null, "message2");
    errors.rejectValue("age", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    tag.setHtmlEscape(true);
    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("name".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat("name1".equals(status.getValue())).as("Correct value").isTrue();
    assertThat("name1".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorMessages().length == 2).as("Correct errorMessages").isTrue();
    assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
    assertThat("message & 1 - message2".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").isTrue();
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.age");
    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("age".equals(status.getExpression())).as("Correct expression").isTrue();
    assertThat(Integer.valueOf(0).equals(status.getValue())).as("Correct value").isTrue();
    assertThat("0".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
    assertThat(status.isError()).as("Correct isError").isTrue();
    assertThat(status.getErrorMessages().length == 1).as("Correct errorMessages").isTrue();
    assertThat("message2".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").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.getErrorMessages().length == 3).as("Correct errorMessages").isTrue();
    assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
    assertThat("message2".equals(status.getErrorMessages()[2])).as("Correct errorMessage").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)

Example 49 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method bindErrorsTagWithoutBean.

@Test
void bindErrorsTagWithoutBean() throws JspException {
    PageContext pc = createPageContext();
    BindErrorsTag tag = new BindErrorsTag();
    tag.setPageContext(pc);
    tag.setName("tb");
    assertThat(tag.doStartTag() == Tag.SKIP_BODY).as("Correct doStartTag return value").isTrue();
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 50 with PageContext

use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.

the class BindTagTests method transformTagWithSettingOfScope.

@Test
void transformTagWithSettingOfScope() throws JspException {
    // first set up the pagecontext and the bean
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
    CustomDateEditor l = new CustomDateEditor(df, true);
    binder.registerCustomEditor(Date.class, l);
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());
    // execute the bind tag using the date property
    BindTag bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.date");
    bind.doStartTag();
    // transform stuff
    TransformTag transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setParent(bind);
    transform.setValue(tb.getDate());
    transform.setVar("theDate");
    transform.setScope("page");
    transform.doStartTag();
    transform.release();
    assertThat(pc.getAttribute("theDate")).isNotNull();
    assertThat(pc.getAttribute("theDate")).isEqualTo(df.format(tb.getDate()));
    // try another time, this time using Strings
    bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.name");
    bind.doStartTag();
    transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setValue("name");
    transform.setParent(bind);
    transform.setVar("theString");
    transform.setScope("page");
    transform.doStartTag();
    transform.release();
    assertThat(pc.getAttribute("theString")).isNotNull();
    assertThat(pc.getAttribute("theString")).isEqualTo("name");
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.jupiter.api.Test)

Aggregations

PageContext (jakarta.servlet.jsp.PageContext)70 Test (org.junit.jupiter.api.Test)62 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)28 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)27 TestBean (org.springframework.beans.testfixture.beans.TestBean)27 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)22 BindStatus (org.springframework.web.servlet.support.BindStatus)19 Errors (org.springframework.validation.Errors)17 DateFormat (java.text.DateFormat)6 SimpleDateFormat (java.text.SimpleDateFormat)6 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)6 JspContext (jakarta.servlet.jsp.JspContext)4 DefaultMessageSourceResolvable (org.springframework.context.support.DefaultMessageSourceResolvable)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)2 RequestContext (org.springframework.web.servlet.support.RequestContext)2 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 JspException (jakarta.servlet.jsp.JspException)1 FeatureDescriptor (java.beans.FeatureDescriptor)1