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
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) {
return false;
@ -27,6 +24,6 @@ public class ConsistentDateParameterValidator implements ConstraintValidator<Con
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) {
if (reservation == null) {
return true;
return false;
}
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.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.time.LocalDate;
@Validated
@ -13,8 +15,10 @@ public class Reservation {
private LocalDate end;
@Valid
private Customer customer;
@Positive
private int room;
@ConsistentDateParameters

View File

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

View File

@ -1,6 +1,7 @@
package org.baeldung.javaxval.methodvalidation;
import org.baeldung.javaxval.methodvalidation.model.Customer;
import org.baeldung.javaxval.methodvalidation.model.Reservation;
import org.baeldung.javaxval.methodvalidation.model.ReservationManagement;
import org.junit.Rule;
import org.junit.Test;
@ -69,9 +70,14 @@ public class ContainerValidationIntegrationTest {
Customer customer = new Customer();
customer.setFirstName("John");
customer.setLastName("Doe");
Reservation reservation = new Reservation(LocalDate.now()
.plusDays(1),
LocalDate.now()
.plusDays(2),
customer, 1);
exception.expect(ConstraintViolationException.class);
reservationManagement.createNewCustomer(customer);
reservationManagement.createReservation(reservation);
}
@Test
@ -80,7 +86,12 @@ public class ContainerValidationIntegrationTest {
Customer customer = new Customer();
customer.setFirstName("William");
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 {
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.setFirstName("John");
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);
assertEquals(2, violations.size());
@ -186,11 +191,16 @@ public class ValidationIntegrationTest {
public void whenValidationWithValidCascadedValue_thenCorrectNumberOfVoilations() throws NoSuchMethodException {
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.setFirstName("William");
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);
assertEquals(0, violations.size());