From: Henri Sara Date: Thu, 22 Dec 2011 10:56:51 +0000 (+0200) Subject: Rename BeanValidationValidator to BeanValidator based on review. X-Git-Tag: 7.0.0.alpha1~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=29d701264725864662cabe39ea8ff8eee17a0830;p=vaadin-framework.git Rename BeanValidationValidator to BeanValidator based on review. --- diff --git a/src/com/vaadin/data/fieldbinder/BeanFieldGroup.java b/src/com/vaadin/data/fieldbinder/BeanFieldGroup.java index ab1e1a5ab2..3cb295bb2a 100644 --- a/src/com/vaadin/data/fieldbinder/BeanFieldGroup.java +++ b/src/com/vaadin/data/fieldbinder/BeanFieldGroup.java @@ -7,7 +7,7 @@ import java.lang.reflect.Method; import com.vaadin.data.Item; import com.vaadin.data.util.BeanItem; -import com.vaadin.data.validator.BeanValidationValidator; +import com.vaadin.data.validator.BeanValidator; import com.vaadin.ui.Field; public class BeanFieldGroup extends FieldGroup { @@ -120,7 +120,7 @@ public class BeanFieldGroup extends FieldGroup { super.configureField(field); // Add Bean validators if there are annotations if (isBeanValidationImplementationAvailable()) { - BeanValidationValidator validator = new BeanValidationValidator( + BeanValidator validator = new BeanValidator( beanType, getPropertyId(field).toString()); field.addValidator(validator); if (field.getLocale() != null) { diff --git a/src/com/vaadin/data/validator/BeanValidationValidator.java b/src/com/vaadin/data/validator/BeanValidationValidator.java deleted file mode 100644 index 90f330c11d..0000000000 --- a/src/com/vaadin/data/validator/BeanValidationValidator.java +++ /dev/null @@ -1,173 +0,0 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.data.validator; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.MessageInterpolator.Context; -import javax.validation.Validation; -import javax.validation.ValidatorFactory; -import javax.validation.metadata.ConstraintDescriptor; - -import com.vaadin.data.Validator; - -/** - * Vaadin {@link Validator} using the JSR-303 (javax.validation) - * annotation-based bean validation. - * - * The annotations of the fields of the beans are used to determine the - * validation to perform. - * - * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean - * Validation - formerly agimatec validation) must be present on the project - * classpath when using bean validation. - * - * @since 7.0 - * - * @author Petri Hakala - * @author Henri Sara - */ -public class BeanValidationValidator implements Validator { - - private static final long serialVersionUID = 1L; - private static ValidatorFactory factory; - - private transient javax.validation.Validator javaxBeanValidator; - private String propertyName; - private Class beanClass; - private Locale locale; - - /** - * Simple implementation of a message interpolator context that returns - * fixed values. - */ - protected static class SimpleContext implements Context, Serializable { - - private final Object value; - private final ConstraintDescriptor descriptor; - - /** - * Create a simple immutable message interpolator context. - * - * @param value - * value being validated - * @param descriptor - * ConstraintDescriptor corresponding to the constraint being - * validated - */ - public SimpleContext(Object value, ConstraintDescriptor descriptor) { - this.value = value; - this.descriptor = descriptor; - } - - public ConstraintDescriptor getConstraintDescriptor() { - return descriptor; - } - - public Object getValidatedValue() { - return value; - } - - } - - /** - * Creates a Vaadin {@link Validator} utilizing JSR-303 bean validation. - * - * @param beanClass - * bean class based on which the validation should be performed - * @param propertyName - * property to validate - */ - public BeanValidationValidator(Class beanClass, String propertyName) { - this.beanClass = beanClass; - this.propertyName = propertyName; - locale = Locale.getDefault(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Validator#validate(java.lang.Object) - */ - public void validate(final Object value) throws InvalidValueException { - Set violations = getJavaxBeanValidator().validateValue(beanClass, - propertyName, value); - if (violations.size() > 0) { - List exceptions = new ArrayList(); - for (Object v : violations) { - final ConstraintViolation violation = (ConstraintViolation) v; - String msg = getJavaxBeanValidatorFactory() - .getMessageInterpolator().interpolate( - violation.getMessageTemplate(), - new SimpleContext(value, violation - .getConstraintDescriptor()), locale); - exceptions.add(msg); - } - StringBuilder b = new StringBuilder(); - for (int i = 0; i < exceptions.size(); i++) { - if (i != 0) { - b.append("
"); - } - b.append(exceptions.get(i)); - } - throw new InvalidValueException(b.toString()); - } - } - - /** - * Sets the locale used for validation error messages. - * - * Revalidation is not automatically triggered by setting the locale. - * - * @param locale - */ - public void setLocale(Locale locale) { - this.locale = locale; - } - - /** - * Gets the locale used for validation error messages. - * - * @return locale used for validation - */ - public Locale getLocale() { - return locale; - } - - /** - * Returns the underlying JSR-303 bean validator factory used. A factory is - * created using {@link Validation} if necessary. - * - * @return {@link ValidatorFactory} to use - */ - protected static ValidatorFactory getJavaxBeanValidatorFactory() { - if (factory == null) { - factory = Validation.buildDefaultValidatorFactory(); - } - - return factory; - } - - /** - * Returns a shared Validator instance to use. An instance is created using - * the validator factory if necessary and thereafter reused by the - * {@link BeanValidationValidator} instance. - * - * @return the JSR-303 {@link javax.validation.Validator} to use - */ - protected javax.validation.Validator getJavaxBeanValidator() { - if (javaxBeanValidator == null) { - javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator(); - } - - return javaxBeanValidator; - } - -} \ No newline at end of file diff --git a/src/com/vaadin/data/validator/BeanValidator.java b/src/com/vaadin/data/validator/BeanValidator.java new file mode 100644 index 0000000000..939fd2e9c4 --- /dev/null +++ b/src/com/vaadin/data/validator/BeanValidator.java @@ -0,0 +1,173 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.data.validator; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.MessageInterpolator.Context; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.metadata.ConstraintDescriptor; + +import com.vaadin.data.Validator; + +/** + * Vaadin {@link Validator} using the JSR-303 (javax.validation) + * annotation-based bean validation. + * + * The annotations of the fields of the beans are used to determine the + * validation to perform. + * + * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean + * Validation - formerly agimatec validation) must be present on the project + * classpath when using bean validation. + * + * @since 7.0 + * + * @author Petri Hakala + * @author Henri Sara + */ +public class BeanValidator implements Validator { + + private static final long serialVersionUID = 1L; + private static ValidatorFactory factory; + + private transient javax.validation.Validator javaxBeanValidator; + private String propertyName; + private Class beanClass; + private Locale locale; + + /** + * Simple implementation of a message interpolator context that returns + * fixed values. + */ + protected static class SimpleContext implements Context, Serializable { + + private final Object value; + private final ConstraintDescriptor descriptor; + + /** + * Create a simple immutable message interpolator context. + * + * @param value + * value being validated + * @param descriptor + * ConstraintDescriptor corresponding to the constraint being + * validated + */ + public SimpleContext(Object value, ConstraintDescriptor descriptor) { + this.value = value; + this.descriptor = descriptor; + } + + public ConstraintDescriptor getConstraintDescriptor() { + return descriptor; + } + + public Object getValidatedValue() { + return value; + } + + } + + /** + * Creates a Vaadin {@link Validator} utilizing JSR-303 bean validation. + * + * @param beanClass + * bean class based on which the validation should be performed + * @param propertyName + * property to validate + */ + public BeanValidator(Class beanClass, String propertyName) { + this.beanClass = beanClass; + this.propertyName = propertyName; + locale = Locale.getDefault(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.Validator#validate(java.lang.Object) + */ + public void validate(final Object value) throws InvalidValueException { + Set violations = getJavaxBeanValidator().validateValue(beanClass, + propertyName, value); + if (violations.size() > 0) { + List exceptions = new ArrayList(); + for (Object v : violations) { + final ConstraintViolation violation = (ConstraintViolation) v; + String msg = getJavaxBeanValidatorFactory() + .getMessageInterpolator().interpolate( + violation.getMessageTemplate(), + new SimpleContext(value, violation + .getConstraintDescriptor()), locale); + exceptions.add(msg); + } + StringBuilder b = new StringBuilder(); + for (int i = 0; i < exceptions.size(); i++) { + if (i != 0) { + b.append("
"); + } + b.append(exceptions.get(i)); + } + throw new InvalidValueException(b.toString()); + } + } + + /** + * Sets the locale used for validation error messages. + * + * Revalidation is not automatically triggered by setting the locale. + * + * @param locale + */ + public void setLocale(Locale locale) { + this.locale = locale; + } + + /** + * Gets the locale used for validation error messages. + * + * @return locale used for validation + */ + public Locale getLocale() { + return locale; + } + + /** + * Returns the underlying JSR-303 bean validator factory used. A factory is + * created using {@link Validation} if necessary. + * + * @return {@link ValidatorFactory} to use + */ + protected static ValidatorFactory getJavaxBeanValidatorFactory() { + if (factory == null) { + factory = Validation.buildDefaultValidatorFactory(); + } + + return factory; + } + + /** + * Returns a shared Validator instance to use. An instance is created using + * the validator factory if necessary and thereafter reused by the + * {@link BeanValidator} instance. + * + * @return the JSR-303 {@link javax.validation.Validator} to use + */ + protected javax.validation.Validator getJavaxBeanValidator() { + if (javaxBeanValidator == null) { + javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator(); + } + + return javaxBeanValidator; + } + +} \ No newline at end of file diff --git a/tests/server-side/com/vaadin/tests/server/validation/TestBeanValidation.java b/tests/server-side/com/vaadin/tests/server/validation/TestBeanValidation.java index 9111ab9a4b..8f6928fc35 100644 --- a/tests/server-side/com/vaadin/tests/server/validation/TestBeanValidation.java +++ b/tests/server-side/com/vaadin/tests/server/validation/TestBeanValidation.java @@ -3,56 +3,54 @@ package com.vaadin.tests.server.validation; import org.junit.Test; import com.vaadin.data.Validator.InvalidValueException; -import com.vaadin.data.validator.BeanValidationValidator; +import com.vaadin.data.validator.BeanValidator; import com.vaadin.tests.data.bean.BeanToValidate; public class TestBeanValidation { @Test(expected = InvalidValueException.class) public void testBeanValidationNull() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "firstname"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "firstname"); validator.validate(null); } @Test(expected = InvalidValueException.class) public void testBeanValidationStringTooShort() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "firstname"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "firstname"); validator.validate("aa"); } @Test public void testBeanValidationStringOk() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "firstname"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "firstname"); validator.validate("aaa"); } @Test(expected = InvalidValueException.class) public void testBeanValidationIntegerTooSmall() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "age"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, "age"); validator.validate(17); } @Test public void testBeanValidationIntegerOk() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "age"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, "age"); validator.validate(18); } @Test(expected = InvalidValueException.class) public void testBeanValidationTooManyDigits() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "decimals"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "decimals"); validator.validate("1234.567"); } @Test public void testBeanValidationDigitsOk() { - BeanValidationValidator validator = new BeanValidationValidator( - BeanToValidate.class, "decimals"); + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "decimals"); validator.validate("123.45"); }