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();
}
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();
}
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();
}
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");
}
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();
}
Aggregations