Author: hardy.ferentschik
Date: 2009-06-23 12:28:21 -0400 (Tue, 23 Jun 2009)
New Revision: 16905
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/PathImplTest.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/PropertyPath.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/PropertyPathTest.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/NodeImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java
Log:
HV-177
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -97,7 +97,7 @@
}
public NodeBuilder inSubNode(String name) {
- PathImpl path = new PathImpl();
+ PathImpl path = PathImpl.createNewRootPath();
path.addNode( new NodeImpl( name ) );
return new NodeBuilderImpl( messageTemplate, path );
}
@@ -139,7 +139,7 @@
}
public ErrorBuilder.InIterablePropertiesBuilder inIterable() {
- return new InIterablePropertiesBuilderImpl(messageTemplate, propertyPath);
+ return new InIterablePropertiesBuilderImpl( messageTemplate, propertyPath );
}
public ErrorBuilder.InIterableNodeBuilder inSubNode(String name) {
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -32,6 +32,7 @@
import javax.validation.ConstraintViolation;
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
+import javax.validation.Path;
import javax.validation.metadata.ConstraintDescriptor;
import org.hibernate.validation.metadata.MetaConstraint;
@@ -148,7 +149,7 @@
beanStack.push( object );
processedObjects = new HashMap<Class<?>, IdentitySet>();
processedPaths = new IdentityHashMap<Object, Set<PathImpl>>();
- propertyPath = new PathImpl();
+ propertyPath = PathImpl.createNewRootPath();
failingConstraintViolations = new ArrayList<ConstraintViolation<T>>();
}
@@ -227,6 +228,10 @@
propertyPath.addNode( new NodeImpl( property ) );
}
+ public void setProperty(PathImpl path) {
+ propertyPath = path;
+ }
+
/**
* Drops the last level of the current property path of this context.
*/
@@ -316,8 +321,8 @@
return false;
}
- for (PathImpl p : pathSet) {
- if (p.isSubPathOf(peekPropertyPath()) || peekPropertyPath().isSubPathOf(p)) {
+ for ( PathImpl p : pathSet ) {
+ if ( p.isSubPathOf( peekPropertyPath() ) || peekPropertyPath().isSubPathOf( p ) ) {
return true;
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/NodeImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/NodeImpl.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/NodeImpl.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -76,7 +76,7 @@
@Override
public String toString() {
- StringBuilder builder = new StringBuilder( name );
+ StringBuilder builder = new StringBuilder( name == null ? "" : name );
if ( isInIterable ) {
builder.append( INDEX_OPEN );
if ( getIndex() != null ) {
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.validation.Path;
/**
@@ -27,19 +29,44 @@
*/
public class PathImpl implements Path {
+ /**
+ * Regular expression used to split a string path into its elements.
+ *
+ * @see <a
href="http://www.regexplanet.com/simple/index.jsp">Regular
expression tester</a>
+ */
+ private static final Pattern pathPattern = Pattern.compile(
"(\\w+)(\\[(\\w+)\\])?(\\.(.*))*" );
+
private static final String PROPERTY_PATH_SEPERATOR = ".";
- private static final String INDEX_OPEN = "[";
- private static final String INDEX_CLOSE = "]";
private static final Node ROOT_NODE = new NodeImpl( ( String ) null );
private final List<Node> nodeList;
- public PathImpl() {
- nodeList = new ArrayList<Node>();
- nodeList.add( ROOT_NODE );
+ /**
+ * Returns a {@code Path} instance representing the path described by the given string.
To create a root node the empty string should be passed.
+ *
+ * @param propertyPath the path as string representation.
+ *
+ * @return a {@code Path} instance representing the path described by the given string.
+ *
+ * @throws IllegalArgumentException in case {@code property == null} or {@code property}
cannot be parsed.
+ */
+ public static PathImpl createPathFromString(String propertyPath) {
+ if ( propertyPath == null ) {
+ throw new IllegalArgumentException( "null is not allowed as property path."
);
+ }
+
+ if ( propertyPath.length() == 0 ) {
+ return createNewRootPath();
+ }
+
+ return parseProperty( propertyPath );
}
+ public static PathImpl createNewRootPath() {
+ return new PathImpl();
+ }
+
public PathImpl(PathImpl path) {
this.nodeList = new ArrayList<Node>();
Iterator<Node> iter = path.iterator();
@@ -48,6 +75,11 @@
}
}
+ private PathImpl() {
+ nodeList = new ArrayList<Node>();
+ nodeList.add( ROOT_NODE );
+ }
+
private PathImpl(List<Node> nodeList) {
this.nodeList = new ArrayList<Node>();
for ( Node node : nodeList ) {
@@ -143,4 +175,33 @@
public int hashCode() {
return nodeList != null ? nodeList.hashCode() : 0;
}
+
+ private static PathImpl parseProperty(String property) {
+ PathImpl path = new PathImpl();
+ String tmp = property;
+ do {
+ Matcher matcher = pathPattern.matcher( tmp );
+ if ( matcher.matches() ) {
+ String value = matcher.group( 1 );
+ String index = matcher.group( 3 );
+ NodeImpl node = new NodeImpl( value );
+ if ( index != null ) {
+ node.setInIterable( true );
+ try {
+ Integer i = Integer.parseInt( index );
+ node.setIndex( i );
+ }
+ catch ( NumberFormatException e ) {
+ node.setKey( index );
+ }
+ }
+ path.addNode( node );
+ tmp = matcher.group( 5 );
+ }
+ else {
+ throw new IllegalArgumentException( "Unable to parse property path " +
property );
+ }
+ } while ( tmp != null );
+ return path;
+ }
}
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-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -31,6 +31,7 @@
import javax.validation.ConstraintValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.MessageInterpolator;
+import javax.validation.Path;
import javax.validation.TraversableResolver;
import javax.validation.ValidationException;
import javax.validation.Validator;
@@ -50,7 +51,6 @@
import org.hibernate.validation.metadata.ConstraintHelper;
import org.hibernate.validation.metadata.MetaConstraint;
import org.hibernate.validation.util.LoggerFactory;
-import org.hibernate.validation.util.PropertyPath;
import org.hibernate.validation.util.ReflectionHelper;
/**
@@ -130,7 +130,9 @@
GroupChain groupChain = determineGroupExecutionOrder( groups );
List<ConstraintViolation<T>> failingConstraintViolations = new
ArrayList<ConstraintViolation<T>>();
- validateProperty( object, new PropertyPath( propertyName ),
failingConstraintViolations, groupChain );
+ validateProperty(
+ object, PathImpl.createPathFromString( propertyName ), failingConstraintViolations,
groupChain
+ );
return new HashSet<ConstraintViolation<T>>( failingConstraintViolations );
}
@@ -146,7 +148,9 @@
GroupChain groupChain = determineGroupExecutionOrder( groups );
List<ConstraintViolation<T>> failingConstraintViolations = new
ArrayList<ConstraintViolation<T>>();
- validateValue( beanType, value, new PropertyPath( propertyName ),
failingConstraintViolations, groupChain );
+ validateValue(
+ beanType, value, PathImpl.createPathFromString( propertyName ),
failingConstraintViolations, groupChain
+ );
return new HashSet<ConstraintViolation<T>>( failingConstraintViolations );
}
@@ -384,7 +388,7 @@
}
}
- private <T> void validateProperty(T object, PropertyPath propertyPath,
List<ConstraintViolation<T>> failingConstraintViolations, GroupChain
groupChain) {
+ private <T> void validateProperty(T object, PathImpl propertyPath,
List<ConstraintViolation<T>> failingConstraintViolations, GroupChain
groupChain) {
@SuppressWarnings("unchecked")
final Class<T> beanType = ( Class<T> ) object.getClass();
@@ -443,7 +447,7 @@
private <T> void validatePropertyForGroup(
T object,
- PropertyPath propertyIter,
+ PathImpl path,
List<ConstraintViolation<T>> failingConstraintViolations,
Set<MetaConstraint<T, ?>> metaConstraints,
Object hostingBeanInstance,
@@ -470,13 +474,12 @@
constraintValidatorFactory,
cachedTraversableResolver
);
- context.pushProperty( propertyIter.getOriginalProperty() );
+ context.setProperty( path );
context.setCurrentGroup( groupClass );
if ( context.isValidationRequired( metaConstraint ) ) {
metaConstraint.validateConstraint( context );
failingConstraintViolations.addAll( context.getFailingConstraints() );
}
- context.popProperty();
}
if ( failingConstraintViolations.size() > numberOfConstraintViolationsBefore ) {
break;
@@ -485,7 +488,7 @@
}
@SuppressWarnings("unchecked")
- private <T> void validateValue(Class<T> beanType, Object value, PropertyPath
propertyPath, List<ConstraintViolation<T>> failingConstraintViolations,
GroupChain groupChain) {
+ private <T> void validateValue(Class<T> beanType, Object value, PathImpl
propertyPath, List<ConstraintViolation<T>> failingConstraintViolations,
GroupChain groupChain) {
Set<MetaConstraint<T, ?>> metaConstraints = new
HashSet<MetaConstraint<T, ?>>();
collectMetaConstraintsForPath( beanType, null, propertyPath.iterator(), metaConstraints
);
@@ -537,7 +540,7 @@
private <T> void validateValueForGroup(
Class<T> beanType,
Object value,
- PropertyPath propertyIter,
+ PathImpl path,
List<ConstraintViolation<T>> failingConstraintViolations,
Set<MetaConstraint<T, ?>> metaConstraints,
Group group,
@@ -560,13 +563,12 @@
ExecutionContext<T> context = ExecutionContext.getContextForValidateValue(
beanType, value, messageInterpolator, constraintValidatorFactory,
cachedTraversableResolver
);
- context.pushProperty( propertyIter.getOriginalProperty() );
+ context.setProperty( path );
context.setCurrentGroup( groupClass );
if ( context.isValidationRequired( metaConstraint ) ) {
metaConstraint.validateConstraint( value, context );
failingConstraintViolations.addAll( context.getFailingConstraints() );
}
- context.popProperty();
}
if ( failingConstraintViolations.size() > numberOfConstraintViolations ) {
break;
@@ -588,23 +590,27 @@
* @return Returns the bean hosting the constraints which match the specified property
path.
*/
@SuppressWarnings("unchecked")
- private <T> Object collectMetaConstraintsForPath(Class<T> clazz, Object
value, Iterator<PropertyPath.PathElement> propertyIter, Set<MetaConstraint<T,
?>> metaConstraints) {
+ private <T> Object collectMetaConstraintsForPath(Class<T> clazz, Object
value, Iterator<Path.Node> propertyIter, Set<MetaConstraint<T, ?>>
metaConstraints) {
- PropertyPath.PathElement elem = propertyIter.next();
+ Path.Node elem = propertyIter.next();
+ if ( elem.getName() == null ) { // skip root node
+ elem = propertyIter.next();
+ }
+
final BeanMetaData<T> metaData = getBeanMetaData( clazz );
if ( !propertyIter.hasNext() ) {
//use metadata first as ReflectionHelper#containsMember is slow
//TODO store some metadata here?
- if ( metaData.getPropertyDescriptor( elem.value() ) == null
- && !ReflectionHelper.containsMember( clazz, elem.value() ) ) {
+ if ( metaData.getPropertyDescriptor( elem.getName() ) == null
+ && !ReflectionHelper.containsMember( clazz, elem.getName() ) ) {
//TODO better error report
throw new IllegalArgumentException( "Invalid property path." );
}
List<MetaConstraint<T, ? extends Annotation>> metaConstraintList =
metaData.geMetaConstraintList();
for ( MetaConstraint<T, ?> metaConstraint : metaConstraintList ) {
- if ( metaConstraint.getPropertyName().equals( elem.value() ) ) {
+ if ( metaConstraint.getPropertyName().equals( elem.getName() ) ) {
metaConstraints.add( metaConstraint );
}
}
@@ -612,17 +618,23 @@
else {
List<Member> cascadedMembers = metaData.getCascadedMembers();
for ( Member m : cascadedMembers ) {
- if ( ReflectionHelper.getPropertyName( m ).equals( elem.value() ) ) {
+ if ( ReflectionHelper.getPropertyName( m ).equals( elem.getName() ) ) {
Type type = ReflectionHelper.typeOf( m );
value = value == null ? null : ReflectionHelper.getValue( m, value );
- if ( elem.isIndexed() ) {
- value = value == null ? null : ReflectionHelper.getIndexedValue(
- value, elem.getIndex()
- );
+ if ( elem.isInIterable() ) {
+ if ( value != null && elem.getIndex() != null ) {
+ value = ReflectionHelper.getIndexedValue( value, elem.getIndex() );
+ }
+ else if ( value != null && elem.getKey() != null ) {
+
+ }
type = ReflectionHelper.getIndexedType( type );
}
return collectMetaConstraintsForPath(
- ( Class<T> ) ( value == null ? type : value.getClass() ), value,
propertyIter, metaConstraints
+ ( Class<T> ) ( value == null ? type : value.getClass() ),
+ value,
+ propertyIter,
+ metaConstraints
);
}
}
Deleted:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/PropertyPath.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/PropertyPath.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/PropertyPath.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -1,125 +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.validation.util;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-
-/**
- * Helper class to iterate over a property path.
- *
- * @author Hardy Ferentschik
- */
-public class PropertyPath implements Iterable<PropertyPath.PathElement> {
-
- /**
- * Regular expression used to split the path into its elements.
- *
- * @see <a
href="http://www.regexplanet.com/simple/index.jsp">Regular
expression tester</a>
- */
- private static final Pattern pathPattern = Pattern.compile(
"(\\w+)(\\[(\\w+)\\])?(\\.(.*))*" );
-
- final String originalProperty;
-
- final List<PathElement> pathList = new ArrayList<PathElement>();
-
- /**
- * Constructs a new {@code PropertyPath}.
- *
- * @param property The string representation of the property path.
- *
- * @throws IllegalArgumentException in case {@code property == null} or {@code property}
cannot be parsed.
- */
- public PropertyPath(String property) {
- if ( property == null ) {
- throw new IllegalArgumentException( "null is not allowed as property path."
);
- }
-
- this.originalProperty = property;
- if ( property.length() > 0 ) {
- parseProperty( property );
- }
- }
-
- private void parseProperty(String property) {
- String tmp = property;
- do {
- Matcher matcher = pathPattern.matcher( tmp );
- if ( matcher.matches() ) {
- String value = matcher.group( 1 );
- String index = matcher.group( 3 );
- PathElement elem = new PathElement( value, index );
- pathList.add( elem );
- tmp = matcher.group( 5 );
- }
- else {
- throw new IllegalArgumentException( "Unable to parse property path " +
property );
- }
- } while ( tmp != null );
- }
-
- public String getOriginalProperty() {
- return originalProperty;
- }
-
- public Iterator<PathElement> iterator() {
- return pathList.iterator();
- }
-
- @Override
- public String toString() {
- return "PropertyPath{" +
- "originalProperty='" + originalProperty + '\'' +
- ", pathList=" + pathList +
- '}';
- }
-
- public static class PathElement {
- private final String value;
- private final String index;
-
- private PathElement(String value, String index) {
- this.value = value;
- this.index = index;
- }
-
- public String value() {
- return value;
- }
-
- public String getIndex() {
- return index;
- }
-
- public boolean isIndexed() {
- return index != null;
- }
-
- @Override
- public String toString() {
- return "PathElement{" +
- "value='" + value + '\'' +
- ", index='" + index + '\'' +
- '}';
- }
- }
-}
\ No newline at end of file
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-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -305,45 +305,29 @@
* @return The indexed value or <code>null</code> if
<code>value</code> is <code>null</code> or not a collection or
array.
* <code>null</code> is also returned in case the index does not
exist.
*/
- public static Object getIndexedValue(Object value, String index) {
+ public static Object getIndexedValue(Object value, Integer index) {
if ( value == null ) {
return null;
}
- // try to create the index
- int numIndex = -1;
- try {
- numIndex = Integer.valueOf( index );
- }
- catch ( NumberFormatException nfe ) {
- // ignore
- }
-
- if ( numIndex == -1 ) { // must be a map indexed by string
- Map<?, ?> map = ( Map<?, ?> ) value;
- //noinspection SuspiciousMethodCalls
- return map.get( index );
- }
-
- Iterator<?> iter = null;
+ Iterator<?> iter;
Type type = value.getClass();
if ( isIterable( type ) ) {
iter = ( ( Iterable<?> ) value ).iterator();
}
- else if ( isMap( type ) ) {
- Map<?, ?> map = ( Map<?, ?> ) value;
- iter = map.values().iterator();
- }
else if ( TypeUtils.isArray( type ) ) {
- List<?> arrayList = Arrays.asList( (Object) value );
+ List<?> arrayList = Arrays.asList( ( Object ) value );
iter = arrayList.iterator();
}
+ else {
+ return null;
+ }
int i = 0;
Object o;
while ( iter.hasNext() ) {
o = iter.next();
- if ( i == numIndex ) {
+ if ( i == index ) {
return o;
}
i++;
@@ -352,6 +336,25 @@
}
/**
+ * Tries to retrieve the mapped value from the specified object.
+ *
+ * @param value The object from which to retrieve the mapped value. The object has to be
non {@code null} and
+ * must implement the @{code Map} interface.
+ * @param key The map key. index.
+ *
+ * @return The mapped value or {@code null} if {@code value} is {@code null} or not
implementing @{code Map}.
+ */
+ public static Object getMappedValue(Object value, Object key) {
+ if ( value == null || !( value instanceof Map ) ) {
+ return null;
+ }
+
+ Map<?, ?> map = ( Map<?, ?> ) value;
+ //noinspection SuspiciousMethodCalls
+ return map.get( key );
+ }
+
+ /**
* Checks whether the specified class contains a field or property matching the given
name.
*
* @param clazz The class to check.
@@ -520,7 +523,7 @@
* @param clazz @{code Class} to check.
* @param superClassOrInterface The super class/interface {@code clazz}.
*
- * @return {@code true} if {@code clazz} extends or implements {@code
superClassOrInterface}, {@code false} otherwise.
+ * @return {@code true} if {@code clazz} extends or implements {@code
superClassOrInterface}, {@code false} otherwise.
*/
private static boolean extendsOrImplements(Class<?> clazz, Class<?>
superClassOrInterface) {
List<Class<?>> classes = new ArrayList<Class<?>>();
Copied:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/PathImplTest.java
(from rev 16866,
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/PropertyPathTest.java)
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/PathImplTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/PathImplTest.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -0,0 +1,129 @@
+// $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;
+
+import java.util.Iterator;
+import javax.validation.Path;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PathImplTest {
+
+ @Test
+ public void testParsing() {
+ String property = "order[3].deliveryAddress.addressline[1]";
+ Path path = PathImpl.createPathFromString( property );
+ Iterator<Path.Node> propIter = path.iterator();
+
+ assertTrue( propIter.hasNext() );
+ Path.Node elem = propIter.next();
+ assertEquals( null, elem.getName() );
+ assertFalse( elem.isInIterable() );
+ assertEquals( property, path.toString() );
+
+ assertTrue( propIter.hasNext() );
+ elem = propIter.next();
+ assertEquals( "order", elem.getName() );
+ assertTrue( elem.isInIterable() );
+ assertEquals( new Integer( 3 ), elem.getIndex() );
+
+ assertTrue( propIter.hasNext() );
+ elem = propIter.next();
+ assertEquals( "deliveryAddress", elem.getName() );
+ assertFalse( elem.isInIterable() );
+ assertEquals( null, elem.getIndex() );
+
+ assertTrue( propIter.hasNext() );
+ elem = propIter.next();
+ assertEquals( "addressline", elem.getName() );
+ assertTrue( elem.isInIterable() );
+ assertEquals( new Integer( 1 ), elem.getIndex() );
+
+ assertFalse( propIter.hasNext() );
+ }
+
+ @Test
+ public void testParseMapBasedProperty() {
+ String property = "order[foo].deliveryAddress";
+ Path path = PathImpl.createPathFromString( property );
+ Iterator<Path.Node> propIter = path.iterator();
+
+ assertTrue( propIter.hasNext() );
+ Path.Node elem = propIter.next();
+ assertEquals( null, elem.getName() );
+ assertFalse( elem.isInIterable() );
+ assertEquals( property, path.toString() );
+
+ assertTrue( propIter.hasNext() );
+ elem = propIter.next();
+ assertEquals( "order", elem.getName() );
+ assertTrue( elem.isInIterable() );
+ assertEquals( "foo", elem.getKey() );
+
+ assertTrue( propIter.hasNext() );
+ elem = propIter.next();
+ assertEquals( "deliveryAddress", elem.getName() );
+ assertFalse( elem.isInIterable() );
+ assertEquals( null, elem.getIndex() );
+
+ assertFalse( propIter.hasNext() );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testNull() {
+ PathImpl.createPathFromString( null );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testUnbalancedBraces() {
+ PathImpl.createPathFromString( "foo[.bar" );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testIndexInMiddleOfProperty() {
+ PathImpl.createPathFromString( "f[1]oo.bar" );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testTrailingPathSeperator() {
+ PathImpl.createPathFromString( "foo.bar." );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testLeadingPathSeperator() {
+ PathImpl.createPathFromString( ".foo.bar" );
+ }
+
+ @Test
+ public void testEmptyString() {
+ Path path = PathImpl.createPathFromString( "" );
+ assertTrue( path.iterator().hasNext() );
+ }
+
+ @Test
+ public void testRootPath() {
+ Path path = PathImpl.createNewRootPath();
+ assertEquals( path.toString(), "", "Wrong string representation of root
property." );
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/PathImplTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Deleted:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/PropertyPathTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/PropertyPathTest.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/PropertyPathTest.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -1,92 +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.validation.util;
-
-import java.util.Iterator;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import org.testng.annotations.Test;
-
-/**
- * @author Hardy Ferentschik
- */
-public class PropertyPathTest {
-
- @Test
- public void testParsing() {
- String property = "order[3].deliveryAddress.addressline[1]";
- PropertyPath path = new PropertyPath( property );
- Iterator<PropertyPath.PathElement> propIter = path.iterator();
-
- assertTrue( propIter.hasNext() );
-
- PropertyPath.PathElement elem = propIter.next();
- assertTrue( propIter.hasNext() );
- assertEquals( "order", elem.value() );
- assertTrue( elem.isIndexed() );
- assertEquals( "3", elem.getIndex() );
- assertEquals( property, path.getOriginalProperty() );
-
- assertTrue( propIter.hasNext() );
- elem = propIter.next();
- assertEquals( "deliveryAddress", elem.value() );
- assertFalse( elem.isIndexed() );
- assertEquals( null, elem.getIndex() );
-
- assertTrue( propIter.hasNext() );
- elem = propIter.next();
- assertEquals( "addressline", elem.value() );
- assertTrue( elem.isIndexed() );
- assertEquals( "1", elem.getIndex() );
-
- assertFalse( propIter.hasNext() );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testNull() {
- new PropertyPath( null );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testUnbalancedBraces() {
- new PropertyPath( "foo[.bar" );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testIndexInMiddleOfProperty() {
- new PropertyPath( "f[1]oo.bar" );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testTrailingPathSeperator() {
- new PropertyPath( "foo.bar." );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testLeadingPathSeperator() {
- new PropertyPath( ".foo.bar" );
- }
-
- @Test
- public void testEmptyString() {
- PropertyPath path = new PropertyPath( "" );
- assertFalse( path.iterator().hasNext() );
- }
-}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java 2009-06-23
16:27:28 UTC (rev 16904)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java 2009-06-23
16:28:21 UTC (rev 16905)
@@ -27,8 +27,8 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeSet;
-import javax.validation.ValidationException;
import javax.validation.ConstraintPayload;
+import javax.validation.ValidationException;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
@@ -51,7 +51,7 @@
Type type = TestTypes.class.getField( "stringList" ).getGenericType();
assertTrue( ReflectionHelper.isIterable( type ) );
- assertTrue( ReflectionHelper.isIterable( new TreeSet<Object>().getClass() ) );
+ assertTrue( ReflectionHelper.isIterable( TreeSet.class ) );
assertTrue( ReflectionHelper.isIterable( List.class ) );
assertTrue( ReflectionHelper.isIterable( HashSet.class ) );
@@ -93,17 +93,13 @@
String key = "key";
map.put( key, testObject );
- Object value = ReflectionHelper.getIndexedValue( map, key );
+ Object value = ReflectionHelper.getMappedValue( map, key );
assertEquals( value, testObject, "We should be able to retrieve the indexed
object" );
- // try to get to the value by integer index
- value = ReflectionHelper.getIndexedValue( map, "0" );
- assertEquals( value, testObject, "We should be able to retrieve the indexed
object" );
-
- value = ReflectionHelper.getIndexedValue( map, "foo" );
+ value = ReflectionHelper.getMappedValue( map, "foo" );
assertNull( value, "A non existent index should return the null value" );
- value = ReflectionHelper.getIndexedValue( map, "2" );
+ value = ReflectionHelper.getMappedValue( map, "2" );
assertNull( value, "A non existent index should return the null value" );
}
@@ -113,16 +109,16 @@
Object testObject = new Object();
list.add( testObject );
- Object value = ReflectionHelper.getIndexedValue( list, "0" );
+ Object value = ReflectionHelper.getIndexedValue( list, 0 );
assertEquals( value, testObject, "We should be able to retrieve the indexed
object" );
- value = ReflectionHelper.getIndexedValue( list, "2" );
+ value = ReflectionHelper.getIndexedValue( list, 2 );
assertNull( value, "A non existent index should return the null value" );
}
@Test
public void testGetIndexedValueForNull() {
- Object value = ReflectionHelper.getIndexedValue( null, "0" );
+ Object value = ReflectionHelper.getIndexedValue( null, 0 );
assertNull( value );
}
@@ -138,7 +134,7 @@
}
public Class<? extends ConstraintPayload>[] payload() {
- @SuppressWarnings( "unchecked")
+ @SuppressWarnings("unchecked")
Class<? extends ConstraintPayload>[] classes = new Class[] { };
return classes;
}