FuzzyDateComparator implements Comparator<Date> { @Override public int compare(Date o1, Date o2) { int compareValue = 0; long o1Time = o1.getTime(); long o2Time = o2.getTime(); long difference = o1Time - o2Time; if(Math.abs(difference) > tolerance){ if(difference > 0){ compareValue = 1; } else{ compareValue = -1; } } return compareValue; } FuzzyDateComparator(long tolerance);  FuzzyDateComparator(); @Override int compare(Date o1, Date o2); static final long DEFAULT_TOLERANCE; }
@Test public void testCompareSameObject() { assertEquals(fdc.compare(baseDate, baseDate), 0); }
@Test public void testCompareExactSameDate(){ Date equalToBaseDate = new Date(start); assertEquals(fdc.compare(baseDate, equalToBaseDate), 0); }
@Test public void testCompareFuzzilySameDate(){ Date fuzzilyEqualDate1 = new Date(start + testTolerance); assertEquals(fdc.compare(baseDate, fuzzilyEqualDate1), 0); Date fuzzilyEqualDate2 = new Date(start - testTolerance); assertEquals(fdc.compare(baseDate, fuzzilyEqualDate2), 0); }
@Test public void testCompareNotSameDate(){ Date notEqualDate1 = new Date(start + testTolerance + 1); assertEquals(fdc.compare(baseDate, notEqualDate1), -1); Date notEqualDate2 = new Date((start - testTolerance) - 1); assertEquals(fdc.compare(baseDate, notEqualDate2), 1); }