Search in sources :

Example 56 with TestBean

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

the class ErrorsTagTests method assertWhenNoErrorsExistingMessagesInScopeAreNotClobbered.

private void assertWhenNoErrorsExistingMessagesInScopeAreNotClobbered(int scope) throws JspException {
    String existingAttribute = "something";
    getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, scope);
    Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    String output = getOutput();
    assertThat(output.length()).isEqualTo(0);
    assertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, scope)).isEqualTo(existingAttribute);
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.beans.testfixture.beans.TestBean)

Example 57 with TestBean

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

the class ErrorsTagTests method withoutErrors.

@Test
public void withoutErrors() throws Exception {
    Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    String output = getOutput();
    assertThat(output.length()).isEqualTo(0);
}
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 58 with TestBean

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

the class ErrorsTagTests method withErrorsAndCustomElement.

@Test
public void withErrorsAndCustomElement() 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);
    this.tag.setElement("div");
    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 59 with TestBean

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

the class ErrorsTagTests method withNonEscapedErrors.

@Test
public void withNonEscapedErrors() throws Exception {
    this.tag.setHtmlEscape(false);
    // 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 60 with TestBean

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

the class OptionsTagTests method withCollectionAndCustomEditor.

@Test
void withCollectionAndCustomEditor() throws Exception {
    PropertyEditor propertyEditor = new SimpleFloatEditor();
    TestBean target = new TestBean();
    target.setMyFloat(Float.valueOf("12.34"));
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
    exposeBindingResult(errors);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.myFloat", false));
    List<Float> floats = new ArrayList<>();
    floats.add(Float.valueOf("12.30"));
    floats.add(Float.valueOf("12.31"));
    floats.add(Float.valueOf("12.32"));
    floats.add(Float.valueOf("12.33"));
    floats.add(Float.valueOf("12.34"));
    floats.add(Float.valueOf("12.35"));
    this.tag.setItems(floats);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    String output = getOutput();
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element rootElement = document.getRootElement();
    List children = rootElement.elements();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(6);
    Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
    assertThat(element).as("Option node should not be null").isNotNull();
    assertThat(element.attribute("selected").getValue()).as("12.34 node not selected").isEqualTo("selected");
    assertThat(element.attribute("id")).as("No id rendered").isNull();
    element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
    assertThat(element).as("Option node should not be null").isNotNull();
    assertThat(element.attribute("selected")).as("12.35 node incorrectly selected").isNull();
    assertThat(element.attribute("id")).as("No id rendered").isNull();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) BindStatus(org.springframework.web.servlet.support.BindStatus) Document(org.dom4j.Document) TestBean(org.springframework.beans.testfixture.beans.TestBean) StringReader(java.io.StringReader) PropertyEditor(java.beans.PropertyEditor) ArrayList(java.util.ArrayList) List(java.util.List) 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