You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by ro...@apache.org on 2010/05/04 18:46:15 UTC
svn commit: r940944 - in /incubator/bval/trunk/bval-jsr303/src:
main/java/org/apache/bval/jsr303/ConstraintValidation.java
test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java
Author: romanstumm
Date: Tue May 4 16:46:14 2010
New Revision: 940944
URL: http://svn.apache.org/viewvc?rev=940944&view=rev
Log:
BVAL-36 Improve @ReportAsSingleValidation implementation - from Carlos Varla
Modified:
incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java
incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java
Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java?rev=940944&r1=940943&r2=940944&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java (original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java Tue May 4 16:46:14 2010
@@ -137,6 +137,10 @@ public class ConstraintValidation<T exte
} finally {
gctx.setListener(oldListener);
}
+
+ // Restore current constraint validation
+ context.setConstraintValidation(this);
+
// stop validating when already failed and ReportAsSingleInvalidConstraint = true ?
if (!listener.getConstaintViolations().isEmpty()) {
// TODO RSt - how should the composed constraint error report look like?
@@ -149,10 +153,10 @@ public class ConstraintValidation<T exte
for (ConstraintValidation composed : getComposingValidations()) {
composed.validate(context);
}
+
+ // Restore current constraint validation
+ context.setConstraintValidation(this);
}
-
- // Restore current constraint validation
- context.setConstraintValidation(this);
if (validator != null) {
ConstraintValidatorContextImpl jsrContext =
@@ -277,6 +281,9 @@ public class ConstraintValidation<T exte
}
public List<Class<? extends ConstraintValidator<T, ?>>> getConstraintValidatorClasses() {
+ if ( validatorClasses == null ) {
+ return Collections.emptyList();
+ }
return Arrays.asList(validatorClasses);
}
Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java?rev=940944&r1=940943&r2=940944&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java (original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintCompositionTest.java Tue May 4 16:46:14 2010
@@ -24,6 +24,7 @@ import junit.framework.TestCase;
import javax.validation.*;
import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.validation.metadata.ConstraintDescriptor;
import java.lang.annotation.Documented;
@@ -165,6 +166,25 @@ public class ConstraintCompositionTest e
violation = constraintViolations.iterator().next();
Assert.assertEquals("Wrong violation", "Id is too long", violation.getMessage());
}
+
+ /**
+ * Checks that errors are reported correctly when using
+ * {@link ReportAsSingleViolation}.
+ */
+ public void testReportAsAsingleViolation() {
+ Validator validator = getValidator();
+
+ Code c = new Code();
+ c.code = "very invalid code";
+ Set<ConstraintViolation<Code>> constraintViolations = validator.validate(c);
+
+ // Only 1 error expected
+ Assert.assertEquals("Only 1 violation expected", 1, constraintViolations.size());
+ ConstraintViolation<Code> violation = constraintViolations.iterator().next();
+ Assert.assertEquals("Wrong violation message", "Invalid code", violation.getMessage());
+ Assert.assertEquals("Wrong violation type", ElevenDigitsCode.class, violation.getConstraintDescriptor().getAnnotation().annotationType());
+
+ }
public static class Person {
@PersonName
@@ -179,6 +199,11 @@ public class ConstraintCompositionTest e
String name;
}
+ public static class Code {
+ @ElevenDigitsCode
+ String code;
+ }
+
@NotNull(message="A person needs a non null name", groups={Group1.class}, payload={})
@Constraint(validatedBy = {})
@Documented
@@ -217,7 +242,19 @@ public class ConstraintCompositionTest e
@OverridesAttribute(constraint=Size.class,constraintIndex=1,name="min")
int minSize() default 0;
-
+ }
+
+ @Size(min=11, max=11)
+ @Pattern(regexp="\\d*")
+ @Constraint(validatedBy = {})
+ @ReportAsSingleViolation
+ @Documented
+ @Target({ METHOD, FIELD, TYPE })
+ @Retention(RUNTIME)
+ public static @interface ElevenDigitsCode {
+ String message() default "Invalid code";
+ Class<?>[] groups() default { };
+ Class<? extends Payload>[] payload() default {};
}
public static interface Group1 {