-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1328 Add documentation on new fail fast mode
- Loading branch information
1 parent
626d4fb
commit 6d6dbbc
Showing
6 changed files
with
236 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
55 changes: 55 additions & 0 deletions
55
...va/org/hibernate/validator/referenceguide/chapter12/failfastonpropertyviolation/Book.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,55 @@ | ||
// tag::include[] | ||
package org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation; | ||
|
||
//end::include[] | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import org.hibernate.validator.constraints.ISBN; | ||
|
||
//tag::include[] | ||
@NonSelfPublishing | ||
public class Book { | ||
|
||
@ISBN | ||
private String isbn; | ||
|
||
@NotBlank | ||
private String title; | ||
|
||
@NotNull | ||
private Person author; | ||
|
||
@NotNull | ||
private Person publisher; | ||
|
||
//constructors, getters and setters... | ||
//end::include[] | ||
|
||
public Book(String isbn, String title, Person author, Person publisher) { | ||
this.isbn = isbn; | ||
this.title = title; | ||
this.author = author; | ||
this.publisher = publisher; | ||
} | ||
|
||
public String getIsbn() { | ||
return this.isbn; | ||
} | ||
|
||
public String getTitle() { | ||
return this.title; | ||
} | ||
|
||
public Person getAuthor() { | ||
return this.author; | ||
} | ||
|
||
public Person getPublisher() { | ||
return this.publisher; | ||
} | ||
|
||
//tag::include[] | ||
} | ||
//end::include[] |
60 changes: 60 additions & 0 deletions
60
...referenceguide/chapter12/failfastonpropertyviolation/FailFastOnPropertyViolationTest.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,60 @@ | ||
package org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import org.hibernate.validator.HibernateValidator; | ||
|
||
import org.junit.Test; | ||
|
||
public class FailFastOnPropertyViolationTest { | ||
|
||
@Test | ||
public void failFastOnPropertyViolation() { | ||
//tag::include[] | ||
Validator validator = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.failFastOnPropertyViolation( true ) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
|
||
Book book = new Book( "978-1-56619-909-4", "Book", null /* author */, null /* publisher */ ); | ||
|
||
Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book ); | ||
|
||
assertEquals( 2, constraintViolations.size() ); | ||
|
||
for ( ConstraintViolation<Book> constraintViolation : constraintViolations ) { | ||
assertEquals( | ||
NotNull.class, | ||
constraintViolation.getConstraintDescriptor() | ||
.getAnnotation() | ||
.annotationType() | ||
); | ||
} | ||
//end::include[] | ||
} | ||
|
||
@Test | ||
public void failFastOnPropertyViolationProperty() { | ||
//tag::property[] | ||
Validator validator = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.addProperty( "hibernate.validator.fail_fast_on_property_violation", "true" ) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
//end::property[] | ||
|
||
Book book = new Book( "978-1-56619-909-4", "Book", null /* author */, null /* publisher */ ); | ||
|
||
Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book ); | ||
|
||
assertEquals( 2, constraintViolations.size() ); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ate/validator/referenceguide/chapter12/failfastonpropertyviolation/NonSelfPublishing.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,38 @@ | ||
package org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.Payload; | ||
|
||
import org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation.NonSelfPublishing.NonSelfPublishingValidator; | ||
|
||
@Documented | ||
@Constraint(validatedBy = { NonSelfPublishingValidator.class }) | ||
@Target({ TYPE }) | ||
@Retention(RUNTIME) | ||
public @interface NonSelfPublishing { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation.NonSelfPublishing.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
// tag::include[] | ||
class NonSelfPublishingValidator implements ConstraintValidator<NonSelfPublishing, Book> { | ||
|
||
@Override | ||
public boolean isValid(Book book, ConstraintValidatorContext context) { | ||
return !book.getAuthor().equals( book.getPublisher() ); | ||
} | ||
} | ||
//end::include[] | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
.../org/hibernate/validator/referenceguide/chapter12/failfastonpropertyviolation/Person.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,24 @@ | ||
// tag::include[] | ||
package org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation; | ||
|
||
//end::include[] | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
//tag::include[] | ||
public class Person { | ||
|
||
@NotNull | ||
private String firstName; | ||
|
||
@NotNull | ||
private String lastName; | ||
|
||
public Person(@NotNull final String firstName, @NotNull final String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
//getters, setters, equals and hashcode... | ||
} | ||
//end::include[] |
10 changes: 10 additions & 0 deletions
10
...bernate/validator/referenceguide/chapter12/fail-fast-on-property-violation-validation.xml
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,10 @@ | ||
<validation-config | ||
xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration | ||
http://xmlns.jcp.org/xml/ns/validation/configuration/validation-configuration-2.0.xsd" | ||
version="2.0"> | ||
|
||
<property name="hibernate.validator.fail_fast_on_property_violation">true</property> | ||
|
||
</validation-config> |