Skip to content
Browse files

HV-902 Adding fail-fast return; Splitting up test

  • Loading branch information...
1 parent 9f83f54 commit b02c9ec561fed379f39548e5e0dbfa44b7ca8c1b @gunnarmorling gunnarmorling committed
View
5 engine/src/main/java/org/hibernate/validator/internal/engine/ValidatorImpl.java
@@ -575,7 +575,7 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,
);
// HV-902: First, validate the value itself
- Iterator<?> valueIter = Arrays.asList( value ).iterator();
+ Iterator<?> valueIter = Collections.singletonList( value ).iterator();
validateCascadedConstraint(
validationContext,
valueIter,
@@ -583,6 +583,9 @@ private void validateCascadedConstraints(ValidationContext<?> validationContext,
valueContext,
validationOrder
);
+ if ( shouldFailFast( validationContext ) ) {
+ return;
+ }
// Second, validate elements contained in the value if it is Iterable, Map, or an Array
Type type = value.getClass();
View
78 .../hibernate/validator/test/internal/engine/cascaded/CascadedIterableMapPropertiesTest.java
@@ -16,10 +16,12 @@
*/
package org.hibernate.validator.test.internal.engine.cascaded;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
+
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validator;
@@ -27,40 +29,64 @@
import javax.validation.constraints.NotNull;
import org.testng.annotations.Test;
-
import org.hibernate.validator.testutil.TestForIssue;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectPropertyPaths;
import static org.hibernate.validator.testutil.ValidatorUtil.getValidator;
-import static org.testng.Assert.assertEquals;
/**
+ * Test for cascaded validation of custom iterable types for which the contained elements but also constraints on the
+ * type themselves should be validated.
+ *
* @author Khalid Alqinyah
+ * @author Gunnar Morling
*/
public class CascadedIterableMapPropertiesTest {
@Test
- @TestForIssue( jiraKey = "HV-902")
- public void testValidateCascadingIterableAndMapProperties() {
+ @TestForIssue(jiraKey = "HV-902")
+ public void testValidateCustomIterableType() {
Validator validator = getValidator();
- Set<ConstraintViolation<CascadingIterMap>> constraintViolations = validator.validate( new CascadingIterMap() );
- assertEquals( constraintViolations.size(), 4 );
- assertCorrectPropertyPaths( constraintViolations,
+ Set<ConstraintViolation<IterableExtHolder>> constraintViolations = validator.validate( new IterableExtHolder() );
+ assertCorrectPropertyPaths(
+ constraintViolations,
"iterableExt.value",
- "iterableExt[].number",
+ "iterableExt[].number"
+ );
+ }
+
+ @Test
+ @TestForIssue(jiraKey = "HV-902")
+ public void testValidateCustomListType() {
+ Validator validator = getValidator();
+ Set<ConstraintViolation<ListExtHolder>> constraintViolations = validator.validate( new ListExtHolder() );
+ assertCorrectPropertyPaths(
+ constraintViolations,
+ "listExt.value",
+ "listExt[1].number"
+ );
+ }
+
+ @Test
+ @TestForIssue(jiraKey = "HV-902")
+ public void testValidateCustomMapType() {
+ Validator validator = getValidator();
+ Set<ConstraintViolation<MapExtHolder>> constraintViolations = validator.validate( new MapExtHolder() );
+ assertCorrectPropertyPaths(
+ constraintViolations,
"mapExt.value",
- "mapExt[second].number" );
+ "mapExt[second].number"
+ );
}
- class CascadingIterMap {
- @Valid
- IterableExt iterableExt = new IterableExt();
+ class IterableExtHolder {
@Valid
- MapExt mapExt = new MapExt();
+ IterableExt iterableExt = new IterableExt();
}
class IterableExt implements Iterable<IntWrapper> {
+
@NotNull
Integer value = null;
@@ -70,7 +96,32 @@ public void testValidateCascadingIterableAndMapProperties() {
}
}
+ class ListExtHolder {
+
+ @Valid
+ ListExt listExt = new ListExt();
+ }
+
+ @SuppressWarnings("serial")
+ class ListExt extends ArrayList<IntWrapper> {
+
+ @NotNull
+ Integer value = null;
+
+ public ListExt() {
+ super( Arrays.asList( new IntWrapper( 2 ), new IntWrapper( 1 ), new IntWrapper( 5 ) ) );
+ }
+ }
+
+ class MapExtHolder {
+
+ @Valid
+ MapExt mapExt = new MapExt();
+ }
+
+ @SuppressWarnings("serial")
class MapExt extends HashMap<String, IntWrapper> {
+
@NotNull
Integer value = null;
@@ -82,6 +133,7 @@ public MapExt() {
}
class IntWrapper {
+
@Min(value = 2)
Integer number;

0 comments on commit b02c9ec

Please sign in to comment.
Something went wrong with that request. Please try again.