Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbdee2f
commit d431f53
Showing
7 changed files
with
378 additions
and
6 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
59 changes: 59 additions & 0 deletions
59
...est/java/org/hibernate/validator/referenceguide/chapter03/groups/groupconversion/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,59 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.groups.groupconversion; | ||
|
||
import javax.validation.GroupSequence; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.AssertTrue; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
import javax.validation.groups.ConvertGroup; | ||
import javax.validation.groups.Default; | ||
|
||
@GroupSequence({ CarChecks.class, Car.class }) | ||
public class Car { | ||
@NotNull | ||
private String manufacturer; | ||
|
||
@NotNull | ||
@Size(min = 2, max = 14) | ||
private String licensePlate; | ||
|
||
@Min(2) | ||
private int seatCount; | ||
|
||
@AssertTrue( | ||
message = "The car has to pass the vehicle inspection first", | ||
groups = CarChecks.class | ||
) | ||
private boolean passedVehicleInspection; | ||
|
||
@Valid | ||
@ConvertGroup(from = Default.class, to = DriverChecks.class) | ||
private Driver driver; | ||
|
||
public Car(String manufacturer, String licencePlate, int seatCount) { | ||
this.manufacturer = manufacturer; | ||
this.licensePlate = licencePlate; | ||
this.seatCount = seatCount; | ||
} | ||
|
||
public boolean isPassedVehicleInspection() { | ||
return passedVehicleInspection; | ||
} | ||
|
||
public void setPassedVehicleInspection(boolean passedVehicleInspection) { | ||
this.passedVehicleInspection = passedVehicleInspection; | ||
} | ||
|
||
public Driver getDriver() { | ||
return driver; | ||
} | ||
|
||
public void setDriver(Driver driver) { | ||
this.driver = driver; | ||
} | ||
|
||
// getters and setters ... | ||
} | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
...va/org/hibernate/validator/referenceguide/chapter03/groups/groupconversion/CarChecks.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,4 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.groups.groupconversion; | ||
|
||
public interface CarChecks { | ||
} |
37 changes: 37 additions & 0 deletions
37
.../java/org/hibernate/validator/referenceguide/chapter03/groups/groupconversion/Driver.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,37 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.groups.groupconversion; | ||
|
||
import javax.validation.constraints.AssertTrue; | ||
import javax.validation.constraints.Min; | ||
|
||
public class Driver extends Person { | ||
@Min( | ||
value = 18, | ||
message = "You have to be 18 to drive a car", | ||
groups = DriverChecks.class | ||
) | ||
public int age; | ||
|
||
@AssertTrue( | ||
message = "You first have to pass the driving test", | ||
groups = DriverChecks.class | ||
) | ||
public boolean hasDrivingLicense; | ||
|
||
public Driver(String name) { | ||
super( name ); | ||
} | ||
|
||
public void passedDrivingTest(boolean b) { | ||
hasDrivingLicense = b; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
} | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
...org/hibernate/validator/referenceguide/chapter03/groups/groupconversion/DriverChecks.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,4 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.groups.groupconversion; | ||
|
||
public interface DriverChecks { | ||
} |
47 changes: 47 additions & 0 deletions
47
...ernate/validator/referenceguide/chapter03/groups/groupconversion/GroupConversionTest.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,47 @@ | ||
package org.hibernate.validator.referenceguide.chapter03.groups.groupconversion; | ||
|
||
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 junit.framework.Assert.assertEquals; | ||
|
||
public class GroupConversionTest { | ||
|
||
private static Validator validator; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
validator = factory.getValidator(); | ||
} | ||
|
||
@Test | ||
public void validateDriverChecksTogetherWithCarChecks() { | ||
// create a car and validate. The Driver is still null and does not get validated | ||
Car car = new Car( "VW", "USD-123", 4 ); | ||
car.setPassedVehicleInspection( true ); | ||
Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car ); | ||
assertEquals( 0, constraintViolations.size() ); | ||
|
||
// create a driver who has not passed the driving test | ||
Driver john = new Driver( "John Doe" ); | ||
john.setAge( 18 ); | ||
|
||
// now let's add a driver to the car | ||
car.setDriver( john ); | ||
constraintViolations = validator.validate( car ); | ||
assertEquals( 1, constraintViolations.size() ); | ||
assertEquals( | ||
"The driver constraint should also be validated as part of the default group", | ||
constraintViolations.iterator().next().getMessage(), | ||
"You first have to pass the driving test" | ||
); | ||
} | ||
} | ||
|
Oops, something went wrong.