Author: hardy.ferentschik
Date: 2009-06-11 06:24:15 -0400 (Thu, 11 Jun 2009)
New Revision: 16759
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java
Log:
HV-167 NotEmpty is now a composition of @NotNull and @Size
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/NotEmpty.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -24,16 +24,20 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
+import javax.validation.ReportAsSingleViolation;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
-import org.hibernate.validation.constraints.impl.NotEmptyValidator;
-
/**
* @author Emmanuel Bernard
*/
@Documented
-@Constraint(validatedBy = NotEmptyValidator.class)
+@Constraint(validatedBy = {})
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
+@ReportAsSingleViolation
+@NotNull
+@Size(min=1)
public @interface NotEmpty {
String message() default
"{org.hibernate.validation.constraints.NotEmpty.message}";
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -107,26 +107,33 @@
* @param <V> Type of the value to be validated.
*/
public <T, V> void validateConstraints(V value, Type type,
ExecutionContext<T> executionContext, List<ConstraintViolation<T>>
constraintViolations) {
+ // first validate composing constraints
for ( ConstraintTree<?> tree : getChildren() ) {
tree.validateConstraints( value, type, executionContext, constraintViolations );
}
- if ( log.isTraceEnabled() ) {
- log.trace( "Validating value {} against constraint defined by {}", value,
descriptor );
- }
- ConstraintValidator<A, V> validator = getInitalizedValidator(
- value, type, executionContext.getConstraintValidatorFactory()
- );
ConstraintValidatorContextImpl constraintValidatorContext = new
ConstraintValidatorContextImpl(
executionContext.peekParentPath(), executionContext.peekProperty(), descriptor
);
- if ( !validator.isValid( value, constraintValidatorContext ) ) {
- constraintViolations.addAll(
- executionContext.createConstraintViolations(
- value, constraintValidatorContext
- )
+
+ // we could have a composing constraint which does not need its own validator.
+ if ( !descriptor.getConstraintValidatorClasses().isEmpty() ) {
+ if ( log.isTraceEnabled() ) {
+ log.trace( "Validating value {} against constraint defined by {}", value,
descriptor );
+ }
+ ConstraintValidator<A, V> validator = getInitalizedValidator(
+ value, type, executionContext.getConstraintValidatorFactory()
);
+
+ if ( !validator.isValid( value, constraintValidatorContext ) ) {
+ constraintViolations.addAll(
+ executionContext.createConstraintViolations(
+ value, constraintValidatorContext
+ )
+ );
+ }
}
+
if ( reportAsSingleViolation() && constraintViolations.size() > 0 ) {
constraintViolations.clear();
final String message = ( String ) getParent().getDescriptor().getAttributes().get(
"message" );
@@ -150,6 +157,7 @@
*
* @return A initalized constraint validator matching the type of the value to be
validated.
*/
+ @SuppressWarnings("unchecked")
private <V> ConstraintValidator<A, V> getInitalizedValidator(V value, Type
type, ConstraintValidatorFactory constraintFactory) {
Class<? extends ConstraintValidator<?, ?>> validatorClass =
findMatchingValidatorClass( value, type );
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -75,7 +75,7 @@
Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
ConstraintViolation<Customer> constraintViolation =
constraintViolations.iterator().next();
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ assertEquals( "may not be empty", constraintViolation.getMessage(),
"Wrong message" );
// get a new factory using a custom configuration
configuration = Validation.byDefaultProvider().configure();
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/Person.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -17,21 +17,17 @@
*/
package org.hibernate.validation.engine;
-import javax.validation.constraints.NotNull;
-
import org.hibernate.validation.constraints.NotEmpty;
/**
* @author Hardy Ferentschik
*/
public interface Person {
- @NotNull
@NotEmpty
String getFirstName();
String getMiddleName();
- @NotNull
@NotEmpty
String getLastName();
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Author.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -32,7 +32,6 @@
@NotEmpty(groups = Last.class)
private String firstName;
- @NotNull(groups = First.class)
@NotEmpty(groups = First.class)
private String lastName;
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/Book.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -31,7 +31,6 @@
* @author Hardy Ferentschik
*/
public class Book {
- @NotNull(groups = First.class)
@NotEmpty(groups = First.class)
private String title;
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java 2009-06-11
10:23:04 UTC (rev 16758)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/groups/GroupTest.java 2009-06-11
10:24:15 UTC (rev 16759)
@@ -118,7 +118,7 @@
constraintViolations = validator.validate( book, Book.All.class );
ConstraintViolation constraintViolation = constraintViolations.iterator().next();
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ assertEquals( "may not be empty", constraintViolation.getMessage(),
"Wrong message" );
assertEquals( constraintViolation.getRootBean(), book, "Wrong root entity"
);
assertEquals( constraintViolation.getInvalidValue(), book.getTitle(), "Wrong
value" );
assertEquals( "title", constraintViolation.getPropertyPath(), "Wrong
propertyName" );