|
| 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.pathWith; |
| 10 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import javax.validation.ConstraintViolation; |
| 17 | +import javax.validation.Valid; |
| 18 | +import javax.validation.Validator; |
| 19 | +import javax.validation.constraints.Email; |
| 20 | +import javax.validation.constraints.NotNull; |
| 21 | +import javax.validation.valueextraction.Unwrapping; |
| 22 | + |
| 23 | +import org.hibernate.validator.testutil.ConstraintViolationAssert; |
| 24 | +import org.hibernate.validator.testutil.TestForIssue; |
| 25 | +import org.hibernate.validator.testutils.CandidateForTck; |
| 26 | +import org.hibernate.validator.testutils.ValidatorUtil; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Guillaume Smet |
| 31 | + */ |
| 32 | +@TestForIssue(jiraKey = "HV-1596") |
| 33 | +@CandidateForTck |
| 34 | +public class ContainerInClassHierarchyTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testContainerInHierarchy() { |
| 38 | + Validator validator = ValidatorUtil.getValidator(); |
| 39 | + |
| 40 | + Set<ConstraintViolation<Bean>> constraintViolations = validator.validate( new Bean() ); |
| 41 | + ConstraintViolationAssert.assertThat( constraintViolations ).containsOnlyViolations( |
| 42 | + violationOf( Email.class ).withPropertyPath( |
| 43 | + pathWith().property( "container" ).containerElement( "<iterable element>", true, null, null, Container.class, null ) ), |
| 44 | + violationOf( NotNull.class ).withPropertyPath( |
| 45 | + pathWith().property( "container" ).property( "property" ) ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + private static class Bean { |
| 50 | + |
| 51 | + @Valid |
| 52 | + @Email(payload = Unwrapping.Unwrap.class) |
| 53 | + private Container container = new Container(); |
| 54 | + } |
| 55 | + |
| 56 | + private static class Container extends ContainerParentClass { |
| 57 | + } |
| 58 | + |
| 59 | + private static class ContainerParentClass implements Iterable<String> { |
| 60 | + |
| 61 | + @NotNull |
| 62 | + private String property; |
| 63 | + |
| 64 | + @Override |
| 65 | + public Iterator<String> iterator() { |
| 66 | + return Arrays. asList( "[email protected]", "invalid-email" ). iterator(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments