Author: hardy.ferentschik
Date: 2010-05-31 10:03:26 -0400 (Mon, 31 May 2010)
New Revision: 19635
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefWrapper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/GenericConstraintDef.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraint.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraintValidator.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefAccessor.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDef.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java
Log:
HV-274 Added implementation for Generic ConstraintDef
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDef.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDef.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDef.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -30,7 +30,7 @@
* @author Hardy Ferentschik
*/
public class ConstraintDef<A extends Annotation> {
- protected final Class<A> constraintType;
+ protected Class<A> constraintType;
protected final Map<String, Object> parameters;
protected final Class<?> beanType;
protected final ElementType elementType;
@@ -46,10 +46,6 @@
throw new ValidationException( "Null is not a valid bean type" );
}
- if ( constraintType == null ) {
- throw new ValidationException( "Null is not a valid constraint type" );
- }
-
if ( mapping == null ) {
throw new ValidationException( "ConstraintMapping cannot be null" );
}
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefAccessor.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefAccessor.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefAccessor.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -1,52 +0,0 @@
-// $Id$
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.validator.cfg;
-
-import java.lang.annotation.Annotation;
-import java.lang.annotation.ElementType;
-import java.util.Map;
-
-/**
- * @author Hardy Ferentschik
- */
-public class ConstraintDefAccessor<A extends Annotation> extends
ConstraintDef<A> {
-
- public ConstraintDefAccessor(Class<?> beanType, Class<A> constraintType,
String property, ElementType elementType, Map<String, Object> parameters,
ConstraintMapping mapping) {
- super( beanType, constraintType, property, elementType, parameters, mapping );
- }
-
- public Class<A> getConstraintType() {
- return constraintType;
- }
-
- public Map<String, Object> getParameters() {
- return this.parameters;
- }
-
- public ElementType getElementType() {
- return elementType;
- }
-
- public Class<?> getBeanType() {
- return beanType;
- }
-
- public String getProperty() {
- return property;
- }
-}
\ No newline at end of file
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefWrapper.java
(from rev 19608,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefAccessor.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefWrapper.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefWrapper.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -0,0 +1,52 @@
+// $Id$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.validator.cfg;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.util.Map;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintDefWrapper<A extends Annotation> extends
ConstraintDef<A> {
+
+ public ConstraintDefWrapper(Class<?> beanType, Class<A> constraintType,
String property, ElementType elementType, Map<String, Object> parameters,
ConstraintMapping mapping) {
+ super( beanType, constraintType, property, elementType, parameters, mapping );
+ }
+
+ public Class<A> getConstraintType() {
+ return constraintType;
+ }
+
+ public Map<String, Object> getParameters() {
+ return this.parameters;
+ }
+
+ public ElementType getElementType() {
+ return elementType;
+ }
+
+ public Class<?> getBeanType() {
+ return beanType;
+ }
+
+ public String getProperty() {
+ return property;
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefWrapper.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -33,75 +33,62 @@
* @author Hardy Ferentschik
*/
public class ConstraintMapping {
- private final Map<Class<?>, List<ConstraintDefAccessor<?>>>
constraintConfig;
+ private final Map<Class<?>, List<ConstraintDef<?>>>
constraintConfig;
private final Map<Class<?>, List<CascadeDef>> cascadeConfig;
private final Set<Class<?>> configuredClasses;
private final Map<Class<?>, List<Class<?>>>
defaultGroupSequences;
public ConstraintMapping() {
- this.constraintConfig = new HashMap<Class<?>,
List<ConstraintDefAccessor<?>>>();
+ this.constraintConfig = new HashMap<Class<?>,
List<ConstraintDef<?>>>();
this.cascadeConfig = new HashMap<Class<?>, List<CascadeDef>>();
this.configuredClasses = new HashSet<Class<?>>();
this.defaultGroupSequences = new HashMap<Class<?>,
List<Class<?>>>();
}
- public ConstraintsForType type(Class<?> beanClass) {
+ /**
+ * Starts defining constraints on the specified bean class.
+ *
+ * @param beanClass The bean class on which to define constraints. All constraints
defined after calling this method
+ * are added to the bean of the type {@code beanClass} until the next call of {@code
type}.
+ *
+ * @return Instance allowing for defining constraints on the specified class.
+ */
+ public final ConstraintsForType type(Class<?> beanClass) {
return new ConstraintsForType( beanClass, this );
}
- protected <A extends Annotation> void addConstraintConfig(ConstraintDef<?>
definition) {
- Class<?> beanClass = definition.beanType;
- @SuppressWarnings( "unchecked")
- ConstraintDefAccessor<A> defAccessor = new ConstraintDefAccessor<A>(
- beanClass,
- ( Class<A> ) definition.constraintType,
- definition.property,
- definition.elementType,
- definition.parameters,
- this
- );
+ public final <A extends Annotation> Map<Class<?>,
List<ConstraintDefWrapper<?>>> getConstraintConfig() {
+ Map<Class<?>, List<ConstraintDefWrapper<?>>> newDefinitions =
new HashMap<Class<?>, List<ConstraintDefWrapper<?>>>();
+ for ( Map.Entry<Class<?>, List<ConstraintDef<?>>> entry :
constraintConfig.entrySet() ) {
- configuredClasses.add( beanClass );
- if ( constraintConfig.containsKey( beanClass ) ) {
- constraintConfig.get( beanClass ).add( defAccessor );
+ List<ConstraintDefWrapper<?>> newList = new
ArrayList<ConstraintDefWrapper<?>>();
+ for ( ConstraintDef<?> definition : entry.getValue() ) {
+ Class<?> beanClass = definition.beanType;
+ @SuppressWarnings("unchecked")
+ ConstraintDefWrapper<A> defAccessor = new ConstraintDefWrapper<A>(
+ beanClass,
+ ( Class<A> ) definition.constraintType,
+ definition.property,
+ definition.elementType,
+ definition.parameters,
+ this
+ );
+ newList.add( defAccessor );
+ }
+ newDefinitions.put( entry.getKey(), newList );
}
- else {
- List<ConstraintDefAccessor<?>> definitionList = new
ArrayList<ConstraintDefAccessor<?>>();
- definitionList.add( defAccessor );
- constraintConfig.put( beanClass, definitionList );
- }
+ return newDefinitions;
}
- protected void addCascadeConfig(CascadeDef cascade) {
- Class<?> beanClass = cascade.getBeanType();
- configuredClasses.add( beanClass );
- if ( cascadeConfig.containsKey( beanClass ) ) {
- cascadeConfig.get( beanClass ).add( cascade );
- }
- else {
- List<CascadeDef> cascadeList = new ArrayList<CascadeDef>();
- cascadeList.add( cascade );
- cascadeConfig.put( beanClass, cascadeList );
- }
- }
-
- protected void addDefaultGroupSequence(Class<?> beanClass,
List<Class<?>> defaultGroupSequence) {
- defaultGroupSequences.put( beanClass, defaultGroupSequence );
- }
-
- public Map<Class<?>, List<ConstraintDefAccessor<?>>>
getConstraintConfig() {
- return constraintConfig;
- }
-
- public Map<Class<?>, List<CascadeDef>> getCascadeConfig() {
+ public final Map<Class<?>, List<CascadeDef>> getCascadeConfig() {
return cascadeConfig;
}
- public Collection<Class<?>> getConfiguredClasses() {
+ public final Collection<Class<?>> getConfiguredClasses() {
return configuredClasses;
}
- public List<Class<?>> getDefaultSequence(Class<?> beanType) {
+ public final List<Class<?>> getDefaultSequence(Class<?> beanType) {
if ( defaultGroupSequences.containsKey( beanType ) ) {
return defaultGroupSequences.get( beanType );
}
@@ -121,6 +108,36 @@
sb.append( '}' );
return sb.toString();
}
+
+ protected final void addCascadeConfig(CascadeDef cascade) {
+ Class<?> beanClass = cascade.getBeanType();
+ configuredClasses.add( beanClass );
+ if ( cascadeConfig.containsKey( beanClass ) ) {
+ cascadeConfig.get( beanClass ).add( cascade );
+ }
+ else {
+ List<CascadeDef> cascadeList = new ArrayList<CascadeDef>();
+ cascadeList.add( cascade );
+ cascadeConfig.put( beanClass, cascadeList );
+ }
+ }
+
+ protected final void addDefaultGroupSequence(Class<?> beanClass,
List<Class<?>> defaultGroupSequence) {
+ defaultGroupSequences.put( beanClass, defaultGroupSequence );
+ }
+
+ protected final void addConstraintConfig(ConstraintDef<?> definition) {
+ Class<?> beanClass = definition.beanType;
+ configuredClasses.add( beanClass );
+ if ( constraintConfig.containsKey( beanClass ) ) {
+ constraintConfig.get( beanClass ).add( definition );
+ }
+ else {
+ List<ConstraintDef<?>> definitionList = new
ArrayList<ConstraintDef<?>>();
+ definitionList.add( definition );
+ constraintConfig.put( beanClass, definitionList );
+ }
+ }
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -27,9 +27,11 @@
import static java.lang.annotation.ElementType.TYPE;
/**
+ * Via instances of this class constraints and cascading properties can be configured for
a single bean class.
+ *
* @author Hardy Ferentschik
*/
-public class ConstraintsForType {
+public final class ConstraintsForType {
private static final String EMPTY_PROPERTY = "";
private final ConstraintMapping mapping;
@@ -48,6 +50,13 @@
this.elementType = type;
}
+ /**
+ * Add a new constraint.
+ *
+ * @param definition The constraint definition class
+ *
+ * @return A constraint definition class allowing to specify additional constraint
parameters.
+ */
public <A extends Annotation, T extends ConstraintDef<A>> T
constraint(Class<T> definition) {
final Constructor<T> constructor = ReflectionHelper.getConstructor(
definition, Class.class, String.class, ElementType.class, ConstraintMapping.class
@@ -60,6 +69,15 @@
return constraintDefinition;
}
+ /**
+ * Changes the property for which added constraints apply. Until this method is called
constraints apply on
+ * class level. After calling this method constraints apply on the specified property
with the given access type.
+ *
+ * @param property The property on which to apply the following constraints (Java Bean
notation)
+ * @param type The access type (field/property)
+ *
+ * @return Returns itself for method chaining
+ */
public ConstraintsForType property(String property, ElementType type) {
return new ConstraintsForType( beanClass, property, type, mapping );
}
@@ -69,13 +87,26 @@
return this;
}
+ /**
+ * Defines the default groups sequence for the bean class of this instance.
+ *
+ * @param defaultGroupSequence the default group sequence.
+ *
+ * @return Returns itself for method chaining.
+ */
public ConstraintsForType defaultGroupSequence(Class<?>... defaultGroupSequence)
{
mapping.addDefaultGroupSequence( beanClass, Arrays.asList( defaultGroupSequence ) );
return this;
}
- public ConstraintsForType type(Class<?> type, Class<?>...
defaultGroupSequence) {
- mapping.addDefaultGroupSequence( type, Arrays.asList( defaultGroupSequence ) );
+ /**
+ * Creates a new {@code ConstraintsForType} in order to define constraints on a new bean
type.
+ *
+ * @param type the bean type
+ *
+ * @return a new {@code ConstraintsForType} instance
+ */
+ public ConstraintsForType type(Class<?> type) {
return new ConstraintsForType( type, mapping );
}
}
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/GenericConstraintDef.java
(from rev 19606,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/AssertFalseDef.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/GenericConstraintDef.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/GenericConstraintDef.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.
+ */
+
+// $Id$
+
+package org.hibernate.validator.cfg.defs;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+
+import org.hibernate.validator.cfg.ConstraintDef;
+import org.hibernate.validator.cfg.ConstraintMapping;
+
+/**
+ * A {@code ConstraintDef} class which can be used to configure any constraint type. For
this purpose the class defines
+ * a {@code constraintType} method to specify the constraint type and a generic {@code
param(Stringkey,Objectvalue)}
+ * to add arbitrary constraint parameters.
+ *
+ * @author Hardy Ferentschik
+ */
+public class GenericConstraintDef extends ConstraintDef {
+
+ public GenericConstraintDef(Class<?> beanType, String property, ElementType
elementType, ConstraintMapping mapping) {
+ super( beanType, null, property, elementType, mapping );
+ }
+
+ public GenericConstraintDef message(String message) {
+ addParameter( "message", message );
+ return this;
+ }
+
+ public GenericConstraintDef groups(Class<?>... groups) {
+ addParameter( "groups", groups );
+ return this;
+ }
+
+ public GenericConstraintDef payload(Class<? extends Payload>... payload) {
+ addParameter( "payload", payload );
+ return this;
+ }
+
+ public GenericConstraintDef param(String key, Object value) {
+ addParameter( key, value );
+ return this;
+ }
+
+ public GenericConstraintDef constraintType(Class<?> constraintType) {
+ this.constraintType = constraintType;
+ return this;
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/defs/GenericConstraintDef.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -35,7 +35,7 @@
import javax.validation.spi.ConfigurationState;
import org.hibernate.validator.cfg.CascadeDef;
-import org.hibernate.validator.cfg.ConstraintDefAccessor;
+import org.hibernate.validator.cfg.ConstraintDefWrapper;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.metadata.AnnotationIgnores;
import org.hibernate.validator.metadata.BeanMetaDataCache;
@@ -228,19 +228,22 @@
}
@SuppressWarnings("unchecked")
- private <T, A extends Annotation> void
addProgrammaticConfiguredConstraints(List<ConstraintDefAccessor<?>>
definitions,
+ private <T, A extends Annotation> void
addProgrammaticConfiguredConstraints(List<ConstraintDefWrapper<?>>
definitions,
Class<T> rootClass, Class<?> hierarchyClass,
Map<Class<?>, List<MetaConstraint<T, ?>>>
constraints) {
- for ( ConstraintDefAccessor<?> config : definitions ) {
+ for ( ConstraintDefWrapper<?> config : definitions ) {
A annotation = ( A ) createAnnotationProxy( config );
ConstraintOrigin definedIn = definedIn( rootClass, hierarchyClass );
ConstraintDescriptorImpl<A> constraintDescriptor = new
ConstraintDescriptorImpl<A>(
annotation, constraintHelper, config.getElementType(), definedIn
);
- Member member = ReflectionHelper.getMember(
- config.getBeanType(), config.getProperty(), config.getElementType()
- );
+ Member member = null;
+ if ( !config.getProperty().isEmpty() ) {
+ member = ReflectionHelper.getMember(
+ config.getBeanType(), config.getProperty(), config.getElementType()
+ );
+ }
MetaConstraint<T, ?> metaConstraint = new MetaConstraint(
config.getBeanType(), member, constraintDescriptor
@@ -299,7 +302,7 @@
}
@SuppressWarnings("unchecked")
- private <A extends Annotation> Annotation
createAnnotationProxy(ConstraintDefAccessor<?> config) {
+ private <A extends Annotation> Annotation
createAnnotationProxy(ConstraintDefWrapper<?> config) {
Class<A> constraintType = ( Class<A> ) config.getConstraintType();
AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(
constraintType );
for ( Map.Entry<String, Object> parameter : config.getParameters().entrySet() )
{
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -30,9 +30,10 @@
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
+import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.defs.AssertTrueDef;
-import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.defs.FutureDef;
+import org.hibernate.validator.cfg.defs.GenericConstraintDef;
import org.hibernate.validator.cfg.defs.MinDef;
import org.hibernate.validator.cfg.defs.NotEmptyDef;
import org.hibernate.validator.cfg.defs.NotNullDef;
@@ -43,6 +44,7 @@
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static org.hibernate.validator.test.util.TestUtil.assertConstraintViolation;
+import static
org.hibernate.validator.test.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
import static org.testng.Assert.assertTrue;
import static org.testng.FileAssert.fail;
@@ -58,9 +60,9 @@
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
.property( "name", METHOD )
- .constraint( NotNullDef.class )
- .property( "numberOfRunners", FIELD )
- .constraint( MinDef.class ).value( 1 );
+ .constraint( NotNullDef.class )
+ .property( "numberOfHelpers", FIELD )
+ .constraint( MinDef.class ).value( 1 );
assertTrue( mapping.getConstraintConfig().containsKey( Marathon.class ) );
assertTrue( mapping.getConstraintConfig().get( Marathon.class ).size() == 2 );
@@ -83,7 +85,7 @@
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
.property( "name", METHOD )
- .constraint( NotNullDef.class );
+ .constraint( NotNullDef.class );
config.addMapping( mapping );
@@ -101,12 +103,12 @@
ConstraintMapping mapping = new ConstraintMapping();
mapping
- .type( Marathon.class )
+ .type( Marathon.class )
.property( "name", METHOD )
- .constraint( NotNullDef.class )
- .type( Tournament.class )
+ .constraint( NotNullDef.class )
+ .type( Tournament.class )
.property( "tournamentDate", METHOD )
- .constraint( FutureDef.class );
+ .constraint( FutureDef.class );
config.addMapping( mapping );
@@ -130,10 +132,10 @@
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
- .valid( "runners", METHOD )
+ .valid( "runners", METHOD )
.type( Runner.class )
- .property( "paidEntryFee", FIELD )
- .constraint( AssertTrueDef.class );
+ .property( "paidEntryFee", FIELD )
+ .constraint( AssertTrueDef.class );
config.addMapping( mapping );
@@ -157,9 +159,9 @@
ConstraintMapping mapping = new ConstraintMapping();
try {
mapping
- .type( Marathon.class )
- .property( "numberOfRunners", METHOD )
- .constraint( NotNullDef.class );
+ .type( Marathon.class )
+ .property( "numberOfHelpers", METHOD )
+ .constraint( NotNullDef.class );
fail();
}
catch ( ValidationException e ) {
@@ -173,12 +175,12 @@
ConstraintMapping mapping = new ConstraintMapping();
mapping
- .type( Marathon.class )
+ .type( Marathon.class )
.defaultGroupSequence( Foo.class, Marathon.class )
.property( "name", METHOD )
- .constraint( NotNullDef.class ).groups( Foo.class )
+ .constraint( NotNullDef.class ).groups( Foo.class )
.property( "runners", METHOD )
- .constraint( NotEmptyDef.class );
+ .constraint( NotEmptyDef.class );
config.addMapping( mapping );
@@ -197,15 +199,15 @@
assertConstraintViolation( violations.iterator().next(), "may not be empty"
);
}
- @Test
+ @Test
public void testMultipleConstraintOfTheSameType() {
HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Marathon.class )
.property( "name", METHOD )
- .constraint( SizeDef.class ).min( 5 )
- .constraint( SizeDef.class ).min( 10 );
+ .constraint( SizeDef.class ).min( 5 )
+ .constraint( SizeDef.class ).min( 10 );
config.addMapping( mapping );
@@ -227,6 +229,65 @@
assertNumberOfViolations( violations, 0 );
}
+ @Test
+ public void testCustomConstraintTypeMissingParameter() {
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .constraint( GenericConstraintDef.class )
+ .constraintType( MarathonConstraint.class );
+
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+ config.addMapping( mapping );
+ try {
+ config.buildValidatorFactory();
+ fail( "MarathonConstraints needs a parameter" );
+ }
+ catch ( ValidationException e ) {
+ assertTrue( e.getMessage().contains( "No value provided for minRunner" ) );
+ }
+ }
+
+ @Test
+ public void testCustomConstraintType() {
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .constraint( GenericConstraintDef.class )
+ .constraintType( MarathonConstraint.class )
+ .param( "minRunner", 100 )
+ .message( "Needs more runners" );
+
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Marathon marathon = new Marathon();
+ marathon.setName( "Stockholm Marathon" );
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate(
marathon );
+ assertNumberOfViolations( violations, 1 );
+ assertCorrectConstraintViolationMessages( violations, "Needs more runners"
);
+
+ for ( int i = 0; i < 100; i++ ) {
+ marathon.addRunner( new Runner() );
+ }
+ violations = validator.validate( marathon );
+ assertNumberOfViolations( violations, 0 );
+ }
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testNullBean() {
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( null )
+ .constraint( GenericConstraintDef.class )
+ .constraintType( MarathonConstraint.class );
+
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+ config.addMapping( mapping );
+ config.buildValidatorFactory();
+ }
+
public interface Foo {
}
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -25,9 +25,10 @@
* @author Hardy Ferentschik
*/
public class Marathon implements Tournament {
+
private String name;
- private long numberOfRunners;
+ private long numberOfHelpers;
private Date tournamentDate;
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraint.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraint.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -0,0 +1,44 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.validator.test.cfg;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Target({ TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = { MarathonConstraintValidator.class })
+public @interface MarathonConstraint {
+ public String message() default "invalid name";
+
+ public Class<?>[] groups() default { };
+
+ public Class<? extends Payload>[] payload() default { };
+
+ public int minRunner();
+}
+
+
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraintValidator.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraintValidator.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/MarathonConstraintValidator.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -0,0 +1,39 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.validator.test.cfg;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class MarathonConstraintValidator implements
ConstraintValidator<MarathonConstraint, Marathon> {
+ private int minRunners;
+
+ public void initialize(MarathonConstraint constraintAnnotation) {
+ minRunners = constraintAnnotation.minRunner();
+ }
+
+ public boolean isValid(Marathon m, ConstraintValidatorContext context) {
+ return m.getRunners().size() >= minRunners;
+ }
+}
+
+
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java 2010-05-31
10:48:08 UTC (rev 19634)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java 2010-05-31
14:03:26 UTC (rev 19635)
@@ -29,8 +29,6 @@
import javax.validation.Path;
import javax.validation.Validation;
import javax.validation.Validator;
-import javax.validation.metadata.ConstraintDescriptor;
-import javax.validation.metadata.ElementDescriptor;
import javax.validation.metadata.PropertyDescriptor;
import javax.validation.spi.ValidationProvider;
@@ -162,16 +160,6 @@
}
}
- public static void assertConstraintViolation(ConstraintViolation violation, String
errorMessage, Class rootBean, Object invalidValue, String propertyPath, Class leafBean) {
- assertEquals(
-
- violation.getLeafBean().getClass(),
- leafBean,
- "Wrong leaf bean type"
- );
- assertConstraintViolation( violation, errorMessage, rootBean, invalidValue,
propertyPath );
- }
-
public static void assertConstraintViolation(ConstraintViolation violation, String
errorMessage, Class rootBean, Object invalidValue, String propertyPath) {
assertEquals(
violation.getPropertyPath(),
@@ -271,18 +259,4 @@
return super.getResourceAsStream( finalPath );
}
}
-
- private static class IgnoringValidationXmlClassLoader extends ClassLoader {
- IgnoringValidationXmlClassLoader() {
- super( IgnoringValidationXmlClassLoader.class.getClassLoader() );
- }
-
- public InputStream getResourceAsStream(String path) {
- if ( "META-INF/validation.xml".equals( path ) ) {
- log.info( "Ignoring call to load validation.xml" );
- return null;
- }
- return super.getResourceAsStream( path );
- }
- }
}