TimeStampBasedIdGenerator { public synchronized String nextIdentifier() { String currentSimpleDate = getCurrentDateFormattedForFiles(); checkPreviousDate(currentSimpleDate); return currentSimpleDate + "." + fourDigitString(); }  synchronized String nextIdentifier();  }
@Test public void testNextIdentifierShouldReturnAProperIdWhenThePreviousTimestampIsNull() { String expected = "20130624145212.0000"; Calendar cal = Calendar.getInstance(); cal.set(2013, 5, 24, 14, 52, 12); Date currentDate = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(currentDate); Whitebox.setInternalState(underTest, "previousSimpleDate", null); String actual = underTest.nextIdentifier(); verify(currentDateProvider).getCurrentDate(); Assert.assertEquals(actual, expected); }
@Test public void testNextIdentifierShouldReturnAProperIdWhenThePreviousTimestampDiffersFromTheCurrent() { String expected = "20130624145212.0000"; Calendar cal = Calendar.getInstance(); cal.set(2013, 5, 24, 14, 52, 12); Date currentDate = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(currentDate); Whitebox.setInternalState(underTest, "previousSimpleDate", "20130624145211"); String actual = underTest.nextIdentifier(); verify(currentDateProvider).getCurrentDate(); Assert.assertEquals(actual, expected); }
@Test public void testNextIdentifierShouldReturnAProperIdWhenThePreviousTimestampEqualsTheCurrent() { String expected = "20130624145212.0888"; Calendar cal = Calendar.getInstance(); cal.set(2013, 5, 24, 14, 52, 12); Date currentDate = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(currentDate); Whitebox.setInternalState(underTest, "previousSimpleDate", "20130624145212"); Whitebox.setInternalState(underTest, "currentNumber", new AtomicInteger(888)); String actual = underTest.nextIdentifier(); verify(currentDateProvider).getCurrentDate(); Assert.assertEquals(actual, expected); }
@Test public void testNextIdentifierShouldReturnAProperIdWhenThePreviousIDIsAtTheoreticalMaximum() { String expected = "20130624145212.10000"; Calendar cal = Calendar.getInstance(); cal.set(2013, 5, 24, 14, 52, 12); Date currentDate = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(currentDate); Whitebox.setInternalState(underTest, "previousSimpleDate", "20130624145212"); Whitebox.setInternalState(underTest, "currentNumber", new AtomicInteger(9999)); underTest.nextIdentifier(); String actual = underTest.nextIdentifier(); Assert.assertEquals(actual, expected); }