|
| 1 | +package com.baeldung.validation; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import javax.validation.ConstraintViolation; |
| 7 | +import javax.validation.Validation; |
| 8 | +import javax.validation.Validator; |
| 9 | +import javax.validation.ValidatorFactory; |
| 10 | + |
| 11 | +import org.apache.bval.jsr.ApacheValidationProvider; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +import com.baeldung.model.User; |
| 19 | + |
| 20 | +public class ValidationTest { |
| 21 | + private static ValidatorFactory validatorFactory; |
| 22 | + private static Validator validator; |
| 23 | + |
| 24 | + @BeforeClass |
| 25 | + public static void setup() { |
| 26 | + validatorFactory = Validation.byProvider(ApacheValidationProvider.class) |
| 27 | + .configure() |
| 28 | + .buildValidatorFactory(); |
| 29 | + validator = validatorFactory.getValidator(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenUser_whenValidate_thenValidationViolations() { |
| 34 | + User user = new User("ana@yahoo.com", "pass", "nameTooLong_______________", 15); |
| 35 | + |
| 36 | + Set<ConstraintViolation<User>> violations = validator.validate(user); |
| 37 | + assertTrue("no violations", violations.size() > 0); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenInvalidAge_whenValidateProperty_thenConstraintViolation() { |
| 42 | + User user = new User("ana@yahoo.com", "pass", "Ana", 12); |
| 43 | + |
| 44 | + Set<ConstraintViolation<User>> propertyViolations = validator.validateProperty(user, "age"); |
| 45 | + assertEquals("size is not 1", 1, propertyViolations.size()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenValidAge_whenValidateValue_thenNoConstraintViolation() { |
| 50 | + User user = new User("ana@yahoo.com", "pass", "Ana", 18); |
| 51 | + |
| 52 | + Set<ConstraintViolation<User>> valueViolations = validator.validateValue(User.class, "age", 20); |
| 53 | + assertEquals("size is not 0", 0, valueViolations.size()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void whenValidateNonJSR_thenCorrect() { |
| 58 | + User user = new User("ana@yahoo.com", "pass", "Ana", 20); |
| 59 | + user.setCardNumber("1234"); |
| 60 | + user.setIban("1234"); |
| 61 | + user.setWebsite("10.0.2.50"); |
| 62 | + user.setMainDirectory(new File(".")); |
| 63 | + |
| 64 | + Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "iban"); |
| 65 | + assertEquals("size is not 1", 1, violations.size()); |
| 66 | + |
| 67 | + violations = validator.validateProperty(user, "website"); |
| 68 | + assertEquals("size is not 0", 0, violations.size()); |
| 69 | + |
| 70 | + violations = validator.validateProperty(user, "mainDirectory"); |
| 71 | + assertEquals("size is not 0", 0, violations.size()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void givenInvalidPassword_whenValidatePassword_thenConstraintViolation() { |
| 76 | + User user = new User("ana@yahoo.com", "password", "Ana", 20); |
| 77 | + Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "password"); |
| 78 | + assertEquals("message incorrect", "Invalid password", violations.iterator() |
| 79 | + .next() |
| 80 | + .getMessage()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenValidPassword_whenValidatePassword_thenNoConstraintViolation() { |
| 85 | + User user = new User("ana@yahoo.com", "password#", "Ana", 20); |
| 86 | + |
| 87 | + Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "password"); |
| 88 | + assertEquals("size is not 0", 0, violations.size()); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterClass |
| 92 | + public static void close() { |
| 93 | + if (validatorFactory != null) { |
| 94 | + validatorFactory.close(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments