Author: epbernard
Date: 2008-10-31 11:28:23 -0400 (Fri, 31 Oct 2008)
New Revision: 15466
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintViolationImpl.java
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.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/ValidatorImplTest.java
validator/trunk/validation-api/src/main/java/javax/validation/Validator.java
Log:
BVAL-40 rename ConstraintViolation
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java 2008-10-31
12:26:41 UTC (rev 15465)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -24,7 +24,7 @@
import java.util.Set;
import java.util.Stack;
-import org.hibernate.validation.impl.InvalidConstraintImpl;
+import org.hibernate.validation.impl.ConstraintViolationImpl;
import org.hibernate.validation.util.IdentitySet;
/**
@@ -55,7 +55,7 @@
/**
* A list of all failing constraints so far.
*/
- private final List<InvalidConstraintImpl<T>> failingConstraints;
+ private final List<ConstraintViolationImpl<T>> failingConstraintViolations;
/**
* The current property path.
@@ -82,7 +82,7 @@
validatedobjectStack.push( object );
processedObjects = new HashMap<String, IdentitySet>();
propertyPath = "";
- failingConstraints = new ArrayList<InvalidConstraintImpl<T>>();
+ failingConstraintViolations = new ArrayList<ConstraintViolationImpl<T>>();
}
public Object peekValidatedObject() {
@@ -125,18 +125,18 @@
return objectsProcessedInCurrentGroups != null &&
objectsProcessedInCurrentGroups.contains( value );
}
- public void addConstraintFailure(InvalidConstraintImpl<T> failingConstraint) {
- int i = failingConstraints.indexOf( failingConstraint );
+ public void addConstraintFailure(ConstraintViolationImpl<T>
failingConstraintViolation) {
+ int i = failingConstraintViolations.indexOf( failingConstraintViolation );
if ( i == -1 ) {
- failingConstraints.add( failingConstraint );
+ failingConstraintViolations.add( failingConstraintViolation );
}
else {
- failingConstraints.get( i ).addGroups( failingConstraint.getGroups() );
+ failingConstraintViolations.get( i ).addGroups( failingConstraintViolation.getGroups()
);
}
}
- public List<InvalidConstraintImpl<T>> getFailingConstraints() {
- return failingConstraints;
+ public List<ConstraintViolationImpl<T>> getFailingConstraints() {
+ return failingConstraintViolations;
}
/**
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-10-31
12:26:41 UTC (rev 15465)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -31,7 +31,7 @@
import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintFactory;
import javax.validation.ElementDescriptor;
-import javax.validation.InvalidConstraint;
+import javax.validation.ConstraintViolation;
import javax.validation.MessageResolver;
import javax.validation.Validator;
@@ -39,7 +39,7 @@
import org.hibernate.validation.ValidatorConstants;
import org.hibernate.validation.impl.ConstraintDescriptorImpl;
import org.hibernate.validation.impl.ConstraintFactoryImpl;
-import org.hibernate.validation.impl.InvalidConstraintImpl;
+import org.hibernate.validation.impl.ConstraintViolationImpl;
import org.hibernate.validation.impl.ResourceBundleMessageResolver;
import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.PropertyIterator;
@@ -66,7 +66,7 @@
}
@SuppressWarnings("unchecked")
- private final List<InvalidConstraintImpl<T>> EMPTY_CONSTRAINTS_LIST =
Collections.EMPTY_LIST;
+ private final List<ConstraintViolationImpl<T>> EMPTY_CONSTRAINTS_LIST =
Collections.EMPTY_LIST;
/**
* A map for caching validators for cascaded entities.
@@ -100,14 +100,14 @@
/**
* {@inheritDoc}
*/
- public Set<InvalidConstraint<T>> validate(T object, String... groups) {
+ public Set<ConstraintViolation<T>> validate(T object, String... groups) {
if ( object == null ) {
throw new IllegalArgumentException( "Validation of a null object" );
}
ValidationContext<T> context = new ValidationContext<T>( object );
- List<InvalidConstraintImpl<T>> list = validate( context, Arrays.asList(
groups ) );
- return new HashSet<InvalidConstraint<T>>( list );
+ List<ConstraintViolationImpl<T>> list = validate( context, Arrays.asList(
groups ) );
+ return new HashSet<ConstraintViolation<T>>( list );
}
/**
@@ -122,7 +122,7 @@
* @todo Currently we iterate the cascaded fields multiple times. Maybe we should change
to an approach where we iterate the object graph only once.
* @todo Context root bean can be a different object than the current Validator<T>
hence two different generics variables
*/
- private List<InvalidConstraintImpl<T>> validate(ValidationContext<T>
context, List<String> groups) {
+ private List<ConstraintViolationImpl<T>> validate(ValidationContext<T>
context, List<String> groups) {
if ( context.peekValidatedObject() == null ) {
return EMPTY_CONSTRAINTS_LIST;
}
@@ -178,7 +178,7 @@
constraintDescriptor,
leafBeanInstance
);
- InvalidConstraintImpl<T> failingConstraint = new
InvalidConstraintImpl<T>(
+ ConstraintViolationImpl<T> failingConstraintViolation = new
ConstraintViolationImpl<T>(
message,
context.getRootBean(),
metaDataProvider.getBeanClass(),
@@ -187,7 +187,7 @@
context.peekPropertyPath(), //FIXME use error.getProperty()
context.getCurrentGroup()
);
- context.addConstraintFailure( failingConstraint );
+ context.addConstraintFailure( failingConstraintViolation );
}
}
context.popProperty();
@@ -271,14 +271,14 @@
/**
* {@inheritDoc}
*/
- public Set<InvalidConstraint<T>> validateProperty(T object, String
propertyName, String... groups) {
- List<InvalidConstraintImpl<T>> failingConstraints = new
ArrayList<InvalidConstraintImpl<T>>();
- validateProperty( object, new PropertyIterator( propertyName ), failingConstraints,
groups );
- return new HashSet<InvalidConstraint<T>>( failingConstraints );
+ public Set<ConstraintViolation<T>> validateProperty(T object, String
propertyName, String... groups) {
+ List<ConstraintViolationImpl<T>> failingConstraintViolations = new
ArrayList<ConstraintViolationImpl<T>>();
+ validateProperty( object, new PropertyIterator( propertyName ),
failingConstraintViolations, groups );
+ return new HashSet<ConstraintViolation<T>>( failingConstraintViolations );
}
- private void validateProperty(T object, PropertyIterator propertyIter,
List<InvalidConstraintImpl<T>> failingConstraints, String... groups) {
+ private void validateProperty(T object, PropertyIterator propertyIter,
List<ConstraintViolationImpl<T>> failingConstraintViolations, String...
groups) {
DesrciptorValueWrapper wrapper = getConstraintDescriptorAndValueForPath( this,
propertyIter, object );
if ( wrapper == null ) {
@@ -311,7 +311,7 @@
wrapper.descriptor,
wrapper.value
);
- InvalidConstraintImpl<T> failingConstraint = new
InvalidConstraintImpl<T>(
+ ConstraintViolationImpl<T> failingConstraintViolation = new
ConstraintViolationImpl<T>(
message,
object,
( Class<T> ) object.getClass(),
@@ -320,11 +320,11 @@
propertyIter.getOriginalProperty(), //FIXME use error.getProperty()
group
);
- addFailingConstraint( failingConstraints, failingConstraint );
+ addFailingConstraint( failingConstraintViolations, failingConstraintViolation );
}
}
- if ( isGroupSequence && failingConstraints.size() > 0 ) {
+ if ( isGroupSequence && failingConstraintViolations.size() > 0 ) {
break;
}
}
@@ -334,14 +334,14 @@
/**
* {@inheritDoc}
*/
- public Set<InvalidConstraint<T>> validateValue(String propertyName, Object
value, String... groups) {
- List<InvalidConstraintImpl<T>> failingConstraints = new
ArrayList<InvalidConstraintImpl<T>>();
- validateValue( value, new PropertyIterator( propertyName ), failingConstraints, groups
);
- return new HashSet<InvalidConstraint<T>>( failingConstraints );
+ public Set<ConstraintViolation<T>> validateValue(String propertyName, Object
value, String... groups) {
+ List<ConstraintViolationImpl<T>> failingConstraintViolations = new
ArrayList<ConstraintViolationImpl<T>>();
+ validateValue( value, new PropertyIterator( propertyName ),
failingConstraintViolations, groups );
+ return new HashSet<ConstraintViolation<T>>( failingConstraintViolations );
}
- private void validateValue(Object object, PropertyIterator propertyIter,
List<InvalidConstraintImpl<T>> failingConstraints, String... groups) {
+ private void validateValue(Object object, PropertyIterator propertyIter,
List<ConstraintViolationImpl<T>> failingConstraintViolations, String...
groups) {
ConstraintDescriptorImpl constraintDescriptor = getConstraintDescriptorForPath( this,
propertyIter );
if ( constraintDescriptor == null ) {
@@ -373,7 +373,7 @@
constraintDescriptor,
object
);
- InvalidConstraintImpl<T> failingConstraint = new
InvalidConstraintImpl<T>(
+ ConstraintViolationImpl<T> failingConstraintViolation = new
ConstraintViolationImpl<T>(
message,
null,
null,
@@ -382,11 +382,11 @@
propertyIter.getOriginalProperty(), //FIXME use error.getProperty()
""
);
- addFailingConstraint( failingConstraints, failingConstraint );
+ addFailingConstraint( failingConstraintViolations, failingConstraintViolation );
}
}
- if ( isGroupSequence && failingConstraints.size() > 0 ) {
+ if ( isGroupSequence && failingConstraintViolations.size() > 0 ) {
break;
}
}
@@ -487,13 +487,13 @@
}
- private void addFailingConstraint(List<InvalidConstraintImpl<T>>
failingConstraints, InvalidConstraintImpl<T> failingConstraint) {
- int i = failingConstraints.indexOf( failingConstraint );
+ private void addFailingConstraint(List<ConstraintViolationImpl<T>>
failingConstraintViolations, ConstraintViolationImpl<T> failingConstraintViolation)
{
+ int i = failingConstraintViolations.indexOf( failingConstraintViolation );
if ( i == -1 ) {
- failingConstraints.add( failingConstraint );
+ failingConstraintViolations.add( failingConstraintViolation );
}
else {
- failingConstraints.get( i ).addGroups( failingConstraint.getGroups() );
+ failingConstraintViolations.get( i ).addGroups( failingConstraintViolation.getGroups()
);
}
}
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintViolationImpl.java
(from rev 15465,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/InvalidConstraintImpl.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintViolationImpl.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintViolationImpl.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -0,0 +1,138 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validation.impl;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+
+/**
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class ConstraintViolationImpl<T> implements ConstraintViolation<T> {
+ private String message;
+ private T rootBean;
+ private Class<T> beanClass;
+ private Object value;
+ private String propertyPath;
+ private HashSet<String> groups;
+ private Object leafBeanInstance;
+
+
+ public ConstraintViolationImpl(String message, T rootBean, Class<T> beanClass,
Object leafBeanInstance, Object value, String propertyPath, String group) {
+ this.message = message;
+ this.rootBean = rootBean;
+ this.beanClass = beanClass;
+ this.value = value;
+ this.propertyPath = propertyPath;
+ groups = new HashSet<String>();
+ groups.add( group );
+ this.leafBeanInstance = leafBeanInstance;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getMessage() {
+ return message;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public T getRootBean() {
+ return rootBean;
+ }
+
+ public Object getLeafBean() {
+ return leafBeanInstance;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Class<T> getBeanClass() {
+ return beanClass;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getValue() {
+ return value;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getPropertyPath() {
+ return propertyPath;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Set<String> getGroups() {
+ return groups;
+ }
+
+ public void addGroups(Set<String> groupSet) {
+ groups.addAll( groupSet );
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if ( this == o ) {
+ return true;
+ }
+ if ( !( o instanceof ConstraintViolationImpl ) ) {
+ return false;
+ }
+
+ ConstraintViolationImpl that = ( ConstraintViolationImpl ) o;
+
+ if ( beanClass != null ? !beanClass.equals( that.beanClass ) : that.beanClass != null )
{
+ return false;
+ }
+ if ( message != null ? !message.equals( that.message ) : that.message != null ) {
+ return false;
+ }
+ if ( propertyPath != null ? !propertyPath.equals( that.propertyPath ) :
that.propertyPath != null ) {
+ return false;
+ }
+ if ( rootBean != null ? !rootBean.equals( that.rootBean ) : that.rootBean != null ) {
+ return false;
+ }
+ if ( value != null ? !value.equals( that.value ) : that.value != null ) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = message != null ? message.hashCode() : 0;
+ result = 31 * result + ( rootBean != null ? rootBean.hashCode() : 0 );
+ result = 31 * result + ( beanClass != null ? beanClass.hashCode() : 0 );
+ result = 31 * result + ( value != null ? value.hashCode() : 0 );
+ result = 31 * result + ( propertyPath != null ? propertyPath.hashCode() : 0 );
+ return result;
+ }
+}
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintViolationImpl.java
___________________________________________________________________
Name: svn:keywords
+ Id
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 2008-10-31
12:26:41 UTC (rev 15465)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -23,7 +23,7 @@
import javax.validation.Constraint;
import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintFactory;
-import javax.validation.InvalidConstraint;
+import javax.validation.ConstraintViolation;
import javax.validation.MessageResolver;
import javax.validation.Validation;
import javax.validation.ValidationException;
@@ -81,13 +81,13 @@
Customer customer = new Customer();
customer.setFirstName( "John" );
- Set<InvalidConstraint<Customer>> invalidConstraints = validator.validate(
customer );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
customer.setLastName( "Doe" );
- invalidConstraints = validator.validate( customer );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@@ -104,10 +104,10 @@
Customer customer = new Customer();
customer.setFirstName( "John" );
- Set<InvalidConstraint<Customer>> invalidConstraints = validator.validate(
customer );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- InvalidConstraint<Customer> constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong message", "may not be null",
constraint.getMessage() );
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ ConstraintViolation<Customer> constraintViolation =
constraintViolations.iterator().next();
+ assertEquals( "Wrong message", "may not be null",
constraintViolation.getMessage() );
//FIXME nothing guarantee that a builder can be reused
// now we modify the builder, get a new factory and valiator and try again
@@ -120,10 +120,10 @@
);
factory = builder.build();
validator = factory.getValidator( Customer.class );
- invalidConstraints = validator.validate( customer );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong message", "my custom message",
constraint.getMessage() );
+ constraintViolations = validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong message", "my custom message",
constraintViolation.getMessage() );
}
@Test
@@ -138,10 +138,10 @@
Customer customer = new Customer();
customer.setFirstName( "John" );
- Set<InvalidConstraint<Customer>> invalidConstraints = validator.validate(
customer );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- InvalidConstraint<Customer> constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong message", "may not be null",
constraint.getMessage() );
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ ConstraintViolation<Customer> constraintViolation =
constraintViolations.iterator().next();
+ assertEquals( "Wrong message", "may not be null",
constraintViolation.getMessage() );
//FIXME nothing guarantee that a builder can be reused
// now we modify the builder, get a new factory and valiator and try again
@@ -157,8 +157,8 @@
);
factory = builder.build();
validator = factory.getValidator( Customer.class );
- invalidConstraints = validator.validate( customer );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@Test
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java 2008-10-31
12:26:41 UTC (rev 15465)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ValidatorImplTest.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -19,7 +19,7 @@
import java.util.HashSet;
import java.util.Set;
-import javax.validation.InvalidConstraint;
+import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
import javax.validation.Validator;
@@ -108,49 +108,49 @@
book.setTitle( "" );
book.setAuthor( author );
- Set<InvalidConstraint<Book>> invalidConstraints = validator.validate( book,
"first", "second", "last" );
- assertEquals( "Wrong number of constraints", 3, invalidConstraints.size() );
+ Set<ConstraintViolation<Book>> constraintViolations = validator.validate(
book, "first", "second", "last" );
+ assertEquals( "Wrong number of constraints", 3, constraintViolations.size()
);
author.setFirstName( "Gavin" );
author.setLastName( "King" );
- invalidConstraints = validator.validate( book, "first", "second",
"last" );
- InvalidConstraint constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "may not be empty",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Book.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", book, constraint.getRootBean() );
- assertEquals( "Wrong value", book.getTitle(), constraint.getValue() );
- assertEquals( "Wrong propertyName", "title",
constraint.getPropertyPath() );
+ constraintViolations = validator.validate( book, "first", "second",
"last" );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "may not be empty",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Book.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", book, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", book.getTitle(), constraintViolation.getValue()
);
+ assertEquals( "Wrong propertyName", "title",
constraintViolation.getPropertyPath() );
book.setTitle( "Hibernate Persistence with JPA" );
book.setSubtitle( "Revised Edition of Hibernate in Action" );
- invalidConstraints = validator.validate( book, "first", "second",
"last" );
- constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "length must be between 0 and 30",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Book.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", book, constraint.getRootBean() );
- assertEquals( "Wrong value", book.getSubtitle(), constraint.getValue() );
- assertEquals( "Wrong propertyName", "subtitle",
constraint.getPropertyPath() );
+ constraintViolations = validator.validate( book, "first", "second",
"last" );
+ constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "length must be between 0 and 30",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Book.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", book, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", book.getSubtitle(),
constraintViolation.getValue() );
+ assertEquals( "Wrong propertyName", "subtitle",
constraintViolation.getPropertyPath() );
book.setSubtitle( "Revised Edition" );
author.setCompany( "JBoss a divison of RedHat" );
- invalidConstraints = validator.validate( book, "first", "second",
"last" );
- constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "length must be between 0 and 20",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Author.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", book, constraint.getRootBean() );
- assertEquals( "Wrong value", author.getCompany(), constraint.getValue() );
- assertEquals( "Wrong propertyName", "author.company",
constraint.getPropertyPath() );
+ constraintViolations = validator.validate( book, "first", "second",
"last" );
+ constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "length must be between 0 and 20",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Author.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", book, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", author.getCompany(),
constraintViolation.getValue() );
+ assertEquals( "Wrong propertyName", "author.company",
constraintViolation.getPropertyPath() );
author.setCompany( "JBoss" );
- invalidConstraints = validator.validate( book, "first", "second",
"last" );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( book, "first", "second",
"last" );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@Test
@@ -163,37 +163,37 @@
Book book = new Book();
book.setAuthor( author );
- Set<InvalidConstraint<Book>> invalidConstraints = validator.validate( book,
"default" );
- assertEquals( "Wrong number of constraints", 2, invalidConstraints.size() );
+ Set<ConstraintViolation<Book>> constraintViolations = validator.validate(
book, "default" );
+ assertEquals( "Wrong number of constraints", 2, constraintViolations.size()
);
author.setFirstName( "Gavin" );
author.setLastName( "King" );
- invalidConstraints = validator.validate( book, "default" );
- InvalidConstraint constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "may not be null",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Book.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", book, constraint.getRootBean() );
- assertEquals( "Wrong value", book.getTitle(), constraint.getValue() );
- assertEquals( "Wrong propertyName", "title",
constraint.getPropertyPath() );
+ constraintViolations = validator.validate( book, "default" );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "may not be null",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Book.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", book, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", book.getTitle(), constraintViolation.getValue()
);
+ assertEquals( "Wrong propertyName", "title",
constraintViolation.getPropertyPath() );
book.setTitle( "Hibernate Persistence with JPA" );
book.setSubtitle( "Revised Edition of Hibernate in Action" );
- invalidConstraints = validator.validate( book, "default" );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ constraintViolations = validator.validate( book, "default" );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
book.setSubtitle( "Revised Edition" );
author.setCompany( "JBoss a divison of RedHat" );
- invalidConstraints = validator.validate( book, "default" );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ constraintViolations = validator.validate( book, "default" );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
author.setCompany( "JBoss" );
- invalidConstraints = validator.validate( book, "default" );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( book, "default" );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@Test
@@ -203,13 +203,13 @@
Customer customer = new Customer();
customer.setFirstName( "John" );
- Set<InvalidConstraint<Customer>> invalidConstraints = validator.validate(
customer );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
customer.setLastName( "Doe" );
- invalidConstraints = validator.validate( customer );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@Test
@@ -224,8 +224,8 @@
author.setCompany( "Langenscheidt Publ." );
dictonary.setAuthor( author );
- Set<InvalidConstraint<Dictonary>> invalidConstraints = validator.validate(
dictonary, "default-alias" );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ Set<ConstraintViolation<Dictonary>> constraintViolations =
validator.validate( dictonary, "default-alias" );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@Test
@@ -235,21 +235,21 @@
elepfant.setName( "" );
elepfant.setDomain( Animal.Domain.EUKARYOTA );
- Set<InvalidConstraint<Animal>> invalidConstraints = validator.validate(
elepfant, "first", "second" );
+ Set<ConstraintViolation<Animal>> constraintViolations = validator.validate(
elepfant, "first", "second" );
assertEquals(
"The should be two invalid constraints since the same propertyName gets
validated in both groups",
1,
- invalidConstraints.size()
+ constraintViolations.size()
);
- InvalidConstraint constraint = invalidConstraints.iterator().next();
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
Set<String> expected = new HashSet<String>();
expected.add( "first" );
expected.add( "second" );
assertEquals(
"The constraint should be invalid for both groups",
expected,
- constraint.getGroups()
+ constraintViolation.getGroups()
);
}
@@ -267,32 +267,32 @@
address.setAddressline2( null );
address.setCity(
"Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch" ); //town in
North Wales
- Set<InvalidConstraint<Address>> invalidConstraints = validator.validate(
address );
+ Set<ConstraintViolation<Address>> constraintViolations =
validator.validate( address );
assertEquals(
"we should have been 2 not null violation for addresslines and one lenth
violation for city",
3,
- invalidConstraints.size()
+ constraintViolations.size()
);
- invalidConstraints = validator.validateProperty( address, "city" );
+ constraintViolations = validator.validateProperty( address, "city" );
assertEquals(
"only city should be validated",
1,
- invalidConstraints.size()
+ constraintViolations.size()
);
- invalidConstraints = validator.validateProperty( address, "city" );
+ constraintViolations = validator.validateProperty( address, "city" );
assertEquals(
"only city should be validated",
1,
- invalidConstraints.size()
+ constraintViolations.size()
);
- invalidConstraints = validator.validateValue( "city", "Paris" );
+ constraintViolations = validator.validateValue( "city", "Paris" );
assertEquals(
"Paris should be a valid city name.",
0,
- invalidConstraints.size()
+ constraintViolations.size()
);
}
@@ -304,20 +304,20 @@
customer.setFirstName( "John" );
customer.setLastName( "Doe" );
- Set<InvalidConstraint<Customer>> invalidConstraints = validator.validate(
customer );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
Order order1 = new Order();
customer.addOrder( order1 );
- invalidConstraints = validator.validate( customer );
- InvalidConstraint constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "may not be null",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Order.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", customer, constraint.getRootBean() );
- assertEquals( "Wrong value", order1.getOrderNumber(), constraint.getValue()
);
- assertEquals( "Wrong propertyName", "orderList[0].orderNumber",
constraint.getPropertyPath() );
+ constraintViolations = validator.validate( customer );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "may not be null",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Order.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", customer,
constraintViolation.getRootBean() );
+ assertEquals( "Wrong value", order1.getOrderNumber(),
constraintViolation.getValue() );
+ assertEquals( "Wrong propertyName", "orderList[0].orderNumber",
constraintViolation.getPropertyPath() );
}
@@ -330,16 +330,16 @@
Engine engine = new Engine();
engine.setSerialNumber( "mail(a)foobar.com" );
- Set<InvalidConstraint<Engine>> invalidConstraints = validator.validate(
engine );
- assertEquals( "Wrong number of constraints", 2, invalidConstraints.size() );
+ Set<ConstraintViolation<Engine>> constraintViolations = validator.validate(
engine );
+ assertEquals( "Wrong number of constraints", 2, constraintViolations.size()
);
engine.setSerialNumber( "ABCDEFGH1234" );
- invalidConstraints = validator.validate( engine );
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ constraintViolations = validator.validate( engine );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
engine.setSerialNumber( "ABCD-EFGH-1234" );
- invalidConstraints = validator.validate( engine );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validate( engine );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
@@ -360,14 +360,14 @@
clint.addPlayedWith( morgan );
Validator<Actor> validator = new ValidatorImpl<Actor>( Actor.class );
- Set<InvalidConstraint<Actor>> invalidConstraints = validator.validate(
clint );
- InvalidConstraint constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "may not be empty",
constraint.getMessage() );
- assertEquals( "Wrong bean class", Actor.class, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", clint, constraint.getRootBean() );
- assertEquals( "Wrong value", morgan.getLastName(), constraint.getValue() );
- assertEquals( "Wrong propertyName",
"playedWith[0].playedWith[1].lastName", constraint.getPropertyPath() );
+ Set<ConstraintViolation<Actor>> constraintViolations = validator.validate(
clint );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "may not be empty",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", Actor.class,
constraintViolation.getBeanClass() );
+ assertEquals( "Wrong root entity", clint, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", morgan.getLastName(),
constraintViolation.getValue() );
+ assertEquals( "Wrong propertyName",
"playedWith[0].playedWith[1].lastName", constraintViolation.getPropertyPath()
);
}
@Test
@@ -376,20 +376,20 @@
Order order = new Order();
- Set<InvalidConstraint<Customer>> invalidConstraints =
validator.validateValue(
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateValue(
"orderList[0].orderNumber", null
);
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
- InvalidConstraint constraint = invalidConstraints.iterator().next();
- assertEquals( "Wrong number of constraints", 1, invalidConstraints.size() );
- assertEquals( "Wrong message", "may not be null",
constraint.getMessage() );
- assertEquals( "Wrong bean class", null, constraint.getBeanClass() );
- assertEquals( "Wrong root entity", null, constraint.getRootBean() );
- assertEquals( "Wrong value", order.getOrderNumber(), constraint.getValue()
);
- assertEquals( "Wrong propertyName", "orderList[0].orderNumber",
constraint.getPropertyPath() );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size()
);
+ assertEquals( "Wrong message", "may not be null",
constraintViolation.getMessage() );
+ assertEquals( "Wrong bean class", null, constraintViolation.getBeanClass()
);
+ assertEquals( "Wrong root entity", null, constraintViolation.getRootBean()
);
+ assertEquals( "Wrong value", order.getOrderNumber(),
constraintViolation.getValue() );
+ assertEquals( "Wrong propertyName", "orderList[0].orderNumber",
constraintViolation.getPropertyPath() );
- invalidConstraints = validator.validateValue( "orderList[0].orderNumber",
"1234" );
- assertEquals( "Wrong number of constraints", 0, invalidConstraints.size() );
+ constraintViolations = validator.validateValue( "orderList[0].orderNumber",
"1234" );
+ assertEquals( "Wrong number of constraints", 0, constraintViolations.size()
);
}
}
Copied:
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
(from rev 15465,
validator/trunk/validation-api/src/main/java/javax/validation/InvalidConstraint.java)
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
(rev 0)
+++
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -0,0 +1,73 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package javax.validation;
+
+import java.util.Set;
+
+/**
+ * Describe a constraint validation defect.
+ *
+ * @author Emmanuel Bernard
+ * @todo add pointers to the metadata?
+ * @todo the rational behind rootBean and propertyPath is to keep the context available
to the user
+ */
+public interface ConstraintViolation<T> {
+
+ /**
+ * @return The error message for this constraint violation.
+ */
+ String getMessage();
+
+ /**
+ * @return The root bean being validated.
+ */
+ T getRootBean();
+
+ /**
+ * If a bean constraint, the bean instance the constraint is applied on
+ * If a property constraint, the bean instance hosting the property the constraint is
applied on
+ *
+ * @return the leaf bean the constraint is applied on or null if Validator#validateValue
is used
+ */
+ Object getLeafBean();
+
+ /**
+ * @return the property path to the value from <code>rootBean</code>
+ * <code>null</code> if the value is the
<code>rootBean<code> itself.
+ */
+ String getPropertyPath();
+
+
+ /**
+ * @return the bean type being validated.
+ */
+ Class<T> getBeanClass();
+
+ /**
+ * @return the value failing to pass the constraint.
+ */
+ Object getValue();
+
+ /**
+ * @return the list of groups that the triggered constraint applies on and which also
are
+ * within the list of groups requested for validation.
+ *
+ * TODO: considering removal, if you think it's important, speak up
+ */
+ Set<String> getGroups();
+}
Property changes on:
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/validation-api/src/main/java/javax/validation/Validator.java
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/Validator.java 2008-10-31
12:26:41 UTC (rev 15465)
+++
validator/trunk/validation-api/src/main/java/javax/validation/Validator.java 2008-10-31
15:28:23 UTC (rev 15466)
@@ -38,7 +38,7 @@
*
* @throws IllegalArgumentException e if object is <code>null</code>.
*/
- Set<InvalidConstraint<T>> validate(T object, String... groups);
+ Set<ConstraintViolation<T>> validate(T object, String... groups);
/**
* validate all constraints on propertyname property of object (unless shortcut)
@@ -53,7 +53,7 @@
* @throws IllegalArgumentException e if object is <code>null</code>.
* @todo Do we keep this method?
*/
- Set<InvalidConstraint<T>> validateProperty(T object, String propertyName,
String... groups);
+ Set<ConstraintViolation<T>> validateProperty(T object, String propertyName,
String... groups);
/**
* Validates all constraints on propertyname property if the property value is value
(unless shortcut)
@@ -68,7 +68,7 @@
* @todo Do we keep this method?
* @todo express limitations of InvalidConstraint in this case.
*/
- Set<InvalidConstraint<T>> validateValue(String propertyName, Object value,
String... groups);
+ Set<ConstraintViolation<T>> validateValue(String propertyName, Object value,
String... groups);
/**
* return true if at least one constraint declaration is present for the given bean