Example usage for org.springframework.validation BindException getErrorCount

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

Introduction

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

Prototype

@Override
    public int getErrorCount() 

Source Link

Usage

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

private void commonTests(final UsernamePasswordCredentials c, int errorCountExpected) {
    final BindException b = new BindException(c, "credentials");
    this.validator.validate(c, b);
    assertTrue(b.hasErrors());/*from   w  ww  .  j  av  a  2  s .  c  om*/
    assertEquals(b.getErrorCount(), errorCountExpected);
}

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();
    {//w w w.  j  av  a2s.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.jasig.cas.services.web.support.RegisteredServiceValidatorTests.java

License:asdf

public void testMaxLength() {
    this.validator.setServicesManager(new TestServicesManager(false));
    final RegisteredServiceImpl impl = new RegisteredServiceImpl();
    impl.setServiceId("test");
    impl.setDescription("fasdfdsafsafsafdsa");

    final BindException exception = new BindException(impl, "registeredService");

    this.validator.validate(impl, exception);

    assertEquals(1, exception.getErrorCount());
}

From source file:org.jasig.cas.services.web.support.RegisteredServiceValidatorTests.java

License:asdf

protected void checkId(final boolean exists, final int expectedErrors, final String name) {
    this.validator.setServicesManager(new TestServicesManager(exists));
    final RegisteredServiceImpl impl = new RegisteredServiceImpl();
    impl.setServiceId(name);/*w w  w.j a v  a 2 s.  co  m*/

    final BindException exception = new BindException(impl, "registeredService");

    this.validator.validate(impl, exception);

    assertEquals(expectedErrors, exception.getErrorCount());

}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies raise an error if retire reason is not filled
 * @see EncounterRoleFormController#retire(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *///from   w w w.j a  va 2s.c o m
@Test
public void retire_shouldRaiseAnErrorIfRetireReasonIsNotFilled() throws Exception {
    executeDataSet(ENC_INITIAL_DATA_XML);
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = Context.getEncounterService().getEncounterRole(1);
    encounterRole.setRetireReason(""); //setting empty retire reason so that it will raise an error.
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.retire(session, encounterRole, errors);
    Assert.assertEquals(1, errors.getErrorCount());
}

From source file:org.openmrs.web.controller.concept.ConceptStopWordFormControllerTest.java

/**
 * @see {@link ConceptStopWordFormController#handleSubmission(HttpSession, ConceptStopWordFormBackingObject, org.springframework.validation.BindingResult)
 *//*  www .jav  a2  s . com*/
@Test
@Verifies(value = "should return error message for an empty ConceptStopWord", method = "handleSubmission(HttpSession, ConceptStopWordFormBackingObject, BindingResult)")
public void handleSubmission_shouldReturnErrorMessageForAnEmptyConceptStopWord() throws Exception {
    ConceptStopWordFormController controller = (ConceptStopWordFormController) applicationContext
            .getBean("conceptStopWordFormController");

    HttpSession mockSession = new MockHttpSession();

    ConceptStopWord conceptStopWord = new ConceptStopWord("", Locale.CANADA);

    mockSession.setAttribute("value", conceptStopWord.getValue());
    BindException errors = new BindException(conceptStopWord, "value");

    controller.handleSubmission(mockSession, conceptStopWord, errors);
    ObjectError objectError = (ObjectError) errors.getAllErrors().get(0);

    Assert.assertTrue(errors.hasErrors());
    Assert.assertEquals(1, errors.getErrorCount());
    Assert.assertEquals("ConceptStopWord.error.value.empty", objectError.getCode());
}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies raise an error if validation of encounter role fails
 * @see EncounterRoleFormController#save(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *//* www. ja va 2 s  . co  m*/
@Test
public void saveEncounterRole_shouldRaiseAnErrorIfValidationOfEncounterRoleFails() throws Exception {
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = new EncounterRole();
    encounterRole.setDescription("person in charge");
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.save(session, encounterRole, errors);
    Assert.assertNull(encounterRole.getId());
    Assert.assertEquals(1, errors.getErrorCount());
}

From source file:org.openmrs.web.controller.patient.MergePatientsFormController.java

protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response,
        Object object, BindException errors) throws Exception {
    //ModelAndView view = super.processFormSubmission(request, response, object, errors);

    log.debug("Number of errors: " + errors.getErrorCount());

    for (Object o : errors.getAllErrors()) {
        ObjectError e = (ObjectError) o;
        log.debug("Error name: " + e.getObjectName());
        log.debug("Error code: " + e.getCode());
        log.debug("Error message: " + e.getDefaultMessage());
        log.debug("Error args: " + Arrays.toString(e.getArguments()));
        log.debug("Error codes: " + e.getCodes());
    }//from w ww  .j  a v a  2  s  .  c o  m

    // call onSubmit manually so that we don't have to call
    // super.processFormSubmission()
    return onSubmit(request, response, object, errors);
}

From source file:edu.wisc.my.portlets.bookmarks.web.BaseBookmarksFormController.java

/**
 * @see org.springframework.web.portlet.mvc.SimpleFormController#processFormSubmission(javax.portlet.ActionRequest, javax.portlet.ActionResponse, java.lang.Object, org.springframework.validation.BindException)
 *//*from ww w .  j a v  a  2  s.  c om*/
@Override
protected void processFormSubmission(ActionRequest request, ActionResponse response, Object command,
        BindException errors) throws Exception {

    // don't save preferences for guest users
    if (request.getRemoteUser() == null)
        return;

    super.processFormSubmission(request, response, command, errors);

    if (errors.getErrorCount() <= 0) {
        response.setRenderParameter(this.handlerMappingParameter, this.getSuccessView());
    }
}

From source file:cherry.foundation.type.format.CustomNumberFormatTest.java

@Test
public void testByte() throws BindException {
    String name = "byteValue";
    assertEquals("44", parseAndPrint(name, "44"));
    assertEquals("44", parseAndPrint(name, "44.4444"));
    assertEquals("55", parseAndPrint(name, "55"));
    assertEquals("55", parseAndPrint(name, "55.5555"));
    assertEquals("-44", parseAndPrint(name, "-44"));
    assertEquals("-44", parseAndPrint(name, "-44.4444"));
    assertEquals("-55", parseAndPrint(name, "-55"));
    assertEquals("-55", parseAndPrint(name, "-55.5555"));
    try {/*from w w  w .j ava2 s . c o  m*/
        parseAndPrint(name, "aaa");
        fail("Exception must be thrown");
    } catch (BindException ex) {
        assertEquals(1, ex.getErrorCount());
        assertTrue(ex.hasFieldErrors(name));
        assertEquals("typeMismatch", ex.getFieldError(name).getCode());
    }
}