diff --git a/src/test/java/org/apache/commons/validator/routines/AbstractCalendarValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/AbstractCalendarValidatorTest.java index 695e5864f..98a07b5a2 100644 --- a/src/test/java/org/apache/commons/validator/routines/AbstractCalendarValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/AbstractCalendarValidatorTest.java @@ -40,12 +40,6 @@ */ public abstract class AbstractCalendarValidatorTest { - protected static final TimeZone GMT = TimeZone.getTimeZone("GMT"); // 0 offset - - protected static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours - protected static final TimeZone EET = TimeZone.getTimeZone("EET"); // + 2 hours - protected static final TimeZone UTC = TimeZone.getTimeZone("UTC"); // 0 offset - /** * Create a calendar instance for a specified time zone, date and time. * diff --git a/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java index 15424abe3..afc4558be 100644 --- a/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java @@ -32,6 +32,8 @@ import java.util.Locale; import java.util.TimeZone; +import org.apache.commons.lang3.time.TimeZones; +import org.apache.commons.validator.util.TestTimeZones; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junitpioneer.jupiter.DefaultLocale; @@ -59,13 +61,13 @@ protected void setUp() { @Test public void testAdjustToTimeZone() { - final Calendar calEST = createCalendar(EST, DATE_2005_11_23, TIME_12_03_45); + final Calendar calEST = createCalendar(TestTimeZones.EST, DATE_2005_11_23, TIME_12_03_45); final Date dateEST = calEST.getTime(); - final Calendar calGMT = createCalendar(GMT, DATE_2005_11_23, TIME_12_03_45); + final Calendar calGMT = createCalendar(TimeZones.GMT, DATE_2005_11_23, TIME_12_03_45); final Date dateGMT = calGMT.getTime(); - final Calendar calCET = createCalendar(EET, DATE_2005_11_23, TIME_12_03_45); + final Calendar calCET = createCalendar(TestTimeZones.EET, DATE_2005_11_23, TIME_12_03_45); final Date dateCET = calCET.getTime(); // Check the dates don't match @@ -74,31 +76,31 @@ public void testAdjustToTimeZone() { assertNotEquals(dateCET.getTime(), dateEST.getTime(), "Check CET != EST"); // EST to GMT and back - CalendarValidator.adjustToTimeZone(calEST, GMT); + CalendarValidator.adjustToTimeZone(calEST, TimeZones.GMT); assertEquals(dateGMT, calEST.getTime(), "EST to GMT"); assertNotSame(dateEST, calEST.getTime(), "Check EST = GMT"); - CalendarValidator.adjustToTimeZone(calEST, EST); + CalendarValidator.adjustToTimeZone(calEST, TestTimeZones.EST); assertEquals(dateEST, calEST.getTime(), "back to EST"); assertNotSame(dateGMT, calEST.getTime(), "Check EST != GMT"); // CET to GMT and back - CalendarValidator.adjustToTimeZone(calCET, GMT); + CalendarValidator.adjustToTimeZone(calCET, TimeZones.GMT); assertEquals(dateGMT, calCET.getTime(), "CET to GMT"); assertNotSame(dateCET, calCET.getTime(), "Check CET = GMT"); - CalendarValidator.adjustToTimeZone(calCET, EET); + CalendarValidator.adjustToTimeZone(calCET, TestTimeZones.EET); assertEquals(dateCET, calCET.getTime(), "back to CET"); assertNotSame(dateGMT, calCET.getTime(), "Check CET != GMT"); // Adjust to TimeZone with Same rules - final Calendar calUTC = createCalendar(UTC, DATE_2005_11_23, TIME_12_03_45); - assertTrue(UTC.hasSameRules(GMT), "SAME: UTC = GMT"); + final Calendar calUTC = createCalendar(TestTimeZones.UTC, DATE_2005_11_23, TIME_12_03_45); + assertTrue(TestTimeZones.UTC.hasSameRules(TimeZones.GMT), "SAME: UTC = GMT"); assertEquals(calUTC.getTime(), calGMT.getTime(), "SAME: Check time (A)"); - assertNotEquals(GMT, calUTC.getTimeZone(), "SAME: Check GMT(A)"); - assertEquals(UTC, calUTC.getTimeZone(), "SAME: Check UTC(A)"); - CalendarValidator.adjustToTimeZone(calUTC, GMT); + assertNotEquals(TimeZones.GMT, calUTC.getTimeZone(), "SAME: Check GMT(A)"); + assertEquals(TestTimeZones.UTC, calUTC.getTimeZone(), "SAME: Check UTC(A)"); + CalendarValidator.adjustToTimeZone(calUTC, TimeZones.GMT); assertEquals(calUTC.getTime(), calGMT.getTime(), "SAME: Check time (B)"); - assertEquals(GMT, calUTC.getTimeZone(), "SAME: Check GMT(B)"); - assertNotEquals(UTC, calUTC.getTimeZone(), "SAME: Check UTC(B)"); + assertEquals(TimeZones.GMT, calUTC.getTimeZone(), "SAME: Check GMT(B)"); + assertNotEquals(TestTimeZones.UTC, calUTC.getTimeZone(), "SAME: Check UTC(B)"); } /** @@ -141,7 +143,7 @@ public void testCalendarValidatorMethods() { assertFalse(CalendarValidator.getInstance().isValid("31 Dec 2005", germanPattern, Locale.GERMAN), "isValid(B) both"); // Test Time Zone - final TimeZone zone = TimeZone.getDefault().getRawOffset() == EET.getRawOffset() ? EST : EET; + final TimeZone zone = TimeZone.getDefault().getRawOffset() == TestTimeZones.EET.getRawOffset() ? TestTimeZones.EST : TestTimeZones.EET; final Date expectedZone = createCalendar(zone, 20051231, 0).getTime(); assertNotEquals(expected.getTime(), expectedZone.getTime(), "default/EET same "); @@ -158,29 +160,29 @@ public void testCalendarValidatorMethods() { public void testCompare() { final int sameTime = 124522; final int testDate = 20050823; - final Calendar diffHour = createCalendar(GMT, testDate, 115922); // same date, different time - final Calendar diffMin = createCalendar(GMT, testDate, 124422); // same date, different time - final Calendar diffSec = createCalendar(GMT, testDate, 124521); // same date, different time + final Calendar diffHour = createCalendar(TimeZones.GMT, testDate, 115922); // same date, different time + final Calendar diffMin = createCalendar(TimeZones.GMT, testDate, 124422); // same date, different time + final Calendar diffSec = createCalendar(TimeZones.GMT, testDate, 124521); // same date, different time - final Calendar value = createCalendar(GMT, testDate, sameTime); // test value - final Calendar cal20050824 = createCalendar(GMT, 20050824, sameTime); // +1 day - final Calendar cal20050822 = createCalendar(GMT, 20050822, sameTime); // -1 day + final Calendar value = createCalendar(TimeZones.GMT, testDate, sameTime); // test value + final Calendar cal20050824 = createCalendar(TimeZones.GMT, 20050824, sameTime); // +1 day + final Calendar cal20050822 = createCalendar(TimeZones.GMT, 20050822, sameTime); // -1 day - final Calendar cal20050830 = createCalendar(GMT, 20050830, sameTime); // +1 week - final Calendar cal20050816 = createCalendar(GMT, 20050816, sameTime); // -1 week + final Calendar cal20050830 = createCalendar(TimeZones.GMT, 20050830, sameTime); // +1 week + final Calendar cal20050816 = createCalendar(TimeZones.GMT, 20050816, sameTime); // -1 week - final Calendar cal20050901 = createCalendar(GMT, 20050901, sameTime); // +1 month - final Calendar cal20050801 = createCalendar(GMT, 20050801, sameTime); // same month - final Calendar cal20050731 = createCalendar(GMT, 20050731, sameTime); // -1 month + final Calendar cal20050901 = createCalendar(TimeZones.GMT, 20050901, sameTime); // +1 month + final Calendar cal20050801 = createCalendar(TimeZones.GMT, 20050801, sameTime); // same month + final Calendar cal20050731 = createCalendar(TimeZones.GMT, 20050731, sameTime); // -1 month - final Calendar cal20051101 = createCalendar(GMT, 20051101, sameTime); // +1 quarter (Feb Start) - final Calendar cal20051001 = createCalendar(GMT, 20051001, sameTime); // +1 quarter - final Calendar cal20050701 = createCalendar(GMT, 20050701, sameTime); // same quarter - final Calendar cal20050630 = createCalendar(GMT, 20050630, sameTime); // -1 quarter + final Calendar cal20051101 = createCalendar(TimeZones.GMT, 20051101, sameTime); // +1 quarter (Feb Start) + final Calendar cal20051001 = createCalendar(TimeZones.GMT, 20051001, sameTime); // +1 quarter + final Calendar cal20050701 = createCalendar(TimeZones.GMT, 20050701, sameTime); // same quarter + final Calendar cal20050630 = createCalendar(TimeZones.GMT, 20050630, sameTime); // -1 quarter - final Calendar cal20060101 = createCalendar(GMT, 20060101, sameTime); // +1 year - final Calendar cal20050101 = createCalendar(GMT, 20050101, sameTime); // same year - final Calendar cal20041231 = createCalendar(GMT, 20041231, sameTime); // -1 year + final Calendar cal20060101 = createCalendar(TimeZones.GMT, 20060101, sameTime); // +1 year + final Calendar cal20050101 = createCalendar(TimeZones.GMT, 20050101, sameTime); // same year + final Calendar cal20041231 = createCalendar(TimeZones.GMT, 20041231, sameTime); // -1 year assertEquals(1, calValidator.compare(value, diffHour, Calendar.HOUR_OF_DAY), "hour GT"); assertEquals(0, calValidator.compare(value, diffMin, Calendar.HOUR_OF_DAY), "hour EQ"); @@ -267,7 +269,7 @@ protected Object processParsedValue(final Object value, final Format formatter) @DefaultLocale(country = "UK", language = "en") @DefaultTimeZone("GMT") public void testFormat() { - final Calendar cal20051231 = createCalendar(GMT, 20051231, 11500); + final Calendar cal20051231 = createCalendar(TimeZones.GMT, 20051231, 11500); // validator defaults to SHORT, but the format varies between JVMs final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); final String val = df.format(cal20051231.getTime()); @@ -285,33 +287,33 @@ public void testFormat() { // EST Time Zone final DateFormat dfest = DateFormat.getDateInstance(DateFormat.SHORT); - dfest.setTimeZone(EST); + dfest.setTimeZone(TestTimeZones.EST); final String valest = dfest.format(cal20051231.getTime()); final DateFormat dfusest = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); - dfusest.setTimeZone(EST); + dfusest.setTimeZone(TestTimeZones.EST); final String valusest = dfusest.format(cal20051231.getTime()); final DateFormat dedfest = new SimpleDateFormat(germanPattern, Locale.GERMAN); - dedfest.setTimeZone(EST); + dedfest.setTimeZone(TestTimeZones.EST); final String devalest = dedfest.format(cal20051231.getTime()); - assertEquals(valest, calValidator.format(cal20051231, EST), "EST default"); - assertEquals(valusest, calValidator.format(cal20051231, Locale.US, EST), "EST locale"); + assertEquals(valest, calValidator.format(cal20051231, TestTimeZones.EST), "EST default"); + assertEquals(valusest, calValidator.format(cal20051231, Locale.US, TestTimeZones.EST), "EST locale"); final String patternA = "yyyy-MM-dd HH:mm"; final DateFormat dfA = new SimpleDateFormat(patternA); - dfA.setTimeZone(EST); + dfA.setTimeZone(TestTimeZones.EST); final String valA = dfA.format(cal20051231.getTime()); - assertEquals(valA, calValidator.format(cal20051231, patternA, EST), "EST patternA"); + assertEquals(valA, calValidator.format(cal20051231, patternA, TestTimeZones.EST), "EST patternA"); final String patternB = "yyyy-MM-dd z"; final DateFormat dfB = new SimpleDateFormat(patternB); - dfB.setTimeZone(EST); + dfB.setTimeZone(TestTimeZones.EST); final String valB = dfB.format(cal20051231.getTime()); - assertEquals(valB, calValidator.format(cal20051231, patternB, EST), "EST patternB"); - assertEquals(devalest, calValidator.format(cal20051231, germanPattern, Locale.GERMAN, EST), "EST both"); + assertEquals(valB, calValidator.format(cal20051231, patternB, TestTimeZones.EST), "EST patternB"); + assertEquals(devalest, calValidator.format(cal20051231, germanPattern, Locale.GERMAN, TestTimeZones.EST), "EST both"); } } diff --git a/src/test/java/org/apache/commons/validator/routines/DateValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/DateValidatorTest.java index f302262d6..27239b683 100644 --- a/src/test/java/org/apache/commons/validator/routines/DateValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/DateValidatorTest.java @@ -29,6 +29,8 @@ import java.util.Locale; import java.util.TimeZone; +import org.apache.commons.lang3.time.TimeZones; +import org.apache.commons.validator.util.TestTimeZones; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junitpioneer.jupiter.DefaultLocale; @@ -56,71 +58,71 @@ protected void setUp() { public void testCompare() { final int sameTime = 124522; final int testDate = 20050823; - final Date diffHour = createDate(GMT, testDate, 115922); // same date, different time - - final Date value = createDate(GMT, testDate, sameTime); // test value - final Date date20050824 = createDate(GMT, 20050824, sameTime); // +1 day - final Date date20050822 = createDate(GMT, 20050822, sameTime); // -1 day - - final Date date20050830 = createDate(GMT, 20050830, sameTime); // +1 week - final Date date20050816 = createDate(GMT, 20050816, sameTime); // -1 week - - final Date date20050901 = createDate(GMT, 20050901, sameTime); // +1 month - final Date date20050801 = createDate(GMT, 20050801, sameTime); // same month - final Date date20050731 = createDate(GMT, 20050731, sameTime); // -1 month - - final Date date20051101 = createDate(GMT, 20051101, sameTime); // +1 quarter (Feb Start) - final Date date20051001 = createDate(GMT, 20051001, sameTime); // +1 quarter - final Date date20050701 = createDate(GMT, 20050701, sameTime); // same quarter - final Date date20050630 = createDate(GMT, 20050630, sameTime); // -1 quarter - final Date date20050110 = createDate(GMT, 20050110, sameTime); // Previous Year qtr (Fen start) - - final Date date20060101 = createDate(GMT, 20060101, sameTime); // +1 year - final Date date20050101 = createDate(GMT, 20050101, sameTime); // same year - final Date date20041231 = createDate(GMT, 20041231, sameTime); // -1 year - - assertEquals(-1, dateValidator.compareDates(value, date20050824, GMT), "date LT"); // +1 day - assertEquals(0, dateValidator.compareDates(value, diffHour, GMT), "date EQ"); // same day, diff hour - assertEquals(1, dateValidator.compareDates(value, date20050822, GMT), "date GT"); // -1 day - - assertEquals(-1, dateValidator.compareWeeks(value, date20050830, GMT), "week LT"); // +1 week - assertEquals(0, dateValidator.compareWeeks(value, date20050824, GMT), "week =1"); // +1 day - assertEquals(0, dateValidator.compareWeeks(value, date20050822, GMT), "week =2"); // same week - assertEquals(0, dateValidator.compareWeeks(value, date20050822, GMT), "week =3"); // -1 day - assertEquals(1, dateValidator.compareWeeks(value, date20050816, GMT), "week GT"); // -1 week - - assertEquals(-1, dateValidator.compareMonths(value, date20050901, GMT), "mnth LT"); // +1 month - assertEquals(0, dateValidator.compareMonths(value, date20050830, GMT), "mnth =1"); // +1 week - assertEquals(0, dateValidator.compareMonths(value, date20050801, GMT), "mnth =2"); // same month - assertEquals(0, dateValidator.compareMonths(value, date20050816, GMT), "mnth =3"); // -1 week - assertEquals(1, dateValidator.compareMonths(value, date20050731, GMT), "mnth GT"); // -1 month - - assertEquals(-1, dateValidator.compareQuarters(value, date20051101, GMT), "qtrA <1"); // +1 quarter (Feb) - assertEquals(-1, dateValidator.compareQuarters(value, date20051001, GMT), "qtrA <2"); // +1 quarter - assertEquals(0, dateValidator.compareQuarters(value, date20050901, GMT), "qtrA =1"); // +1 month - assertEquals(0, dateValidator.compareQuarters(value, date20050701, GMT), "qtrA =2"); // same quarter - assertEquals(0, dateValidator.compareQuarters(value, date20050731, GMT), "qtrA =3"); // -1 month - assertEquals(1, dateValidator.compareQuarters(value, date20050630, GMT), "qtrA GT"); // -1 quarter + final Date diffHour = createDate(TimeZones.GMT, testDate, 115922); // same date, different time + + final Date value = createDate(TimeZones.GMT, testDate, sameTime); // test value + final Date date20050824 = createDate(TimeZones.GMT, 20050824, sameTime); // +1 day + final Date date20050822 = createDate(TimeZones.GMT, 20050822, sameTime); // -1 day + + final Date date20050830 = createDate(TimeZones.GMT, 20050830, sameTime); // +1 week + final Date date20050816 = createDate(TimeZones.GMT, 20050816, sameTime); // -1 week + + final Date date20050901 = createDate(TimeZones.GMT, 20050901, sameTime); // +1 month + final Date date20050801 = createDate(TimeZones.GMT, 20050801, sameTime); // same month + final Date date20050731 = createDate(TimeZones.GMT, 20050731, sameTime); // -1 month + + final Date date20051101 = createDate(TimeZones.GMT, 20051101, sameTime); // +1 quarter (Feb Start) + final Date date20051001 = createDate(TimeZones.GMT, 20051001, sameTime); // +1 quarter + final Date date20050701 = createDate(TimeZones.GMT, 20050701, sameTime); // same quarter + final Date date20050630 = createDate(TimeZones.GMT, 20050630, sameTime); // -1 quarter + final Date date20050110 = createDate(TimeZones.GMT, 20050110, sameTime); // Previous Year qtr (Fen start) + + final Date date20060101 = createDate(TimeZones.GMT, 20060101, sameTime); // +1 year + final Date date20050101 = createDate(TimeZones.GMT, 20050101, sameTime); // same year + final Date date20041231 = createDate(TimeZones.GMT, 20041231, sameTime); // -1 year + + assertEquals(-1, dateValidator.compareDates(value, date20050824, TimeZones.GMT), "date LT"); // +1 day + assertEquals(0, dateValidator.compareDates(value, diffHour, TimeZones.GMT), "date EQ"); // same day, diff hour + assertEquals(1, dateValidator.compareDates(value, date20050822, TimeZones.GMT), "date GT"); // -1 day + + assertEquals(-1, dateValidator.compareWeeks(value, date20050830, TimeZones.GMT), "week LT"); // +1 week + assertEquals(0, dateValidator.compareWeeks(value, date20050824, TimeZones.GMT), "week =1"); // +1 day + assertEquals(0, dateValidator.compareWeeks(value, date20050822, TimeZones.GMT), "week =2"); // same week + assertEquals(0, dateValidator.compareWeeks(value, date20050822, TimeZones.GMT), "week =3"); // -1 day + assertEquals(1, dateValidator.compareWeeks(value, date20050816, TimeZones.GMT), "week GT"); // -1 week + + assertEquals(-1, dateValidator.compareMonths(value, date20050901, TimeZones.GMT), "mnth LT"); // +1 month + assertEquals(0, dateValidator.compareMonths(value, date20050830, TimeZones.GMT), "mnth =1"); // +1 week + assertEquals(0, dateValidator.compareMonths(value, date20050801, TimeZones.GMT), "mnth =2"); // same month + assertEquals(0, dateValidator.compareMonths(value, date20050816, TimeZones.GMT), "mnth =3"); // -1 week + assertEquals(1, dateValidator.compareMonths(value, date20050731, TimeZones.GMT), "mnth GT"); // -1 month + + assertEquals(-1, dateValidator.compareQuarters(value, date20051101, TimeZones.GMT), "qtrA <1"); // +1 quarter (Feb) + assertEquals(-1, dateValidator.compareQuarters(value, date20051001, TimeZones.GMT), "qtrA <2"); // +1 quarter + assertEquals(0, dateValidator.compareQuarters(value, date20050901, TimeZones.GMT), "qtrA =1"); // +1 month + assertEquals(0, dateValidator.compareQuarters(value, date20050701, TimeZones.GMT), "qtrA =2"); // same quarter + assertEquals(0, dateValidator.compareQuarters(value, date20050731, TimeZones.GMT), "qtrA =3"); // -1 month + assertEquals(1, dateValidator.compareQuarters(value, date20050630, TimeZones.GMT), "qtrA GT"); // -1 quarter // Change quarter 1 to start in Feb - assertEquals(-1, dateValidator.compareQuarters(value, date20051101, GMT, 2), "qtrB LT"); // +1 quarter (Feb) - assertEquals(0, dateValidator.compareQuarters(value, date20051001, GMT, 2), "qtrB =1"); // same quarter - assertEquals(0, dateValidator.compareQuarters(value, date20050901, GMT, 2), "qtrB =2"); // +1 month - assertEquals(1, dateValidator.compareQuarters(value, date20050701, GMT, 2), "qtrB =3"); // same quarter - assertEquals(1, dateValidator.compareQuarters(value, date20050731, GMT, 2), "qtrB =4"); // -1 month - assertEquals(1, dateValidator.compareQuarters(value, date20050630, GMT, 2), "qtrB GT"); // -1 quarter - assertEquals(1, dateValidator.compareQuarters(value, date20050110, GMT, 2), "qtrB prev"); // Jan Prev year qtr - - assertEquals(-1, dateValidator.compareYears(value, date20060101, GMT), "year LT"); // +1 year - assertEquals(0, dateValidator.compareYears(value, date20050101, GMT), "year EQ"); // same year - assertEquals(1, dateValidator.compareYears(value, date20041231, GMT), "year GT"); // -1 year + assertEquals(-1, dateValidator.compareQuarters(value, date20051101, TimeZones.GMT, 2), "qtrB LT"); // +1 quarter (Feb) + assertEquals(0, dateValidator.compareQuarters(value, date20051001, TimeZones.GMT, 2), "qtrB =1"); // same quarter + assertEquals(0, dateValidator.compareQuarters(value, date20050901, TimeZones.GMT, 2), "qtrB =2"); // +1 month + assertEquals(1, dateValidator.compareQuarters(value, date20050701, TimeZones.GMT, 2), "qtrB =3"); // same quarter + assertEquals(1, dateValidator.compareQuarters(value, date20050731, TimeZones.GMT, 2), "qtrB =4"); // -1 month + assertEquals(1, dateValidator.compareQuarters(value, date20050630, TimeZones.GMT, 2), "qtrB GT"); // -1 quarter + assertEquals(1, dateValidator.compareQuarters(value, date20050110, TimeZones.GMT, 2), "qtrB prev"); // Jan Prev year qtr + + assertEquals(-1, dateValidator.compareYears(value, date20060101, TimeZones.GMT), "year LT"); // +1 year + assertEquals(0, dateValidator.compareYears(value, date20050101, TimeZones.GMT), "year EQ"); // same year + assertEquals(1, dateValidator.compareYears(value, date20041231, TimeZones.GMT), "year GT"); // -1 year // Compare using alternative TimeZone - final Date sameDayTwoAm = createDate(GMT, testDate, 20000); - assertEquals(-1, dateValidator.compareDates(value, date20050824, EST), "date LT"); // +1 day - assertEquals(0, dateValidator.compareDates(value, diffHour, EST), "date EQ"); // same day, diff hour - assertEquals(1, dateValidator.compareDates(value, sameDayTwoAm, EST), "date EQ"); // same day, diff hour - assertEquals(1, dateValidator.compareDates(value, date20050822, EST), "date GT"); // -1 day + final Date sameDayTwoAm = createDate(TimeZones.GMT, testDate, 20000); + assertEquals(-1, dateValidator.compareDates(value, date20050824, TestTimeZones.EST), "date LT"); // +1 day + assertEquals(0, dateValidator.compareDates(value, diffHour, TestTimeZones.EST), "date EQ"); // same day, diff hour + assertEquals(1, dateValidator.compareDates(value, sameDayTwoAm, TestTimeZones.EST), "date EQ"); // same day, diff hour + assertEquals(1, dateValidator.compareDates(value, date20050822, TestTimeZones.EST), "date GT"); // -1 day } /** @@ -164,7 +166,7 @@ public void testDateValidatorMethods() { assertFalse(DateValidator.getInstance().isValid("31 Dec 2005", germanPattern, Locale.GERMAN), "isValid(B) both"); // Test Time Zone - final TimeZone zone = TimeZone.getDefault().getRawOffset() == EET.getRawOffset() ? EST : EET; + final TimeZone zone = TimeZone.getDefault().getRawOffset() == TestTimeZones.EET.getRawOffset() ? TestTimeZones.EST : TestTimeZones.EET; final Date expectedZone = createCalendar(zone, 20051231, 0).getTime(); assertNotEquals(expected.getTime(), expectedZone.getTime(), "default/zone same " + zone); diff --git a/src/test/java/org/apache/commons/validator/routines/TimeValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/TimeValidatorTest.java index 336af62b4..2b3dba97b 100644 --- a/src/test/java/org/apache/commons/validator/routines/TimeValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/TimeValidatorTest.java @@ -29,6 +29,8 @@ import java.util.Locale; import java.util.TimeZone; +import org.apache.commons.lang3.time.TimeZones; +import org.apache.commons.validator.util.TestTimeZones; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,9 +42,6 @@ */ public class TimeValidatorTest { - protected static final TimeZone GMT = TimeZone.getTimeZone("GMT"); // 0 offset - protected static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours - /** * Create a date instance for a specified time zone, date and time. * @@ -134,18 +133,18 @@ public void testCompare() { final int min = 100; final int hour = 10000; - final Calendar milliGreater = createTime(GMT, testTime, 500); // > milli sec - final Calendar value = createTime(GMT, testTime, 400); // test value - final Calendar milliLess = createTime(GMT, testTime, 300); // < milli sec + final Calendar milliGreater = createTime(TimeZones.GMT, testTime, 500); // > milli sec + final Calendar value = createTime(TimeZones.GMT, testTime, 400); // test value + final Calendar milliLess = createTime(TimeZones.GMT, testTime, 300); // < milli sec - final Calendar secGreater = createTime(GMT, testTime + 1, 100); // +1 sec - final Calendar secLess = createTime(GMT, testTime - 1, 100); // -1 sec + final Calendar secGreater = createTime(TimeZones.GMT, testTime + 1, 100); // +1 sec + final Calendar secLess = createTime(TimeZones.GMT, testTime - 1, 100); // -1 sec - final Calendar minGreater = createTime(GMT, testTime + min, 100); // +1 min - final Calendar minLess = createTime(GMT, testTime - min, 100); // -1 min + final Calendar minGreater = createTime(TimeZones.GMT, testTime + min, 100); // +1 min + final Calendar minLess = createTime(TimeZones.GMT, testTime - min, 100); // -1 min - final Calendar hourGreater = createTime(GMT, testTime + hour, 100); // +1 hour - final Calendar hourLess = createTime(GMT, testTime - hour, 100); // -1 hour + final Calendar hourGreater = createTime(TimeZones.GMT, testTime + hour, 100); // +1 hour + final Calendar hourLess = createTime(TimeZones.GMT, testTime - hour, 100); // -1 hour assertEquals(-1, validator.compareTime(value, milliGreater), "milli LT"); // > milli assertEquals(0, validator.compareTime(value, value), "milli EQ"); // same time @@ -255,23 +254,23 @@ public void testPatternValid() { public void testTimeZone() { Calendar result = validator.validate("18:01"); assertNotNull(result, "default result"); - assertEquals(GMT, result.getTimeZone(), "default zone"); + assertEquals(TimeZones.GMT, result.getTimeZone(), "default zone"); assertEquals(18, result.get(Calendar.HOUR_OF_DAY), "default hour"); assertEquals(01, result.get(Calendar.MINUTE), "default minute"); result = null; // Default Locale, diff TimeZone - result = validator.validate("16:49", EST); + result = validator.validate("16:49", TestTimeZones.EST); assertNotNull(result, "zone result"); - assertEquals(EST, result.getTimeZone(), "zone zone"); + assertEquals(TestTimeZones.EST, result.getTimeZone(), "zone zone"); assertEquals(16, result.get(Calendar.HOUR_OF_DAY), "zone hour"); assertEquals(49, result.get(Calendar.MINUTE), "zone minute"); result = null; // Pattern, diff TimeZone - result = validator.validate("14-34", "HH-mm", EST); + result = validator.validate("14-34", "HH-mm", TestTimeZones.EST); assertNotNull(result, "pattern result"); - assertEquals(EST, result.getTimeZone(), "pattern zone"); + assertEquals(TestTimeZones.EST, result.getTimeZone(), "pattern zone"); assertEquals(14, result.get(Calendar.HOUR_OF_DAY), "pattern hour"); assertEquals(34, result.get(Calendar.MINUTE), "pattern minute"); result = null; @@ -281,9 +280,9 @@ public void testTimeZone() { final Calendar uscal = Calendar.getInstance(Locale.US); uscal.set(2005, 0, 1, 19, 18); // month is 0-based final String usVal = usdf.format(uscal.getTime()); - result = validator.validate(usVal, Locale.US, EST); + result = validator.validate(usVal, Locale.US, TestTimeZones.EST); assertNotNull(result, "locale result: " + usVal); - assertEquals(EST, result.getTimeZone(), "locale zone: " + usVal); + assertEquals(TestTimeZones.EST, result.getTimeZone(), "locale zone: " + usVal); assertEquals(19, result.get(Calendar.HOUR_OF_DAY), "locale hour: " + usVal); assertEquals(18, result.get(Calendar.MINUTE), "locale minute: " + usVal); result = null; @@ -294,9 +293,9 @@ public void testTimeZone() { final String germanSample = new SimpleDateFormat(dateTimePattern, Locale.GERMAN).format(decal.getTime()); // Locale & Pattern, diff TimeZone - result = validator.validate(germanSample, dateTimePattern, Locale.GERMAN, EST); + result = validator.validate(germanSample, dateTimePattern, Locale.GERMAN, TestTimeZones.EST); assertNotNull(result, "pattern result: " + germanSample); - assertEquals(EST, result.getTimeZone(), "pattern zone"); + assertEquals(TestTimeZones.EST, result.getTimeZone(), "pattern zone"); assertEquals(2005, result.get(Calendar.YEAR), "pattern day"); assertEquals(11, result.get(Calendar.MONTH), "pattern day"); // months are 0-11 assertEquals(31, result.get(Calendar.DATE), "pattern day"); @@ -307,7 +306,7 @@ public void testTimeZone() { // Locale & Pattern, default TimeZone result = validator.validate(germanSample, dateTimePattern, Locale.GERMAN); assertNotNull(result, "pattern result: " + germanSample); - assertEquals(GMT, result.getTimeZone(), "pattern zone"); + assertEquals(TimeZones.GMT, result.getTimeZone(), "pattern zone"); assertEquals(2005, result.get(Calendar.YEAR), "pattern day"); assertEquals(11, result.get(Calendar.MONTH), "pattern day"); // months are 0-11 assertEquals(31, result.get(Calendar.DATE), "pattern day"); diff --git a/src/test/java/org/apache/commons/validator/util/TestTimeZones.java b/src/test/java/org/apache/commons/validator/util/TestTimeZones.java new file mode 100644 index 000000000..eccd7a4bc --- /dev/null +++ b/src/test/java/org/apache/commons/validator/util/TestTimeZones.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.validator.util; + +import java.util.TimeZone; + +/** + * TimeZone test fixtures. + */ +public class TestTimeZones { + + public static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours + public static final TimeZone EET = TimeZone.getTimeZone("EET"); // + 2 hours + public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); // 0 offset + +}