Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class FutureValidatorForDate extends AbstractFutureInstantBasedValidator<

@Override
protected Instant getInstant(Date value) {
return value.toInstant();
// we don't use Date.toInstant() as it's not supported by java.sql.Date
return Instant.ofEpochMilli( value.getTime() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class FutureOrPresentValidatorForDate extends AbstractFutureOrPresentInst

@Override
protected Instant getInstant(Date value) {
return value.toInstant();
// we don't use Date.toInstant() as it's not supported by java.sql.Date
return Instant.ofEpochMilli( value.getTime() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class PastValidatorForDate extends AbstractPastInstantBasedValidator<Date

@Override
protected Instant getInstant(Date value) {
return value.toInstant();
// we don't use Date.toInstant() as it's not supported by java.sql.Date
return Instant.ofEpochMilli( value.getTime() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class PastOrPresentValidatorForDate extends AbstractPastOrPresentInstantB

@Override
protected Instant getInstant(Date value) {
return value.toInstant();
// we don't use Date.toInstant() as it's not supported by java.sql.Date
return Instant.ofEpochMilli( value.getTime() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;
import static org.hibernate.validator.testutils.ValidatorUtil.getConfiguration;

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import jakarta.validation.ClockProvider;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.Future;
Expand Down Expand Up @@ -89,19 +87,4 @@ private static class Order {
private ReadablePartial shipmentDateAsReadablePartial;

}

private static class FixedClockProvider implements ClockProvider {

private Clock clock;

public FixedClockProvider(ZonedDateTime dateTime) {
clock = Clock.fixed( dateTime.toInstant(), dateTime.getZone() );
}

@Override
public Clock getClock() {
return clock;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;
import static org.hibernate.validator.testutils.ValidatorUtil.getConfiguration;

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import jakarta.validation.ClockProvider;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.Past;

import org.hibernate.validator.testutil.TestForIssue;
import org.joda.time.DateTime;
import org.joda.time.ReadableInstant;
Expand All @@ -27,6 +21,10 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.Past;

/**
* Test for using the {@code ClockProvider} contract in {@code @Past} validators not covered by the TCK.
*
Expand Down Expand Up @@ -88,19 +86,4 @@ private static class Order {
private ReadablePartial orderDateAsReadablePartial;

}

private static class FixedClockProvider implements ClockProvider {

private Clock clock;

public FixedClockProvider(ZonedDateTime dateTime) {
clock = Clock.fixed( dateTime.toInstant(), dateTime.getZone() );
}

@Override
public Clock getClock() {
return clock;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.constraintvalidators.bv.time;

import java.time.Clock;
import java.time.ZonedDateTime;

import jakarta.validation.ClockProvider;

class FixedClockProvider implements ClockProvider {

private Clock clock;

public FixedClockProvider(ZonedDateTime dateTime) {
clock = Clock.fixed( dateTime.toInstant(), dateTime.getZone() );
}

@Override
public Clock getClock() {
return clock;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.test.internal.constraintvalidators.bv.time;

import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat;
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;
import static org.hibernate.validator.testutils.ValidatorUtil.getConfiguration;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;

import org.hibernate.validator.testutil.TestForIssue;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.PastOrPresent;

/**
* @author Guillaume Smet
*/
@TestForIssue(jiraKey = "HV-1878")
public class JavaSqlDateTest {

private static final ZoneId TZ_BERLIN = ZoneId.of( "Europe/Berlin" );

private Validator validator;

@BeforeMethod
public void setupValidator() {
FixedClockProvider clockProvider = new FixedClockProvider(
ZonedDateTime.of(
2000, 2, 15, 4, 0, 0, 0,
TZ_BERLIN ) );
ValidatorFactory validatorFactory = getConfiguration()
.clockProvider( clockProvider )
.buildValidatorFactory();

validator = validatorFactory.getValidator();
}

@Test
public void testFuture() {
JavaSqlDateHolder javaSqlDateHolder = new JavaSqlDateHolder(
new java.sql.Date( LocalDate.of( 2010, 2, 2 ).atStartOfDay( ZoneId.systemDefault() ).toInstant().toEpochMilli() ) );
Set<ConstraintViolation<JavaSqlDateHolder>> constraintViolations = validator.validate( javaSqlDateHolder );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Past.class ),
violationOf( PastOrPresent.class ) );
}

@Test
public void testPast() {
JavaSqlDateHolder javaSqlDateHolder = new JavaSqlDateHolder(
new java.sql.Date( LocalDate.of( 1990, 2, 2 ).atStartOfDay( ZoneId.systemDefault() ).toInstant().toEpochMilli() ) );
Set<ConstraintViolation<JavaSqlDateHolder>> constraintViolations = validator.validate( javaSqlDateHolder );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Future.class ),
violationOf( FutureOrPresent.class ) );
}

private static class JavaSqlDateHolder {

@Future
private java.sql.Date future;

@FutureOrPresent
private java.sql.Date futureOrPresent;

@Past
private java.sql.Date past;

@PastOrPresent
private java.sql.Date pastOrPresent;

JavaSqlDateHolder(java.sql.Date date) {
this.future = date;
this.futureOrPresent = date;
this.past = date;
this.pastOrPresent = date;
}
}
}