-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HV-1494 hibernate validator error when @NotEmpty used on return type #862
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
167 changes: 167 additions & 0 deletions
167
...rnate/validator/test/internal/engine/methodvalidation/CustomCompositeConstrainedTest.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,167 @@ | ||
/* | ||
* 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.assertNoViolations; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.pathWith; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; | ||
|
||
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 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 | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public class CustomCompositeConstrainedTest { | ||
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 testNotEmptyInvalid() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createBarString", String.class ), | ||
"" | ||
); | ||
assertThat( violations ).containsOnlyViolations( | ||
violationOf( NotEmpty.class ) | ||
); | ||
|
||
violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createBarString", String.class ), | ||
" " | ||
); | ||
assertNoViolations( violations ); | ||
} | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HV-1494") | ||
public void testCustomComposingConstraintReturnValues() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
"a" | ||
); | ||
assertThat( violations ).containsOnlyViolations( | ||
violationOf( CustomCompositeConstraint.class ) | ||
.withPropertyPath( pathWith() | ||
.method( "createCustomBarString" ) | ||
.returnValue() | ||
) | ||
); | ||
|
||
violations = validator.forExecutables() | ||
.validateReturnValue( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
"1" | ||
); | ||
assertNoViolations( violations ); | ||
} | ||
|
||
@Test | ||
@TestForIssue( jiraKey = "HV-1494") | ||
public void testCustomComposingConstraintParameters() throws Exception { | ||
Set<ConstraintViolation<Foo>> violations = validator.forExecutables() | ||
.validateParameters( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
new String[] { "abc" } | ||
); | ||
assertThat( violations ).containsOnlyViolations( | ||
violationOf( CustomCompositeConstraint.class ) | ||
.withPropertyPath( pathWith() | ||
.method( "createCustomBarString" ) | ||
.parameter( "a", 0 ) | ||
) | ||
); | ||
|
||
violations = validator.forExecutables() | ||
.validateParameters( | ||
foo, | ||
Foo.class.getDeclaredMethod( "createCustomBarString", String.class ), | ||
new String[] { "1" } | ||
); | ||
assertNoViolations( violations ); | ||
} | ||
|
||
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 = { }) | ||
@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 { }; | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already call
parseComposingConstraints()
; could we somehow combine this, instead of running the annotation tree another time?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll see what can be done about it and open another PR if something good comes out.