Search in sources :

Example 1 with MockBodyContent

use of org.springframework.web.testfixture.servlet.MockBodyContent in project spring-framework by spring-projects.

the class ArgumentTagTests method argumentWithBodyValue.

@Test
public void argumentWithBodyValue() throws JspException {
    tag.setBodyContent(new MockBodyContent("value2", new MockHttpServletResponse()));
    int action = tag.doEndTag();
    assertThat(action).isEqualTo(Tag.EVAL_PAGE);
    assertThat(parent.getArgument()).isEqualTo("value2");
}
Also used : MockBodyContent(org.springframework.web.testfixture.servlet.MockBodyContent) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with MockBodyContent

use of org.springframework.web.testfixture.servlet.MockBodyContent in project spring-framework by spring-projects.

the class ErrorsTagTests method asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered.

/**
 * https://jira.spring.io/browse/SPR-2788
 */
@Test
public void asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered() throws Exception {
    String existingAttribute = "something";
    getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, PageContext.APPLICATION_SCOPE);
    Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
    errors.rejectValue("name", "some.code", "Default Message");
    errors.rejectValue("name", "too.short", "Too Short");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
    assertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)).isNotNull();
    boolean condition = getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List;
    assertThat(condition).isTrue();
    String bodyContent = "Foo";
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    this.tag.doEndTag();
    this.tag.doFinally();
    assertThat(getOutput()).isEqualTo(bodyContent);
    assertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, PageContext.APPLICATION_SCOPE)).isEqualTo(existingAttribute);
}
Also used : Errors(org.springframework.validation.Errors) MockBodyContent(org.springframework.web.testfixture.servlet.MockBodyContent) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with MockBodyContent

use of org.springframework.web.testfixture.servlet.MockBodyContent in project spring-framework by spring-projects.

the class ErrorsTagTests method withExplicitEmptyWhitespaceBodyContent.

@Test
public void withExplicitEmptyWhitespaceBodyContent() throws Exception {
    this.tag.setBodyContent(new MockBodyContent("", getWriter()));
    // construct an errors instance of the tag
    TestBean target = new TestBean();
    target.setName("Rob Harrop");
    Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.rejectValue("name", "some.code", "Default Message");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    String output = getOutput();
    assertElementTagOpened(output);
    assertElementTagClosed(output);
    assertContainsAttribute(output, "id", "name.errors");
    assertBlockTagContains(output, "Default Message");
}
Also used : MockBodyContent(org.springframework.web.testfixture.servlet.MockBodyContent) Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 4 with MockBodyContent

use of org.springframework.web.testfixture.servlet.MockBodyContent in project spring-framework by spring-projects.

the class ErrorsTagTests method withExplicitWhitespaceBodyContent.

@Test
public void withExplicitWhitespaceBodyContent() throws Exception {
    this.tag.setBodyContent(new MockBodyContent("\t\n   ", getWriter()));
    // construct an errors instance of the tag
    TestBean target = new TestBean();
    target.setName("Rob Harrop");
    Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.rejectValue("name", "some.code", "Default Message");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    String output = getOutput();
    assertElementTagOpened(output);
    assertElementTagClosed(output);
    assertContainsAttribute(output, "id", "name.errors");
    assertBlockTagContains(output, "Default Message");
}
Also used : MockBodyContent(org.springframework.web.testfixture.servlet.MockBodyContent) Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 5 with MockBodyContent

use of org.springframework.web.testfixture.servlet.MockBodyContent in project spring-framework by spring-projects.

the class ParamTagTests method paramWithBodyValue.

@Test
public void paramWithBodyValue() throws JspException {
    tag.setName("name");
    tag.setBodyContent(new MockBodyContent("value", new MockHttpServletResponse()));
    int action = tag.doEndTag();
    assertThat(action).isEqualTo(Tag.EVAL_PAGE);
    assertThat(parent.getParam().getName()).isEqualTo("name");
    assertThat(parent.getParam().getValue()).isEqualTo("value");
}
Also used : MockBodyContent(org.springframework.web.testfixture.servlet.MockBodyContent) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)10 MockBodyContent (org.springframework.web.testfixture.servlet.MockBodyContent)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)6 Errors (org.springframework.validation.Errors)6 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)4 List (java.util.List)2