public void testContinuousInterval() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList((Collection) timeKvpParser.parse(CONTINUOUS_PERIOD)); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-31T13Z"); end.setTime(end.getTime() - 1); assertEquals(end, range.getMaxValue()); }
public void testInclusions() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList( (Collection) timeKvpParser.parse( CONTINUOUS_PERIOD + ",2007-01-29T12Z," + "2007-01-12T12Z,2007-01-17T12Z,2007-01-01T12Z/2007-01-15T12Z")); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.size() == 1); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertRange(range, format.parse("2007-01-01T12Z"), format.parse("2007-01-31T13Z")); }
private static void assertRange(DateRange range, Date start, Date end) { assertRangeStarts(range, start); assertRangeEnds(range, new Date(end.getTime() - 1)); }
private static void assertInstant(Date expected, Object object) { if (object instanceof DateRange) { assertEquals(object + " Should start at", expected, ((DateRange) object).getMinValue()); assertEquals(object + " Should end at", expected, ((DateRange) object).getMaxValue()); } else if (object instanceof Date) { assertEquals(expected, object); } else { fail("Should have a DateRange: " + object); } }
public void testMixedValues() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList( (Collection) timeKvpParser.parse(CONTINUOUS_PERIOD + ",2007-02-01T12Z")); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-31T13Z"); end.setTime(end.getTime() - 1); assertEquals(end, range.getMaxValue()); assertRange( (DateRange) l.get(1), format.parse("2007-02-01T12Z"), format.parse("2007-02-01T13Z")); }
/** * Compares the dates obtained by parsing the time parameter with the expected values. * * @throws ParseException if the string can't be parsed. */ public void testInterval() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList((Collection) timeKvpParser.parse(PERIOD)); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertInstant(format.parse("2007-01-01T12Z"), l.get(0)); assertInstant(format.parse("2007-01-03T00Z"), l.get(1)); assertInstant(format.parse("2007-01-04T12Z"), l.get(2)); assertInstant(format.parse("2007-01-06T00Z"), l.get(3)); assertInstant(format.parse("2007-01-07T12Z"), l.get(4)); assertInstant(format.parse("2007-01-09T00Z"), l.get(5)); assertInstant(format.parse("2007-01-10T12Z"), l.get(6)); assertInstant(format.parse("2007-01-12T00Z"), l.get(7)); l = new ArrayList((Collection) timeKvpParser.parse("2007-01-01T12Z/2007-01-01T13Z/PT10M")); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertEquals(12, l.size()); assertInstant(format.parse("2007-01-01T12Z"), l.get(0)); }
public void testInvalidDualDuration() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); boolean exception = false; try { timeKvpParser.parse(CONTINUOUS_PERIOD_INVALID_DURATION); // Verify that an exception was encountered for the invalid duration fail("No exception thrown for invalid duration"); } catch (ParseException ex) { assertTrue(ex.getMessage().startsWith("Invalid time period")); } }
/** * Tests only the increment part of the time parameter. * * @throws ParseException if the string can't be parsed. */ public void testPeriod() throws ParseException { final long millisInDay = TimeParser.MILLIS_IN_DAY; assertEquals(millisInDay, TimeParser.parsePeriod("P1D")); assertEquals(3 * millisInDay, TimeParser.parsePeriod("P3D")); assertEquals(14 * millisInDay, TimeParser.parsePeriod("P2W")); assertEquals(8 * millisInDay, TimeParser.parsePeriod("P1W1D")); assertEquals(millisInDay, TimeParser.parsePeriod("PT24H")); assertEquals(Math.round(1.5 * millisInDay), TimeParser.parsePeriod("P1.5D")); }
public static void assertRangeEnds(DateRange range, Date expectedEnd) { if (range.getMaxValue() == null) fail("Expected valid end date in range " + range); assertEquals("Range " + range + " should have end", expectedEnd, range.getMaxValue()); } }
public void testNegativeYearCompliance() throws Exception { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); // base assertion - 0001 is year 1 DateRange date = (DateRange) ((List) timeKvpParser.parse("01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.AD, cal.get(Calendar.ERA)); date = (DateRange) ((List) timeKvpParser.parse("00-06-01")).get(0); cal.setTime(date.getMinValue()); // calendar calls it year 1, ISO spec means it's year 0 // but we're just parsing here... assertEquals(1, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // so, the next year should be 2 date = (DateRange) ((List) timeKvpParser.parse("-01-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(2, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); // now, big negative year compliance (see the spec, appendix D 2.2, pp 57-58) date = (DateRange) ((List) timeKvpParser.parse("-18000-06-01")).get(0); cal.setTime(date.getMinValue()); assertEquals(18001, cal.get(Calendar.YEAR)); assertEquals(GregorianCalendar.BC, cal.get(Calendar.ERA)); }
public void testContinuousIntervalDuration() throws ParseException { TimeKvpParser timeKvpParser = new TimeKvpParser("TIME"); List l = new ArrayList((Collection) timeKvpParser.parse(CONTINUOUS_PERIOD_TIME_DURATION)); // Verify that the list contains at least one element. assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); final DateRange range = (DateRange) l.get(0); assertEquals(format.parse("2007-01-01T12Z"), range.getMinValue()); Date end = format.parse("2007-01-02T13Z"); assertEquals(end, range.getMaxValue()); }
+ "2007-01-17T12Z,2007-01-01T12Z,2007-01-05T12Z")); assertFalse(l.isEmpty()); assertTrue(l.size() == 5); assertRange( (DateRange) l.get(0), format.parse("2007-01-01T12Z"), format.parse("2007-01-01T13Z")); assertRange( (DateRange) l.get(1), format.parse("2007-01-05T12Z"), format.parse("2007-01-05T13Z")); assertRange( (DateRange) l.get(2), format.parse("2007-01-12T12Z"), format.parse("2007-01-12T13Z")); assertRange( (DateRange) l.get(3), format.parse("2007-01-17T12Z"), format.parse("2007-01-17T13Z")); assertRange( (DateRange) l.get(4), format.parse("2007-01-29T12Z"),
public static void assertRangeStarts(DateRange range, Date expectedStart) { if (range.getMinValue() == null) fail("Expected valid start date in range " + range); assertEquals("Range " + range + " should have start", expectedStart, range.getMinValue()); }
public void testReducedAccuracyYear() throws Exception { Calendar c = new GregorianCalendar(); c.setTimeZone(TimeParser.UTC_TZ); DateRange year = (DateRange) TimeParser.getFuzzyDate("2000"); c.clear(); c.set(Calendar.YEAR, 2000); assertRangeStarts(year, c.getTime()); c.set(Calendar.YEAR, 2001); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(year, c.getTime()); year = (DateRange) TimeParser.getFuzzyDate("2001"); c.clear(); c.set(Calendar.YEAR, 2001); assertRangeStarts(year, c.getTime()); c.set(Calendar.YEAR, 2002); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(year, c.getTime()); year = (DateRange) TimeParser.getFuzzyDate("-6052"); c.clear(); c.set(Calendar.ERA, GregorianCalendar.BC); c.set(Calendar.YEAR, 6053); assertRangeStarts(year, c.getTime()); c.set(Calendar.YEAR, 6052); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(year, c.getTime()); }
c.set(Calendar.DAY_OF_MONTH, 4); c.set(Calendar.HOUR_OF_DAY, 12); assertEquals(instant, c.getTime()); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 60); assertEquals(instant, c.getTime()); c.set(Calendar.MINUTE, 15); c.set(Calendar.MILLISECOND, 123); assertEquals(instant, c.getTime());
back = (Calendar) now.clone(); back.add(Calendar.HOUR, -2); assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); back = (Calendar) now.clone(); back.add(Calendar.MILLISECOND, millisInDay * -10); assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); back = (Calendar) now.clone(); back.add(Calendar.MILLISECOND, millisInDay * -2 * 7); assertFalse(l.isEmpty()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue());
public static void assertRangeLength(DateRange range, long expectedLength) { if (range.getMinValue() == null) fail("Expected finite range, saw: " + range); if (range.getMaxValue() == null) fail("Expected finite range, saw: " + range); long min = range.getMinValue().getTime(); long max = range.getMaxValue().getTime(); assertEquals("Range " + range + " should have length", expectedLength, max - min); }
c.set(Calendar.DAY_OF_MONTH, 4); c.set(Calendar.HOUR_OF_DAY, 12); assertRangeStarts(hour, c.getTime()); c.add(Calendar.HOUR_OF_DAY, 1); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(hour, c.getTime()); c.set(Calendar.DAY_OF_MONTH, 31); c.set(Calendar.HOUR_OF_DAY, 23); assertRangeStarts(hour, c.getTime()); c.add(Calendar.HOUR_OF_DAY, 1); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(hour, c.getTime()); c.set(Calendar.DAY_OF_MONTH, 8); c.set(Calendar.HOUR_OF_DAY, 17); assertRangeStarts(hour, c.getTime()); c.add(Calendar.HOUR_OF_DAY, 1); c.add(Calendar.MILLISECOND, -1); assertRangeEnds(hour, c.getTime());