Hibernate SVN: r16863 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-06-22 14:09:41 -0400 (Mon, 22 Jun 2009)
New Revision: 16863
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/metadata/ElementDescriptor.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ElementDescriptorTest.java
Log:
BVAL-168 Return the list of matching ConstraintDescriptor for a given set of groups
BVAL-169 Rename ElementDescriptor.getType to getElementClass
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/metadata/ElementDescriptor.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/metadata/ElementDescriptor.java 2009-06-22 16:51:54 UTC (rev 16862)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/metadata/ElementDescriptor.java 2009-06-22 18:09:41 UTC (rev 16863)
@@ -34,10 +34,8 @@
/**
* @return Statically defined returned type.
- *
- * @todo should it be Type or even completly removed
*/
- Class<?> getType();
+ Class<?> getElementClass();
/**
* Return all constraint descriptors for this element or an
@@ -46,4 +44,17 @@
* @return Set of constraint descriptors for this element
*/
Set<ConstraintDescriptor<?>> getConstraintDescriptors();
+
+ /**
+ * Return the list of matching constraints for a given set of groups for this element.
+ *
+ * This method respects group sequences and group inheritance (including
+ * class-level Default group overriding) but does not return ConstraintDescriptors
+ * in any particular order. Specifically, ordering of the group sequence is not
+ * respected.
+ *
+ * @param groups groups targeted
+ * @return list of matching ConstraintDescriptors
+ */
+ Set<ConstraintDescriptor<?>> getUnorderdConstraintDescriptorsMatchingGroups(Class<?>... groups);
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java 2009-06-22 16:51:54 UTC (rev 16862)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/metadata/ElementDescriptorImpl.java 2009-06-22 18:09:41 UTC (rev 16863)
@@ -51,7 +51,7 @@
/**
* {@inheritDoc}
*/
- public Class<?> getType() {
+ public Class<?> getElementClass() {
return type;
}
@@ -61,4 +61,11 @@
public Set<ConstraintDescriptor<?>> getConstraintDescriptors() {
return Collections.unmodifiableSet( constraintDescriptors );
}
+
+ /**
+ * {@inheritDoc}
+ */
+ public Set<ConstraintDescriptor<?>> getUnorderdConstraintDescriptorsMatchingGroups(Class<?>... groups) {
+ throw new UnsupportedOperationException( "Not yet implemented HV-176");
+ }
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ElementDescriptorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ElementDescriptorTest.java 2009-06-22 16:51:54 UTC (rev 16862)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/metadata/ElementDescriptorTest.java 2009-06-22 18:09:41 UTC (rev 16863)
@@ -41,13 +41,13 @@
public void testGetTypeForConstrainedBean() {
Validator validator = TestUtil.getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Customer.class );
- assertEquals( beanDescriptor.getType(), Customer.class, "Wrong type." );
+ assertEquals( beanDescriptor.getElementClass(), Customer.class, "Wrong type." );
}
@Test
public void testGetTypeForConstrainedProperty() {
ElementDescriptor elementDescriptor = TestUtil.getPropertyDescriptor( Order.class, "orderNumber" );
- assertEquals( elementDescriptor.getType(), Integer.class, "Wrong type." );
+ assertEquals( elementDescriptor.getElementClass(), Integer.class, "Wrong type." );
}
/**
15 years, 5 months
Hibernate SVN: r16862 - core/trunk/annotations/src/main/java/org/hibernate/cfg/beanvalidation.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-06-22 12:51:54 -0400 (Mon, 22 Jun 2009)
New Revision: 16862
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/beanvalidation/HibernateTraversableResolver.java
Log:
BVAL-143 update to the latest BV API
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/beanvalidation/HibernateTraversableResolver.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/beanvalidation/HibernateTraversableResolver.java 2009-06-22 15:07:34 UTC (rev 16861)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/beanvalidation/HibernateTraversableResolver.java 2009-06-22 16:51:54 UTC (rev 16862)
@@ -5,16 +5,22 @@
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.TraversableResolver;
+import javax.validation.Path;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.Hibernate;
+import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;
-import org.hibernate.type.ComponentType;
import org.hibernate.type.CollectionType;
import org.hibernate.type.AbstractComponentType;
/**
+ * Use Hibernate metadata to ignore cascade on entities.
+ * cascade on embeddable objects or collection of embeddable objects are accepted
+ *
+ * Also use Hibernate's native isInitialized method call.
+ *
* @author Emmanuel Bernard
*/
public class HibernateTraversableResolver implements TraversableResolver {
@@ -59,34 +65,39 @@
}
}
- private String getCleanPathWoBracket(String traversableProperty, String pathToTraversableObject) {
- String path = pathToTraversableObject.equals( "" ) ?
- traversableProperty :
- pathToTraversableObject + "." + traversableProperty;
- String[] paths = path.split( "\\[.*\\]" );
- path = "";
- for (String subpath : paths) {
- path += subpath;
+ private String getStringBasedPath(Path.Node traversableProperty, Path pathToTraversableObject) {
+ StringBuilder path = new StringBuilder( );
+ for ( Path.Node node : pathToTraversableObject ) {
+ if (node.getName() != null) {
+ path.append( node.getName() ).append( "." );
+ }
}
- return path;
+ if ( traversableProperty.getName() == null ) {
+ throw new AssertionFailure(
+ "TraversableResolver being passed a traversableProperty with null name. pathToTraversableObject: "
+ + path.toString() );
+ }
+ path.append( traversableProperty.getName() );
+
+ return path.toString();
}
public boolean isReachable(Object traversableObject,
- String traversableProperty,
- Class<?> rootBeanType,
- String pathToTraversableObject,
- ElementType elementType) {
+ Path.Node traversableProperty,
+ Class<?> rootBeanType,
+ Path pathToTraversableObject,
+ ElementType elementType) {
//lazy, don't load
return Hibernate.isInitialized( traversableObject )
- && Hibernate.isPropertyInitialized( traversableObject, traversableProperty );
+ && Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
}
public boolean isCascadable(Object traversableObject,
- String traversableProperty,
+ Path.Node traversableProperty,
Class<?> rootBeanType,
- String pathToTraversableObject,
+ Path pathToTraversableObject,
ElementType elementType) {
- String path = getCleanPathWoBracket( traversableProperty, pathToTraversableObject );
+ String path = getStringBasedPath( traversableProperty, pathToTraversableObject );
return ! associations.contains(path);
}
}
15 years, 5 months
Hibernate SVN: r16861 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 11:07:34 -0400 (Mon, 22 Jun 2009)
New Revision: 16861
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
Log:
HV-175 made sure that if value is not null value itslef is used to dtermine type
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 2009-06-22 15:05:32 UTC (rev 16860)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-06-22 15:07:34 UTC (rev 16861)
@@ -141,6 +141,7 @@
if ( beanType == null ) {
throw new IllegalArgumentException( "The bean type cannot be null." );
}
+
sanityCheckPropertyPath( propertyName );
GroupChain groupChain = determineGroupExecutionOrder( groups );
@@ -615,15 +616,14 @@
Type type = ReflectionHelper.typeOf( m );
value = value == null ? null : ReflectionHelper.getValue( m, value );
if ( elem.isIndexed() ) {
- type = ReflectionHelper.getIndexedType( type );
value = value == null ? null : ReflectionHelper.getIndexedValue(
value, elem.getIndex()
);
- if ( type == null ) {
- continue;
- }
+ type = ReflectionHelper.getIndexedType( type );
}
- return collectMetaConstraintsForPath( ( Class<T> ) type, value, propertyIter, metaConstraints );
+ return collectMetaConstraintsForPath(
+ ( Class<T> ) ( value == null ? type : value.getClass() ), value, propertyIter, metaConstraints
+ );
}
}
}
15 years, 5 months
Hibernate SVN: r16860 - beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 11:05:32 -0400 (Mon, 22 Jun 2009)
New Revision: 16860
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
Log:
Updated tests for validateValue and validateProperty
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 14:48:50 UTC (rev 16859)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 15:05:32 UTC (rev 16860)
@@ -147,34 +147,34 @@
assertCorrectNumberOfViolations( constraintViolations, 0 );
}
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "4.1.1", id = "d"),
-// @SpecAssertion(section = "4.1.1", id = "c")
-// })
-// public void testValidatePropertyWithIndexedPath() {
-// Validator validator = TestUtil.getDefaultValidator();
-//
-// ActorListBased clint = new ActorListBased( "Clint", "Eastwood" );
-// ActorListBased morgan = new ActorListBased( "Morgan", null );
-// ActorListBased charlie = new ActorListBased( "Charlie", "Sheen" );
-//
-// clint.addPlayedWith( charlie );
-// charlie.addPlayedWith( clint );
-// charlie.addPlayedWith( morgan );
-// morgan.addPlayedWith( charlie );
-// morgan.addPlayedWith( clint );
-// clint.addPlayedWith( morgan );
-//
-// String property = "playedWith[0].playedWith[1].lastName";
-// Set<ConstraintViolation<ActorListBased>> constraintViolations = validator.validateProperty(
-// clint, property
-// );
-// assertCorrectNumberOfViolations( constraintViolations, 1 );
-//
-// ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
-// assertCorrectConstraintType( violation, NotNull.class );
-// assertConstraintViolation( violation, ActorListBased.class, null, property );
-// assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
-// }
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "4.1.1", id = "d"),
+ @SpecAssertion(section = "4.1.1", id = "c")
+ })
+ public void testValidatePropertyWithIndexedPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ ActorListBased clint = new ActorListBased( "Clint", "Eastwood" );
+ ActorListBased morgan = new ActorListBased( "Morgan", null );
+ ActorListBased charlie = new ActorListBased( "Charlie", "Sheen" );
+
+ clint.addPlayedWith( charlie );
+ charlie.addPlayedWith( clint );
+ charlie.addPlayedWith( morgan );
+ morgan.addPlayedWith( charlie );
+ morgan.addPlayedWith( clint );
+ clint.addPlayedWith( morgan );
+
+ String property = "playedWith[0].playedWith[1].lastName";
+ Set<ConstraintViolation<ActorListBased>> constraintViolations = validator.validateProperty(
+ clint, property
+ );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+
+ ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
+ assertCorrectConstraintType( violation, NotNull.class );
+ assertConstraintViolation( violation, ActorListBased.class, null, property );
+ assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
+ }
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java 2009-06-22 14:48:50 UTC (rev 16859)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java 2009-06-22 15:05:32 UTC (rev 16860)
@@ -166,7 +166,7 @@
fail();
}
catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
+ // success
}
try {
@@ -174,7 +174,7 @@
fail();
}
catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
+ // success
}
try {
@@ -182,7 +182,7 @@
fail();
}
catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
+ // success
}
}
}
\ No newline at end of file
15 years, 5 months
Hibernate SVN: r16859 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 10:48:50 -0400 (Mon, 22 Jun 2009)
New Revision: 16859
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
Log:
HV-173
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-06-22 14:46:49 UTC (rev 16858)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-06-22 14:48:50 UTC (rev 16859)
@@ -335,7 +335,7 @@
iter = map.values().iterator();
}
else if ( TypeUtils.isArray( type ) ) {
- List<?> arrayList = Arrays.asList( value );
+ List<?> arrayList = Arrays.asList( (Object) value );
iter = arrayList.iterator();
}
15 years, 5 months
Hibernate SVN: r16858 - beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 10:46:49 -0400 (Mon, 22 Jun 2009)
New Revision: 16858
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
Log:
disabled test
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 14:22:20 UTC (rev 16857)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 14:46:49 UTC (rev 16858)
@@ -147,34 +147,34 @@
assertCorrectNumberOfViolations( constraintViolations, 0 );
}
- @Test
- @SpecAssertions({
- @SpecAssertion(section = "4.1.1", id = "d"),
- @SpecAssertion(section = "4.1.1", id = "c")
- })
- public void testValidatePropertyWithIndexedPath() {
- Validator validator = TestUtil.getDefaultValidator();
-
- ActorListBased clint = new ActorListBased( "Clint", "Eastwood" );
- ActorListBased morgan = new ActorListBased( "Morgan", null );
- ActorListBased charlie = new ActorListBased( "Charlie", "Sheen" );
-
- clint.addPlayedWith( charlie );
- charlie.addPlayedWith( clint );
- charlie.addPlayedWith( morgan );
- morgan.addPlayedWith( charlie );
- morgan.addPlayedWith( clint );
- clint.addPlayedWith( morgan );
-
- String property = "playedWith[0].playedWith[1].lastName";
- Set<ConstraintViolation<ActorListBased>> constraintViolations = validator.validateProperty(
- clint, property
- );
- assertCorrectNumberOfViolations( constraintViolations, 1 );
-
- ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
- assertCorrectConstraintType( violation, NotNull.class );
- assertConstraintViolation( violation, ActorListBased.class, null, property );
- assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
- }
+// @Test
+// @SpecAssertions({
+// @SpecAssertion(section = "4.1.1", id = "d"),
+// @SpecAssertion(section = "4.1.1", id = "c")
+// })
+// public void testValidatePropertyWithIndexedPath() {
+// Validator validator = TestUtil.getDefaultValidator();
+//
+// ActorListBased clint = new ActorListBased( "Clint", "Eastwood" );
+// ActorListBased morgan = new ActorListBased( "Morgan", null );
+// ActorListBased charlie = new ActorListBased( "Charlie", "Sheen" );
+//
+// clint.addPlayedWith( charlie );
+// charlie.addPlayedWith( clint );
+// charlie.addPlayedWith( morgan );
+// morgan.addPlayedWith( charlie );
+// morgan.addPlayedWith( clint );
+// clint.addPlayedWith( morgan );
+//
+// String property = "playedWith[0].playedWith[1].lastName";
+// Set<ConstraintViolation<ActorListBased>> constraintViolations = validator.validateProperty(
+// clint, property
+// );
+// assertCorrectNumberOfViolations( constraintViolations, 1 );
+//
+// ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
+// assertCorrectConstraintType( violation, NotNull.class );
+// assertConstraintViolation( violation, ActorListBased.class, null, property );
+// assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
+// }
}
\ No newline at end of file
15 years, 5 months
Hibernate SVN: r16857 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 10:22:20 -0400 (Mon, 22 Jun 2009)
New Revision: 16857
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
Log:
HV-174 Made sure that replacement string are properly escaped
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-06-22 14:21:28 UTC (rev 16856)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-06-22 14:22:20 UTC (rev 16857)
@@ -199,7 +199,7 @@
parameter, bundle, locale, recurse
);
- matcher.appendReplacement( sb, resolvedParameterValue );
+ matcher.appendReplacement( sb, escapeMetaCharacters( resolvedParameterValue ) );
}
matcher.appendTail( sb );
return sb.toString();
@@ -213,7 +213,7 @@
String parameter = matcher.group( 1 );
Object variable = annotationParameters.get( removeCurlyBrace( parameter ) );
if ( variable != null ) {
- resolvedParameterValue = variable.toString();
+ resolvedParameterValue = escapeMetaCharacters( variable.toString() );
}
else {
resolvedParameterValue = message;
@@ -269,4 +269,15 @@
}
return bundle;
}
+
+ /**
+ * @param s The string in which to replace the meta characters '$' and '\'.
+ *
+ * @return A string where meta characters relevant for {@link Matcher#appendReplacement} are escaped.
+ */
+ private String escapeMetaCharacters(String s) {
+ String escapedString = s.replace( "\\", "\\\\" );
+ escapedString = escapedString.replace( "$", "\\$" );
+ return escapedString;
+ }
}
15 years, 5 months
Hibernate SVN: r16856 - in beanvalidation/trunk/validation-tck/src/main: resources and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 10:21:28 -0400 (Mon, 22 Jun 2009)
New Revision: 16856
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
removed duplicate assertion
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java 2009-06-22 14:15:57 UTC (rev 16855)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java 2009-06-22 14:21:28 UTC (rev 16856)
@@ -48,5 +48,5 @@
public abstract Pattern[] patterns();
- //public abstract UserType userType() default UserType.BUYER;
+ public abstract UserType userType() default UserType.BUYER;
}
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-22 14:15:57 UTC (rev 16855)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-22 14:21:28 UTC (rev 16856)
@@ -243,20 +243,12 @@
</assertion>
<assertion id="d">
<text>When a property is annotated with a constraint declaration, property access
- strategy is used to access the state val- idated by such constraint</text>
+ strategy is used to access the state validated by such constraint</text>
</assertion>
<assertion id="e">
- <text>When using field access strategy, the bean validation provider accesses the
- instance variable directly</text>
- </assertion>
- <assertion id="f">
- <text>When using the property access strategy, the bean validation provider accesses the
- state via the property accessor method.</text>
- </assertion>
- <assertion id="g">
<text>The fields or methods visibility are not constrained</text>
</assertion>
- <assertion id="h">
+ <assertion id="f">
<text>Constraints on non getter methods are not supported</text>
</assertion>
</section>
15 years, 5 months
Hibernate SVN: r16855 - in beanvalidation/trunk/validation-tck/src/main: java/org/hibernate/jsr303/tck/tests/xmlconfiguration and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-06-22 10:15:57 -0400 (Mon, 22 Jun 2009)
New Revision: 16855
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorArrayBased.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorListBased.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/PlayedWith.java
Removed:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Engine.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
mapped constrinat violation exceptions
Deleted: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -1,88 +0,0 @@
-// $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.jsr303.tck.tests.validation;
-
-import java.util.ArrayList;
-import java.util.List;
-import javax.validation.Valid;
-
-/**
- * @author Hardy Ferentschik
- */
-public class Actor implements Person {
-
- private String firstName;
- private String middleName;
- private String lastName;
-
- @Valid
- private List<Actor> playedWith = new ArrayList<Actor>();
-
- public Actor() {
- }
-
- public Actor(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
-
- public List<Actor> getPlayedWith() {
- return playedWith;
- }
-
- public void setPlayedWith(List<Actor> playedWith) {
- this.playedWith = playedWith;
- }
-
- public void addPlayedWith(Actor playedWith) {
- this.playedWith.add( playedWith );
- }
-
- public String getFirstName() {
- return firstName;
- }
-
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
- public String getMiddleName() {
- return middleName;
- }
-
- public void setMiddleName(String middleName) {
- this.middleName = middleName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
- @Override
- public String toString() {
- return "Actor{" +
- "firstName='" + firstName + '\'' +
- ", middleName='" + middleName + '\'' +
- ", lastName='" + lastName + '\'' +
- '}';
- }
-}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -0,0 +1,67 @@
+// $Id: ActorListBased.java 16840 2009-06-19 11:38:29Z hardy.ferentschik $
+/*
+* 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.jsr303.tck.tests.validation;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public abstract class Actor implements Person, PlayedWith {
+
+ private String firstName;
+ private String middleName;
+ private String lastName;
+
+
+ public Actor(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getMiddleName() {
+ return middleName;
+ }
+
+ public void setMiddleName(String middleName) {
+ this.middleName = middleName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ @Override
+ public String toString() {
+ return "Actor{" +
+ "firstName='" + firstName + '\'' +
+ ", middleName='" + middleName + '\'' +
+ ", lastName='" + lastName + '\'' +
+ '}';
+ }
+}
\ No newline at end of file
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorArrayBased.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorArrayBased.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorArrayBased.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -0,0 +1,50 @@
+// $Id: ActorListBased.java 16840 2009-06-19 11:38:29Z hardy.ferentschik $
+/*
+* 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.jsr303.tck.tests.validation;
+
+import java.util.Arrays;
+import java.util.List;
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ActorArrayBased extends Actor {
+ public static final int MAX_ACTOR_SIZE = 100;
+
+ @Valid
+ private Actor[] playedWith = new Actor[MAX_ACTOR_SIZE];
+
+ int currentPointer = 0;
+
+ public ActorArrayBased(String firstName, String lastName) {
+ super( firstName, lastName );
+ }
+
+ public List<Actor> getPlayedWith() {
+ return Arrays.asList( playedWith );
+ }
+
+ public void addPlayedWith(Actor playedWith) {
+ if ( currentPointer == MAX_ACTOR_SIZE ) {
+ throw new RuntimeException( "Exceeded allowed number of actors." );
+ }
+ this.playedWith[currentPointer] = playedWith;
+ currentPointer++;
+ }
+}
\ No newline at end of file
Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorListBased.java (from rev 16842, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorListBased.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorListBased.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -0,0 +1,50 @@
+// $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.jsr303.tck.tests.validation;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ActorListBased extends Actor {
+
+ @Valid
+ private List<Actor> playedWith = new ArrayList<Actor>();
+
+ public ActorListBased(String firstName, String lastName) {
+ super( firstName, lastName );
+ }
+
+ public List<Actor> getPlayedWith() {
+ return playedWith;
+ }
+
+ public void addPlayedWith(Actor playedWith) {
+ this.playedWith.add( playedWith );
+ }
+
+ @Override
+ public String toString() {
+ return "ActorListBased{" +
+ "playedWith=" + playedWith +
+ '}';
+ }
+}
\ No newline at end of file
Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ActorListBased.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Engine.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Engine.java 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Engine.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -27,7 +27,7 @@
@Pattern.List({
@Pattern(regexp = "^[A-Z0-9-]+$",
message = "must contain alphabetical characters only"),
- @Pattern(regexp = "^....-....-....$", message = "must match ....-....-....")
+ @Pattern(regexp = "^....-....-....$", message = "must match {regexp}")
})
private String serialNumber;
Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/PlayedWith.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/PlayedWith.java (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/PlayedWith.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -0,0 +1,29 @@
+// $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.jsr303.tck.tests.validation;
+
+import java.util.List;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public interface PlayedWith {
+ List<Actor> getPlayedWith();
+
+ void addPlayedWith(Actor playedWith);
+}
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -155,9 +155,9 @@
public void testValidatePropertyWithIndexedPath() {
Validator validator = TestUtil.getDefaultValidator();
- Actor clint = new Actor( "Clint", "Eastwood" );
- Actor morgan = new Actor( "Morgan", null );
- Actor charlie = new Actor( "Charlie", "Sheen" );
+ ActorListBased clint = new ActorListBased( "Clint", "Eastwood" );
+ ActorListBased morgan = new ActorListBased( "Morgan", null );
+ ActorListBased charlie = new ActorListBased( "Charlie", "Sheen" );
clint.addPlayedWith( charlie );
charlie.addPlayedWith( clint );
@@ -167,14 +167,14 @@
clint.addPlayedWith( morgan );
String property = "playedWith[0].playedWith[1].lastName";
- Set<ConstraintViolation<Actor>> constraintViolations = validator.validateProperty(
+ Set<ConstraintViolation<ActorListBased>> constraintViolations = validator.validateProperty(
clint, property
);
assertCorrectNumberOfViolations( constraintViolations, 1 );
- ConstraintViolation<Actor> violation = constraintViolations.iterator().next();
+ ConstraintViolation<ActorListBased> violation = constraintViolations.iterator().next();
assertCorrectConstraintType( violation, NotNull.class );
- assertConstraintViolation( violation, Actor.class, null, property );
+ assertConstraintViolation( violation, ActorListBased.class, null, property );
assertCorrectConstraintViolationMessage( violation, "Everyone has a last name." );
}
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -21,17 +21,20 @@
import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
import javax.validation.Validator;
+import javax.validation.constraints.Pattern;
import javax.validation.groups.Default;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import org.jboss.test.audit.annotations.SpecAssertion;
+import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.testharness.AbstractTest;
import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ArtifactType;
import org.jboss.testharness.impl.packaging.Classes;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import org.testng.annotations.Test;
@@ -48,7 +51,10 @@
public class ValidateTest extends AbstractTest {
@Test
- @SpecAssertion(section = "5.1", id = "a")
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "a"),
+ @SpecAssertion(section = "5.1", id = "a")
+ })
public void testValidatedPropertyDoesNotFollowJavaBeansConvention() {
try {
Boy boy = new Boy();
@@ -58,65 +64,33 @@
catch ( ValidationException e ) {
// success
}
+
}
@Test
- @SpecAssertion(section = "5.1", id = "a")
- public void testBasicValidation() {
+ @SpecAssertion(section = "4.1.1", id = "c")
+ public void testMultipleViolationOfTheSameType() {
Validator validator = TestUtil.getDefaultValidator();
- Customer customer = new Customer();
- customer.setFirstName( "John" );
+ Engine engine = new Engine();
+ engine.setSerialNumber( "mail(a)foobar.com" );
+ Set<ConstraintViolation<Engine>> constraintViolations = validator.validate( engine );
+ assertEquals( constraintViolations.size(), 2, "Wrong number of constraints" );
- Set<ConstraintViolation<Customer>> constraintViolations = validator.validate( customer );
+ engine.setSerialNumber( "ABCDEFGH1234" );
+ constraintViolations = validator.validate( engine );
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- customer.setLastName( "Doe" );
-
- constraintViolations = validator.validate( customer );
+ engine.setSerialNumber( "ABCD-EFGH-1234" );
+ constraintViolations = validator.validate( engine );
assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
}
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testNullParamterToValidatorImplConstructor() {
- TestUtil.getDefaultValidator().getConstraintsForClass( null );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testValidateWithNull() {
- Validator validator = TestUtil.getDefaultValidator();
- validator.validate( null );
- }
-
@Test
- @SuppressWarnings("NullArgumentToVariableArgMethod")
- public void testPassingNullAsGroup() {
+ @SpecAssertion(section = "4.1.1", id = "c")
+ public void testMultipleConstraintViolationOfDifferentTypes() {
Validator validator = TestUtil.getDefaultValidator();
- Customer customer = new Customer();
- try {
- validator.validate( customer, null );
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
- }
- @Test
- public void testValidateWithNullProperty() {
- Validator validator = TestUtil.getDefaultValidator();
- try {
- validator.validate( null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
- }
-
- @Test
- public void testMultipleValidationMethods() {
- Validator validator = TestUtil.getDefaultValidator();
-
Address address = new Address();
address.setAddressline1( null );
address.setAddressline2( null );
@@ -126,58 +100,77 @@
assertEquals(
constraintViolations.size(),
3,
- "we should have been 2 not null violation for addresslines and one length violation for city"
+ "We should have been 2 @NotNull violations for addresslines and one @Length violation for city."
);
}
@Test
- public void testValidateSet() {
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "a"),
+ @SpecAssertion(section = "4.2", id = "a"),
+ @SpecAssertion(section = "4.2", id = "b"),
+ @SpecAssertion(section = "4.2", id = "c"),
+ @SpecAssertion(section = "4.2", id = "d"),
+ @SpecAssertion(section = "4.2", id = "e"),
+ @SpecAssertion(section = "4.2", id = "g")
+ })
+ public void testConstraintViolation() {
Validator validator = TestUtil.getDefaultValidator();
- Customer customer = new Customer();
- customer.setFirstName( "John" );
- customer.setLastName( "Doe" );
+ Engine engine = new Engine();
+ engine.setSerialNumber( "mail(a)foobar.com" );
+ engine.setSerialNumber( "ABCDEFGH1234" );
+ Set<ConstraintViolation<Engine>> constraintViolations = validator.validate( engine );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- Set<ConstraintViolation<Customer>> constraintViolations = validator.validate( customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
+ ConstraintViolation<Engine> violation = constraintViolations.iterator().next();
- Order order = new Order();
- customer.addOrder( order );
+ assertEquals( violation.getMessage(), "must match ^....-....-....$", "Wrong message" );
+ assertEquals( violation.getMessageTemplate(), "must match {regexp}", "Wrong message template" );
+ assertEquals( violation.getRootBean(), engine, "Wrong root entity." );
+ assertEquals( violation.getInvalidValue(), "ABCDEFGH1234", "Wrong validated value" );
+ assertNotNull( violation.getConstraintDescriptor(), "Constraint descriptor should not be null" );
+ assertEquals(
+ violation.getConstraintDescriptor().getAnnotation().annotationType(),
+ Pattern.class, "Wrong annotation type"
+ );
+ assertEquals( violation.getPropertyPath(), "serialNumber", "Wrong property path." );
- constraintViolations = validator.validate( customer );
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- assertEquals( "may not be null", constraintViolation.getMessage(), "Wrong message" );
- org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer, "Wrong root entity" );
- org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(), order.getOrderNumber(), "Wrong value" );
- assertEquals( "orders[].orderNumber", constraintViolation.getPropertyPath(), "Wrong propertyName" );
+ engine.setSerialNumber( "ABCD-EFGH-1234" );
+ constraintViolations = validator.validate( engine );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
}
@Test
- public void testMultiValueConstraint() {
+ @SpecAssertions({
+ @SpecAssertion(section = "4.2", id = "f"),
+ @SpecAssertion(section = "4.2", id = "g")
+ })
+ public void testValidateAssociation() {
Validator validator = TestUtil.getDefaultValidator();
- Engine engine = new Engine();
- engine.setSerialNumber( "mail(a)foobar.com" );
- Set<ConstraintViolation<Engine>> constraintViolations = validator.validate( engine );
- assertEquals( constraintViolations.size(), 2, "Wrong number of constraints" );
+ Customer customer = new Customer();
+ customer.setFirstName( "John" );
+ customer.setLastName( "Doe" );
+ Order order = new Order();
+ customer.addOrder( order );
- engine.setSerialNumber( "ABCDEFGH1234" );
- constraintViolations = validator.validate( engine );
+ Set<ConstraintViolation<Customer>> constraintViolations = validator.validate( customer );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
-
- engine.setSerialNumber( "ABCD-EFGH-1234" );
- constraintViolations = validator.validate( engine );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
+ assertEquals( constraintViolation.getRootBean(), customer, "Wrong root entity" );
+ assertEquals( constraintViolation.getInvalidValue(), order.getOrderNumber(), "Wrong value" );
+ assertEquals( "orders[].orderNumber", constraintViolation.getPropertyPath(), "Wrong propertyName" );
}
@Test
- public void testGraphValidation() {
+ @SpecAssertion(section = "4.2", id = "h")
+ public void testGraphValidationWithList() {
Validator validator = TestUtil.getDefaultValidator();
- Actor clint = new Actor( "Clint", "Eastwood" );
- Actor morgan = new Actor( "Morgan", null );
- Actor charlie = new Actor( "Charlie", "Sheen" );
+ Actor clint = new ActorListBased( "Clint", "Eastwood" );
+ Actor morgan = new ActorListBased( "Morgan", null );
+ Actor charlie = new ActorListBased( "Charlie", "Sheen" );
clint.addPlayedWith( charlie );
charlie.addPlayedWith( clint );
@@ -199,81 +192,57 @@
}
@Test
- public void testValidateValue() {
+ @SpecAssertion(section = "4.2", id = "h")
+ public void testGraphValidationWithArray() {
Validator validator = TestUtil.getDefaultValidator();
- Set<ConstraintViolation<Customer>> constraintViolations = validator.validateValue(
- Customer.class, "orders[0].orderNumber", null
- );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
+ Actor clint = new ActorArrayBased( "Clint", "Eastwood" );
+ Actor morgan = new ActorArrayBased( "Morgan", null );
+ Actor charlie = new ActorArrayBased( "Charlie", "Sheen" );
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ clint.addPlayedWith( charlie );
+ charlie.addPlayedWith( clint );
+ charlie.addPlayedWith( morgan );
+ morgan.addPlayedWith( charlie );
+ morgan.addPlayedWith( clint );
+ clint.addPlayedWith( morgan );
+
+ Set<ConstraintViolation<Actor>> constraintViolations = validator.validate( clint );
assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- assertEquals( "may not be null", constraintViolation.getMessage(), "Wrong message" );
- assertEquals( constraintViolation.getRootBean(), null, "Wrong root entity" );
- assertEquals( constraintViolation.getRootBeanClass(), Customer.class, "Wrong root bean class" );
- assertEquals( constraintViolation.getInvalidValue(), null, "Wrong value" );
- assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(), "Wrong propertyName" );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Everyone has a last name.", constraintViolation.getMessage(), "Wrong message" );
+ org.testng.Assert.assertEquals( constraintViolation.getRootBean(), clint, "Wrong root entity" );
+ org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(), morgan.getLastName(), "Wrong value" );
+ assertEquals(
+ constraintViolation.getPropertyPath(), "playedWith[0].playedWith[1].lastName", "Wrong propertyName"
+ );
+ }
- constraintViolations = validator.validateValue( Customer.class, "orders[0].orderNumber", 1234 );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testNullParamterToValidatorImplConstructor() {
+ TestUtil.getDefaultValidator().getConstraintsForClass( null );
}
- @Test
- public void testValidateValueWithInvalidPropertyPath() {
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testValidateWithNull() {
Validator validator = TestUtil.getDefaultValidator();
+ validator.validate( null );
+ }
+ @Test
+ @SuppressWarnings("NullArgumentToVariableArgMethod")
+ public void testPassingNullAsGroup() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Customer customer = new Customer();
try {
- validator.validateValue( Customer.class, "", null );
- fail();
+ validator.validate( customer, null );
}
catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
+ // success
}
-
- try {
- validator.validateValue( Customer.class, "foobar", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
- }
-
- try {
- validator.validateValue( Customer.class, "orders[0].foobar", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
- }
}
@Test
- public void testValidateProperty() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- Order order = new Order();
- customer.addOrder( order );
-
- Set<ConstraintViolation<Customer>> constraintViolations = validator.validateProperty(
- customer, "orders[0].orderNumber"
- );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
-
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints" );
- assertEquals( "may not be null", constraintViolation.getMessage(), "Wrong message" );
- org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer, "Wrong root entity" );
- org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(), order.getOrderNumber(), "Wrong value" );
- assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(), "Wrong propertyName" );
-
- order.setOrderNumber( 1234 );
- constraintViolations = validator.validateProperty( customer, "orderList[0].orderNumber" );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints" );
- }
-
- @Test
public void testValidationIsPolymorphic() {
Validator validator = TestUtil.getDefaultValidator();
Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/xmlconfiguration/ConsistentUserInformation.java 2009-06-22 14:15:57 UTC (rev 16855)
@@ -48,5 +48,5 @@
public abstract Pattern[] patterns();
- public abstract UserType userType() default UserType.BUYER;
+ //public abstract UserType userType() default UserType.BUYER;
}
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-22 13:54:37 UTC (rev 16854)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-22 14:15:57 UTC (rev 16855)
@@ -573,7 +573,7 @@
</assertion>
<assertion id="f">
<text>if the association is not hosted by the root object (ie hosted on an associated
- object) a dot (.) is concaten- ated to the propertyPath</text>
+ object) a dot (.) is concatenated to the propertyPath</text>
</assertion>
<assertion id="g">
<text>the name of the association property (field name or Java Bean property name) is
@@ -581,7 +581,7 @@
</assertion>
<assertion id="h">
<text>if the association is a List or an array, the index value surrounded by square
- brackets ([index]) is concat- enated to the propertyPath (for example
+ brackets ([index]) is concatenated to the propertyPath (for example
order.orderLines[1])</text>
</assertion>
<assertion id="i">
@@ -590,18 +590,9 @@
the propertyPath (for example item.evaluation["quality"])</text>
</assertion>
<assertion id="j">
- <text>if the property level constraint is not hosted by the root object (ie hosted on an
- associated object) a dot (.) is concatenated to the propertyPath</text>
+ <text>If the propertyPath is empty, "" is returned (typically a class-level constraint
+ on the root object)</text>
</assertion>
- <assertion id="k">
- <text>the name of the property (field name or Java Bean property name) is concatenated
- to the propertyPath</text>
- </assertion>
- <assertion id="l">
- <text>nothing is concatenated to the propertyPath, it is considered complete. If the
- propertyPath is empty, "" is returned (typically a class-level constraint on the
- root object)</text>
- </assertion>
</section>
<section id="4.3.1" title="Default message interpolation">
<assertion id="a">
@@ -921,7 +912,8 @@
returned</text>
</assertion>
<assertion id="f">
- <text>The groups of a composing constraint are the groups of the composed constraint</text>
+ <text>The groups of a composing constraint are the groups of the composed
+ constraint</text>
</assertion>
</section>
<section id="6" title="Built-in Constraint definitions">
15 years, 5 months
Hibernate SVN: r16854 - beanvalidation/trunk/validation-api/src/main/java/javax/validation.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-06-22 09:54:37 -0400 (Mon, 22 Jun 2009)
New Revision: 16854
Added:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Path.java
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintValidatorContext.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/TraversableResolver.java
Log:
BVAL-143 Describe path with an object model
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintValidatorContext.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintValidatorContext.java 2009-06-22 13:53:50 UTC (rev 16853)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintValidatorContext.java 2009-06-22 13:54:37 UTC (rev 16854)
@@ -27,52 +27,190 @@
* Disable the default error message and default ConstraintViolation object generation.
* Useful to set a different error message or generate a ConstraintViolation based on
* a different property
- *
- * @see #addError(String)
- * @see #addError(String, String)
*/
void disableDefaultError();
/**
* @return the current uninterpolated default message
*/
- String getDefaultErrorMessage();
+ String getDefaultErrorMessageTemplate();
/**
- * Add a new error message. This error message will be interpolated.
+ * Return an error builder building an error allowing to optionally associate
+ * the error to a sub path.
+ * The error message will be interpolated.
* <p/>
+ * To create the error, one must call either one of
+ * the #addError method available in one of the
+ * interfaces of the fluent API.
+ * If another method is called after #addError() on
+ * ErrorBuilder or any of its associated nested interfaces
+ * an IllegalStateException is raised.
+ * <p/>
* If <code>isValid<code> returns <code>false</code>, a <code>ConstraintViolation</code> object will be built
- * per error message including the default one unless {@link #disableDefaultError()} has been called.
+ * per error including the default one unless {@link #disableDefaultError()} has been called.
* <p/>
- * Aside from the error message, <code>ConstraintViolation</code> objects generated from such a call
- * contain the same contextual information (root bean, path and so on)
+ * <code>ConstraintViolation</code> objects generated from such a call
+ * contain the same contextual information (root bean, path and so on) unless
+ * the path has been overriden.
* <p/>
- * This method can be called multiple times. One <code>ConstraintViolation</code> instance per
- * call is created.
+ * To create a different error, a new error builder has to be retrieved from
+ * ConstraintValidatorContext
*
- * @param message new uninterpolated error message.
+ * Here are a few usage examples:
+ * <pre>//create new error with the default path the constraint
+ * //is located on
+ * context.buildErrorWithMessageTemplate( "way too long" )
+ * .addError();
+ *
+ * //create new error in the "street" subnode of the default
+ * //path the constraint is located on
+ * context.buildErrorWithMessageTemplate( "way too long" )
+ * .inSubNode( "street" )
+ * .addError();
+ *
+ * //create new error in the "addresses["home"].city.name
+ * //subnode of the default path the constraint is located on
+ * context.buildErrorWithMessageTemplate( "this detail is wrong" )
+ * .inSubNode( "addresses" )
+ * .inSubNode( "country" )
+ * .inIterable().atKey( "home" )
+ * .inSubNode( "name" )
+ * .addError();
+ * </pre>
+ *
+ * @param messageTemplate new uninterpolated error message.
*/
- void addError(String message);
+ ErrorBuilder buildErrorWithMessageTemplate(String messageTemplate);
/**
- * Add a new error message to a given sub property <code>property</code>. The subproperty
- * is relative to the path to the bean or property hosting the constraint.
+ * Error builder allowing to optionally associate
+ * the error to a sub path.
*
- * This error message will be interpolated.
- * <p/>
- * If <code>isValid</code> returns <code>false</code>, a <code>ConstraintViolation</code> object will be built
- * per error message including the default one unless {@link #disableDefaultError()}
- * has been called.
- * <p/>
- * Aside from the error message and the property path, <code>ConstraintViolation</code> objects
- * generated from such a call contain the same contextual information
- * (root bean, leaf bean etc)
- * <p/>
- * This method can be called multiple times. One <code>ConstraintViolation</code> instance per
- * call is created.
- *
- * @param message new uninterpolated error message.
- * @param property property name the </code>ConstraintViolation</code> is targeting.
+ * To create the error, one must call either one of
+ * the #addError method available in one of the
+ * interfaces of the fluent API.
+ * If another method is called after #addError() on
+ * ErrorBuilder or any of its associated objects
+ * an IllegalStateException is raised.
+ *
*/
- void addError(String message, String property);
+ interface ErrorBuilder {
+ /**
+ * Add a subNode to the path the error will be associated to
+ *
+ * @param name property
+ * @return a builder representing the first level node
+ */
+ NodeBuilder inSubNode(String name);
+
+ /**
+ * Add the new error report to be generated if the
+ * constraint validator mark the value as invalid.
+ * Methods of this ErrorBuilder instance and its nested
+ * objects returns IllegalStateException from now on.
+ *
+ * @return ConstraintValidatorContext instance the ErrorBuilder comes from
+ */
+ ConstraintValidatorContext addError();
+
+ /**
+ * Represent asubnode whose context is known
+ * (ie index, key and isInIterable)
+ */
+ interface NodeBuilder {
+ /**
+ * Add a subNode to the path the error will be associated to
+ *
+ * @param name property
+ * @return a builder representing this node
+ */
+ InIterableNodeBuilder inSubNode(String name);
+
+ /**
+ * Add the new error report to be generated if the
+ * constraint validator mark the value as invalid.
+ * Methods of the ErrorBuilder instance this object comes
+ * from and the error builder nested
+ * objects returns IllegalStateException from now on.
+ *
+ * @return ConstraintValidatorContext instance the ErrorBuilder comes from
+ */
+ ConstraintValidatorContext addError();
+ }
+
+ /**
+ * Represent a subnode whose context is
+ * configurable (ie index, key and isInIterable)
+ */
+ interface InIterableNodeBuilder {
+ /**
+ * Mark the node as being in an Iterable or a Map
+ * @return a builder representing iterable details
+ */
+ InIterablePropertiesBuilder inIterable();
+
+ /**
+ * Add a subNode to the path the error will be associated to
+ *
+ * @param name property
+ * @return a builder representing this node
+ */
+ InIterableNodeBuilder inSubNode(String name);
+
+ /**
+ * Add the new error report to be generated if the
+ * constraint validator mark the value as invalid.
+ * Methods of the ErrorBuilder instance this object comes
+ * from and the error builder nested
+ * objects returns IllegalStateException from now on.
+ *
+ * @return ConstraintValidatorContext instance the ErrorBuilder comes from
+ */
+ ConstraintValidatorContext addError();
+ }
+
+ /**
+ * Represent choices for a node which is
+ * in an Iterator or Map.
+ * If the iterator is an indexed collection or a map,
+ * the index or the key should be set.
+ */
+ interface InIterablePropertiesBuilder {
+ /**
+ * Define the key the object is into the Map
+ *
+ * @param key map key
+ * @return a builder representing the current node
+ */
+ NodeBuilder atKey(Object key);
+
+ /**
+ * Define the index the object is into the List or array
+ *
+ * @param index index
+ * @return a builder representing the current node
+ */
+ NodeBuilder atIndex(Integer index);
+
+ /**
+ * Add a subNode to the path the error will be associated to
+ *
+ * @param name property
+ * @return a builder representing this node
+ */
+ InIterableNodeBuilder inSubNode(String name);
+
+ /**
+ * Add the new error report to be generated if the
+ * constraint validator mark the value as invalid.
+ * Methods of the ErrorBuilder instance this object comes
+ * from and the error builder nested
+ * objects returns IllegalStateException from now on.
+ *
+ * @return ConstraintValidatorContext instance the ErrorBuilder comes from
+ */
+ ConstraintValidatorContext addError();
+ }
+ }
}
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java 2009-06-22 13:53:50 UTC (rev 16853)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java 2009-06-22 13:54:37 UTC (rev 16854)
@@ -18,6 +18,7 @@
package javax.validation;
import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.Path;
/**
* Describe a constraint violation. This object describe the error context as
@@ -59,10 +60,9 @@
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.
+ * @return the property path to the value from <code>rootBean</code>.
*/
- String getPropertyPath();
+ Path getPropertyPath();
/**
* @return the value failing to pass the constraint.
Added: beanvalidation/trunk/validation-api/src/main/java/javax/validation/Path.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/Path.java (rev 0)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/Path.java 2009-06-22 13:54:37 UTC (rev 16854)
@@ -0,0 +1,44 @@
+package javax.validation;
+
+/**
+ * Represent a navigation path from an object to another.
+ * Each path element is represented by a Node.
+ *
+ * The path corresponds to the succession of nodes
+ * in the order they are retured by the Iterator
+ *
+ * @author Emmanuel Bernard
+ */
+public interface Path extends Iterable<Path.Node> {
+
+ /**
+ * Represents an element of a navigation path
+ */
+ interface Node {
+ /**
+ * Property name the node represents
+ * or null if the leaf node and representing an entity
+ * (in particular the node representing the root object
+ * has its name null)
+ */
+ String getName();
+
+ /**
+ * True if the node represents an object contained in an Iterable
+ * or in a Map.
+ */
+ boolean isInIterable();
+
+ /**
+ * The index the node is placed in if contained
+ * in an array or List. Null otherwise.
+ */
+ Integer getIndex();
+
+ /**
+ * The key the node is placed in if contained
+ * in a Map. Null otherwise.
+ */
+ Object getKey();
+ }
+}
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/TraversableResolver.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/TraversableResolver.java 2009-06-22 13:53:50 UTC (rev 16853)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/TraversableResolver.java 2009-06-22 13:54:37 UTC (rev 16854)
@@ -18,6 +18,7 @@
package javax.validation;
import java.lang.annotation.ElementType;
+import javax.validation.Path;
/**
* Contract determining if a property can be accessed by the Bean Validation provider
@@ -32,11 +33,10 @@
* Determine if Bean Validation is allowed to reach the property state
*
* @param traversableObject object hosting <code>traversableProperty</code>, null if validateValue is called
- * @param traversableProperty name of the traversable property.
+ * @param traversableProperty the traversable property.
* @param rootBeanType type of the root object passed to the Validator.
* @param pathToTraversableObject path from the root object to
- * <code>traversableObject</code> ("" if the <code>traversableObject</code>
- * is the root object)
+ * <code>traversableObject</code>
* (using the path specification defined by Bean Validator).
* @param elementType either <code>FIELD</code> or <code>METHOD</code>.
*
@@ -44,9 +44,9 @@
* <code>false</code> otherwise.
*/
boolean isReachable(Object traversableObject,
- String traversableProperty,
+ Path.Node traversableProperty,
Class<?> rootBeanType,
- String pathToTraversableObject,
+ Path pathToTraversableObject,
ElementType elementType);
/**
@@ -57,11 +57,10 @@
* arguments and if the property is marked as <code>@Valid</code>
*
* @param traversableObject object hosting <code>traversableProperty</code>, null if validateValue is called
- * @param traversableProperty name of the traversable property.
+ * @param traversableProperty the traversable property.
* @param rootBeanType type of the root object passed to the Validator.
* @param pathToTraversableObject path from the root object to
- * <code>traversableObject</code> ("" if the <code>traversableObject</code>
- * is the root object)
+ * <code>traversableObject</code>
* (using the path specification defined by Bean Validator).
* @param elementType either <code>FIELD</code> or <code>METHOD</code>.
*
@@ -69,8 +68,8 @@
* <code>false</code> otherwise.
*/
boolean isCascadable(Object traversableObject,
- String traversableProperty,
+ Path.Node traversableProperty,
Class<?> rootBeanType,
- String pathToTraversableObject,
+ Path pathToTraversableObject,
ElementType elementType);
}
15 years, 5 months