Formatted using the eclipse formatter
This commit is contained in:
parent
76e0fe4642
commit
86dd449968
|
@ -20,7 +20,6 @@ public class Customer {
|
|||
@PositiveOrZero
|
||||
private OptionalInt numberOfOrders;
|
||||
|
||||
//@NotBlank
|
||||
private Profile profile;
|
||||
|
||||
public String getName() {
|
||||
|
|
|
@ -2,13 +2,17 @@ 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) {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
Validator validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
UserNotBlank user = new UserNotBlank(" ");
|
||||
validator.validate(user).stream().forEach(violation -> System.out.println(violation.getMessage()));
|
||||
validator.validate(user)
|
||||
.stream()
|
||||
.forEach(violation -> System.out.println(violation.getMessage()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.baeldung.javabeanconstraints.validationgroup;
|
|||
|
||||
import javax.validation.GroupSequence;
|
||||
|
||||
@GroupSequence({BasicInfo.class, AdvanceInfo.class})
|
||||
@GroupSequence({ BasicInfo.class, AdvanceInfo.class })
|
||||
public interface CompleteInfo {
|
||||
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ public class RegistrationForm {
|
|||
@NotBlank(groups = BasicInfo.class)
|
||||
private String phone;
|
||||
|
||||
@NotBlank(groups = {BasicInfo.class, AdvanceInfo.class})
|
||||
@NotBlank(groups = { BasicInfo.class, AdvanceInfo.class })
|
||||
private String captcha;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
|
@ -25,7 +25,7 @@ public class RegistrationForm {
|
|||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String city;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String contry;
|
||||
private String country;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
|
@ -59,12 +59,12 @@ public class RegistrationForm {
|
|||
this.city = city;
|
||||
}
|
||||
|
||||
public String getContry() {
|
||||
return contry;
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setContry(String contry) {
|
||||
this.contry = contry;
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
|
|
|
@ -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> {
|
||||
|
||||
|
@ -24,9 +25,9 @@ public class ValidReservationValidator implements ConstraintValidator<ValidReser
|
|||
}
|
||||
|
||||
return (reservation.getBegin()
|
||||
.isAfter(LocalDate.now())
|
||||
&& reservation.getBegin()
|
||||
.isAfter(LocalDate.now())
|
||||
&& reservation.getBegin()
|
||||
.isBefore(reservation.getEnd())
|
||||
&& reservation.getRoom() > 0);
|
||||
&& reservation.getRoom() > 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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.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");
|
||||
|
|
|
@ -13,51 +13,49 @@ import org.junit.BeforeClass;
|
|||
import org.junit.Test;
|
||||
|
||||
public class InvoiceUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
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,63 +1,67 @@
|
|||
package org.baeldung.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 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();
|
||||
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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
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();
|
||||
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,10 +1,13 @@
|
|||
package org.baeldung.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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.baeldung.javabeanconstraints.entities.UserNotNull;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
@ -15,40 +18,41 @@ public class UserNotNullUnitTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,126 +12,133 @@ import org.junit.BeforeClass;
|
|||
import org.junit.Test;
|
||||
|
||||
public class RegistrationFormUnitTest {
|
||||
private static Validator validator;
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
@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 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 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_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 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 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 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_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);
|
||||
}
|
||||
@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 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 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 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);
|
||||
}
|
||||
private RegistrationForm buildRegistrationFormWithBasicAndAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
return populateAdvanceInfo(form);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,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 {
|
||||
|
||||
|
|
Loading…
Reference in New Issue