public void testBeanValidation() throws Exception { Artist a = new Artist(); a.setName("TOO OLD ARTIST"); a.setAge(120); XPersistence.getManager().persist(a); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof javax.validation.ConstraintViolationException) { javax.validation.ConstraintViolationException vex = (javax.validation.ConstraintViolationException) ex.getCause(); assertEquals("1 invalid value is expected", 1, vex.getConstraintViolations().size()); ConstraintViolation violation = vex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Artist", violation.getRootBeanClass().getSimpleName()); String expectedMessage = "es".equals(Locale.getDefault().getLanguage()) ? "tiene que ser menor o igual que 90" : "must be less than or equal to 90"; assertEquals("Message text", expectedMessage, violation.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
private void displayContraintViolations(Set<ConstraintViolation<Book03>> constraintViolations) { for (ConstraintViolation constraintViolation : constraintViolations) { System.out.println( "### " + constraintViolation.getRootBeanClass().getSimpleName() + "." + constraintViolation.getPropertyPath() + " - Invalid Value = " + constraintViolation.getInvalidValue() + " - Error Msg = " + constraintViolation.getMessage()); } }
@Override protected boolean matchesSafely(ConstraintViolation item, Description mismatchDescription) { if (matcher.matches(item.getValidatorClazz())) { return true; } matcher.describeMismatch(item, mismatchDescription); return false; }
/** * When dealing with @ValidateWith annotations, and message parameter is not used in the * annotation, extract the message from validator's getErrorMessageKey() method */ protected String getMessageForConstraintViolation(ConstraintViolation<Object> violation) { String errorMessage = violation.getMessage(); Annotation annotation = violation.getConstraintDescriptor().getAnnotation(); if (annotation instanceof Constraints.ValidateWith) { Constraints.ValidateWith validateWithAnnotation = (Constraints.ValidateWith) annotation; if (violation.getMessage().equals(Constraints.ValidateWithValidator.defaultMessage)) { Constraints.ValidateWithValidator validateWithValidator = new Constraints.ValidateWithValidator(); validateWithValidator.initialize(validateWithAnnotation); Tuple<String, Object[]> errorMessageKey = validateWithValidator.getErrorMessageKey(); if (errorMessageKey != null && errorMessageKey._1 != null) { errorMessage = errorMessageKey._1; } } } return errorMessage; }
public void testRequiredAsBeanValidationAnnotation() throws Exception { DrivingLicence dl = new DrivingLicence(); dl.setType("X"); dl.setLevel(1); dl.setDescription(""); // This is annotated with @Required XPersistence.getManager().persist(dl); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Property", "description", v.getPropertyPath().toString()); assertEquals("Message text", "{required}", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
public void testPropertyValidatorsAsBeanValidationAnnotation() throws Exception { Product p = new Product(); p.setNumber(66); p.setDescription("MOTO"); p.setFamilyNumber(1); p.setSubfamilyNumber(1); p.setWarehouseKey(new Warehouse()); p.setUnitPrice(new BigDecimal("900")); XPersistence.getManager().persist(p); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Property", "description", v.getPropertyPath().toString()); assertEquals("Message text", "", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
public void testEntityValidatorsAsHibernateAnnotation() throws Exception { Product p = new Product(); p.setNumber(66); p.setDescription("BUENO, BONITO, BARATO"); // It's cheap ('BARATO') thus... p.setFamilyNumber(1); p.setSubfamilyNumber(1); p.setWarehouseKey(new Warehouse()); p.setUnitPrice(new BigDecimal("900")); // ... it cannot cost 900 (max 100) XPersistence.getManager().persist(p); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cex = (ConstraintViolationException) ex.getCause(); assertEquals("1 constraint violation expected", 1, cex.getConstraintViolations().size()); ConstraintViolation v = cex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Product", v.getRootBean().getClass().getSimpleName()); assertEquals("Message text", "", v.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
/** * Creates localized messages of all the constraint violations that has occured. * * <p>The key "<code>Qi4j_ConstraintViolation_<i><strong>CompositeType</strong></code></i>" * will be used to lookup the text formatting pattern from the ResourceBundle, where <strong> * <code><i>CompositeType</i></code></strong> is the class name of the Composite where the * constraint was violated. If such key does not exist, then the key "<code> * Qi4j_ConstraintViolation</code>" will be used, and if that one also doesn't exist, or the * resourceBundle argument is null, then the default patterns will be used; * * <table><tr><th>Type of Composite</th><th>Pattern used</th></tr> * <tr><td>Composite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in composite \n{0} of type {1}</code></td> * </tr> * <tr><td>EntityComposite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in entity {1}[id={0}]</code></td> * </tr> * <tr><td>ServiceComposite</td> * <td><code>Constraint Violation in {2}.{3} with constraint {4}, in service {0}</code></td> * </tr> * </table> * * Then format each ConstraintViolation according to such pattern, where the following argument * are passed; * * <table><tr><th>Arg</th><th>Value</th></tr> * <tr> * <td>{0}</td> * <td>Composite instance toString()</td> * </tr> * <tr> * <td>{1}</td> * <td>CompositeType class name</td> * </tr> * <tr> * <td>{2}</td> * <td>MixinType class name</td> * </tr> * <tr> * <td>{3}</td> * <td>MixinType method name</td> * </tr> * <tr> * <td>{4}</td> * <td>Annotation toString()</td> * </tr> * <tr> * <td>{5}</td> * <td>toString() of value passed as the argument, or "null" text if argument was null.</td> * </tr> * </table> * * <p><b>NOTE!!!</b> This class is still under construction and will be modified further. * * @param bundle The ResourceBundle for Localization, or null if default formatting and locale to * be used. * @return An array of localized messages of the violations incurred. */ public String[] localizedMessagesFrom(ResourceBundle bundle) { String pattern = "Constraint violation in {0}.{1} for method ''{3}'' with constraint \"{4}({6})\", for value ''{5}''"; ArrayList<String> list = new ArrayList<String>(); for (ConstraintViolation violation : constraintViolations) { Locale locale; if (bundle != null) { try { pattern = bundle.getString("qi4j.constraint." + mixinTypeName + "." + methodName); } catch (MissingResourceException e1) { try { pattern = bundle.getString("qi4j.constraint"); } catch (MissingResourceException e2) { // ignore. The default pattern will be used. } } locale = bundle.getLocale(); } else { locale = Locale.getDefault(); } MessageFormat format = new MessageFormat(pattern, locale); Annotation annotation = violation.constraint(); String name = violation.name(); Object value = violation.value(); String classes; if (Iterables.count(instanceTypes) == 1) { classes = Iterables.first(instanceTypes).getSimpleName(); } else { classes = "[" + Iterables.<Class<?>>toString( instanceTypes, new Function<Class<?>, String>() { @Override public String map(Class<?> from) { return from.getSimpleName(); } }, ",") + "]"; } Object[] args = new Object[] { instanceToString, classes, mixinTypeName, methodName, annotation.toString(), "" + value, name }; StringBuffer text = new StringBuffer(); format.format(args, text, null); list.add(text.toString()); } String[] result = new String[list.size()]; list.toArray(result); return result; }
/** * Binds data to this form - that is, handles form submission. * * @param data data to submit * @return a copy of this form filled with the new data */ @SuppressWarnings("unchecked") public Form<T> bind(Map<String, String> data, String... allowedFields) { DataBinder dataBinder; Map<String, String> objectData = data; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); objectData = new HashMap<>(); for (String key : data.keySet()) { if (key.startsWith(rootName + ".")) { objectData.put(key.substring(rootName.length() + 1), data.get(key)); } } } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(play.data.validation.Validation.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); final Map<String, String> objectDataFinal = objectData; withRequestLocale( () -> { dataBinder.bind(new MutablePropertyValues(objectDataFinal)); return null; }); Set<ConstraintViolation<Object>> validationErrors; if (groups != null) { validationErrors = validator.validate(dataBinder.getTarget(), groups); } else { validationErrors = validator.validate(dataBinder.getTarget()); } BindingResult result = dataBinder.getBindingResult(); for (ConstraintViolation<Object> violation : validationErrors) { String field = violation.getPropertyPath().toString(); FieldError fieldError = result.getFieldError(field); if (fieldError == null || !fieldError.isBindingFailure()) { try { result.rejectValue( field, violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), getArgumentsForConstraint( result.getObjectName(), field, violation.getConstraintDescriptor()), getMessageForConstraintViolation(violation)); } catch (NotReadablePropertyException ex) { throw new IllegalStateException( "JSR-303 validated property '" + field + "' does not have a corresponding accessor for data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex); } } } if (result.hasErrors() || result.getGlobalErrorCount() > 0) { Map<String, List<ValidationError>> errors = new HashMap<>(); for (FieldError error : result.getFieldErrors()) { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } if (!errors.containsKey(key)) { errors.put(key, new ArrayList<>()); } ValidationError validationError; if (error.isBindingFailure()) { ImmutableList.Builder<String> builder = ImmutableList.builder(); Optional<Messages> msgs = Optional.of(Http.Context.current.get()).map(c -> messagesApi.preferred(c.request())); for (String code : error.getCodes()) { code = code.replace("typeMismatch", "error.invalid"); if (!msgs.isPresent() || msgs.get().isDefinedAt(code)) { builder.add(code); } } validationError = new ValidationError( key, builder.build().reverse(), convertErrorArguments(error.getArguments())); } else { validationError = new ValidationError( key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); } errors.get(key).add(validationError); } List<ValidationError> globalErrors = new ArrayList<>(); for (ObjectError error : result.getGlobalErrors()) { globalErrors.add( new ValidationError( "", error.getDefaultMessage(), convertErrorArguments(error.getArguments()))); } if (!globalErrors.isEmpty()) { errors.put("", globalErrors); } return new Form( rootName, backedType, data, errors, Optional.empty(), groups, messagesApi, formatters); } else { Object globalError = null; if (result.getTarget() != null) { try { java.lang.reflect.Method v = result.getTarget().getClass().getMethod("validate"); globalError = v.invoke(result.getTarget()); } catch (NoSuchMethodException e) { // do nothing } catch (Throwable e) { throw new RuntimeException(e); } } if (globalError != null) { Map<String, List<ValidationError>> errors = new HashMap<>(); if (globalError instanceof String) { errors.put("", new ArrayList<>()); errors.get("").add(new ValidationError("", (String) globalError, new ArrayList())); } else if (globalError instanceof List) { for (ValidationError error : (List<ValidationError>) globalError) { List<ValidationError> errorsForKey = errors.get(error.key()); if (errorsForKey == null) { errors.put(error.key(), errorsForKey = new ArrayList<>()); } errorsForKey.add(error); } } else if (globalError instanceof Map) { errors = (Map<String, List<ValidationError>>) globalError; } return new Form( rootName, backedType, data, errors, Optional.empty(), groups, messagesApi, formatters); } return new Form( rootName, backedType, new HashMap<>(data), new HashMap<>(errors), Optional.ofNullable((T) result.getTarget()), groups, messagesApi, formatters); } }
public static Map<String, String> toRequestMap( PaypalClassicModel paypalClassicModel, String format) { Map<String, String> returnMap = new HashMap<>(); if (paypalClassicModel == null) { return returnMap; } ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); // ValidatorFactory factory = Validation.byDefaultProvider() // .configure() // .messageInterpolator( new MyMessageInterpolator() ) // .buildValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<PaypalClassicModel>> violations = validator.validate(paypalClassicModel); if (violations.size() == 0) { try { for (Field field : FieldUtils.getAllFields(paypalClassicModel.getClass())) { if (field.getType().isAssignableFrom(String.class)) { if (BeanUtils.getProperty(paypalClassicModel, field.getName()) != null) { if (StringUtils.isNumeric(format)) { returnMap.put( field.getName().toUpperCase() + format, BeanUtils.getProperty(paypalClassicModel, field.getName())); } else { returnMap.put( getFormatedKeyName(format, field.getName(), null), BeanUtils.getProperty(paypalClassicModel, field.getName())); } } } if (PaypalClassicModel.class.isAssignableFrom(field.getType())) { if (PropertyUtils.getProperty(paypalClassicModel, field.getName()) != null) { returnMap.putAll( toRequestMap( (PaypalClassicModel) PropertyUtils.getProperty(paypalClassicModel, field.getName()), format)); } } if (List.class.isAssignableFrom(field.getType())) { List listItem = (List) PropertyUtils.getProperty(paypalClassicModel, field.getName()); if (listItem != null) { for (int i = 0; i < listItem.size(); i++) { if (listItem.get(i) instanceof PaypalClassicModel) { PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class); if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) { String formatStr = field.getAnnotation(PaypalCollection.class).format(); returnMap.putAll( toRequestMap( (PaypalClassicModel) listItem.get(i), getFormatedKeyName(formatStr, null, i))); } else { if (StringUtils.isNoneEmpty(format)) { returnMap.putAll( toRequestMap( (PaypalClassicModel) listItem.get(i), format + String.valueOf(i))); } else { returnMap.putAll( toRequestMap((PaypalClassicModel) listItem.get(i), String.valueOf(i))); } } } if (listItem.get(i) instanceof List) { PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class); if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) { String formatStr = field.getAnnotation(PaypalCollection.class).format(); returnMap.putAll( toRequestMap( (List) listItem.get(i), getFormatedKeyName(formatStr, null, i))); } else { if (StringUtils.isNoneEmpty(format)) { returnMap.putAll( toRequestMap((List) listItem.get(i), format + String.valueOf(i))); } else { returnMap.putAll(toRequestMap((List) listItem.get(i), String.valueOf(i))); } } } if (listItem.get(i) instanceof String) { PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class); if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) { String formatStr = paypalCollection.format(); formatStr = getFormatedKeyName(formatStr, field.getName(), i); returnMap.put( getFormatedKeyName(format, formatStr, null), listItem.get(i).toString()); } else { returnMap.put( getFormatedKeyName(format, field.getName(), null) + i, listItem.get(i).toString()); } } } } } } } catch (IllegalAccessException e) { throw new ValidationException(e.getMessage()); } catch (InvocationTargetException e) { throw new ValidationException(e.getMessage()); } catch (NoSuchMethodException e) { throw new ValidationException(e.getMessage()); } } else { StringBuffer buf = new StringBuffer(); for (ConstraintViolation<PaypalClassicModel> violation : violations) { buf.append(violation.getMessage() + "\n"); } throw new ValidationException(buf.toString()); } return returnMap; }
/** * Binds data to this form - that is, handles form submission. * * @param data data to submit * @return a copy of this form filled with the new data */ public Form<T> bind(Map<String, String> data, String... allowedFields) { DataBinder dataBinder = null; Map<String, String> objectData = data; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); objectData = new HashMap<String, String>(); for (String key : data.keySet()) { if (key.startsWith(rootName + ".")) { objectData.put(key.substring(rootName.length() + 1), data.get(key)); } } } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(play.data.validation.Validation.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(play.data.format.Formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); dataBinder.bind(new MutablePropertyValues(objectData)); Set<ConstraintViolation<Object>> validationErrors; if (groups != null) { validationErrors = validator.validate(dataBinder.getTarget(), groups); } else { validationErrors = validator.validate(dataBinder.getTarget()); } BindingResult result = dataBinder.getBindingResult(); for (ConstraintViolation<Object> violation : validationErrors) { String field = violation.getPropertyPath().toString(); FieldError fieldError = result.getFieldError(field); if (fieldError == null || !fieldError.isBindingFailure()) { try { result.rejectValue( field, violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), getArgumentsForConstraint( result.getObjectName(), field, violation.getConstraintDescriptor()), violation.getMessage()); } catch (NotReadablePropertyException ex) { throw new IllegalStateException( "JSR-303 validated property '" + field + "' does not have a corresponding accessor for data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex); } } } if (result.hasErrors()) { Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>(); for (FieldError error : result.getFieldErrors()) { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } List<Object> arguments = new ArrayList<Object>(); for (Object arg : error.getArguments()) { if (!(arg instanceof org.springframework.context.support.DefaultMessageSourceResolvable)) { arguments.add(arg); } } if (!errors.containsKey(key)) { errors.put(key, new ArrayList<ValidationError>()); } errors .get(key) .add( new ValidationError( key, error.isBindingFailure() ? "error.invalid" : error.getDefaultMessage(), arguments)); } return new Form(rootName, backedType, data, errors, None(), groups); } else { Object globalError = null; if (result.getTarget() != null) { try { java.lang.reflect.Method v = result.getTarget().getClass().getMethod("validate"); globalError = v.invoke(result.getTarget()); } catch (NoSuchMethodException e) { } catch (Throwable e) { throw new RuntimeException(e); } } if (globalError != null) { Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>(); if (globalError instanceof String) { errors.put("", new ArrayList<ValidationError>()); errors.get("").add(new ValidationError("", (String) globalError, new ArrayList())); } else if (globalError instanceof List) { errors.put("", (List<ValidationError>) globalError); } else if (globalError instanceof Map) { errors = (Map<String, List<ValidationError>>) globalError; } return new Form(rootName, backedType, data, errors, None(), groups); } return new Form( rootName, backedType, new HashMap<String, String>(data), new HashMap<String, List<ValidationError>>(errors), Some((T) result.getTarget()), groups); } }