Author: hardy.ferentschik
Date: 2010-05-21 11:48:20 -0400 (Fri, 21 May 2010)
New Revision: 19582
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertFalseDefinition.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertTrueDefinition.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/CascadeDefinition.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Runner.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.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/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.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/engine/messageinterpolation/MessageInterpolationWithDefaultBundleTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java
Log:
HV-274 added support for cascading
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertFalseDefinition.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertFalseDefinition.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertFalseDefinition.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -0,0 +1,47 @@
+// $Id: NotNullDefinition.java 19559 2010-05-19 16:20:53Z hardy.ferentschik $
+/*
+ * 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.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.AssertFalse;
+import javax.validation.constraints.AssertTrue;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class AssertFalseDefinition extends ConstraintDefinition<AssertFalse> {
+ public AssertFalseDefinition(Class<?> beanType, String property, ElementType
elementType, ConstraintMapping mapping) {
+ super( beanType, AssertFalse.class, property, elementType, mapping );
+ }
+
+ public AssertFalseDefinition message(String message) {
+ addParameter( "message", message );
+ return this;
+ }
+
+ public AssertFalseDefinition groups(Class<?>... groups) {
+ addParameter( "groups", groups );
+ return this;
+ }
+
+ public AssertFalseDefinition payload(Class<? extends Payload>... payload) {
+ addParameter( "payload", payload );
+ return this;
+ }
+}
\ No newline at end of file
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertTrueDefinition.java
(from rev 19559,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertTrueDefinition.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/AssertTrueDefinition.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -0,0 +1,47 @@
+// $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.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.AssertTrue;
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class AssertTrueDefinition extends ConstraintDefinition<AssertTrue> {
+ public AssertTrueDefinition(Class<?> beanType, String property, ElementType
elementType, ConstraintMapping mapping) {
+ super( beanType, AssertTrue.class, property, elementType, mapping );
+ }
+
+ public AssertTrueDefinition message(String message) {
+ addParameter( "message", message );
+ return this;
+ }
+
+ public AssertTrueDefinition groups(Class<?>... groups) {
+ addParameter( "groups", groups );
+ return this;
+ }
+
+ public AssertTrueDefinition payload(Class<? extends Payload>... payload) {
+ addParameter( "payload", payload );
+ return this;
+ }
+}
\ No newline at end of file
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/CascadeDefinition.java
(from rev 19573,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/CascadeDefinition.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/CascadeDefinition.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -0,0 +1,78 @@
+// $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.ElementType;
+import javax.validation.ValidationException;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class CascadeDefinition {
+ private final Class<?> beanType;
+ private final ElementType elementType;
+ private final String property;
+
+ public CascadeDefinition(Class<?> beanType, String property, ElementType
elementType) {
+ if ( beanType == null ) {
+ throw new ValidationException( "Null is not a valid bean type" );
+ }
+
+ if ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType
) ) {
+ if ( property == null || property.length() == 0 ) {
+ throw new ValidationException( "A valid property name has to be specified"
);
+ }
+
+ if ( !ReflectionHelper.propertyExists( beanType, property, elementType ) ) {
+ throw new ValidationException(
+ "The class " + beanType + " does not have a property '"
+ + property + "' with access " + elementType
+ );
+ }
+ }
+
+ this.beanType = beanType;
+ this.property = property;
+ this.elementType = elementType;
+ }
+
+ public ElementType getElementType() {
+ return elementType;
+ }
+
+ public Class<?> getBeanType() {
+ return beanType;
+ }
+
+ public String getProperty() {
+ return property;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "CascadeDefinition" );
+ sb.append( "{beanType=" ).append( beanType );
+ sb.append( ", elementType=" ).append( elementType );
+ sb.append( ", property='" ).append( property ).append( '\''
);
+ sb.append( '}' );
+ return sb.toString();
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/CascadeDefinition.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -99,7 +99,8 @@
}
public ConstraintsForType valid(String property, ElementType type) {
- return null;
+ mapping.addCascadeConfig( new CascadeDefinition( beanType, property, type ) );
+ return new ConstraintsForType( beanType, mapping );
}
public Class<A> getConstraintType() {
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -18,20 +18,25 @@
package org.hibernate.validator.cfg;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
-import org.hibernate.validator.util.ReflectionHelper;
-
/**
* @author Hardy Ferentschik
*/
public class ConstraintMapping {
- private final Map<Class<?>, List<ConstraintDefinition<?>>>
configData;
+ private final Map<Class<?>, List<ConstraintDefinition<?>>>
constraintConfig;
+ private final Map<Class<?>, List<CascadeDefinition>> cascadeConfig;
+ private final Set<Class<?>> configuredClasses;
public ConstraintMapping() {
- configData = new HashMap<Class<?>,
List<ConstraintDefinition<?>>>();
+ constraintConfig = new HashMap<Class<?>,
List<ConstraintDefinition<?>>>();
+ cascadeConfig = new HashMap<Class<?>, List<CascadeDefinition>>();
+ configuredClasses = new HashSet<Class<?>>();
}
public ConstraintsForType type(Class<?> beanClass) {
@@ -40,26 +45,48 @@
protected void addConstraintConfig(ConstraintDefinition<?> definition) {
Class<?> beanClass = definition.getBeanType();
-
- if ( configData.containsKey( beanClass ) ) {
- configData.get( beanClass ).add( definition );
+ configuredClasses.add( beanClass );
+ if ( constraintConfig.containsKey( beanClass ) ) {
+ constraintConfig.get( beanClass ).add( definition );
}
else {
List<ConstraintDefinition<?>> definitionList = new
ArrayList<ConstraintDefinition<?>>();
definitionList.add( definition );
- configData.put( beanClass, definitionList );
+ constraintConfig.put( beanClass, definitionList );
}
}
- public Map<Class<?>, List<ConstraintDefinition<?>>>
getConfigData() {
- return configData;
+ protected void addCascadeConfig(CascadeDefinition cascade) {
+ Class<?> beanClass = cascade.getBeanType();
+ configuredClasses.add( beanClass );
+ if ( cascadeConfig.containsKey( beanClass ) ) {
+ cascadeConfig.get( beanClass ).add( cascade );
+ }
+ else {
+ List<CascadeDefinition> cascadeList = new ArrayList<CascadeDefinition>();
+ cascadeList.add( cascade );
+ cascadeConfig.put( beanClass, cascadeList );
+ }
}
+ public Map<Class<?>, List<ConstraintDefinition<?>>>
getConstraintConfig() {
+ return constraintConfig;
+ }
+
+ public Map<Class<?>, List<CascadeDefinition>> getCascadeConfig() {
+ return cascadeConfig;
+ }
+
+ public Collection<Class<?>> getConfiguredClasses() {
+ return configuredClasses;
+ }
+
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "ConstraintMapping" );
- sb.append( "{configData=" ).append( configData );
+ sb.append( "{cascadeConfig=" ).append( cascadeConfig );
+ sb.append( ", constraintConfig=" ).append( constraintConfig );
sb.append( '}' );
return sb.toString();
}
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -29,13 +29,15 @@
* @author Hardy Ferentschik
*/
public class ConstraintsForType {
+ private static final String EMPTY_PROPERTY = "";
+
private final ConstraintMapping mapping;
private final Class<?> beanClass;
private String property;
private ElementType elementType;
public ConstraintsForType(Class<?> beanClass, ConstraintMapping mapping) {
- this( beanClass, "", TYPE, mapping );
+ this( beanClass, EMPTY_PROPERTY, TYPE, mapping );
}
public ConstraintsForType(Class<?> beanClass, String property, ElementType type,
ConstraintMapping mapping) {
@@ -62,8 +64,13 @@
}
public ConstraintsForType valid(String property, ElementType type) {
- return null;
+ mapping.addCascadeConfig(new CascadeDefinition( beanClass, property, type));
+ return this;
}
+
+ public ConstraintsForType type(Class<?> type) {
+ return new ConstraintsForType( type, mapping );
+ }
}
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -34,6 +34,7 @@
import javax.validation.ValidatorFactory;
import javax.validation.spi.ConfigurationState;
+import org.hibernate.validator.cfg.CascadeDefinition;
import org.hibernate.validator.cfg.ConstraintDefinition;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.metadata.AnnotationIgnores;
@@ -123,10 +124,8 @@
*
* @param mapping The constraint configuration created via the programmatic API.
*/
- private <A extends Annotation, T> void
initProgrammaticConfiguration(ConstraintMapping mapping) {
- Map<Class<?>, List<ConstraintDefinition<?>>> configData =
mapping.getConfigData();
-
- for ( Class<?> clazz : mapping.getConfigData().keySet() ) {
+ private <T> void initProgrammaticConfiguration(ConstraintMapping mapping) {
+ for ( Class<?> clazz : mapping.getConfiguredClasses() ) {
@SuppressWarnings("unchecked")
Class<T> beanClass = ( Class<T> ) clazz;
@@ -139,19 +138,29 @@
for ( Class<?> classInHierarchy : classes ) {
// if the programmatic config contains constraints for the class in the hierarchy
create a meta constraint
- if ( mapping.getConfigData().keySet().contains( classInHierarchy ) ) {
+ if ( mapping.getConstraintConfig().keySet().contains( classInHierarchy ) ) {
addProgrammaticConfiguredConstraints(
- mapping.getConfigData().get( classInHierarchy ), beanClass, classInHierarchy,
constraints
+ mapping.getConstraintConfig().get( classInHierarchy ),
+ beanClass,
+ classInHierarchy,
+ constraints
);
}
+
+ if ( mapping.getCascadeConfig().keySet().contains( classInHierarchy ) ) {
+ addProgrammaticConfiguredCascade(
+ mapping.getCascadeConfig().get( classInHierarchy ), cascadedMembers
+ );
+ }
}
+ // TODO handle redefinition of default group
BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
beanClass,
constraintHelper,
new ArrayList<Class<?>>(),
constraints,
- new ArrayList<Member>(),
+ cascadedMembers,
new AnnotationIgnores(),
beanMetaDataCache
);
@@ -224,7 +233,7 @@
Class<T> rootClass, Class<?> hierarchyClass,
Map<Class<?>, List<MetaConstraint<T, ?>>>
constraints) {
for ( ConstraintDefinition<?> config : definitions ) {
- A annotation = (A) createAnnotationProxy( config );
+ A annotation = ( A ) createAnnotationProxy( config );
ConstraintOrigin definedIn = definedIn( rootClass, hierarchyClass );
ConstraintDescriptorImpl<A> constraintDescriptor = new
ConstraintDescriptorImpl<A>(
annotation, constraintHelper, config.getElementType(), definedIn
@@ -261,6 +270,19 @@
}
}
+ private void addProgrammaticConfiguredCascade(List<CascadeDefinition> cascades,
+ List<Member> cascadedMembers) {
+ if ( cascades == null ) {
+ return;
+ }
+ for ( CascadeDefinition cascade : cascades ) {
+ Member m = ReflectionHelper.getMember(
+ cascade.getBeanType(), cascade.getProperty(), cascade.getElementType()
+ );
+ cascadedMembers.add( m );
+ }
+ }
+
/**
* @param rootClass The root class. That is the class for which we currently create a
{@code BeanMetaData}
* @param hierarchyClass The class on which the current constraint is defined on
@@ -279,7 +301,7 @@
@SuppressWarnings("unchecked")
private <A extends Annotation> Annotation
createAnnotationProxy(ConstraintDefinition<?> config) {
- Class<A> constraintType = (Class<A>) config.getConstraintType();
+ Class<A> constraintType = ( Class<A> ) config.getConstraintType();
AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(
constraintType );
for ( Map.Entry<String, Object> parameter : config.getParameters().entrySet() )
{
annotationDescriptor.setValue( parameter.getKey(), parameter.getValue() );
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java 2010-05-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -112,8 +112,8 @@
public BeanMetaDataImpl(Class<T> beanClass,
ConstraintHelper constraintHelper,
List<Class<?>> defaultGroupSequence,
- Map<Class<?>, List<MetaConstraint<T, ?>>>
xmlConfiguredConstraints,
- List<Member> xmlConfiguredMember,
+ Map<Class<?>, List<MetaConstraint<T, ?>>> constraints,
+ List<Member> cascadedMembers,
AnnotationIgnores annotationIgnores,
BeanMetaDataCache beanMetaDataCache) {
this.beanClass = beanClass;
@@ -122,13 +122,13 @@
if ( !defaultGroupSequence.isEmpty() ) {
setDefaultGroupSequence( defaultGroupSequence );
}
- for ( Map.Entry<Class<?>, List<MetaConstraint<T, ?>>> entry :
xmlConfiguredConstraints.entrySet() ) {
+ for ( Map.Entry<Class<?>, List<MetaConstraint<T, ?>>> entry :
constraints.entrySet() ) {
Class<?> clazz = entry.getKey();
for ( MetaConstraint<T, ?> constraint : entry.getValue() ) {
addMetaConstraint( clazz, constraint );
}
}
- for ( Member member : xmlConfiguredMember ) {
+ for ( Member member : cascadedMembers ) {
addCascadedMember( member );
}
}
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -30,6 +30,7 @@
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
+import org.hibernate.validator.cfg.AssertTrueDefinition;
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.FutureDefinition;
import org.hibernate.validator.cfg.MinDefinition;
@@ -59,8 +60,8 @@
.property( "numberOfRunners", FIELD )
.constraint( MinDefinition.class ).value( 1 );
- assertTrue( mapping.getConfigData().containsKey( Marathon.class ) );
- assertTrue( mapping.getConfigData().get( Marathon.class ).size() == 2 );
+ assertTrue( mapping.getConstraintConfig().containsKey( Marathon.class ) );
+ assertTrue( mapping.getConstraintConfig().get( Marathon.class ).size() == 2 );
}
@Test
@@ -121,6 +122,34 @@
}
@Test
+ public void testValid() {
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .valid( "runners", METHOD )
+ .type( Runner.class )
+ .property( "paidEntryFee", FIELD )
+ .constraint( AssertTrueDefinition.class );
+
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Marathon marathon = new Marathon();
+ marathon.setName( "New York Marathon" );
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate(
marathon );
+ assertNumberOfViolations( violations, 0 );
+
+ marathon.addRunner( new Runner() );
+ violations = validator.validate( marathon );
+ assertNumberOfViolations( violations, 1 );
+ assertConstraintViolation( violations.iterator().next(), "must be true" );
+ }
+
+ @Test
public void testSingleConstraintWrongAccessType() {
ConstraintMapping mapping = new ConstraintMapping();
try {
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -17,7 +17,9 @@
*/
package org.hibernate.validator.test.cfg;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
/**
* @author Hardy Ferentschik
@@ -29,6 +31,12 @@
private Date tournamentDate;
+ private List<Runner> runners;
+
+ public Marathon() {
+ runners = new ArrayList<Runner>();
+ }
+
public String getName() {
return name;
}
@@ -44,6 +52,14 @@
public void setTournamentDate(Date tournamentDate) {
this.tournamentDate = tournamentDate;
}
+
+ public List<Runner> getRunners() {
+ return runners;
+ }
+
+ public void addRunner(Runner runner) {
+ runners.add( runner );
+ }
}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Runner.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Runner.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Runner.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -0,0 +1,46 @@
+// $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;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Runner {
+ private String name;
+
+ private boolean paidEntryFee;
+
+ public boolean isPaidEntryFee() {
+ return paidEntryFee;
+ }
+
+ public void setPaidEntryFee(boolean paidEntryFee) {
+ this.paidEntryFee = paidEntryFee;
+ }
+
+ public String getName() {
+
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
+
+
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/MessageInterpolationWithDefaultBundleTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/MessageInterpolationWithDefaultBundleTest.java 2010-05-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/engine/messageinterpolation/MessageInterpolationWithDefaultBundleTest.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -57,8 +57,7 @@
*/
@Test
public void testEmailAndRangeMessageEnglishLocale() {
- Locale.setDefault( Locale.ENGLISH );
- Configuration<?> config = TestUtil.getConfiguration();
+ Configuration<?> config = TestUtil.getConfiguration( Locale.ENGLISH );
config.messageInterpolator( new ResourceBundleMessageInterpolator() );
Validator validator = config.buildValidatorFactory().getValidator();
User user = new User();
@@ -76,8 +75,7 @@
*/
@Test
public void testEmailAndRangeMessageGermanLocale() {
- Locale.setDefault( Locale.GERMAN );
- Configuration<?> config = TestUtil.getConfiguration();
+ Configuration<?> config = TestUtil.getConfiguration( Locale.GERMAN );
config.messageInterpolator( new ResourceBundleMessageInterpolator() );
Validator validator = config.buildValidatorFactory().getValidator();
User user = new User();
@@ -95,8 +93,7 @@
*/
@Test
public void testEmailAndRangeMessageFrenchLocale() {
- Locale.setDefault( Locale.FRENCH );
- Configuration<?> config = TestUtil.getConfiguration();
+ Configuration<?> config = TestUtil.getConfiguration( Locale.FRENCH );
config.messageInterpolator( new ResourceBundleMessageInterpolator() );
Validator validator = config.buildValidatorFactory().getValidator();
User user = new User();
@@ -115,9 +112,7 @@
*/
@Test
public void testThatExplicitlySetEnglishLocaleHasPrecedenceOverDefaultLocale() {
-
- Locale.setDefault( Locale.FRENCH );
- Configuration<?> config = TestUtil.getConfiguration();
+ Configuration<?> config = TestUtil.getConfiguration( Locale.FRENCH );
config.messageInterpolator( new LocalizedMessageInterpolator( Locale.ENGLISH ) );
Validator validator = config.buildValidatorFactory().getValidator();
User user = new User();
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-21
10:31:30 UTC (rev 19581)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/TestUtil.java 2010-05-21
15:48:20 UTC (rev 19582)
@@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
import java.util.Set;
import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
@@ -59,7 +60,7 @@
public static Validator getValidator() {
if ( hibernateValidator == null ) {
- Configuration configuration = getConfiguration();
+ Configuration configuration = getConfiguration( Locale.ENGLISH );
configuration.traversableResolver( new DummyTraversableResolver() );
hibernateValidator = configuration.buildValidatorFactory().getValidator();
}
@@ -67,10 +68,19 @@
}
public static Configuration<HibernateValidatorConfiguration> getConfiguration() {
- return Validation.byProvider( HibernateValidator.class ).configure();
+ return getConfiguration( HibernateValidator.class, Locale.ENGLISH );
}
+ public static Configuration<HibernateValidatorConfiguration>
getConfiguration(Locale locale) {
+ return getConfiguration( HibernateValidator.class, locale );
+ }
+
public static <T extends Configuration<T>, U extends
ValidationProvider<T>> T getConfiguration(Class<U> type) {
+ return getConfiguration( type, Locale.ENGLISH );
+ }
+
+ public static <T extends Configuration<T>, U extends
ValidationProvider<T>> T getConfiguration(Class<U> type, Locale locale) {
+ Locale.setDefault( locale );
return Validation.byProvider( type ).configure();
}
@@ -83,43 +93,14 @@
*/
public static Validator getValidatorWithCustomConfiguration(String path) {
Thread.currentThread().setContextClassLoader( new CustomValidationXmlClassLoader( path
) );
-
- HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidator.class )
- .configure();
- return configuration.buildValidatorFactory().getValidator();
+ return getConfiguration().buildValidatorFactory().getValidator();
}
- /**
- * @return A <code>Validator</code> instance which ignores
<i>validation.xml</code>.
- */
- public static Validator getValidatorIgnoringValidationXml() {
- Thread.currentThread().setContextClassLoader( new IgnoringValidationXmlClassLoader()
);
-
- HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidator.class )
- .configure();
- return configuration.buildValidatorFactory().getValidator();
- }
-
- public static ConstraintDescriptor<?>
getSingleConstraintDescriptorFor(Class<?> clazz, String property) {
- Set<ConstraintDescriptor<?>> constraintDescriptors =
getConstraintDescriptorsFor( clazz, property );
- assertTrue(
- constraintDescriptors.size() == 1, "This method should only be used when there
is a single constraint"
- );
- 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 <T> void
assertCorrectConstraintViolationMessages(Set<ConstraintViolation<T>>
violations, String... messages) {
List<String> actualMessages = new ArrayList<String>();
for ( ConstraintViolation<?> violation : violations ) {