Search in sources :

Example 6 with MockBodyContent

use of org.springframework.mock.web.test.MockBodyContent in project spring-framework by spring-projects.

the class OptionTagTests method asBodyTag.

@Test
public void asBodyTag() throws Exception {
    String selectName = "testBean.name";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    String bodyContent = "some content";
    this.tag.setValue("foo");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "selected", "selected");
    assertBlockTagContains(output, bodyContent);
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 7 with MockBodyContent

use of org.springframework.mock.web.test.MockBodyContent in project spring-framework by spring-projects.

the class OptionTagTests method asBodyTagSelected.

@Test
public void asBodyTagSelected() throws Exception {
    String selectName = "testBean.name";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    String bodyContent = "some content";
    this.tag.setValue("Rob Harrop");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertBlockTagContains(output, bodyContent);
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 8 with MockBodyContent

use of org.springframework.mock.web.test.MockBodyContent in project spring-framework by spring-projects.

the class OptionTagTests method asBodyTagCollapsed.

@Test
public void asBodyTagCollapsed() throws Exception {
    String selectName = "testBean.name";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    String bodyContent = "some content";
    this.tag.setValue(bodyContent);
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "value", bodyContent);
    assertBlockTagContains(output, bodyContent);
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 9 with MockBodyContent

use of org.springframework.mock.web.test.MockBodyContent in project spring-framework by spring-projects.

the class ErrorsTagTests method withExplicitNonWhitespaceBodyContent.

@Test
public void withExplicitNonWhitespaceBodyContent() throws Exception {
    String mockContent = "This is some explicit body content";
    this.tag.setBodyContent(new MockBodyContent(mockContent, 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    assertEquals(mockContent, getOutput());
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 10 with MockBodyContent

use of org.springframework.mock.web.test.MockBodyContent in project spring-framework by spring-projects.

the class ErrorsTagTests method asBodyTag.

@Test
public void asBodyTag() throws Exception {
    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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
    String bodyContent = "Foo";
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    this.tag.doEndTag();
    this.tag.doFinally();
    assertEquals(bodyContent, getOutput());
    assertNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
}
Also used : Errors(org.springframework.validation.Errors) MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)13 TestBean (org.springframework.tests.sample.beans.TestBean)6 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)6 Errors (org.springframework.validation.Errors)6 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 BindStatus (org.springframework.web.servlet.support.BindStatus)3 List (java.util.List)2