Search in sources :

Example 66 with PageContext

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

the class MessageTagTests method messageTagWithTextAndHtmlEscapeAndJavaScriptEscape.

@Test
void messageTagWithTextAndHtmlEscapeAndJavaScriptEscape() 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.setText("' test & text \\");
    tag.setHtmlEscape(true);
    tag.setJavaScriptEscape(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 & text \\\\");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 67 with PageContext

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

the class MessageTagTests method messageTagWithCodeAndNestedArguments.

@Test
void messageTagWithCodeAndNestedArguments() 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("arg1");
    tag.addArgument(6);
    assertThat(tag.doEndTag()).as("Correct doEndTag return value").isEqualTo(Tag.EVAL_PAGE);
    assertThat(message.toString()).as("Correct message").isEqualTo("test arg1 message 6");
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 68 with PageContext

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

the class MessageTagTests method requestContext.

@Test
@SuppressWarnings("rawtypes")
void requestContext() throws ServletException {
    PageContext pc = createPageContext();
    RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest(), pc.getServletContext());
    assertThat(rc.getMessage("test")).isEqualTo("test message");
    assertThat(rc.getMessage("test", (Object[]) null)).isEqualTo("test message");
    assertThat(rc.getMessage("test", "default")).isEqualTo("test message");
    assertThat(rc.getMessage("test", (Object[]) null, "default")).isEqualTo("test message");
    assertThat(rc.getMessage("testArgs", new String[] { "arg1", "arg2" }, "default")).isEqualTo("test arg1 message arg2");
    assertThat(rc.getMessage("testArgs", Arrays.asList(new String[] { "arg1", "arg2" }), "default")).isEqualTo("test arg1 message arg2");
    assertThat(rc.getMessage("testa", "default")).isEqualTo("default");
    assertThat(rc.getMessage("testa", (List) null, "default")).isEqualTo("default");
    MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] { "test" });
    assertThat(rc.getMessage(resolvable)).isEqualTo("test message");
}
Also used : DefaultMessageSourceResolvable(org.springframework.context.support.DefaultMessageSourceResolvable) MessageSourceResolvable(org.springframework.context.MessageSourceResolvable) DefaultMessageSourceResolvable(org.springframework.context.support.DefaultMessageSourceResolvable) PageContext(jakarta.servlet.jsp.PageContext) RequestContext(org.springframework.web.servlet.support.RequestContext) Test(org.junit.jupiter.api.Test)

Example 69 with PageContext

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

the class BindTagTests method propertyExposing.

@Test
void propertyExposing() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new BindException(tb, "tb");
    errors.rejectValue("name", "code1", null, "message & 1");
    errors.rejectValue("name", "code2", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    // test global property (should be null)
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    assertThat(tag.getProperty()).isNull();
    // test property set (tb.name)
    tag.release();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    assertThat(tag.getProperty()).isEqualTo("name");
}
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) BindException(org.springframework.validation.BindException) PageContext(jakarta.servlet.jsp.PageContext) Test(org.junit.jupiter.api.Test)

Example 70 with PageContext

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

the class ParamTagTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    PageContext context = createPageContext();
    tag.setPageContext(context);
    tag.setParent(parent);
}
Also used : PageContext(jakarta.servlet.jsp.PageContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

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