Multpile issues resolved
- fix formatting issues - add email field to Person - add email validation test
This commit is contained in:
@@ -1,30 +1,42 @@
|
||||
package org.baeldung.javaxval.messageinterpolator;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class Person {
|
||||
|
||||
@Size(min = 10, max = 200, message = "Name should be in-between {min} and {max} characters")
|
||||
private String name;
|
||||
@Size(min = 10, max = 100, message = "Name should be between {min} and {max} characters")
|
||||
private String name;
|
||||
|
||||
@Min(value = 18, message = "Age should not be less than {value} but it is ${validatedValue}")
|
||||
private int age;
|
||||
@Min(value = 18, message = "Age should not be less than {value}")
|
||||
private int age;
|
||||
|
||||
@Email(message = "Email address should be in a correct format: ${validatedValue}")
|
||||
private String email;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,44 +15,56 @@ import org.junit.Test;
|
||||
|
||||
public class ParameterMessageInterpolaterIntegrationTest {
|
||||
|
||||
private static Validator validator;
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
ValidatorFactory validatorFactory = Validation.byDefaultProvider()
|
||||
.configure()
|
||||
.messageInterpolator(new ParameterMessageInterpolator())
|
||||
.buildValidatorFactory();
|
||||
|
||||
validator = validatorFactory.getValidator();
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
ValidatorFactory validatorFactory = Validation.byDefaultProvider()
|
||||
.configure()
|
||||
.messageInterpolator(new ParameterMessageInterpolator())
|
||||
.buildValidatorFactory();
|
||||
|
||||
@Test
|
||||
public void ifNameLengthIsLess_nameValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Doe");
|
||||
person.setAge(18);
|
||||
validator = validatorFactory.getValidator();
|
||||
}
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
@Test
|
||||
public void ifNameLengthIsLess_nameValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Doe");
|
||||
person.setAge(18);
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Name should be in-between 10 and 200 characters", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifAgeIsLess_ageMinValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Stephaner Doe");
|
||||
person.setAge(16);
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Name should be between 10 and 100 characters", violation.getMessage());
|
||||
}
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Age should not be less than 18 but it is ${validatedValue}", violation.getMessage());
|
||||
}
|
||||
@Test
|
||||
public void ifAgeIsLess_ageMinValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Stephaner Doe");
|
||||
person.setAge(16);
|
||||
|
||||
}
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Age should not be less than 18", violation.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifEmailIsMalformed_emailFormatValidationFails() {
|
||||
Person person = new Person();
|
||||
person.setName("John Stephaner Doe");
|
||||
person.setAge(18);
|
||||
person.setEmail("johndoe.dev");
|
||||
|
||||
Set<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
assertEquals(1, violations.size());
|
||||
|
||||
ConstraintViolation<Person> violation = violations.iterator().next();
|
||||
assertEquals("Email address should be in a correct format: ${validatedValue}", violation.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user