Posts

Showing posts from March, 2014

Bean validation

Bean validation, validating the properties of a Java bean, is the most oft used pattern in any application. In the past, people have been creating their own frameworks to handle the same. JSR-309/JSR-349 strives to create a standardized way of addressing this issue. Below is an example of simplicity and elegance of this solution. import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import static org.junit.Assert.assertEquals; import org.junit.Test; /** * @author Anand.Tamariya * */ public class TestValidation { @Test public void testForm() { Validator validator = Validation.buildDefaultValidatorFactory() .getValidator(); Car car = new Car(null, "DD-AB-123", 4); Set<ConstraintViolation<Car>> constraintViolations = valid