Author: hardy.ferentschik
Date: 2009-03-19 12:00:24 -0400 (Thu, 19 Mar 2009)
New Revision: 16187
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ConstraintDescriptorTest.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/PropertyDescriptorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
Log:
HV-116
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java 2009-03-19
14:16:07 UTC (rev 16186)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintDescriptorImpl.java 2009-03-19
16:00:24 UTC (rev 16187)
@@ -137,44 +137,26 @@
}
}
- /**
- * {@inheritDoc}
- */
public T getAnnotation() {
return annotation;
}
- /**
- * {@inheritDoc}
- */
public Set<Class<?>> getGroups() {
return Collections.unmodifiableSet( groups );
}
- /**
- * {@inheritDoc}
- */
public List<Class<? extends ConstraintValidator<T, ?>>>
getConstraintValidatorClasses() {
return Collections.unmodifiableList( constraintValidatorDefinitonClasses );
}
- /**
- * {@inheritDoc}
- */
public Map<String, Object> getAttributes() {
return Collections.unmodifiableMap( attributes );
}
- /**
- * {@inheritDoc}
- */
public Set<ConstraintDescriptor<?>> getComposingConstraints() {
return Collections.unmodifiableSet( composingConstraints );
}
- /**
- * {@inheritDoc}
- */
public boolean isReportAsSingleViolation() {
return isReportAsSingleInvalidConstraint;
}
Copied:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ConstraintDescriptorTest.java
(from rev 16186,
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/PropertyDescriptorTest.java)
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ConstraintDescriptorTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ConstraintDescriptorTest.java 2009-03-19
16:00:24 UTC (rev 16187)
@@ -0,0 +1,136 @@
+// $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.engine.metadata;
+
+import java.lang.annotation.Annotation;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.ConstraintDescriptor;
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+import org.hibernate.validation.constraints.NotNullValidator;
+import org.hibernate.validation.engine.Order;
+import org.hibernate.validation.engine.constraintcomposition.GermanAddress;
+import org.hibernate.validation.util.TestUtil;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintDescriptorTest {
+ @Test
+ public void testConstraintDescriptorImmutable() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+
+ try {
+ descriptor.getGroups().add( Default.class );
+ fail( "Should be immutable" );
+ }
+ catch ( UnsupportedOperationException e ) {
+
+ }
+
+ try {
+ descriptor.getAttributes().put( "foo", "bar" );
+ fail( "Should be immutable" );
+ }
+ catch ( UnsupportedOperationException e ) {
+
+ }
+
+ try {
+ descriptor.getConstraintValidatorClasses().add( null );
+ fail( "Should be immutable" );
+ }
+ catch ( UnsupportedOperationException e ) {
+
+ }
+
+ try {
+ descriptor.getComposingConstraints().add( null );
+ fail( "Should be immutable" );
+ }
+ catch ( UnsupportedOperationException e ) {
+
+ }
+ }
+
+ @Test
+ public void testReportAsSingleViolation() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ assertFalse( descriptor.isReportAsSingleViolation() );
+
+ descriptor = TestUtil.getSingleConstraintDescriptorFor( GermanAddress.class,
"zipCode" );
+ assertTrue( descriptor.isReportAsSingleViolation() );
+ }
+
+ @Test
+ public void testComposingConstraints() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ assertTrue( descriptor.getComposingConstraints().isEmpty() );
+ }
+
+ /**
+ * @todo Is getComposingConstraints() recursive and hence the result should be 4?
+ */
+ @Test
+ public void testEmptyComposingConstraints() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
+ GermanAddress.class, "zipCode"
+ );
+ assertEquals( "Wrong number of composing constraints", 1,
descriptor.getComposingConstraints().size() );
+ }
+
+ @Test
+ public void testGetAnnotation() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ Annotation annotation = descriptor.getAnnotation();
+ assertNotNull( annotation );
+ assertTrue( annotation instanceof NotNull );
+ }
+
+ @Test
+ public void testDefaultGroupIsReturned() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ Set<Class<?>> groups = descriptor.getGroups();
+ assertTrue( groups.size() == 1 );
+ assertEquals( "Wrong group", Default.class, groups.iterator().next() );
+ }
+
+ @Test
+ public void testGetConstraintValidatorClasses() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ assertEquals( "Wrong classes", NotNullValidator.class,
descriptor.getConstraintValidatorClasses().get( 0 ) );
+ }
+
+ @Test
+ public void testGetAttributes() {
+ ConstraintDescriptor<?> descriptor = TestUtil.getSingleConstraintDescriptorFor(
Order.class, "orderNumber" );
+ Map<String, Object> attributes = descriptor.getAttributes();
+ assertTrue( attributes.containsKey( "message" ) );
+ assertTrue( attributes.containsKey( "groups" ) );
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ConstraintDescriptorTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorTest.java 2009-03-19
14:16:07 UTC (rev 16186)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorTest.java 2009-03-19
16:00:24 UTC (rev 16187)
@@ -46,9 +46,7 @@
@Test
public void testGetTypeForConstrainedProperty() {
- Validator validator = TestUtil.getValidator();
- ElementDescriptor elementDescriptor = validator.getConstraintsForClass( Order.class )
- .getConstraintsForProperty( "orderNumber" );
+ ElementDescriptor elementDescriptor = TestUtil.getPropertyDescriptor( Order.class,
"orderNumber" );
assertEquals( "Wrong type.", Integer.class, elementDescriptor.getType() );
}
@@ -57,9 +55,7 @@
*/
@Test
public void testElementDescriptorForProperty() {
- Validator validator = TestUtil.getValidator();
- ElementDescriptor elementDescriptor = validator.getConstraintsForClass( Order.class )
- .getConstraintsForProperty( "orderNumber" );
+ ElementDescriptor elementDescriptor = TestUtil.getPropertyDescriptor( Order.class,
"orderNumber" );
Set<ConstraintDescriptor<?>> constraintDescriptors =
elementDescriptor.getConstraintDescriptors();
assertTrue( "There should be a descriptor", constraintDescriptors.size() == 1
);
}
@@ -69,14 +65,11 @@
*/
@Test
public void testElementDescriptorImmutable() {
- Validator validator = TestUtil.getValidator();
- ElementDescriptor elementDescriptor = validator.getConstraintsForClass( Order.class )
- .getConstraintsForProperty( "orderNumber" );
+ ElementDescriptor elementDescriptor = TestUtil.getPropertyDescriptor( Order.class,
"orderNumber" );
Set<ConstraintDescriptor<?>> constraintDescriptors =
elementDescriptor.getConstraintDescriptors();
- ConstraintDescriptor<?> descriptor = constraintDescriptors.iterator().next();
try {
- constraintDescriptors.add( descriptor );
+ constraintDescriptors.add( null );
fail( "Set should be immutable" );
}
catch ( UnsupportedOperationException e ) {
@@ -84,7 +77,7 @@
}
try {
- constraintDescriptors.remove( descriptor );
+ constraintDescriptors.remove( null );
fail( "Set should be immutable" );
}
catch ( UnsupportedOperationException e ) {
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/PropertyDescriptorTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/PropertyDescriptorTest.java 2009-03-19
14:16:07 UTC (rev 16186)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/PropertyDescriptorTest.java 2009-03-19
16:00:24 UTC (rev 16187)
@@ -18,7 +18,6 @@
package org.hibernate.validation.engine.metadata;
import javax.validation.PropertyDescriptor;
-import javax.validation.Validator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -35,26 +34,20 @@
public class PropertyDescriptorTest {
@Test
public void testIsNotCascaded() {
- Validator validator = TestUtil.getValidator();
- PropertyDescriptor descriptor = validator.getConstraintsForClass( Order.class )
- .getConstraintsForProperty( "orderNumber" );
+ PropertyDescriptor descriptor = TestUtil.getPropertyDescriptor( Order.class,
"orderNumber" );
assertFalse( "Should not be cascaded", descriptor.isCascaded() );
}
@Test
public void testIsCascaded() {
- Validator validator = TestUtil.getValidator();
- PropertyDescriptor descriptor = validator.getConstraintsForClass( Customer.class )
- .getConstraintsForProperty( "orderList" );
+ PropertyDescriptor descriptor = TestUtil.getPropertyDescriptor( Customer.class,
"orderList" );
assertTrue( "Should be cascaded", descriptor.isCascaded() );
}
@Test
public void testPropertyName() {
- Validator validator = TestUtil.getValidator();
String propertyName = "orderList";
- PropertyDescriptor descriptor = validator.getConstraintsForClass( Customer.class )
- .getConstraintsForProperty( propertyName );
+ PropertyDescriptor descriptor = TestUtil.getPropertyDescriptor( Customer.class,
propertyName );
assertEquals( "Wrong property name", propertyName,
descriptor.getPropertyName() );
}
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-03-19
14:16:07 UTC (rev 16186)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-03-19
16:00:24 UTC (rev 16187)
@@ -18,11 +18,15 @@
package org.hibernate.validation.util;
import java.util.Set;
+import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintViolation;
+import javax.validation.ElementDescriptor;
import javax.validation.Validation;
import javax.validation.Validator;
+import javax.validation.PropertyDescriptor;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.hibernate.validation.engine.HibernateValidatorConfiguration;
@@ -48,7 +52,25 @@
return hibernateValidator;
}
- public static void assertConstraintViolation(ConstraintViolation violation, String
errorMessage, Class rootBean, Object invalidValue, String propertyPath, Class leafBean)
{
+ public static ConstraintDescriptor<?>
getSingleConstraintDescriptorFor(Class<?> clazz, String property) {
+ Set<ConstraintDescriptor<?>> constraintDescriptors =
getConstraintDescriptorsFor( clazz, property );
+ assertTrue(
+ "This method should only be used when there is a single constraint",
constraintDescriptors.size() == 1
+ );
+ return constraintDescriptors.iterator().next();
+ }
+
+ public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String
property) {
+ Validator validator = getValidator();
+ return validator.getConstraintsForClass( clazz ).getConstraintsForProperty( property
);
+ }
+
+ public static Set<ConstraintDescriptor<?>>
getConstraintDescriptorsFor(Class<?> clazz, String property) {
+ ElementDescriptor elementDescriptor = getPropertyDescriptor( clazz, property );
+ return elementDescriptor.getConstraintDescriptors();
+ }
+
+ public static void assertConstraintViolation(ConstraintViolation violation, String
errorMessage, Class rootBean, Object invalidValue, String propertyPath, Class leafBean) {
assertEquals(
"Wrong leaf bean type",
leafBean,