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); } }
/** * 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 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 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()); }
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)); }
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());
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")); }
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()); } }
assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue()); assertTrue(l.get(0) instanceof DateRange); range = (DateRange) l.get(0); assertEquals(back.getTime(), range.getMinValue()); assertEquals(now.getTime(), range.getMaxValue());
/** * 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 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 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); }