Search in sources :

Example 21 with PageContext

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

the class ThemeTagTests method requestContext.

@Test
@SuppressWarnings("rawtypes")
void requestContext() throws ServletException {
    PageContext pc = createPageContext();
    RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest());
    assertThat(rc.getThemeMessage("themetest")).isEqualTo("theme test message");
    assertThat(rc.getThemeMessage("themetest", (String[]) null)).isEqualTo("theme test message");
    assertThat(rc.getThemeMessage("themetest", "default")).isEqualTo("theme test message");
    assertThat(rc.getThemeMessage("themetest", (Object[]) null, "default")).isEqualTo("theme test message");
    assertThat(rc.getThemeMessage("themetestArgs", new String[] { "arg1" })).isEqualTo("theme test message arg1");
    assertThat(rc.getThemeMessage("themetestArgs", Arrays.asList(new String[] { "arg1" }))).isEqualTo("theme test message arg1");
    assertThat(rc.getThemeMessage("themetesta", "default")).isEqualTo("default");
    assertThat(rc.getThemeMessage("themetesta", (List) null, "default")).isEqualTo("default");
    MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] { "themetest" });
    assertThat(rc.getThemeMessage(resolvable)).isEqualTo("theme 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 22 with PageContext

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

the class BindTagTests method bindTagWithGlobalErrorsAndDefaultMessageOnly.

@Test
void bindTagWithGlobalErrorsAndDefaultMessageOnly() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject(null, "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.getErrorMessages().length == 1).as("Correct errorMessages").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.getErrorMessages().length == 1).as("Correct errorMessages").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 23 with PageContext

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

the class BindTagTests method bindTagWithSetValueAndHtmlEscaping.

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

Example 24 with PageContext

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

the class BindTagTests method bindTagWithBeanButWithoutErrorsInstance.

@Test
void bindTagWithBeanButWithoutErrorsInstance() throws JspException {
    PageContext pc = createPageContext();
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    pc.getRequest().setAttribute("tb", new TestBean("juergen", 99));
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status.getExpression()).isNull();
    assertThat(status.getValue()).isNull();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) PageContext(jakarta.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.jupiter.api.Test)

Example 25 with PageContext

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

the class BindTagTests method nestedPathWithBindTag.

@Test
void nestedPathWithBindTag() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    NestedPathTag nestedPathTag = new NestedPathTag();
    nestedPathTag.setPath("tb");
    nestedPathTag.setPageContext(pc);
    nestedPathTag.doStartTag();
    BindTag bindTag = new BindTag();
    bindTag.setPageContext(pc);
    bindTag.setPath("name");
    assertThat(bindTag.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.getPath()).isEqualTo("tb.name");
    assertThat(status.getDisplayValue()).as("Correct field value").isEqualTo("");
    BindTag bindTag2 = new BindTag();
    bindTag2.setPageContext(pc);
    bindTag2.setPath("age");
    assertThat(bindTag2.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status2 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status2 != null).as("Has status variable").isTrue();
    assertThat(status2.getPath()).isEqualTo("tb.age");
    assertThat(status2.getDisplayValue()).as("Correct field value").isEqualTo("0");
    bindTag2.doEndTag();
    BindStatus status3 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status3).as("Status matches previous status").isSameAs(status);
    assertThat(status.getPath()).isEqualTo("tb.name");
    assertThat(status.getDisplayValue()).as("Correct field value").isEqualTo("");
    bindTag.doEndTag();
    nestedPathTag.doEndTag();
}
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)

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