Search in sources :

Example 51 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ErrorsTagTests method withErrorsAndDynamicAttributes.

@Test
public void withErrorsAndDynamicAttributes() throws Exception {
    String dynamicAttribute1 = "attr1";
    String dynamicAttribute2 = "attr2";
    this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
    this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);
    // 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");
    errors.rejectValue("name", "too.short", "Too Short");
    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");
    assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1);
    assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2);
    assertBlockTagContains(output, "<br/>");
    assertBlockTagContains(output, "Default Message");
    assertBlockTagContains(output, "Too Short");
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 52 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean 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 53 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.

the class ErrorsTagTests method withErrors.

@Test
public void withErrors() throws Exception {
    // 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");
    errors.rejectValue("name", "too.short", "Too Short");
    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, "<br/>");
    assertBlockTagContains(output, "Default Message");
    assertBlockTagContains(output, "Too Short");
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 54 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean 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 55 with TestBean

use of org.springframework.beans.testfixture.beans.TestBean 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)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28