@Test public void shouldUnwrapParameterValueBasedOnProgrammaticConfiguration() throws Exception { HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(); ConstraintMapping mapping = configuration.createConstraintMapping(); mapping .type(OrderLine.class) .method("setId", Property.class) .parameter(0) .unwrapValidatedValue(true); Validator validator = configuration .addMapping(mapping) .addValidatedValueHandler(new PropertyValueUnwrapper()) .buildValidatorFactory() .getValidator(); OrderLine orderLine = new OrderLine(); Method method = OrderLine.class.getMethod("setId", Property.class); Object[] parameterValues = new Object[] {new Property<Long>(0L)}; Set<ConstraintViolation<OrderLine>> violations = validator.forExecutables().validateParameters(orderLine, method, parameterValues); assertEquals(violations.size(), 1); }
@Provides protected ValidatorFactory getValidationFactory( Provider<MessageInterpolator> interpolatorProvider) { HibernateValidatorConfiguration configure = Validation.byProvider(HibernateValidator.class).configure(); configure.messageInterpolator(interpolatorProvider.get()); return configure.buildValidatorFactory(); }
static { HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure().failFast(true); FACTORY = configuration .messageInterpolator( new ContextualMessageInterpolator(configuration.getDefaultMessageInterpolator())) .buildValidatorFactory(); }
@Test public void shouldUnwrapPropertyValueBasedOnProgrammaticConfiguration() { HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(); ConstraintMapping mapping = configuration.createConstraintMapping(); mapping.type(OrderLine.class).property("id", ElementType.FIELD).unwrapValidatedValue(true); Validator validator = configuration .addMapping(mapping) .addValidatedValueHandler(new PropertyValueUnwrapper()) .buildValidatorFactory() .getValidator(); Set<ConstraintViolation<OrderLine>> violations = validator.validate(new OrderLine()); assertEquals(violations.size(), 1); }
@Test( expectedExceptions = UnexpectedTypeException.class, expectedExceptionsMessageRegExp = "HV000030.*") public void explicitly_skipping_unwrapping_leads_to_exception_due_to_missing_constraint_validator() { HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(); ConstraintMapping mapping = configuration.createConstraintMapping(); mapping.type(OrderLine.class).property("id", ElementType.FIELD).unwrapValidatedValue(false); Validator validator = configuration .addMapping(mapping) .addValidatedValueHandler(new PropertyValueUnwrapper()) .buildValidatorFactory() .getValidator(); validator.validate(new OrderLine()); }
@Test public void withConfiguration() { HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class) .providerResolver( new ValidationProviderResolver() { @Override public List<ValidationProvider<?>> getValidationProviders() { ValidationProvider<HibernateValidatorConfiguration> prov = new HibernateValidator(); List<ValidationProvider<?>> provs = new ArrayList<>(); provs.add(prov); return provs; } }) .configure(); configuration.externalClassLoader(Thread.currentThread().getContextClassLoader()); Validator validator = configuration.buildValidatorFactory().getValidator(); Thread.currentThread().setContextClassLoader(null); Set<ConstraintViolation<ValidateMe>> violations = validator.validate(new ValidateMe(0)); Assert.assertEquals(1, violations.size()); }