TargetAccessValidator extends AbstractBaseValidator { public void validate(Object comm, Errors errors) { TargetAccessCommand command = (TargetAccessCommand) comm; if(command.getDisplayNote() != null) { ValidatorUtil.validateStringMaxLength(errors, command.getDisplayNote(), Target.MAX_DISPLAY_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetAccessCommand.PARAM_DISPLAY_NOTE, Target.MAX_DISPLAY_NOTE_LENGTH), "Display Note is too long"); } if(command.getDisplayChangeReason() != null) { ValidatorUtil.validateStringMaxLength(errors, command.getDisplayChangeReason(), Target.MAX_DISPLAY_CHANGE_REASON_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetAccessCommand.PARAM_DISPLAY_CHANGE_REASON, Target.MAX_DISPLAY_CHANGE_REASON_LENGTH), "Display Note is too long"); } }  boolean supports(Class clazz); void validate(Object comm, Errors errors);  }
@Test public void testValidateCase01() { try { TargetAccessCommand cmd = new TargetAccessCommand(); TargetAccessValidator validator = new TargetAccessValidator(); Errors errors = new BindException(cmd, "TargetAccessCommand"); validator.validate(cmd, errors); assertEquals("Case01: Not expecting errors with newly initialised objects", 0, errors.getErrorCount()); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
@Test public void testValidateCase02() { try { TargetAccessCommand cmd = new TargetAccessCommand(); TargetAccessValidator validator = new TargetAccessValidator(); Errors errors = new BindException(cmd, "TargetAccessCommand"); cmd.setDisplayNote(makeLongString(33, 'X')); validator.validate(cmd, errors); assertEquals("Case02: Expecting 0 errors", 0, errors.getErrorCount()); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
@Test public void testValidateCase03() { try { TargetAccessCommand cmd = new TargetAccessCommand(); TargetAccessValidator validator = new TargetAccessValidator(); Errors errors = new BindException(cmd, "TargetAccessCommand"); cmd.setDisplayNote(makeLongString(4001, 'X')); validator.validate(cmd, errors); assertEquals("Case02: Expecting 1 errors", 1, errors.getErrorCount()); assertEquals("Case02: Expecting error[0] for field Display Note", true, errors.getAllErrors().toArray()[0].toString().contains("Display Note")); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }