|
| 1 | +/* |
| 2 | + * Hibernate Validator, declare and validate application constraints |
| 3 | + * |
| 4 | + * License: Apache License, Version 2.0 |
| 5 | + * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. |
| 6 | + */ |
| 7 | +package org.hibernate.validator.test.internal.engine.valueextraction; |
| 8 | + |
| 9 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; |
| 10 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.pathWith; |
| 11 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; |
| 12 | +import static org.hibernate.validator.testutils.ValidatorUtil.getValidator; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +import javax.validation.ConstraintViolation; |
| 21 | +import javax.validation.Valid; |
| 22 | +import javax.validation.Validator; |
| 23 | +import javax.validation.constraints.NotNull; |
| 24 | + |
| 25 | +import org.hibernate.validator.internal.util.CollectionHelper; |
| 26 | +import org.hibernate.validator.testutil.TestForIssue; |
| 27 | +import org.hibernate.validator.testutils.CandidateForTck; |
| 28 | +import org.testng.annotations.Test; |
| 29 | + |
| 30 | +@CandidateForTck |
| 31 | +public class GenericModelLegacyCascadingTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + @TestForIssue(jiraKey = "HV-1481") |
| 35 | + public void testCascadingOnObject() { |
| 36 | + Validator validator = getValidator(); |
| 37 | + |
| 38 | + GenericModelHolder<InvalidModel> holder = new GenericModelHolder<>(); |
| 39 | + holder.setModel( new InvalidModel() ); |
| 40 | + |
| 41 | + Set<ConstraintViolation<GenericModelHolder<?>>> constraintViolations = validator.validate( holder ); |
| 42 | + |
| 43 | + assertThat( constraintViolations ).containsOnlyViolations( |
| 44 | + violationOf( NotNull.class ) |
| 45 | + .withPropertyPath( pathWith() |
| 46 | + .property( "model" ) |
| 47 | + .property( "notNullProperty" ) ) ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @TestForIssue(jiraKey = "HV-1481") |
| 52 | + public void testCascadingOnList() { |
| 53 | + Validator validator = getValidator(); |
| 54 | + |
| 55 | + GenericModelHolder<List<InvalidModel>> holder = new GenericModelHolder<>(); |
| 56 | + holder.setModel( Arrays.asList( new InvalidModel() ) ); |
| 57 | + |
| 58 | + Set<ConstraintViolation<GenericModelHolder<?>>> constraintViolations = validator.validate( holder ); |
| 59 | + |
| 60 | + assertThat( constraintViolations ).containsOnlyViolations( |
| 61 | + violationOf( NotNull.class ) |
| 62 | + .withPropertyPath( pathWith() |
| 63 | + .property( "model" ) |
| 64 | + .property( "notNullProperty", true, null, 0, List.class, 0 ) ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @TestForIssue(jiraKey = "HV-1481") |
| 69 | + public void testCascadingOnMap() { |
| 70 | + Validator validator = getValidator(); |
| 71 | + |
| 72 | + Map<String, InvalidModel> map = new HashMap<>(); |
| 73 | + map.put( "invalidModel", new InvalidModel() ); |
| 74 | + |
| 75 | + GenericModelHolder<Map<String, InvalidModel>> holder = new GenericModelHolder<>(); |
| 76 | + holder.setModel( map ); |
| 77 | + |
| 78 | + Set<ConstraintViolation<GenericModelHolder<?>>> constraintViolations = validator.validate( holder ); |
| 79 | + |
| 80 | + assertThat( constraintViolations ).containsOnlyViolations( |
| 81 | + violationOf( NotNull.class ) |
| 82 | + .withPropertyPath( pathWith() |
| 83 | + .property( "model" ) |
| 84 | + .property( "notNullProperty", true, "invalidModel", null, Map.class, 1 ) ) ); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @TestForIssue(jiraKey = "HV-1481") |
| 89 | + public void testCascadingOnIterable() { |
| 90 | + Validator validator = getValidator(); |
| 91 | + |
| 92 | + GenericModelHolder<Set<InvalidModel>> holder = new GenericModelHolder<>(); |
| 93 | + holder.setModel( CollectionHelper.asSet( new InvalidModel() ) ); |
| 94 | + |
| 95 | + Set<ConstraintViolation<GenericModelHolder<?>>> constraintViolations = validator.validate( holder ); |
| 96 | + |
| 97 | + assertThat( constraintViolations ).containsOnlyViolations( |
| 98 | + violationOf( NotNull.class ) |
| 99 | + .withPropertyPath( pathWith() |
| 100 | + .property( "model" ) |
| 101 | + .property( "notNullProperty", true, null, null, Iterable.class, 0 ) ) ); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + @TestForIssue(jiraKey = "HV-1481") |
| 106 | + public void testCascadingOnArray() { |
| 107 | + Validator validator = getValidator(); |
| 108 | + |
| 109 | + GenericModelHolder<InvalidModel[]> holder = new GenericModelHolder<>(); |
| 110 | + holder.setModel( new InvalidModel[]{ new InvalidModel() } ); |
| 111 | + |
| 112 | + Set<ConstraintViolation<GenericModelHolder<?>>> constraintViolations = validator.validate( holder ); |
| 113 | + |
| 114 | + assertThat( constraintViolations ).containsOnlyViolations( |
| 115 | + violationOf( NotNull.class ) |
| 116 | + .withPropertyPath( pathWith() |
| 117 | + .property( "model" ) |
| 118 | + .property( "notNullProperty", true, null, 0, Object[].class, null ) ) ); |
| 119 | + } |
| 120 | + |
| 121 | + private class GenericModelHolder<M> { |
| 122 | + |
| 123 | + private M model; |
| 124 | + |
| 125 | + @Valid |
| 126 | + public M getModel() { |
| 127 | + return model; |
| 128 | + } |
| 129 | + |
| 130 | + public void setModel(M model) { |
| 131 | + this.model = model; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public class InvalidModel { |
| 136 | + |
| 137 | + @NotNull |
| 138 | + public Object getNotNullProperty() { |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments