Search in sources :

Example 56 with PageContext

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

the class MessageTagTests method messageTagWithCodeAndStringArgumentWithCustomSeparator.

@Test
void messageTagWithCodeAndStringArgumentWithCustomSeparator() 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");
    tag.setArguments("arg1,1;arg2,2");
    tag.setArgumentSeparator(";");
    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 arg1,1 message arg2,2");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 57 with PageContext

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

the class MessageTagTests method messageTagWithCodeAndArrayArgument.

@Test
void messageTagWithCodeAndArrayArgument() 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");
    tag.setArguments(new Object[] { "arg1", 5 });
    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 arg1 message 5");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 58 with PageContext

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

the class MessageTagTests method messageWithVarAndScope.

@Test
void messageWithVarAndScope() throws JspException {
    PageContext pc = createPageContext();
    MessageTag tag = new MessageTag();
    tag.setPageContext(pc);
    tag.setText("text & text");
    tag.setVar("testvar");
    tag.setScope("page");
    tag.doStartTag();
    assertThat(tag.doEndTag()).as("Correct doEndTag return value").isEqualTo(Tag.EVAL_PAGE);
    assertThat(pc.getAttribute("testvar")).isEqualTo("text & text");
    tag.release();
    tag = new MessageTag();
    tag.setPageContext(pc);
    tag.setCode("test");
    tag.setVar("testvar2");
    tag.doStartTag();
    assertThat(tag.doEndTag()).as("Correct doEndTag return value").isEqualTo(Tag.EVAL_PAGE);
    assertThat(pc.getAttribute("testvar2")).as("Correct message").isEqualTo("test message");
    tag.release();
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 59 with PageContext

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

the class HtmlEscapeTagTests method htmlEscapeTag.

@Test
void htmlEscapeTag() throws JspException {
    PageContext pc = createPageContext();
    HtmlEscapeTag tag = new HtmlEscapeTag();
    tag.setPageContext(pc);
    tag.doStartTag();
    HtmlEscapingAwareTag testTag = new HtmlEscapingAwareTag() {

        @Override
        public int doStartTagInternal() throws Exception {
            return EVAL_BODY_INCLUDE;
        }
    };
    testTag.setPageContext(pc);
    testTag.doStartTag();
    boolean condition7 = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition7).as("Correct default").isTrue();
    boolean condition6 = !testTag.isHtmlEscape();
    assertThat(condition6).as("Correctly applied").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();
    assertThat(testTag.isHtmlEscape()).as("Correctly applied").isTrue();
    tag.setDefaultHtmlEscape(false);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    boolean condition5 = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition5).as("Correctly disabled").isTrue();
    boolean condition4 = !testTag.isHtmlEscape();
    assertThat(condition4).as("Correctly applied").isTrue();
    tag.setDefaultHtmlEscape(true);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    testTag.setHtmlEscape(true);
    assertThat(tag.getRequestContext().isDefaultHtmlEscape()).as("Correctly enabled").isTrue();
    assertThat(testTag.isHtmlEscape()).as("Correctly applied").isTrue();
    testTag.setHtmlEscape(false);
    assertThat(tag.getRequestContext().isDefaultHtmlEscape()).as("Correctly enabled").isTrue();
    boolean condition3 = !testTag.isHtmlEscape();
    assertThat(condition3).as("Correctly applied").isTrue();
    tag.setDefaultHtmlEscape(false);
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    testTag.setHtmlEscape(true);
    boolean condition2 = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition2).as("Correctly disabled").isTrue();
    assertThat(testTag.isHtmlEscape()).as("Correctly applied").isTrue();
    testTag.setHtmlEscape(false);
    boolean condition1 = !tag.getRequestContext().isDefaultHtmlEscape();
    assertThat(condition1).as("Correctly disabled").isTrue();
    boolean condition = !testTag.isHtmlEscape();
    assertThat(condition).as("Correctly applied").isTrue();
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 60 with PageContext

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

the class HtmlEscapeTagTests method htmlEscapeTagWithContextParamFalse.

@Test
void htmlEscapeTagWithContextParamFalse() throws JspException {
    PageContext pc = createPageContext();
    MockServletContext sc = (MockServletContext) pc.getServletContext();
    HtmlEscapeTag tag = new HtmlEscapeTag();
    tag.setPageContext(pc);
    tag.doStartTag();
    sc.addInitParameter(WebUtils.HTML_ESCAPE_CONTEXT_PARAM, "false");
    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