FieldValidatorDefaultSourceImpl implements FieldValidatorDefaultSource { public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) { List<FieldValidator> validators = newList(); for (String constraint : validationConstraintGenerator.buildConstraints( propertyType, propertyAnnotations)) { int equalsx = constraint.indexOf('='); String validatorType = equalsx > 0 ? constraint.substring(0, equalsx) : constraint; String constraintValue = equalsx > 0 ? constraint.substring(equalsx + 1) : null; FieldValidator validator = fieldValidatorSource.createValidator( field, validatorType, constraintValue, overrideId, overrideMessages, locale); validators.add(validator); } return validators.size() == 1 ? validators.get(0) : new CompositeFieldValidator(validators); } FieldValidatorDefaultSourceImpl(
            ValidationConstraintGenerator validationConstraintGenerator,
            FieldValidatorSource fieldValidatorSource); FieldValidator createDefaultValidator(Field field, String overrideId,
                                                 Messages overrideMessages, Locale locale, Class propertyType,
                                                 AnnotationProvider propertyAnnotations); FieldValidator createDefaultValidator(ComponentResources resources, String parameterName);  }
@SuppressWarnings("unchecked") @Test public void invokes_all_constraint_generators() throws Exception { getMocksControl().checkOrder(true); ValidationConstraintGenerator gen = mockValidationConstraintGenerator(); FieldValidator fv1 = mockFieldValidator(); FieldValidator fv2 = mockFieldValidator(); FieldValidatorSource source = mockFieldValidatorSource(); Class propertyType = Integer.class; AnnotationProvider provider = mockAnnotationProvider(); String overrideId = "overrideId"; Messages overrideMessages = mockMessages(); Field field = mockField(); Locale locale = Locale.ENGLISH; String value = "*VALUE*"; train_buildConstraints(gen, propertyType, provider, "cons1", "cons2"); train_createValidator( source, field, "cons1", null, overrideId, overrideMessages, locale, fv1); train_createValidator( source, field, "cons2", null, overrideId, overrideMessages, locale, fv2); fv1.validate(value); fv2.validate(value); replay(); FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source); FieldValidator composite = fieldValidatorSource.createDefaultValidator( field, overrideId, overrideMessages, locale, propertyType, provider); composite.validate(value); verify(); }
@SuppressWarnings("unchecked") @Test public void validator_with_constraint() throws Exception { ValidationConstraintGenerator gen = mockValidationConstraintGenerator(); FieldValidator fv = mockFieldValidator(); FieldValidatorSource source = mockFieldValidatorSource(); Class propertyType = Integer.class; AnnotationProvider provider = mockAnnotationProvider(); String overrideId = "overrideId"; Messages overrideMessages = mockMessages(); Field field = mockField(); Locale locale = Locale.ENGLISH; train_buildConstraints(gen, propertyType, provider, "foo=bar"); train_createValidator(source, field, "foo", "bar", overrideId, overrideMessages, locale, fv); replay(); FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source); FieldValidator composite = fieldValidatorSource.createDefaultValidator( field, overrideId, overrideMessages, locale, propertyType, provider); assertSame(composite, fv); verify(); }
@SuppressWarnings("unchecked") @Test public void no_validators_at_all() throws Exception { ValidationConstraintGenerator gen = mockValidationConstraintGenerator(); FieldValidatorSource source = mockFieldValidatorSource(); Class propertyType = Integer.class; AnnotationProvider provider = mockAnnotationProvider(); String overrideId = "overrideId"; Messages overrideMessages = mockMessages(); Field field = mockField(); Locale locale = Locale.ENGLISH; String value = "*VALUE*"; train_buildConstraints(gen, propertyType, provider); replay(); FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source); FieldValidator composite = fieldValidatorSource.createDefaultValidator( field, overrideId, overrideMessages, locale, propertyType, provider); composite.validate(value); verify(); }