Example usage for org.springframework.validation BindException BindException

List of usage examples for org.springframework.validation BindException BindException

Introduction

In this page you can find the example usage for org.springframework.validation BindException BindException.

Prototype

public BindException(Object target, String objectName) 

Source Link

Document

Create a new BindException instance for a target bean.

Usage

From source file:org.opensprout.osaf.model.support.MemberValidatorTest.java

@Test
public void validate() throws Exception {
    MemberValidator validator = new MemberValidator();
    Member member = new Member();

    BindException errors = new BindException(member, "model");
    validator.validate(member, errors);//  www.ja  va  2s. com

    assertNotNull(errors.getAllErrors());
}

From source file:gov.nih.nci.cabig.ctms.web.WebTestCase.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    mockRegistry = new MockRegistry();
    servletContext = new MockServletContext();
    request = new MockHttpServletRequest(servletContext);
    response = new MockHttpServletResponse();
    errors = new BindException(new Object(), "command");
}

From source file:org.sventon.web.command.BaseCommandValidatorTest.java

@Test
public void testValidate() {
    BaseCommandValidator validator = new BaseCommandValidator();

    BaseCommand command = new BaseCommand();

    BindException exception = new BindException(command, "test");

    validator.validate(command, exception);

    // An empty base command is valid
    assertEquals(0, exception.getAllErrors().size());

    // Valid (typical) input
    command.setPath("/test/Test.java");
    command.setRevision(Revision.parse("12"));

    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    // Valid (typical) input
    command.setPath("/test/Test.java");
    command.setRevision(Revision.parse("12"));

    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    //Both HEAD and head (and HeAd) are valid revisions. These are not really
    //accepted by the validator in any other form than HEAD, but other case variations
    //are automatically converted when set using the setRevision method on BaseCommand
    command.setRevision(Revision.parse("HEAD"));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRevision(Revision.parse("{2007-01-01}"));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRevision(Revision.parse("head "));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRevision(Revision.parse("HEad"));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRevision(Revision.parse(" 123 "));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRevision(Revision.parse("{2007-01-01}"));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    // Illegal date format
    //    command.setRevision(Revision.parse("{2007-01}"));
    //    validator.validate(command, exception);
    //    assertEquals(1, exception.getAllErrors().size());

    //Other non numerical revisions are however not allowed
    command.setRevision(Revision.parse("2007-01-01"));
    validator.validate(command, exception);
    assertEquals(1, exception.getAllErrors().size());
    assertEquals("browse.error.illegal-revision", exception.getFieldError("revision").getCode());

    exception = new BindException(command, "test2");
    command.setRevision(Revision.create(1));
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    exception = new BindException(command, "test2");
    command.setSortMode(null);//www  .  j a  va2s.c o m
    command.setSortType(null);
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    exception = new BindException(command, "test2");
    command.setSortMode(DirEntrySorter.SortMode.ASC);
    command.setSortType(DirEntryComparator.SortType.SIZE);
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

}

From source file:org.jasig.cas.validation.UsernamePasswordCredentialsValidatorTests.java

public void testValidationPasses() {
    final UsernamePasswordCredentials c = TestUtils.getCredentialsWithSameUsernameAndPassword();
    final BindException b = new BindException(c, "credentials");
    this.validator.validate(c, b);
    assertFalse(b.hasErrors());/*w ww  .j a  v  a2s  . c om*/
}

From source file:org.openmrs.module.sdmxhdintegration.SDMXHDMessageValidatorTest.java

@Test
@Verifies(value = "should accept if valid", method = "validate(Object, Errors)")
public void validate_shouldAcceptIfValid() {
    SDMXHDMessage message = new SDMXHDMessage();
    message.setName("Test");
    message.setDescription("Description");
    message.setZipFilename("dummy.zip");

    Errors errors = new BindException(message, "message");
    validator.validate(message, errors);
    Assert.assertFalse(errors.hasErrors());
}

From source file:net.sf.oval.test.integration.spring.SpringValidatorTest.java

