You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by si...@apache.org on 2010/05/11 17:44:32 UTC
svn commit: r943148 - in /incubator/bval/sandbox/guice-integration/src:
main/java/org/apache/bval/extentions/guice/
test/java/org/apache/bval/extentions/guice/
Author: simonetripodi
Date: Tue May 11 15:44:32 2010
New Revision: 943148
URL: http://svn.apache.org/viewvc?rev=943148&view=rev
Log:
integrated Carlos' Vara hints
- methods arguments are validated through the MethodValidator;
- methods returned values are validated through the MethodValidator.
Modified:
incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/Validate.java
incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/ValidateMethodInterceptor.java
incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/DummyCountryDao.java
incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/GuiceAwareValidationTestCase.java
Modified: incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/Validate.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/Validate.java?rev=943148&r1=943147&r2=943148&view=diff
==============================================================================
--- incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/Validate.java (original)
+++ incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/Validate.java Tue May 11 15:44:32 2010
@@ -30,9 +30,21 @@ import com.google.inject.BindingAnnotati
*/
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.METHOD)
+@Target({ ElementType.METHOD })
public @interface Validate {
+ /**
+ * TODO fill me
+ *
+ * @return
+ */
Class<?>[] groups() default {};
+ /**
+ * TODO fill me
+ *
+ * @return
+ */
+ boolean validateReturnedValue() default false;
+
}
Modified: incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/ValidateMethodInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/ValidateMethodInterceptor.java?rev=943148&r1=943147&r2=943148&view=diff
==============================================================================
--- incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/ValidateMethodInterceptor.java (original)
+++ incubator/bval/sandbox/guice-integration/src/main/java/org/apache/bval/extentions/guice/ValidateMethodInterceptor.java Tue May 11 15:44:32 2010
@@ -15,6 +15,8 @@
*/
package org.apache.bval.extentions.guice;
+import java.lang.reflect.Method;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@@ -24,6 +26,7 @@ import javax.validation.Validator;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
+import org.apache.bval.jsr303.extensions.MethodValidator;
import com.google.inject.Inject;
@@ -53,19 +56,40 @@ public final class ValidateMethodInterce
* {@inheritDoc}
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
- Set<ConstraintViolation<?>> constraintViolations = new HashSet<ConstraintViolation<?>>();
+ Validate validate = invocation.getMethod().getAnnotation(Validate.class);
+ MethodValidator methodValidator = this.validator.unwrap(MethodValidator.class);
- Class<?>[] groups = invocation.getMethod().getAnnotation(Validate.class).groups();
+ Set<ConstraintViolation<?>> constraintViolations = new HashSet<ConstraintViolation<?>>();
+ Class<?> clazz = invocation.getMethod().getDeclaringClass();
+ Method method = invocation.getMethod();
+ Object[] arguments = invocation.getArguments();
+ Class<?>[] groups = validate.groups();
+
+ constraintViolations.addAll(methodValidator.validateParameters(clazz,
+ method,
+ arguments,
+ groups));
- for (Object arg : invocation.getArguments()) {
- constraintViolations.addAll(this.validator.validate(arg, groups));
+ if (!constraintViolations.isEmpty()) {
+ throw new ConstraintViolationException("Validation error when calling method '"
+ + method
+ + "' with arguments "
+ + Arrays.deepToString(arguments), constraintViolations);
}
- if (!constraintViolations.isEmpty()) {
- throw new ConstraintViolationException(constraintViolations);
+ Object returnedValue = invocation.proceed();
+
+ if (validate.validateReturnedValue()) {
+ constraintViolations.addAll(methodValidator.validateReturnedValue(clazz, method, returnedValue, groups));
+ if (!constraintViolations.isEmpty()) {
+ throw new ConstraintViolationException("Method '"
+ + method
+ + "' returned a not valid value "
+ + returnedValue, constraintViolations);
+ }
}
- return invocation.proceed();
+ return returnedValue;
}
}
Modified: incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/DummyCountryDao.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/DummyCountryDao.java?rev=943148&r1=943147&r2=943148&view=diff
==============================================================================
--- incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/DummyCountryDao.java (original)
+++ incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/DummyCountryDao.java Tue May 11 15:44:32 2010
@@ -16,6 +16,9 @@
*/
package org.apache.bval.extentions.guice;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
/**
*
*
@@ -23,9 +26,18 @@ package org.apache.bval.extentions.guice
*/
public class DummyCountryDao {
- @Validate(groups = { Insert.class })
- public void insertCountry(Country country) {
- // do nothing
+ @Validate(
+ groups = { Insert.class },
+ validateReturnedValue = true
+ )
+ public Country insertCountry(@NotNull(groups = { Insert.class }) String name,
+ @NotNull(groups = { Insert.class }) @Size(max = 2, groups = { Insert.class, Update.class }) String iso2Code,
+ @NotNull(groups = { Insert.class }) @Size(max = 3, groups = { Insert.class, Update.class }) String iso3Code) {
+ Country country = new Country();
+ country.setName(name);
+ country.setIso2Code(iso2Code);
+ country.setIso3Code(iso3Code);
+ return country;
}
}
Modified: incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/GuiceAwareValidationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/GuiceAwareValidationTestCase.java?rev=943148&r1=943147&r2=943148&view=diff
==============================================================================
--- incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/GuiceAwareValidationTestCase.java (original)
+++ incubator/bval/sandbox/guice-integration/src/test/java/org/apache/bval/extentions/guice/GuiceAwareValidationTestCase.java Tue May 11 15:44:32 2010
@@ -66,13 +66,12 @@ public final class GuiceAwareValidationT
}
public void testAOPInjectedValidation() {
- Country country = new Country();
- country.setName("Italy");
- country.setIso2Code("ita");
- country.setIso3Code("ita");
+ this.dummyCountryDao.insertCountry("Italy", "it", "ita");
+ }
+ public void testAOPInjectedFailedValidation() {
try {
- this.dummyCountryDao.insertCountry(country);
+ this.dummyCountryDao.insertCountry("Italy", "ita", "ita");
fail("javax.validation.ConstraintViolationException expected");
} catch (ConstraintViolationException cve) {
// do nothing