Merge pull request #7948 from martinvw/feature/pre-cleanup
Feature/pre cleanup
This commit is contained in:
commit
e67c4b3573
|
@ -8,3 +8,4 @@ This module contains articles about Bean Validation.
|
|||
- [Method Constraints with Bean Validation 2.0](https://www.baeldung.com/javax-validation-method-constraints)
|
||||
- [Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation](https://www.baeldung.com/java-bean-validation-not-null-empty-blank)
|
||||
- [Javax BigDecimal Validation](https://www.baeldung.com/javax-bigdecimal-validation)
|
||||
- [Grouping Javax Validation Constraints](https://www.baeldung.com/javax-validation-groups)
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.appplication;
|
||||
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import org.baeldung.javabeanconstraints.entities.UserNotBlank;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
UserNotBlank user = new UserNotBlank(" ");
|
||||
validator.validate(user).stream().forEach(violation -> System.out.println(violation.getMessage()));
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.bigdecimal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.Digits;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
@DecimalMin(value = "0.0", inclusive = false)
|
||||
@Digits(integer=3, fraction=2)
|
||||
private BigDecimal price;
|
||||
private String description;
|
||||
|
||||
public Invoice(BigDecimal price, String description) {
|
||||
this.price = price;
|
||||
this.description = description;
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
public interface AdvanceInfo {
|
||||
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
public interface BasicInfo {
|
||||
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
import javax.validation.GroupSequence;
|
||||
|
||||
@GroupSequence({BasicInfo.class, AdvanceInfo.class})
|
||||
public interface CompleteInfo {
|
||||
|
||||
}
|
|
@ -1,110 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class RegistrationForm {
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String firstName;
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String lastName;
|
||||
@Email(groups = BasicInfo.class)
|
||||
private String email;
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String phone;
|
||||
|
||||
@NotBlank(groups = { BasicInfo.class, AdvanceInfo.class })
|
||||
private String captcha;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String street;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String houseNumber;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String zipCode;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String city;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String contry;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getHouseNumber() {
|
||||
return houseNumber;
|
||||
}
|
||||
|
||||
public void setHouseNumber(String houseNumber) {
|
||||
this.houseNumber = houseNumber;
|
||||
}
|
||||
|
||||
public String getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(String zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getContry() {
|
||||
return contry;
|
||||
}
|
||||
|
||||
public void setContry(String contry) {
|
||||
this.contry = contry;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getCaptcha() {
|
||||
return captcha;
|
||||
}
|
||||
|
||||
public void setCaptcha(String captcha) {
|
||||
this.captcha = captcha;
|
||||
}
|
||||
|
||||
}
|
|
@ -8,10 +8,10 @@ import javax.validation.constraints.AssertTrue;
|
|||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Past;
|
||||
import javax.validation.constraints.Size;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class User {
|
||||
|
||||
|
@ -30,9 +30,9 @@ public class User {
|
|||
|
||||
@Email(message = "Email should be valid")
|
||||
private String email;
|
||||
|
||||
List<@NotBlank String> preferences;
|
||||
|
||||
|
||||
private List<@NotBlank String> preferences;
|
||||
|
||||
private LocalDate dateOfBirth;
|
||||
|
||||
public int getAge() {
|
||||
|
@ -90,5 +90,4 @@ public class User {
|
|||
public void setPreferences(List<String> preferences) {
|
||||
this.preferences = preferences;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package org.baeldung.javaxval.bigdecimal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.Digits;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
@DecimalMin(value = "0.0", inclusive = false)
|
||||
@Digits(integer = 3, fraction = 2)
|
||||
private BigDecimal price;
|
||||
private String description;
|
||||
|
||||
public Invoice(BigDecimal price, String description) {
|
||||
this.price = price;
|
||||
this.description = description;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package org.baeldung.javaxval.container.validation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
@ -10,17 +10,17 @@ import javax.validation.constraints.PositiveOrZero;
|
|||
|
||||
public class Customer {
|
||||
|
||||
@NotBlank(message="Name cannot be empty")
|
||||
@NotBlank(message = "Name cannot be empty")
|
||||
private String name;
|
||||
|
||||
private List<@NotBlank(message="Address must not be blank") String> addresses;
|
||||
|
||||
private List<@NotBlank(message = "Address must not be blank") String> addresses;
|
||||
|
||||
private Integer age;
|
||||
|
||||
|
||||
@PositiveOrZero
|
||||
private OptionalInt numberOfOrders;
|
||||
|
||||
//@NotBlank
|
||||
|
||||
@NotBlank
|
||||
private Profile profile;
|
||||
|
||||
public String getName() {
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package org.baeldung.javaxval.container.validation;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -6,8 +6,8 @@ import javax.validation.constraints.Email;
|
|||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class CustomerMap {
|
||||
|
||||
private Map<@Email(message="Must be a valid email") String, @NotNull Customer> customers;
|
||||
|
||||
private Map<@Email(message = "Must be a valid email") String, @NotNull Customer> customers;
|
||||
|
||||
public Map<String, Customer> getCustomers() {
|
||||
return customers;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package org.baeldung.javaxval.container.validation;
|
||||
|
||||
public class Profile {
|
||||
private String companyName;
|
|
@ -1,10 +1,10 @@
|
|||
package org.baeldung.valueextractors;
|
||||
package org.baeldung.javaxval.container.validation.valueextractors;
|
||||
|
||||
import javax.validation.valueextraction.ExtractedValue;
|
||||
import javax.validation.valueextraction.UnwrapByDefault;
|
||||
import javax.validation.valueextraction.ValueExtractor;
|
||||
|
||||
import org.baeldung.Profile;
|
||||
import org.baeldung.javaxval.container.validation.Profile;
|
||||
|
||||
@UnwrapByDefault
|
||||
public class ProfileValueExtractor implements ValueExtractor<@ExtractedValue(type = String.class) Profile> {
|
|
@ -0,0 +1,18 @@
|
|||
package org.baeldung.javaxval.javabeanconstraints.application;
|
||||
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.javabeanconstraints.entities.UserNotBlank;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
UserNotBlank user = new UserNotBlank(" ");
|
||||
validator.validate(user)
|
||||
.stream()
|
||||
.forEach(violation -> System.out.println(violation.getMessage()));
|
||||
}
|
||||
}
|
|
@ -1,22 +1,22 @@
|
|||
package org.baeldung.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class UserNotBlank {
|
||||
|
||||
@NotBlank(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotBlank(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class UserNotBlank {
|
||||
|
||||
@NotBlank(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotBlank(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
|
@ -1,22 +1,22 @@
|
|||
package org.baeldung.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class UserNotEmpty {
|
||||
|
||||
@NotEmpty(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotEmpty(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class UserNotEmpty {
|
||||
|
||||
@NotEmpty(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotEmpty(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
|
@ -1,22 +1,22 @@
|
|||
package org.baeldung.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class UserNotNull {
|
||||
|
||||
@NotNull(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotNull(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.entities;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class UserNotNull {
|
||||
|
||||
@NotNull(message = "Name is mandatory")
|
||||
private final String name;
|
||||
|
||||
public UserNotNull(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + "}";
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package org.baeldung.javaxval.methodvalidation;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Customer;
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
|
@ -9,8 +11,6 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan({ "org.baeldung.javaxval.methodvalidation.model" })
|
||||
public class MethodValidationConfig {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import javax.validation.constraintvalidation.SupportedValidationTarget;
|
||||
import javax.validation.constraintvalidation.ValidationTarget;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
|
||||
public class ConsistentDateParameterValidator implements ConstraintValidator<ConsistentDateParameters, Object[]> {
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = ConsistentDateParameterValidator.class)
|
||||
@Target({ METHOD, CONSTRUCTOR })
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = ValidReservationValidator.class)
|
||||
@Target({ METHOD, CONSTRUCTOR })
|
||||
@Retention(RUNTIME)
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
|
||||
public class ValidReservationValidator implements ConstraintValidator<ValidReservation, Reservation> {
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package org.baeldung.javaxval.methodvalidation.model;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Validated
|
||||
public class Customer {
|
||||
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
package org.baeldung.javaxval.methodvalidation.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Positive;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters;
|
||||
import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Positive;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Validated
|
||||
public class Reservation {
|
||||
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
package org.baeldung.javaxval.methodvalidation.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Future;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParameters;
|
||||
import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@Validated
|
||||
public class ReservationManagement {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package org.baeldung.javaxval.validationgroup;
|
||||
|
||||
public interface AdvanceInfo {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package org.baeldung.javaxval.validationgroup;
|
||||
|
||||
public interface BasicInfo {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.javaxval.validationgroup;
|
||||
|
||||
import javax.validation.GroupSequence;
|
||||
|
||||
@GroupSequence({ BasicInfo.class, AdvanceInfo.class })
|
||||
public interface CompleteInfo {
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package org.baeldung.javaxval.validationgroup;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class RegistrationForm {
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String firstName;
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String lastName;
|
||||
@Email(groups = BasicInfo.class)
|
||||
private String email;
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String phone;
|
||||
|
||||
@NotBlank(groups = { BasicInfo.class, AdvanceInfo.class })
|
||||
private String captcha;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String street;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String houseNumber;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String zipCode;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String city;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String country;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getHouseNumber() {
|
||||
return houseNumber;
|
||||
}
|
||||
|
||||
public void setHouseNumber(String houseNumber) {
|
||||
this.houseNumber = houseNumber;
|
||||
}
|
||||
|
||||
public String getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(String zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getCaptcha() {
|
||||
return captcha;
|
||||
}
|
||||
|
||||
public void setCaptcha(String captcha) {
|
||||
this.captcha = captcha;
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +1 @@
|
|||
org.baeldung.valueextractors.ProfileValueExtractor
|
||||
org.baeldung.javaxval.container.validation.valueextractors.ProfileValueExtractor
|
|
@ -1,137 +0,0 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegistrationFormUnitTest {
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForBasicInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setFirstName("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, BasicInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("firstName");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
|
||||
form.setZipCode("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, AdvanceInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("zipCode");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForBasicInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, BasicInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, AdvanceInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsForBasicInfoOnly() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setFirstName("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("firstName");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsCompleteAndAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
form.setZipCode("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("zipCode");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolationsWithCompleteInfoValidationGroup() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolations() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithBasicInfo() {
|
||||
RegistrationForm form = new RegistrationForm();
|
||||
form.setFirstName("devender");
|
||||
form.setLastName("kumar");
|
||||
form.setEmail("anyemail@yopmail.com");
|
||||
form.setPhone("12345");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithAdvanceInfo() {
|
||||
RegistrationForm form = new RegistrationForm();
|
||||
return populateAdvanceInfo(form);
|
||||
}
|
||||
|
||||
private RegistrationForm populateAdvanceInfo(RegistrationForm form) {
|
||||
form.setCity("Berlin");
|
||||
form.setContry("DE");
|
||||
form.setStreet("alexa str.");
|
||||
form.setZipCode("19923");
|
||||
form.setHouseNumber("2a");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithBasicAndAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
return populateAdvanceInfo(form);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.javaxval;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
public abstract class LocaleAwareUnitTest {
|
||||
private static Locale previousDefault;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupLocale() {
|
||||
previousDefault = Locale.getDefault();
|
||||
|
||||
Locale.setDefault(Locale.US);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void resetLocale() {
|
||||
Locale.setDefault(previousDefault);
|
||||
}
|
||||
|
||||
}
|
|
@ -94,7 +94,7 @@ public class ValidationIntegrationTest {
|
|||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
assertEquals(1, violations.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenBlankPreference_thenValidationFails() {
|
||||
User user = createUser();
|
||||
|
@ -107,18 +107,18 @@ public class ValidationIntegrationTest {
|
|||
@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());
|
||||
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.javabeanconstraints.bigdecimal;
|
||||
package org.baeldung.javaxval.bigdecimal;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
@ -9,55 +9,54 @@ import javax.validation.ConstraintViolation;
|
|||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.LocaleAwareUnitTest;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InvoiceUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
public class InvoiceUnitTest extends LocaleAwareUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitLessThanThreeWithDecimalValue_thenShouldGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased");
|
||||
Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action-> assertThat(action.getMessage())
|
||||
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitLessThanThreeWithIntegerValue_thenShouldNotGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(10), "Book purchased");
|
||||
Invoice invoice = new Invoice(new BigDecimal(10), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitGreaterThanThree_thenShouldGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(1021.21), "Book purchased");
|
||||
Invoice invoice = new Invoice(new BigDecimal(1021.21), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action-> assertThat(action.getMessage())
|
||||
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(000.00), "Book purchased");
|
||||
Invoice invoice = new Invoice(new BigDecimal(000.00), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action-> assertThat(action.getMessage())
|
||||
.isEqualTo("must be greater than 0.0"));
|
||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("must be greater than 0.0"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(100.50), "Book purchased");
|
||||
Invoice invoice = new Invoice(new BigDecimal(100.50), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package org.baeldung.javaxval.container.validation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -10,8 +10,8 @@ import javax.validation.ConstraintViolation;
|
|||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import org.baeldung.valueextractors.ProfileValueExtractor;
|
||||
|
||||
import org.baeldung.javaxval.container.validation.valueextractors.ProfileValueExtractor;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -20,8 +20,10 @@ public class ContainerValidationIntegrationTest {
|
|||
|
||||
@Before
|
||||
public void setup() {
|
||||
ValidatorFactory factory = Validation.byDefaultProvider().configure()
|
||||
.addValueExtractor(new ProfileValueExtractor()).buildValidatorFactory();
|
||||
ValidatorFactory factory = Validation.byDefaultProvider()
|
||||
.configure()
|
||||
.addValueExtractor(new ProfileValueExtractor())
|
||||
.buildValidatorFactory();
|
||||
validator = factory.getValidator();
|
||||
}
|
||||
|
||||
|
@ -74,7 +76,7 @@ public class ContainerValidationIntegrationTest {
|
|||
assertEquals(0, violations.size());
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void whenProfileCompanyNameBlank_thenValidationFails() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
|
@ -1,63 +1,67 @@
|
|||
package org.baeldung.javabeanconstraints.test;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.baeldung.javabeanconstraints.entities.UserNotBlank;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotBlankUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotBlankName_thenNoConstraintViolations() {
|
||||
UserNotBlank user = new UserNotBlank("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBlankName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank(" ");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank("");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotBlank user = new UserNotBlank("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.javabeanconstraints.entities.UserNotBlank;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotBlankUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotBlankName_thenNoConstraintViolations() {
|
||||
UserNotBlank user = new UserNotBlank("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBlankName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank(" ");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank("");
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotBlank user = new UserNotBlank(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotBlank user = new UserNotBlank("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
|
@ -1,54 +1,58 @@
|
|||
package org.baeldung.javabeanconstraints.test;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.baeldung.javabeanconstraints.entities.UserNotEmpty;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotEmptyUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotEmptyName_thenNoConstraintViolations() {
|
||||
UserNotEmpty user = new UserNotEmpty("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenOneConstraintViolation() {
|
||||
UserNotEmpty user = new UserNotEmpty("");
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotEmpty user = new UserNotEmpty(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotEmpty user = new UserNotEmpty("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.javabeanconstraints.entities.UserNotEmpty;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotEmptyUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotEmptyName_thenNoConstraintViolations() {
|
||||
UserNotEmpty user = new UserNotEmpty("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenOneConstraintViolation() {
|
||||
UserNotEmpty user = new UserNotEmpty("");
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotEmpty user = new UserNotEmpty(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotEmpty user = new UserNotEmpty("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
|
@ -1,54 +1,58 @@
|
|||
package org.baeldung.javabeanconstraints.test;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.baeldung.javabeanconstraints.entities.UserNotNull;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotNullUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotNullName_thenNoConstraintViolations() {
|
||||
UserNotNull user = new UserNotNull("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotNull user = new UserNotNull(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenNoConstraintViolations() {
|
||||
UserNotNull user = new UserNotNull("");
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotNull user = new UserNotNull("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
||||
package org.baeldung.javaxval.javabeanconstraints.test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.javabeanconstraints.entities.UserNotNull;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserNotNullUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotNullName_thenNoConstraintViolations() {
|
||||
UserNotNull user = new UserNotNull("John");
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullName_thenOneConstraintViolation() {
|
||||
UserNotNull user = new UserNotNull(null);
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyName_thenNoConstraintViolations() {
|
||||
UserNotNull user = new UserNotNull("");
|
||||
|
||||
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToString_thenCorrect() {
|
||||
UserNotNull user = new UserNotNull("John");
|
||||
|
||||
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||
}
|
||||
}
|
|
@ -1,5 +1,10 @@
|
|||
package org.baeldung.javaxval.methodvalidation;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Customer;
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
import org.baeldung.javaxval.methodvalidation.model.ReservationManagement;
|
||||
|
@ -12,10 +17,6 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MethodValidationConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ContainerValidationIntegrationTest {
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
package org.baeldung.javaxval.methodvalidation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Customer;
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
import org.baeldung.javaxval.methodvalidation.model.ReservationManagement;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationIntegrationTest {
|
||||
|
||||
|
|
|
@ -0,0 +1,145 @@
|
|||
package org.baeldung.javaxval.validationgroup;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
|
||||
import org.baeldung.javaxval.LocaleAwareUnitTest;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegistrationFormUnitTest extends LocaleAwareUnitTest {
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForBasicInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setFirstName("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, BasicInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("firstName");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
|
||||
form.setZipCode("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, AdvanceInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("zipCode");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForBasicInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, BasicInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, AdvanceInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsForBasicInfoOnly() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setFirstName("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("firstName");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsCompleteAndAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
form.setZipCode("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath()
|
||||
.toString()).isEqualTo("zipCode");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolationsWithCompleteInfoValidationGroup() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolations() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithBasicInfo() {
|
||||
RegistrationForm form = new RegistrationForm();
|
||||
form.setFirstName("devender");
|
||||
form.setLastName("kumar");
|
||||
form.setEmail("anyemail@yopmail.com");
|
||||
form.setPhone("12345");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithAdvanceInfo() {
|
||||
RegistrationForm form = new RegistrationForm();
|
||||
return populateAdvanceInfo(form);
|
||||
}
|
||||
|
||||
private RegistrationForm populateAdvanceInfo(RegistrationForm form) {
|
||||
form.setCity("Berlin");
|
||||
form.setCountry("DE");
|
||||
form.setStreet("alexa str.");
|
||||
form.setZipCode("19923");
|
||||
form.setHouseNumber("2a");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
private RegistrationForm buildRegistrationFormWithBasicAndAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
return populateAdvanceInfo(form);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue