From 0fa168957f006ffb40d37747b6e31f82c13d2a4a Mon Sep 17 00:00:00 2001 From: lor6 Date: Sat, 30 Sep 2017 08:35:32 +0300 Subject: [PATCH] update to 2.0, formatting (#2688) * update to 2.0, formatting * formatting --- javaxval/pom.xml | 4 +- javaxval/src/main/java/org/baeldung/User.java | 104 +++++++---- .../baeldung/ValidationIntegrationTest.java | 161 +++++++++++------- 3 files changed, 176 insertions(+), 93 deletions(-) diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 4d27b3e0c9..6a83a25f01 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -6,8 +6,8 @@ 0.1-SNAPSHOT - 1.1.0.Final - 5.3.4.Final + 2.0.0.Final + 6.0.2.Final 3.0.0 2.2.6 diff --git a/javaxval/src/main/java/org/baeldung/User.java b/javaxval/src/main/java/org/baeldung/User.java index 2d86a4ec2f..e2f2732399 100644 --- a/javaxval/src/main/java/org/baeldung/User.java +++ b/javaxval/src/main/java/org/baeldung/User.java @@ -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 getPreferences() { + return preferences; + } + + public void setPreferences(List preferences) { + this.preferences = preferences; + } + } diff --git a/javaxval/src/test/java/org/baeldung/ValidationIntegrationTest.java b/javaxval/src/test/java/org/baeldung/ValidationIntegrationTest.java index 63c08f64d8..78745a1af2 100644 --- a/javaxval/src/test/java/org/baeldung/ValidationIntegrationTest.java +++ b/javaxval/src/test/java/org/baeldung/ValidationIntegrationTest.java @@ -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> 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> 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> 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> violations = validator.validate(user); - Assert.assertEquals(violations.isEmpty(), false); - } + Set> 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> 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> violations = validator.validate(user); + assertEquals(violations.isEmpty(), false); + } - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - Validator validator = factory.getValidator(); - Set> 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> 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> violations = validator.validate(user); + assertEquals(violations.isEmpty(), false); + assertEquals(violations.size(), 3); + } + + @Test + public void givenInvalidEmail_thenValidationFails() { + User user = createUser(); + user.setEmail("john"); + + Set> violations = validator.validate(user); + assertEquals(1, violations.size()); + } + + @Test + public void givenBlankPreference_thenValidationFails() { + User user = createUser(); + user.setPreferences(Collections.singletonList(" ")); + + Set> violations = validator.validate(user); + assertEquals(1, violations.size()); + } + + @Test + public void givenEmptyOptional_thenValidationSucceeds() { + User user = createUser(); + + Set> violations = validator.validate(user); + assertEquals(0, violations.size()); + } + + @Test + public void givenPastDateOfBirth_thenValidationSuccess() { + User user = createUser(); + user.setDateOfBirth(LocalDate.of(1980, 5, 20)); + + Set> violations = validator.validate(user); + assertEquals(0, violations.size()); + + } }