Skip to content

Commit

Permalink
BVTCK-32 Making assertion failures a bit more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jan 28, 2013
1 parent a166fff commit 4a84d6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.validation.Path;
import javax.validation.Path.Node;
Expand Down Expand Up @@ -88,6 +89,20 @@ else if ( !nodeNames.equals( other.nodeNames ) ) {

@Override
public String toString() {
return "PathNodeNames [nodeNames=" + nodeNames + "]";
StringBuilder sb = new StringBuilder();

Iterator<String> nodeNameIterator = nodeNames.iterator();

while ( nodeNameIterator.hasNext() ) {
sb.append( nodeNameIterator.next() );
if ( nodeNameIterator.hasNext() ) {
sb.append( "." );
}
else {
return sb.toString();
}
}

return sb.toString();
}
}
Expand Up @@ -17,7 +17,6 @@
package org.hibernate.beanvalidation.tck.util;

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -125,26 +124,25 @@ public static <T> void assertCorrectConstraintViolationMessages(Set<ConstraintVi
}

public static <T> void assertCorrectConstraintTypes(Set<ConstraintViolation<T>> violations, Class<?>... expectedConstraintTypes) {
List<String> actualConstraintTypes = new ArrayList<String>();
List<String> actualConstraintTypeNames = new ArrayList<String>();
for ( ConstraintViolation<?> violation : violations ) {
actualConstraintTypes.add(
( (Annotation) violation.getConstraintDescriptor()
.getAnnotation() ).annotationType().getName()
actualConstraintTypeNames.add(
violation.getConstraintDescriptor()
.getAnnotation()
.annotationType()
.getName()
);
}

assertEquals(
actualConstraintTypes.size(),
expectedConstraintTypes.length,
"Wrong number of constraint types."
);

List<String> expectedConstraintTypeNames = new ArrayList<String>();
for ( Class<?> expectedConstraintType : expectedConstraintTypes ) {
assertTrue(
actualConstraintTypes.contains( expectedConstraintType.getName() ),
"The constraint type " + expectedConstraintType.getName() + " is not in the list of actual violated constraint types: " + actualConstraintTypes
);
expectedConstraintTypeNames.add( expectedConstraintType.getName() );
}

Collections.sort( actualConstraintTypeNames );
Collections.sort( expectedConstraintTypeNames );

assertEquals( actualConstraintTypeNames, expectedConstraintTypeNames );
}

public static <T> void assertCorrectPropertyPaths(Set<ConstraintViolation<T>> violations, String... propertyPaths) {
Expand Down

0 comments on commit 4a84d6d

Please sign in to comment.