Creating a simple constraint - Using the constraint

WARNING - TOPIC NOT WRITTEN - TOPIC ID: 2458

This topic has not yet been written. The content below is from the topic description.
3.1.4. Using the constraint Now that our first custom constraint is completed, we can use it in the Car class from the Chapter 1, Getting started chapter to specify that the licensePlate field shall only contain upper-case strings: Example 3.7. Applying the CheckCase constraint package com.mycompany; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Car {     @NotNull     private String manufacturer;     @NotNull     @Size(min = 2, max = 14)     @CheckCase(CaseMode.UPPER)     private String licensePlate;     @Min(2)     private int seatCount;          public Car(String manufacturer, String licencePlate, int seatCount) {         this.manufacturer = manufacturer;         this.licensePlate = licencePlate;         this.seatCount = seatCount;     }     //getters and setters ... } Finally let's demonstrate in a little test that the @CheckCase constraint is properly validated: Example 3.8. Testcase demonstrating the CheckCase validation package com.mycompany; import static org.junit.Assert.*; 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; 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 > 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> constraintViolations =             validator.validate(car);         assertEquals(0, constraintViolations.size());     } }