Updates after editor feedback

This commit is contained in:
Markus Gulden 2018-02-03 12:51:59 +01:00
parent 81e8da29a4
commit b9ac0dc78b
6 changed files with 39 additions and 17 deletions

View File

@ -15,9 +15,6 @@ public class ConsistentDateParameterValidator implements ConstraintValidator<Con
@Override @Override
public boolean isValid(Object[] value, ConstraintValidatorContext context) { public boolean isValid(Object[] value, ConstraintValidatorContext context) {
if (value.length != 4 && value.length != 3) {
throw new IllegalArgumentException("Illegal method signature");
}
if (value[0] == null || value[1] == null) { if (value[0] == null || value[1] == null) {
return false; return false;
@ -27,6 +24,6 @@ public class ConsistentDateParameterValidator implements ConstraintValidator<Con
throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate."); throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate.");
} }
return ((LocalDate) value[0]).isBefore((LocalDate) value[1]); return ((LocalDate) value[0]).isAfter(LocalDate.now()) && ((LocalDate) value[0]).isBefore((LocalDate) value[1]);
} }
} }

View File

@ -16,7 +16,7 @@ public class ValidReservationValidator implements ConstraintValidator<ValidReser
public boolean isValid(Reservation reservation, ConstraintValidatorContext context) { public boolean isValid(Reservation reservation, ConstraintValidatorContext context) {
if (reservation == null) { if (reservation == null) {
return true; return false;
} }
if (!(reservation instanceof Reservation)) { if (!(reservation instanceof Reservation)) {

View File

@ -4,6 +4,8 @@ import org.baeldung.javaxval.methodvalidation.constraints.ConsistentDateParamete
import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation; import org.baeldung.javaxval.methodvalidation.constraints.ValidReservation;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.time.LocalDate; import java.time.LocalDate;
@Validated @Validated
@ -13,8 +15,10 @@ public class Reservation {
private LocalDate end; private LocalDate end;
@Valid
private Customer customer; private Customer customer;
@Positive
private int room; private int room;
@ConsistentDateParameters @ConsistentDateParameters

View File

@ -30,6 +30,11 @@ public class ReservationManagement {
// ... // ...
} }
public void createReservation(@Valid Reservation reservation) {
// ...
}
@NotNull @NotNull
@Size(min = 1) @Size(min = 1)
public List<@NotNull Customer> getAllCustomers() { public List<@NotNull Customer> getAllCustomers() {
@ -37,13 +42,8 @@ public class ReservationManagement {
return null; return null;
} }
public void createNewCustomer(@Valid Customer customer) {
// ...
}
@Valid @Valid
public Customer getCustomerById() { public Reservation getReservationById(int id) {
return null; return null;
} }

View File

@ -1,6 +1,7 @@
package org.baeldung.javaxval.methodvalidation; package org.baeldung.javaxval.methodvalidation;
import org.baeldung.javaxval.methodvalidation.model.Customer; import org.baeldung.javaxval.methodvalidation.model.Customer;
import org.baeldung.javaxval.methodvalidation.model.Reservation;
import org.baeldung.javaxval.methodvalidation.model.ReservationManagement; import org.baeldung.javaxval.methodvalidation.model.ReservationManagement;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -69,9 +70,14 @@ public class ContainerValidationIntegrationTest {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setFirstName("John"); customer.setFirstName("John");
customer.setLastName("Doe"); customer.setLastName("Doe");
Reservation reservation = new Reservation(LocalDate.now()
.plusDays(1),
LocalDate.now()
.plusDays(2),
customer, 1);
exception.expect(ConstraintViolationException.class); exception.expect(ConstraintViolationException.class);
reservationManagement.createNewCustomer(customer); reservationManagement.createReservation(reservation);
} }
@Test @Test
@ -80,7 +86,12 @@ public class ContainerValidationIntegrationTest {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setFirstName("William"); customer.setFirstName("William");
customer.setLastName("Smith"); customer.setLastName("Smith");
Reservation reservation = new Reservation(LocalDate.now()
.plusDays(1),
LocalDate.now()
.plusDays(2),
customer, 1);
reservationManagement.createNewCustomer(customer); reservationManagement.createReservation(reservation);
} }
} }

View File

@ -172,11 +172,16 @@ public class ValidationIntegrationTest {
public void whenValidationWithInvalidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { public void whenValidationWithInvalidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException {
ReservationManagement object = new ReservationManagement(); ReservationManagement object = new ReservationManagement();
Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setFirstName("John"); customer.setFirstName("John");
customer.setLastName("Doe"); customer.setLastName("Doe");
Object[] parameterValues = { customer }; Reservation reservation = new Reservation(LocalDate.now()
.plusDays(1),
LocalDate.now()
.plusDays(2),
customer, 1);
Object[] parameterValues = { reservation };
Set<ConstraintViolation<ReservationManagement>> violations = executableValidator.validateParameters(object, method, parameterValues); Set<ConstraintViolation<ReservationManagement>> violations = executableValidator.validateParameters(object, method, parameterValues);
assertEquals(2, violations.size()); assertEquals(2, violations.size());
@ -186,11 +191,16 @@ public class ValidationIntegrationTest {
public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException { public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException {
ReservationManagement object = new ReservationManagement(); ReservationManagement object = new ReservationManagement();
Method method = ReservationManagement.class.getMethod("createNewCustomer", Customer.class); Method method = ReservationManagement.class.getMethod("createReservation", Reservation.class);
Customer customer = new Customer(); Customer customer = new Customer();
customer.setFirstName("William"); customer.setFirstName("William");
customer.setLastName("Smith"); customer.setLastName("Smith");
Object[] parameterValues = { customer }; Reservation reservation = new Reservation(LocalDate.now()
.plusDays(1),
LocalDate.now()
.plusDays(2),
customer, 1);
Object[] parameterValues = { reservation };
Set<ConstraintViolation<ReservationManagement>> violations = executableValidator.validateParameters(object, method, parameterValues); Set<ConstraintViolation<ReservationManagement>> violations = executableValidator.validateParameters(object, method, parameterValues);
assertEquals(0, violations.size()); assertEquals(0, violations.size());