Search in sources :

Example 11 with Errors

use of org.springframework.validation.Errors in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithMappedProperties.

@Test
public void bindTagWithMappedProperties() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("map[key1]", "code1", "message1");
    errors.rejectValue("map[key1]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.map[key1]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "map[key1]".equals(status.getExpression()));
    assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
    assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
    assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 12 with Errors

use of org.springframework.validation.Errors in project spring-framework by spring-projects.

the class BindTagTests method nestedPathWithBindTag.

@Test
public void nestedPathWithBindTag() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    NestedPathTag nestedPathTag = new NestedPathTag();
    nestedPathTag.setPath("tb");
    nestedPathTag.setPageContext(pc);
    nestedPathTag.doStartTag();
    BindTag bindTag = new BindTag();
    bindTag.setPageContext(pc);
    bindTag.setPath("name");
    assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertEquals("tb.name", status.getPath());
    assertEquals("Correct field value", "", status.getDisplayValue());
    BindTag bindTag2 = new BindTag();
    bindTag2.setPageContext(pc);
    bindTag2.setPath("age");
    assertTrue("Correct doStartTag return value", bindTag2.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status2 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status2 != null);
    assertEquals("tb.age", status2.getPath());
    assertEquals("Correct field value", "0", status2.getDisplayValue());
    bindTag2.doEndTag();
    BindStatus status3 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertSame("Status matches previous status", status, status3);
    assertEquals("tb.name", status.getPath());
    assertEquals("Correct field value", "", status.getDisplayValue());
    bindTag.doEndTag();
    nestedPathTag.doEndTag();
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 13 with Errors

use of org.springframework.validation.Errors in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithGlobalErrors.

@Test
public void bindTagWithGlobalErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", "message1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", status.getExpression() == null);
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.*");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "*".equals(status.getExpression()));
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 14 with Errors

use of org.springframework.validation.Errors in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithIndexedProperties.

@Test
public void bindTagWithIndexedProperties() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
    assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
    assertTrue("Correct value", "name0".equals(((TestBean) status.getValue()).getName()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
    assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 15 with Errors

use of org.springframework.validation.Errors in project spring-framework by spring-projects.

the class BindTagTests method nestedPathWithBindTagWithIgnoreNestedPath.

@Test
public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors);
    NestedPathTag tag = new NestedPathTag();
    tag.setPath("tb");
    tag.setPageContext(pc);
    tag.doStartTag();
    BindTag bindTag = new BindTag();
    bindTag.setPageContext(pc);
    bindTag.setIgnoreNestedPath(true);
    bindTag.setPath("tb2.name");
    assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertEquals("tb2.name", status.getPath());
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) 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