Search in sources :

Example 51 with PageContext

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

the class BindTagTests method bindStatusGetErrorMessagesAsString.

@Test
void bindStatusGetErrorMessagesAsString() throws JspException {
    // one error (should not include delimiter)
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", null, "message1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getErrorMessagesAsString(",")).as("Error messages String should be 'message1'").isEqualTo("message1");
    // two errors
    pc = createPageContext();
    errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", null, "message1");
    errors.reject("code1", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getErrorMessagesAsString(",")).as("Error messages String should be 'message1,message2'").isEqualTo("message1,message2");
    // no errors
    pc = createPageContext();
    errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getErrorMessagesAsString(",")).as("Error messages String should be ''").isEqualTo("");
}
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 52 with PageContext

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

the class BindTagTests method bindTagWithToStringAndHtmlEscaping.

@Test
void bindTagWithToStringAndHtmlEscaping() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.doctor");
    tag.setHtmlEscape(true);
    TestBean tb = new TestBean("somebody", 99);
    NestedTestBean ntb = new NestedTestBean("juergen&eva");
    tb.setDoctor(ntb);
    pc.getRequest().setAttribute("tb", tb);
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getExpression()).isEqualTo("doctor");
    boolean condition = status.getValue() instanceof NestedTestBean;
    assertThat(condition).isTrue();
    assertThat(status.getDisplayValue().contains("juergen&eva")).isTrue();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 53 with PageContext

use of jakarta.servlet.jsp.PageContext 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)

Example 54 with PageContext

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

the class MessageTagTests method messageTagWithTextEncodingEscaped.

@Test
void messageTagWithTextEncodingEscaped() throws JspException {
    PageContext pc = createPageContext();
    pc.getServletContext().setInitParameter(WebUtils.RESPONSE_ENCODED_HTML_ESCAPE_CONTEXT_PARAM, "true");
    pc.getResponse().setCharacterEncoding("UTF-8");
    final StringBuilder message = new StringBuilder();
    MessageTag tag = new MessageTag() {

        @Override
        protected void writeMessage(String msg) {
            message.append(msg);
        }
    };
    tag.setPageContext(pc);
    tag.setText("test <&> é");
    tag.setHtmlEscape(true);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    assertThat(tag.doEndTag()).as("Correct doEndTag return value").isEqualTo(Tag.EVAL_PAGE);
    assertThat(message.toString()).as("Correct message").isEqualTo("test &lt;&amp;&gt; é");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 55 with PageContext

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

the class MessageTagTests method messageTagWithCodeAndNestedArgument.

@Test
void messageTagWithCodeAndNestedArgument() throws JspException {
    PageContext pc = createPageContext();
    final StringBuilder message = new StringBuilder();
    MessageTag tag = new MessageTag() {

        @Override
        protected void writeMessage(String msg) {
            message.append(msg);
        }
    };
    tag.setPageContext(pc);
    tag.setCode("testArgs");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    tag.addArgument(7);
    assertThat(tag.doEndTag()).as("Correct doEndTag return value").isEqualTo(Tag.EVAL_PAGE);
    assertThat(message.toString()).as("Correct message").isEqualTo("test 7 message {1}");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) 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