public void testSpringValidator() {
    final SpringValidator v = new SpringValidator(new Validator());
    final Entity e = new Entity();
    {/* ww  w .  java 2  s. c om*/
        e.name = null;
        e.age = -1;
        final BindException errors = new BindException(e, e.getClass().getName());
        v.validate(e, errors);
        assertEquals(2, errors.getErrorCount());
        assertEquals(2, errors.getFieldErrorCount());
        assertEquals(null, errors.getFieldError("name").getRejectedValue());
        assertTrue(errors.getFieldError("name").getCodes()[0].startsWith("E1"));
        assertEquals(-1, errors.getFieldError("age").getRejectedValue());
        assertTrue(errors.getFieldError("age").getCodes()[0].startsWith("E2"));
    }
    {
        final BindException errors = new BindException(e, e.getClass().getName());
        e.name = "";
        e.age = -1;
        v.validate(e, errors);
        assertEquals(1, errors.getErrorCount());
        assertEquals(1, errors.getFieldErrorCount());
        assertEquals(-1, errors.getFieldError("age").getRejectedValue());
        assertTrue(errors.getFieldError("age").getCodes()[0].startsWith("E2"));
    }
    {
        final BindException errors = new BindException(e, e.getClass().getName());
        e.name = null;
        e.age = 0;
        v.validate(e, errors);
        assertEquals(1, errors.getErrorCount());
        assertEquals(1, errors.getFieldErrorCount());
        assertEquals(null, errors.getFieldError("name").getRejectedValue());
        assertTrue(errors.getFieldError("name").getCodes()[0].startsWith("E1"));
    }
    {
        final BindException errors = new BindException(e, e.getClass().getName());
        e.name = "";
        e.age = 0;
        v.validate(e, errors);
        assertEquals(0, errors.getErrorCount());
        assertEquals(0, errors.getFieldErrorCount());
    }
}

From source file:org.toobsframework.exception.ValidationException.java

public ValidationException(Object bean, String name, String field, String errorCode) {
    BindException bindException = new BindException(bean, name);
    bindException.rejectValue(field, errorCode);
    this.errors.add(bindException);
}

From source file:org.openmrs.module.casereport.CaseReportTriggerValidatorTest.java

/**
 * @see CaseReportTriggerValidator#validate(Object,Errors)
 * @verifies fail if the case report trigger object is null
 *//*from w w w.j a  v a  2  s. c o  m*/
@Test
public void validate_shouldFailIfTheCaseReportTriggerObjectIsNull() throws Exception {
    Errors errors = new BindException(new CaseReportTrigger(), "trigger");
    expectedException.expect(IllegalArgumentException.class);
    String expectedMsg = "The parameter obj should not be null and must be of type" + CaseReportTrigger.class;
    expectedException.expectMessage(expectedMsg);
    validator.validate(null, errors);
}

From source file:org.openmrs.module.casereport.CaseReportValidatorTest.java

/**
 * @see CaseReportValidator#validate(Object,Errors)
 * @verifies fail if the case report object is null
 *///from w ww.j  a v  a  2  s . com
@Test
public void validate_shouldFailIfTheCaseReportObjectIsNull() throws Exception {
    Errors errors = new BindException(new CaseReport(), "casereport");
    expectedException.expect(IllegalArgumentException.class);
    expectedException
            .expectMessage("The parameter obj should not be null and must be of type" + CaseReport.class);
    validator.validate(null, errors);
}

From source file:org.openmrs.module.radiology.validator.RadiologyOrderValidatorComponentTest.java

/**
 * @see {@link RadiologyOrderValidator#validate(Object, Errors)}
 *//*from  w ww  . j  av a2s  .com*/
@Test
@Verifies(value = "should fail validation if radiologyOrder is null", method = "validate(Object, Errors)")
public void validate_shouldFailValidationIfRadiologyOrderIsNull() throws Exception {

    Errors errors = new BindException(new RadiologyOrder(), "radiologyOrder");
    new RadiologyOrderValidator().validate(null, errors);

    Assert.assertTrue(errors.hasErrors());
    Assert.assertEquals("error.general", ((List<ObjectError>) errors.getAllErrors()).get(0).getCode());
}