Search in sources :

Example 16 with Errors

use of org.springframework.validation.Errors 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    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.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 17 with Errors

use of org.springframework.validation.Errors 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertElementTagOpened(output);
    assertElementTagClosed(output);
    assertContainsAttribute(output, "id", "name.errors");
    assertBlockTagContains(output, "Default Message");
}
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 18 with Errors

use of org.springframework.validation.Errors 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    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.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 19 with Errors

use of org.springframework.validation.Errors 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertElementTagOpened(output);
    assertElementTagClosed(output);
    assertContainsAttribute(output, "id", "name.errors");
    assertBlockTagContains(output, "Default Message");
}
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 20 with Errors

use of org.springframework.validation.Errors 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();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
    assertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List);
    String bodyContent = "Foo";
    this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
    this.tag.doEndTag();
    this.tag.doFinally();
    assertEquals(bodyContent, getOutput());
    assertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, PageContext.APPLICATION_SCOPE));
}
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) List(java.util.List) Test(org.junit.Test)

Aggregations

Errors (org.springframework.validation.Errors)84 Test (org.junit.Test)36 TestBean (org.springframework.tests.sample.beans.TestBean)33 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)18 PageContext (javax.servlet.jsp.PageContext)17 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)17 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)16 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)16 BindException (org.springframework.validation.BindException)15 DataBinder (org.springframework.validation.DataBinder)14 BindStatus (org.springframework.web.servlet.support.BindStatus)14 Locale (java.util.Locale)11 DistributedStatusDetailsCommand (org.opennms.web.svclayer.model.DistributedStatusDetailsCommand)8 FieldError (org.springframework.validation.FieldError)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 DOMSource (javax.xml.transform.dom.DOMSource)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)5 Element (org.w3c.dom.Element)5