// Check if a calendar object used to create a duration object and
// a calendar object created from the same duration are equal
// that's good test for handling of miliseconds
Calendar calendar = new GregorianCalendar(1, 2, 20, 10, 3, 11);
calendar.set(Calendar.MILLISECOND, 15);
Duration duration = new Duration(true, calendar);
Calendar durationCalendar = duration.getAsCalendar();
assertTrue("Negative flag does not match", duration.isNegative());
assertEquals("Years do not match",
calendar.get(Calendar.YEAR),
durationCalendar.get(Calendar.YEAR));
assertEquals("Months do not match",
calendar.get(Calendar.MONTH),
durationCalendar.get(Calendar.MONTH));
assertEquals("Days do not match",
calendar.get(Calendar.DATE),
durationCalendar.get(Calendar.DATE));
assertEquals("Hours do not match",
calendar.get(Calendar.HOUR),
durationCalendar.get(Calendar.HOUR));
assertEquals("Minutes do not match",
calendar.get(Calendar.MINUTE),
durationCalendar.get(Calendar.MINUTE));
assertEquals("Seconds do not match",
calendar.get(Calendar.SECOND),
durationCalendar.get(Calendar.SECOND));
assertEquals("Miliseconds do not match",
calendar.get(Calendar.MILLISECOND),
durationCalendar.get(Calendar.MILLISECOND));
// test for overflows - Calendar class does automatic conversion
// of dates
duration.setMonths(20);
durationCalendar = duration.getAsCalendar();
assertEquals("Years do not match",
duration.getYears() + 1,
durationCalendar.get(Calendar.YEAR));
assertEquals("Months do not match",
duration.getMonths() - 12,
durationCalendar.get(Calendar.MONTH));
// make sure that Duration enforces milliseconds precision
duration.setSeconds(10.1234);
assertTrue("Milliseconds precision is not enforced",
duration.getSeconds() == 10.12);
}