diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderFutureOrPresentTest.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderFutureOrPresentTest.java new file mode 100644 index 00000000..f541f9a1 --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderFutureOrPresentTest.java @@ -0,0 +1,130 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.assertNumberOfViolations; +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.assertThat; +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.pathWith; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import org.hibernate.beanvalidation.tck.beanvalidation.Sections; +import org.hibernate.beanvalidation.tck.tests.AbstractTCKTest; +import org.hibernate.beanvalidation.tck.util.TestUtil; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.audit.annotations.SpecAssertion; +import org.jboss.test.audit.annotations.SpecVersion; +import org.testng.annotations.Test; + +/** + * Checks that the {@code ClockProvider} contract is used in {@code @FutureOrPresent} validators. + * + * @author Guillaume Smet + */ +@SpecVersion(spec = "beanvalidation", version = "2.0.0") +public class ClockProviderFutureOrPresentTest extends AbstractTCKTest { + + private static final ZoneId TZ_BERLIN = ZoneId.of( "Europe/Berlin" ); + + @Deployment + public static WebArchive createTestArchive() { + return webArchiveBuilder() + .withTestClassPackage( ClockProviderPastTest.class ) + .build(); + } + + @Test + @SpecAssertion(section = Sections.BUILTINCONSTRAINTS, id = "t") + public void clockProviderIsUsed() { + FutureOrPresentDummyEntity dummy = new FutureOrPresentDummyEntity( ZonedDateTime.of( 2099, 1, 12, 5, 0, 0, 0, TZ_BERLIN ) ); + + Validator validator = TestUtil.getValidatorUnderTest(); + + assertNumberOfViolations( validator.validate( dummy ), 0 ); + + FixedClockProvider clockProvider = new FixedClockProvider( + ZonedDateTime.of( + 2100, 2, 15, 4, 0, 0, 0, + TZ_BERLIN + ) + ); + ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + validator = validatorFactory.getValidator(); + + Set> violations = validator.validate( dummy ); + assertNumberOfViolations( violations, 13 ); + assertThat( violations ).containsOnlyPaths( + pathWith() + .property( "date" ), + pathWith() + .property( "calendar" ), + pathWith() + .property( "instant" ), + pathWith() + .property( "hijrahDate" ), + pathWith() + .property( "japaneseDate" ), + pathWith() + .property( "localDate" ), + pathWith() + .property( "localDateTime" ), + pathWith() + .property( "minguoDate" ), + pathWith() + .property( "offsetDateTime" ), + pathWith() + .property( "thaiBuddhistDate" ), + pathWith() + .property( "year" ), + pathWith() + .property( "yearMonth" ), + pathWith() + .property( "zonedDateTime" ) + ); + } + + @Test + @SpecAssertion(section = Sections.BUILTINCONSTRAINTS, id = "t") + public void clockProviderIsUsedForRelativePartials() { + FutureOrPresentRelativePartialDummyEntity dummy = new FutureOrPresentRelativePartialDummyEntity( ZonedDateTime.of( 2016, 6, 6, 14, 45, 0, 0, TZ_BERLIN ) ); + + FixedClockProvider clockProvider = new FixedClockProvider( ZonedDateTime.of( 2015, 2, 15, 4, 0, 0, 0, TZ_BERLIN ) ); + ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + Validator validator = validatorFactory.getValidator(); + + assertNumberOfViolations( validator.validate( dummy ), 0 ); + + clockProvider = new FixedClockProvider( ZonedDateTime.of( 2016, 8, 17, 17, 45, 0, 0, TZ_BERLIN ) ); + validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + validator = validatorFactory.getValidator(); + + Set> violations = validator.validate( dummy ); + assertNumberOfViolations( violations, 3 ); + assertThat( violations ).containsOnlyPaths( + pathWith() + .property( "localTime" ), + pathWith() + .property( "monthDay" ), + pathWith() + .property( "offsetTime" ) + ); + } + +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderPastOrPresentTest.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderPastOrPresentTest.java new file mode 100644 index 00000000..eafc53ef --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/ClockProviderPastOrPresentTest.java @@ -0,0 +1,134 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.assertNumberOfViolations; +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.assertThat; +import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.pathWith; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Set; +import java.util.TimeZone; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import org.hibernate.beanvalidation.tck.beanvalidation.Sections; +import org.hibernate.beanvalidation.tck.tests.AbstractTCKTest; +import org.hibernate.beanvalidation.tck.util.TestUtil; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.audit.annotations.SpecAssertion; +import org.jboss.test.audit.annotations.SpecVersion; +import org.testng.annotations.Test; + +/** + * Checks that the {@code ClockProvider} contract is used in {@code @PastOrPresent} validators. + * + * @author Gunnar Morling + * @author Guillaume Smet + */ +@SpecVersion(spec = "beanvalidation", version = "2.0.0") +public class ClockProviderPastOrPresentTest extends AbstractTCKTest { + + private static final ZoneId TZ_BERLIN = ZoneId.of( "Europe/Berlin" ); + + @Deployment + public static WebArchive createTestArchive() { + return webArchiveBuilder() + .withTestClassPackage( ClockProviderPastOrPresentTest.class ) + .build(); + } + + @Test + @SpecAssertion(section = Sections.BUILTINCONSTRAINTS, id = "r") + public void clockProviderIsUsed() { + PastOrPresentDummyEntity dummy = new PastOrPresentDummyEntity( ZonedDateTime.of( 1985, 6, 12, 3, 0, 0, 0, TZ_BERLIN ) ); + + Validator validator = TestUtil.getValidatorUnderTest(); + + assertNumberOfViolations( validator.validate( dummy ), 0 ); + + FixedClockProvider clockProvider = new FixedClockProvider( ZonedDateTime.of( 1984, 2, 15, 4, 0, 0, 0, TZ_BERLIN ) ); + ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + validator = validatorFactory.getValidator(); + + Set> violations = validator.validate( dummy ); + assertNumberOfViolations( violations, 13 ); + assertThat( violations ).containsOnlyPaths( + pathWith() + .property( "date" ), + pathWith() + .property( "calendar" ), + pathWith() + .property( "instant" ), + pathWith() + .property( "hijrahDate" ), + pathWith() + .property( "japaneseDate" ), + pathWith() + .property( "localDate" ), + pathWith() + .property( "localDateTime" ), + pathWith() + .property( "minguoDate" ), + pathWith() + .property( "offsetDateTime" ), + pathWith() + .property( "thaiBuddhistDate" ), + pathWith() + .property( "year" ), + pathWith() + .property( "yearMonth" ), + pathWith() + .property( "zonedDateTime" ) + ); + } + + @Test + @SpecAssertion(section = Sections.BUILTINCONSTRAINTS, id = "r") + public void clockProviderIsUsedForRelativePartials() { + Calendar cal = GregorianCalendar.getInstance( TimeZone.getTimeZone( TZ_BERLIN ) ); + cal.set( 2016, 6, 6 ); + cal.set( Calendar.HOUR_OF_DAY, 14 ); + cal.set( Calendar.MINUTE, 45 ); + + PastOrPresentRelativePartialDummyEntity dummy = new PastOrPresentRelativePartialDummyEntity( ZonedDateTime.of( 2016, 6, 6, 14, 45, 0, 0, TZ_BERLIN ) ); + + FixedClockProvider clockProvider = new FixedClockProvider( ZonedDateTime.of( 2016, 8, 15, 16, 15, 0, 0, TZ_BERLIN ) ); + ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + Validator validator = validatorFactory.getValidator(); + + assertNumberOfViolations( validator.validate( dummy ), 0 ); + + clockProvider = new FixedClockProvider( ZonedDateTime.of( 2014, 4, 4, 9, 45, 0, 0, TZ_BERLIN ) ); + validatorFactory = TestUtil.getConfigurationUnderTest() + .clockProvider( clockProvider ) + .buildValidatorFactory(); + validator = validatorFactory.getValidator(); + + Set> violations = validator.validate( dummy ); + assertNumberOfViolations( violations, 3 ); + assertThat( violations ).containsOnlyPaths( + pathWith() + .property( "localTime" ), + pathWith() + .property( "monthDay" ), + pathWith() + .property( "offsetTime" ) + ); + } + +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentDummyEntity.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentDummyEntity.java new file mode 100644 index 00000000..ad3b25ed --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentDummyEntity.java @@ -0,0 +1,87 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.HijrahDate; +import java.time.chrono.JapaneseDate; +import java.time.chrono.MinguoDate; +import java.time.chrono.ThaiBuddhistDate; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import javax.validation.constraints.FutureOrPresent; + +public class FutureOrPresentDummyEntity { + + @FutureOrPresent + private Calendar calendar; + + @FutureOrPresent + private Date date; + + @FutureOrPresent + private HijrahDate hijrahDate; + + @FutureOrPresent + private Instant instant; + + @FutureOrPresent + private JapaneseDate japaneseDate; + + @FutureOrPresent + private LocalDate localDate; + + @FutureOrPresent + private LocalDateTime localDateTime; + + @FutureOrPresent + private MinguoDate minguoDate; + + @FutureOrPresent + private OffsetDateTime offsetDateTime; + + @FutureOrPresent + private ThaiBuddhistDate thaiBuddhistDate; + + @FutureOrPresent + private Year year; + + @FutureOrPresent + private YearMonth yearMonth; + + @FutureOrPresent + private ZonedDateTime zonedDateTime; + + public FutureOrPresentDummyEntity() { + } + + public FutureOrPresentDummyEntity(ZonedDateTime dateTime) { + calendar = GregorianCalendar.from( dateTime ); + date = calendar.getTime(); + + instant = dateTime.toInstant(); + localDateTime = dateTime.toLocalDateTime(); + + hijrahDate = HijrahDate.from( dateTime ); + japaneseDate = JapaneseDate.from( dateTime ); + localDate = LocalDate.from( dateTime ); + minguoDate = MinguoDate.from( dateTime ); + offsetDateTime = dateTime.toOffsetDateTime(); + thaiBuddhistDate = ThaiBuddhistDate.from( dateTime ); + year = Year.from( dateTime ); + yearMonth = YearMonth.from( dateTime ); + zonedDateTime = dateTime; + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentRelativePartialDummyEntity.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentRelativePartialDummyEntity.java new file mode 100644 index 00000000..38e0d86a --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/FutureOrPresentRelativePartialDummyEntity.java @@ -0,0 +1,35 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetTime; +import java.time.ZonedDateTime; + +import javax.validation.constraints.FutureOrPresent; + +public class FutureOrPresentRelativePartialDummyEntity { + + @FutureOrPresent + private LocalTime localTime; + + @FutureOrPresent + private MonthDay monthDay; + + @FutureOrPresent + private OffsetTime offsetTime; + + public FutureOrPresentRelativePartialDummyEntity() { + } + + public FutureOrPresentRelativePartialDummyEntity(ZonedDateTime dateTime) { + localTime = dateTime.toLocalTime(); + monthDay = MonthDay.from( dateTime ); + offsetTime = OffsetTime.from( dateTime ); + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentDummyEntity.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentDummyEntity.java new file mode 100644 index 00000000..fdf91b9b --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentDummyEntity.java @@ -0,0 +1,87 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.HijrahDate; +import java.time.chrono.JapaneseDate; +import java.time.chrono.MinguoDate; +import java.time.chrono.ThaiBuddhistDate; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import javax.validation.constraints.PastOrPresent; + +public class PastOrPresentDummyEntity { + + @PastOrPresent + private Calendar calendar; + + @PastOrPresent + private Date date; + + @PastOrPresent + private HijrahDate hijrahDate; + + @PastOrPresent + private Instant instant; + + @PastOrPresent + private JapaneseDate japaneseDate; + + @PastOrPresent + private LocalDate localDate; + + @PastOrPresent + private LocalDateTime localDateTime; + + @PastOrPresent + private MinguoDate minguoDate; + + @PastOrPresent + private OffsetDateTime offsetDateTime; + + @PastOrPresent + private ThaiBuddhistDate thaiBuddhistDate; + + @PastOrPresent + private Year year; + + @PastOrPresent + private YearMonth yearMonth; + + @PastOrPresent + private ZonedDateTime zonedDateTime; + + public PastOrPresentDummyEntity() { + } + + public PastOrPresentDummyEntity(ZonedDateTime dateTime) { + calendar = GregorianCalendar.from( dateTime ); + date = calendar.getTime(); + + instant = dateTime.toInstant(); + localDateTime = dateTime.toLocalDateTime(); + + hijrahDate = HijrahDate.from( dateTime ); + japaneseDate = JapaneseDate.from( dateTime ); + localDate = LocalDate.from( dateTime ); + minguoDate = MinguoDate.from( dateTime ); + offsetDateTime = dateTime.toOffsetDateTime(); + thaiBuddhistDate = ThaiBuddhistDate.from( dateTime ); + year = Year.from( dateTime ); + yearMonth = YearMonth.from( dateTime ); + zonedDateTime = dateTime; + } +} diff --git a/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentRelativePartialDummyEntity.java b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentRelativePartialDummyEntity.java new file mode 100644 index 00000000..5eb27be8 --- /dev/null +++ b/tests/src/main/java/org/hibernate/beanvalidation/tck/tests/time/PastOrPresentRelativePartialDummyEntity.java @@ -0,0 +1,35 @@ +/** + * Bean Validation TCK + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.beanvalidation.tck.tests.time; + +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetTime; +import java.time.ZonedDateTime; + +import javax.validation.constraints.PastOrPresent; + +public class PastOrPresentRelativePartialDummyEntity { + + @PastOrPresent + private LocalTime localTime; + + @PastOrPresent + private MonthDay monthDay; + + @PastOrPresent + private OffsetTime offsetTime; + + public PastOrPresentRelativePartialDummyEntity() { + } + + public PastOrPresentRelativePartialDummyEntity(ZonedDateTime dateTime) { + localTime = dateTime.toLocalTime(); + monthDay = MonthDay.from( dateTime ); + offsetTime = OffsetTime.from( dateTime ); + } +}