update to 2.0, formatting (#2688)

* update to 2.0, formatting

* formatting
This commit is contained in:
lor6 2017-09-30 08:35:32 +03:00 committed by Grzegorz Piwowarek
parent 49482041d2
commit 0fa168957f
3 changed files with 176 additions and 93 deletions

View File

@ -6,8 +6,8 @@
<version>0.1-SNAPSHOT</version>
<properties>
<validation-api.version>1.1.0.Final</validation-api.version>
<hibernate-validator.version>5.3.4.Final</hibernate-validator.version>
<validation-api.version>2.0.0.Final</validation-api.version>
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
<javax.el-api.version>3.0.0</javax.el-api.version>
<javax.el.version>2.2.6</javax.el.version>
</properties>

View File

@ -1,56 +1,94 @@
package org.baeldung;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import javax.validation.constraints.NotBlank;
public class User {
@NotNull(message = "Name cannot be null")
private String name;
@NotNull(message = "Name cannot be null")
private String name;
@AssertTrue
private boolean working;
@AssertTrue
private boolean working;
@Size(min = 10, max = 200, message = "Number of characters should be in between 10 and 200 inclusive")
private String aboutMe;
@Size(min = 10, max = 200, message = "Number of characters should be in between 10 and 200 inclusive")
private String aboutMe;
@Min(value = 18, message = "Age should not be less than 18")
@Max(value = 150, message = "Age should not be more than 150")
private int age;
@Min(value = 18, message = "Age should not be less than 18")
@Max(value = 150, message = "Age should not be more than 150")
private int age;
public int getAge() {
return age;
}
@Email(message = "Email should be valid")
private String email;
List<@NotBlank String> preferences;
private LocalDate dateOfBirth;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public boolean isWorking() {
return working;
}
public void setAge(int age) {
this.age = age;
}
public void setWorking(boolean working) {
this.working = working;
}
public boolean isWorking() {
return working;
}
public String getAboutMe() {
return aboutMe;
}
public void setWorking(boolean working) {
this.working = working;
}
public void setAboutMe(String aboutMe) {
this.aboutMe = aboutMe;
}
public String getAboutMe() {
return aboutMe;
}
public String getName() {
return name;
}
public void setAboutMe(String aboutMe) {
this.aboutMe = aboutMe;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Optional<@Past LocalDate> getDateOfBirth() {
return Optional.ofNullable(dateOfBirth);
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getPreferences() {
return preferences;
}
public void setPreferences(List<String> preferences) {
this.preferences = preferences;
}
}

View File

@ -1,81 +1,126 @@
package org.baeldung;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.Optional;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
public class ValidationIntegrationTest {
@Test
public void ifNameIsNull_nameValidationFails() {
User user = new User();
user.setWorking(true);
user.setAboutMe("Its all about me!!");
user.setAge(50);
private Validator validator;
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
Assert.assertEquals(violations.isEmpty(), false);
}
@Before
public void setup() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
@Test
public void ifSizeNotInRange_aboutMeValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(50);
private User createUser() {
User user = new User();
user.setName("John");
user.setWorking(true);
user.setAge(18);
return user;
}
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
Assert.assertEquals(violations.isEmpty(), false);
}
@Test
public void ifNameIsNull_nameValidationFails() {
User user = new User();
user.setWorking(true);
user.setAboutMe("Its all about me!!");
user.setAge(50);
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(violations.isEmpty(), false);
}
@Test
public void ifWorkingIsFalse_workingValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(50);
@Test
public void ifSizeNotInRange_aboutMeValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(50);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
Assert.assertEquals(violations.isEmpty(), false);
}
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(violations.isEmpty(), false);
}
@Test
public void ifAgeNotRange_ageValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(8);
@Test
public void ifWorkingIsFalse_workingValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(50);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
Assert.assertEquals(violations.isEmpty(), false);
}
@Test
public void ifFnameNullAgeNotRangeAndWorkingIsFalse_validationFailsWithThreeErrors() {
User user = new User();
user.setAboutMe("Its all about me!!");
user.setAge(300);
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(violations.isEmpty(), false);
}
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
Assert.assertEquals(violations.isEmpty(), false);
Assert.assertEquals(violations.size(), 3);
}
@Test
public void ifAgeNotRange_ageValidationFails() {
User user = new User();
user.setName("MyName");
user.setAboutMe("Its all about me!!");
user.setAge(8);
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(violations.isEmpty(), false);
}
@Test
public void ifFnameNullAgeNotRangeAndWorkingIsFalse_validationFailsWithThreeErrors() {
User user = new User();
user.setAboutMe("Its all about me!!");
user.setAge(300);
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(violations.isEmpty(), false);
assertEquals(violations.size(), 3);
}
@Test
public void givenInvalidEmail_thenValidationFails() {
User user = createUser();
user.setEmail("john");
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(1, violations.size());
}
@Test
public void givenBlankPreference_thenValidationFails() {
User user = createUser();
user.setPreferences(Collections.singletonList(" "));
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(1, violations.size());
}
@Test
public void givenEmptyOptional_thenValidationSucceeds() {
User user = createUser();
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(0, violations.size());
}
@Test
public void givenPastDateOfBirth_thenValidationSuccess() {
User user = createUser();
user.setDateOfBirth(LocalDate.of(1980, 5, 20));
Set<ConstraintViolation<User>> violations = validator.validate(user);
assertEquals(0, violations.size());
}
}