-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1494 Adding SupportedValidationTarget to HV composite constraints
- Loading branch information
1 parent
c15772e
commit 2b89528
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...ator/test/internal/engine/methodvalidation/PureCompositeConstraintsOnReturnValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.test.internal.engine.methodvalidation; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectConstraintTypes; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNumberOfViolations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.Set; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Payload; | ||
import javax.validation.ReportAsSingleViolation; | ||
import javax.validation.Validator; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
import javax.validation.constraints.Size; | ||
import javax.validation.constraintvalidation.SupportedValidationTarget; | ||
import javax.validation.constraintvalidation.ValidationTarget; | ||
|
||
import org.hibernate.validator.constraints.NotEmpty; | ||
import org.hibernate.validator.testutil.TestForIssue; | ||
import org.hibernate.validator.testutils.ValidatorUtil; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* @author Marko Bekhta | ||
*/ | ||
public class PureCompositeConstraintsOnReturnValueTest { | ||
private Validator validator; | ||
private Foo foo; | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception { | ||
validator = ValidatorUtil.getValidator(); | ||
foo = new Foo( "" ); | ||
} | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HV-1494") | ||
public void testHVSpecificNotEmpty() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createBarString", String.class ), | ||
"" | ||
); | ||
assertCorrectConstraintTypes( violations, NotEmpty.class ); | ||
|
||
violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createBarString", String.class ), | ||
" " | ||
); | ||
assertNumberOfViolations( violations, 0 ); | ||
} | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HV-1494") | ||
public void testCustomComposingConstraintOnReturnValue() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
"a" | ||
); | ||
assertCorrectConstraintTypes( violations, CustomCompositeConstraint.class ); | ||
|
||
violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
"1" | ||
); | ||
assertNumberOfViolations( violations, 0 ); | ||
} | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HV-1494") | ||
public void testCustomComposingConstraintOnParameters() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateParameters( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
new String[] { "abc" } | ||
); | ||
assertCorrectConstraintTypes( violations, CustomCompositeConstraint.class ); | ||
|
||
violations = validator.forExecutables() | ||
.validateParameters( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
new String[] { "1" } | ||
); | ||
assertNumberOfViolations( violations, 0 ); | ||
} | ||
|
||
private static class Foo { | ||
|
||
private String bar; | ||
|
||
public Foo(String bar) { | ||
this.bar = bar; | ||
} | ||
|
||
@NotEmpty | ||
public String createBarString(String a) { | ||
return bar; | ||
} | ||
|
||
@CustomCompositeConstraint | ||
public String createCustomBarString(@CustomCompositeConstraint String a) { | ||
return bar; | ||
} | ||
} | ||
|
||
@Documented | ||
@Constraint(validatedBy = { }) | ||
@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@ReportAsSingleViolation | ||
@NotNull | ||
@Size(min = 1) | ||
@Pattern(regexp = "\\d*") | ||
public @interface CustomCompositeConstraint { | ||
String message() default "no message"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
} | ||
} |