@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); }
@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()); }