Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-725 Chapter 3 updates - adding examples to source tree and updatin…
…g docs.
- Loading branch information
1 parent
a61da2a
commit c959280
Showing
21 changed files
with
682 additions
and
179 deletions.
There are no files selected for viewing
417 changes: 238 additions & 179 deletions
417
documentation/src/main/docbook/en-US/modules/customconstraints.xml
Large diffs are not rendered by default.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/Car.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,27 @@ | ||
package org.hibernate.validator.referenceguide.chapter03; | ||
|
||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
public class Car { | ||
|
||
@NotNull | ||
private final String manufacturer; | ||
|
||
@NotNull | ||
@Size(min = 2, max = 14) | ||
@CheckCase(CaseMode.UPPER) | ||
private final String licensePlate; | ||
|
||
@Min(2) | ||
private final int seatCount; | ||
|
||
public Car ( String manufacturer, String licencePlate, int seatCount ) { | ||
this.manufacturer = manufacturer; | ||
this.licensePlate = licencePlate; | ||
this.seatCount = seatCount; | ||
} | ||
|
||
//getters and setters ... | ||
} |
48 changes: 48 additions & 0 deletions
48
documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/CarTest.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,48 @@ | ||
package org.hibernate.validator.referenceguide.chapter03; | ||
|
||
import java.util.Set; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.ValidatorFactory; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CarTest { | ||
|
||
private static Validator validator; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
validator = factory.getValidator(); | ||
} | ||
|
||
@Test | ||
public void testLicensePlateNotUpperCase() { | ||
|
||
Car car = new Car( "Morris", "dd-ab-123", 4 ); | ||
|
||
Set<ConstraintViolation<Car>> constraintViolations = | ||
validator.validate( car ); | ||
assertEquals( 1, constraintViolations.size() ); | ||
assertEquals( | ||
"Case mode must be UPPER.", | ||
constraintViolations.iterator().next().getMessage() | ||
); | ||
} | ||
|
||
@Test | ||
public void carIsValid() { | ||
|
||
Car car = new Car( "Morris", "DD-AB-123", 4 ); | ||
|
||
Set<ConstraintViolation<Car>> constraintViolations = | ||
validator.validate( car ); | ||
|
||
assertEquals( 0, constraintViolations.size() ); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/CaseMode.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,6 @@ | ||
package org.hibernate.validator.referenceguide.chapter03; | ||
|
||
public enum CaseMode { | ||
UPPER, | ||
LOWER; | ||
} |
35 changes: 35 additions & 0 deletions
35
documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/CheckCase.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,35 @@ | ||
package org.hibernate.validator.referenceguide.chapter03; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
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.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = CheckCaseValidator.class) | ||
@Documented | ||
public @interface CheckCase { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter03.CheckCase.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
CaseMode value(); | ||
|
||
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
CheckCase[] value(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...on/src/test/java/org/hibernate/validator/referenceguide/chapter03/CheckCaseValidator.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,28 @@ | ||
package org.hibernate.validator.referenceguide.chapter03; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> { | ||
|
||
private CaseMode caseMode; | ||
|
||
@Override | ||
public void initialize(CheckCase constraintAnnotation) { | ||
this.caseMode = constraintAnnotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String object, ConstraintValidatorContext constraintContext) { | ||
if ( object == null ) { | ||
return true; | ||
} | ||
|
||
if ( caseMode == CaseMode.UPPER ) { | ||
return object.equals( object.toUpperCase() ); | ||
} | ||
else { | ||
return object.equals( object.toLowerCase() ); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...test/java/org/hibernate/validator/referenceguide/chapter03/constraintcomposition/Car.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,9 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintcomposition; | ||
|
||
public class Car { | ||
|
||
@ValidLicensePlate | ||
private String licensePlate; | ||
|
||
//... | ||
} |
6 changes: 6 additions & 0 deletions
6
...java/org/hibernate/validator/referenceguide/chapter03/constraintcomposition/CaseMode.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,6 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintcomposition; | ||
|
||
public enum CaseMode { | ||
UPPER, | ||
LOWER; | ||
} |
27 changes: 27 additions & 0 deletions
27
...ava/org/hibernate/validator/referenceguide/chapter03/constraintcomposition/CheckCase.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,27 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintcomposition; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ METHOD, FIELD, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = { }) | ||
@Documented | ||
public @interface CheckCase { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter03.constraintcomposition.CheckCase.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
CaseMode value(); | ||
} |
30 changes: 30 additions & 0 deletions
30
...hibernate/validator/referenceguide/chapter03/constraintcomposition/ValidLicensePlate.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,30 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintcomposition; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@NotNull | ||
@Size(min = 2, max = 14) | ||
@CheckCase(CaseMode.UPPER) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = { }) | ||
@Documented | ||
public @interface ValidLicensePlate { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter03.constraintcomposition.ValidLicensePlate.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
} |
15 changes: 15 additions & 0 deletions
15
...ator/referenceguide/chapter03/constraintcomposition/reportassingle/ValidLicensePlate.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,15 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintcomposition.reportassingle; | ||
|
||
import javax.validation.Payload; | ||
import javax.validation.ReportAsSingleViolation; | ||
|
||
//... | ||
@ReportAsSingleViolation | ||
public @interface ValidLicensePlate { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter03.constraintcomposition.ValidLicensePlate.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
} |
6 changes: 6 additions & 0 deletions
6
...org/hibernate/validator/referenceguide/chapter03/constraintvalidatorcontext/CaseMode.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,6 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintvalidatorcontext; | ||
|
||
public enum CaseMode { | ||
UPPER, | ||
LOWER; | ||
} |
27 changes: 27 additions & 0 deletions
27
...rg/hibernate/validator/referenceguide/chapter03/constraintvalidatorcontext/CheckCase.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,27 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintvalidatorcontext; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ METHOD, FIELD, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = CheckCaseValidator.class) | ||
@Documented | ||
public @interface CheckCase { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter03.constraintvalidatorcontext.CheckCase.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
CaseMode value(); | ||
} |
39 changes: 39 additions & 0 deletions
39
...ate/validator/referenceguide/chapter03/constraintvalidatorcontext/CheckCaseValidator.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,39 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.constraintvalidatorcontext; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> { | ||
|
||
private CaseMode caseMode; | ||
|
||
@Override | ||
public void initialize(CheckCase constraintAnnotation) { | ||
this.caseMode = constraintAnnotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String object, ConstraintValidatorContext constraintContext) { | ||
if ( object == null ) { | ||
return true; | ||
} | ||
|
||
boolean isValid; | ||
if ( caseMode == CaseMode.UPPER ) { | ||
isValid = object.equals( object.toUpperCase() ); | ||
} | ||
else { | ||
isValid = object.equals( object.toLowerCase() ); | ||
} | ||
|
||
if ( !isValid ) { | ||
constraintContext.disableDefaultConstraintViolation(); | ||
constraintContext.buildConstraintViolationWithTemplate( | ||
"{org.hibernate.validator.referenceguide.chapter03.constraintvalidatorcontext.CheckCase.message}" | ||
) | ||
.addConstraintViolation(); | ||
} | ||
|
||
return isValid; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...tation/src/test/java/org/hibernate/validator/referenceguide/chapter03/custompath/Car.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,23 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.custompath; | ||
|
||
import java.util.List; | ||
|
||
@ValidPassengerCount | ||
public class Car { | ||
|
||
private final int seatCount; | ||
private final List<String> passengers; | ||
|
||
public Car(int seatCount, List<String> passengers) { | ||
this.seatCount = seatCount; | ||
this.passengers = passengers; | ||
} | ||
|
||
public int getSeatCount() { | ||
return seatCount; | ||
} | ||
|
||
public List<String> getPassengers() { | ||
return passengers; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...on/src/test/java/org/hibernate/validator/referenceguide/chapter03/custompath/CarTest.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.chapter03.custompath; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.ValidatorFactory; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CarTest { | ||
|
||
private static Validator validator; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
validator = factory.getValidator(); | ||
} | ||
|
||
@Test | ||
public void morePassengersThanSeats() { | ||
Car car = new Car( 2, Arrays.asList( "Bob", "Alice", "Bill" ) ); | ||
|
||
Set<ConstraintViolation<Car>> constraintViolations = | ||
validator.validate( car ); | ||
|
||
assertEquals( 1, constraintViolations.size() ); | ||
|
||
ConstraintViolation<Car> violation = constraintViolations.iterator().next(); | ||
assertEquals( "{my.custom.template}", violation.getMessage() ); | ||
assertEquals( "passengers", violation.getPropertyPath().iterator().next().getName() ); | ||
} | ||
} |
Oops, something went wrong.