|
| 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.methodvalidation; |
| 8 | + |
| 9 | +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; |
| 10 | +import static java.lang.annotation.ElementType.CONSTRUCTOR; |
| 11 | +import static java.lang.annotation.ElementType.FIELD; |
| 12 | +import static java.lang.annotation.ElementType.METHOD; |
| 13 | +import static java.lang.annotation.ElementType.PARAMETER; |
| 14 | +import static java.lang.annotation.ElementType.TYPE_USE; |
| 15 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 16 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectConstraintTypes; |
| 17 | +import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNumberOfViolations; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.Target; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import javax.validation.Constraint; |
| 25 | +import javax.validation.ConstraintViolation; |
| 26 | +import javax.validation.Payload; |
| 27 | +import javax.validation.ReportAsSingleViolation; |
| 28 | +import javax.validation.Validator; |
| 29 | +import javax.validation.constraints.NotNull; |
| 30 | +import javax.validation.constraints.Pattern; |
| 31 | +import javax.validation.constraints.Size; |
| 32 | +import javax.validation.constraintvalidation.SupportedValidationTarget; |
| 33 | +import javax.validation.constraintvalidation.ValidationTarget; |
| 34 | + |
| 35 | +import org.hibernate.validator.constraints.NotEmpty; |
| 36 | +import org.hibernate.validator.testutil.TestForIssue; |
| 37 | +import org.hibernate.validator.testutils.ValidatorUtil; |
| 38 | +import org.testng.annotations.BeforeMethod; |
| 39 | +import org.testng.annotations.Test; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Marko Bekhta |
| 43 | + */ |
| 44 | +public class PureCompositeConstraintsOnReturnValueTest { |
| 45 | + private Validator validator; |
| 46 | + private Foo foo; |
| 47 | + |
| 48 | + @BeforeMethod |
| 49 | + public void setUp() throws Exception { |
| 50 | + validator = ValidatorUtil.getValidator(); |
| 51 | + foo = new Foo( "" ); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @TestForIssue( jiraKey = "HV-1494") |
| 56 | + public void testHVSpecificNotEmpty() throws Exception { |
| 57 | + Set<ConstraintViolation<Foo>> violations = validator.forExecutables() |
| 58 | + .validateReturnValue( |
| 59 | + foo, |
| 60 | + Foo.class.getDeclaredMethod( "createBarString", String.class ), |
| 61 | + "" |
| 62 | + ); |
| 63 | + assertCorrectConstraintTypes( violations, NotEmpty.class ); |
| 64 | + |
| 65 | + violations = validator.forExecutables() |
| 66 | + .validateReturnValue( |
| 67 | + foo, |
| 68 | + Foo.class.getDeclaredMethod( "createBarString", String.class ), |
| 69 | + " " |
| 70 | + ); |
| 71 | + assertNumberOfViolations( violations, 0 ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @TestForIssue( jiraKey = "HV-1494") |
| 76 | + public void testCustomComposingConstraintOnReturnValue() throws Exception { |
| 77 | + Set<ConstraintViolation<Foo>> violations = validator.forExecutables() |
| 78 | + .validateReturnValue( |
| 79 | + foo, |
| 80 | + Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), |
| 81 | + "a" |
| 82 | + ); |
| 83 | + assertCorrectConstraintTypes( violations, CustomCompositeConstraint.class ); |
| 84 | + |
| 85 | + violations = validator.forExecutables() |
| 86 | + .validateReturnValue( |
| 87 | + foo, |
| 88 | + Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), |
| 89 | + "1" |
| 90 | + ); |
| 91 | + assertNumberOfViolations( violations, 0 ); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @TestForIssue( jiraKey = "HV-1494") |
| 96 | + public void testCustomComposingConstraintOnParameters() throws Exception { |
| 97 | + Set<ConstraintViolation<Foo>> violations = validator.forExecutables() |
| 98 | + .validateParameters( |
| 99 | + foo, |
| 100 | + Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), |
| 101 | + new String[] { "abc" } |
| 102 | + ); |
| 103 | + assertCorrectConstraintTypes( violations, CustomCompositeConstraint.class ); |
| 104 | + |
| 105 | + violations = validator.forExecutables() |
| 106 | + .validateParameters( |
| 107 | + foo, |
| 108 | + Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), |
| 109 | + new String[] { "1" } |
| 110 | + ); |
| 111 | + assertNumberOfViolations( violations, 0 ); |
| 112 | + } |
| 113 | + |
| 114 | + private static class Foo { |
| 115 | + |
| 116 | + private String bar; |
| 117 | + |
| 118 | + public Foo(String bar) { |
| 119 | + this.bar = bar; |
| 120 | + } |
| 121 | + |
| 122 | + @NotEmpty |
| 123 | + public String createBarString(String a) { |
| 124 | + return bar; |
| 125 | + } |
| 126 | + |
| 127 | + @CustomCompositeConstraint |
| 128 | + public String createCustomBarString(@CustomCompositeConstraint String a) { |
| 129 | + return bar; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Documented |
| 134 | + @Constraint(validatedBy = { }) |
| 135 | + @SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT) |
| 136 | + @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) |
| 137 | + @Retention(RUNTIME) |
| 138 | + @ReportAsSingleViolation |
| 139 | + @NotNull |
| 140 | + @Size(min = 1) |
| 141 | + @Pattern(regexp = "\\d*") |
| 142 | + public @interface CustomCompositeConstraint { |
| 143 | + String message() default "no message"; |
| 144 | + |
| 145 | + Class<?>[] groups() default { }; |
| 146 | + |
| 147 | + Class<? extends Payload>[] payload() default { }; |
| 148 | + } |
| 149 | +} |
0 commit comments