use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithFieldErrorsAndNoDefaultMessage.
@Test
void bindTagWithFieldErrorsAndNoDefaultMessage() throws JspException {
PageContext pc = createPageContext();
TestBean tb = new TestBean();
tb.setName("name1");
Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
errors.rejectValue("name", "code1");
errors.rejectValue("name", "code2");
errors.rejectValue("age", "code2");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.name");
tag.setHtmlEscape(true);
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("name".equals(status.getExpression())).as("Correct expression").isTrue();
assertThat("name1".equals(status.getValue())).as("Correct value").isTrue();
assertThat("name1".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
assertThat(status.isError()).as("Correct isError").isTrue();
assertThat(status.getErrorCodes().length == 2).as("Correct errorCodes").isTrue();
assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.age");
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("age".equals(status.getExpression())).as("Correct expression").isTrue();
assertThat(Integer.valueOf(0).equals(status.getValue())).as("Correct value").isTrue();
assertThat("0".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
assertThat(status.isError()).as("Correct isError").isTrue();
assertThat(status.getErrorCodes().length == 1).as("Correct errorCodes").isTrue();
assertThat("code2".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 == 3).as("Correct errorCodes").isTrue();
assertThat("code1".equals(status.getErrorCode())).as("Correct errorCode").isTrue();
assertThat("code1".equals(status.getErrorCodes()[0])).as("Correct errorCode").isTrue();
assertThat("code2".equals(status.getErrorCodes()[1])).as("Correct errorCode").isTrue();
assertThat("code2".equals(status.getErrorCodes()[2])).as("Correct errorCode").isTrue();
}
use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithoutBean.
@Test
void bindTagWithoutBean() throws JspException {
PageContext pc = createPageContext();
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb");
assertThatExceptionOfType(JspException.class).isThrownBy(tag::doStartTag);
}
use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithFieldErrorsAndDefaultMessageOnly.
@Test
void bindTagWithFieldErrorsAndDefaultMessageOnly() throws JspException {
PageContext pc = createPageContext();
TestBean tb = new TestBean();
tb.setName("name1");
Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
errors.rejectValue("name", null, "message & 1");
errors.rejectValue("name", null, "message2");
errors.rejectValue("age", null, "message2");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.name");
tag.setHtmlEscape(true);
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("name".equals(status.getExpression())).as("Correct expression").isTrue();
assertThat("name1".equals(status.getValue())).as("Correct value").isTrue();
assertThat("name1".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
assertThat(status.isError()).as("Correct isError").isTrue();
assertThat(status.getErrorMessages().length == 2).as("Correct errorMessages").isTrue();
assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
assertThat("message & 1 - message2".equals(status.getErrorMessagesAsString(" - "))).as("Correct errorMessagesAsString").isTrue();
tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.age");
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("age".equals(status.getExpression())).as("Correct expression").isTrue();
assertThat(Integer.valueOf(0).equals(status.getValue())).as("Correct value").isTrue();
assertThat("0".equals(status.getDisplayValue())).as("Correct displayValue").isTrue();
assertThat(status.isError()).as("Correct isError").isTrue();
assertThat(status.getErrorMessages().length == 1).as("Correct errorMessages").isTrue();
assertThat("message2".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
assertThat("message2".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 == 3).as("Correct errorMessages").isTrue();
assertThat("message & 1".equals(status.getErrorMessage())).as("Correct errorMessage").isTrue();
assertThat("message & 1".equals(status.getErrorMessages()[0])).as("Correct errorMessage").isTrue();
assertThat("message2".equals(status.getErrorMessages()[1])).as("Correct errorMessage").isTrue();
assertThat("message2".equals(status.getErrorMessages()[2])).as("Correct errorMessage").isTrue();
}
use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.
the class BindTagTests method bindErrorsTagWithoutBean.
@Test
void bindErrorsTagWithoutBean() throws JspException {
PageContext pc = createPageContext();
BindErrorsTag tag = new BindErrorsTag();
tag.setPageContext(pc);
tag.setName("tb");
assertThat(tag.doStartTag() == Tag.SKIP_BODY).as("Correct doStartTag return value").isTrue();
}
use of jakarta.servlet.jsp.PageContext in project spring-framework by spring-projects.
the class BindTagTests method transformTagWithSettingOfScope.
@Test
void transformTagWithSettingOfScope() throws JspException {
// first set up the pagecontext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());
// execute the bind tag using the date property
BindTag bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.date");
bind.doStartTag();
// transform stuff
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
transform.setParent(bind);
transform.setValue(tb.getDate());
transform.setVar("theDate");
transform.setScope("page");
transform.doStartTag();
transform.release();
assertThat(pc.getAttribute("theDate")).isNotNull();
assertThat(pc.getAttribute("theDate")).isEqualTo(df.format(tb.getDate()));
// try another time, this time using Strings
bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.name");
bind.doStartTag();
transform = new TransformTag();
transform.setPageContext(pc);
transform.setValue("name");
transform.setParent(bind);
transform.setVar("theString");
transform.setScope("page");
transform.doStartTag();
transform.release();
assertThat(pc.getAttribute("theString")).isNotNull();
assertThat(pc.getAttribute("theString")).isEqualTo("name");
}
Aggregations