Search in sources :

Example 6 with PageContext

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

the class BindTagTests method bindErrorsTagWithErrors.

@Test
void bindErrorsTagWithErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("test", null, "test");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindErrorsTag tag = new BindErrorsTag();
    tag.setPageContext(pc);
    tag.setName("tb");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    assertThat(pc.getAttribute(BindErrorsTag.ERRORS_VARIABLE_NAME, PageContext.REQUEST_SCOPE) == errors).as("Has errors variable").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) Test(org.junit.jupiter.api.Test)

Example 7 with PageContext

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

the class BindTagTests method nestedPathDoEndTagWithNesting.

@Test
void nestedPathDoEndTagWithNesting() throws JspException {
    PageContext pc = createPageContext();
    NestedPathTag tag = new NestedPathTag();
    tag.setPath("foo");
    tag.setPageContext(pc);
    tag.doStartTag();
    NestedPathTag anotherTag = new NestedPathTag();
    anotherTag.setPageContext(pc);
    anotherTag.setPath("bar");
    anotherTag.doStartTag();
    anotherTag.doEndTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isEqualTo("foo.");
    tag.doEndTag();
    assertThat(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)).isNull();
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 8 with PageContext

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

the class BindTagTests method bindTagWithGlobalErrors.

@Test
void bindTagWithGlobalErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", "message1");
    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(status.getErrorMessages().length == 1).as("Correct errorMessages").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("message1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message1".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.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
    assertThat(status.getErrorMessages().length == 1).as("Correct errorMessages").isTrue();
    assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
    assertThat("message1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
    assertThat("message1".equals(status.getErrorMessagesAsString(","))).as("Correct errorMessagesAsString").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 9 with PageContext

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

the class HtmlEscapeTagTests method escapeBody.

@Test
void escapeBody() throws JspException {
    PageContext pc = createPageContext();
    final StringBuilder result = new StringBuilder();
    EscapeBodyTag tag = new EscapeBodyTag() {

        @Override
        protected String readBodyContent() {
            return "test text";
        }

        @Override
        protected void writeBodyContent(String content) {
            result.append(content);
        }
    };
    tag.setPageContext(pc);
    assertThat(tag.doStartTag()).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
    assertThat(tag.doAfterBody()).isEqualTo(Tag.SKIP_BODY);
    assertThat(result.toString()).isEqualTo("test text");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 10 with PageContext

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

the class HtmlEscapeTagTests method htmlEscapeTagWithContextParamTrue.

@Test
void htmlEscapeTagWithContextParamTrue() throws JspException {
    PageContext pc = createPageContext();
    MockServletContext sc = (MockServletContext) pc.getServletContext();
    sc.addInitParameter(WebUtils.HTML_ESCAPE_CONTEXT_PARAM, "true");
    HtmlEscapeTag tag = new HtmlEscapeTag();
    tag.setDefaultHtmlEscape(false);
    tag.setPageContext(pc);
    tag.doStartTag();
    boolean condition1 = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition1).as("Correct default").isTrue();
    tag.setDefaultHtmlEscape(true);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    assertThat(tag.getRequestContext().isDefaultHtmlEscape()).as("Correctly enabled").isTrue();
    tag.setDefaultHtmlEscape(false);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    boolean condition = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition).as("Correctly disabled").isTrue();
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) 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