@Test
 public void shouldBeAbleToAdvanceClockTimeOnEachQuery() throws Exception {
   Date startOfTheCentury = ymdDateFormat.parse("2000-01-01");
   new DateAlteringClock(startOfTheCentury).advanceMillisOnEachQuery();
   assertThat(Clock.currentTimeInMillis(), is(startOfTheCentury.getTime() + 1));
   assertThat(Clock.currentTimeInMillis(), is(startOfTheCentury.getTime() + 2));
   assertThat(Clock.currentTimeInMillis(), is(startOfTheCentury.getTime() + 3));
 }
 @Test
 public void currentClockTimeInMillisShouldTickOnFromZero() throws Exception {
   Date endOfTheDecade = ymdDateFormat.parse("2010-12-31");
   new DateAlteringClock(endOfTheDecade);
   long before = 0, after = 0;
   while (after == before) {
     after = Clock.currentTimeInMillis();
     if (before == 0) {
       before = after;
     }
   }
   assertTrue(Clock.currentTimeInMillis() - endOfTheDecade.getTime() < 1000);
 }
 @Test
 public void shouldBeAbleToFreezeClockTime() throws Exception {
   SystemClock systemClock = new SystemClock();
   long before = 0, after = 0;
   new DateAlteringClock(systemClock.currentClockDate()).freeze();
   long frozenTime = Clock.currentTimeInMillis();
   while (after == before) {
     after = systemClock.currentClockTimeInMillis();
     if (before == 0) {
       before = after;
     }
   }
   assertThat(Clock.currentTimeInMillis(), is(frozenTime));
 }
Esempio n. 4
0
 @Test
 public void testPutReturnValue() {
   cache.put(1, "apple");
   assertNotNull(cache.put(1, "banana"));
   Clock.setTime(3000);
   assertNull(cache.put(1, "mango"));
 }
Esempio n. 5
0
 @Test
 public void testSize() throws Exception {
   assertEquals(0, cache.size());
   cache.put(1, "apple");
   assertEquals(1, cache.size());
   Clock.setTime(3000);
   assertEquals(0, cache.size());
 }
Esempio n. 6
0
  @Test
  public void testExpiry() throws Exception {

    cache.put(1, "apple");
    assertEquals("apple", cache.get(1));
    assertFalse(cache.isEmpty());
    Clock.setTime(3000);
    assertNull(cache.get(1));
    assertTrue(cache.isEmpty());
  }
Esempio n. 7
0
  @Test
  public void testPartialExpiry() throws Exception {
    // Add an apple, it will expire at 2000
    // System.out.println(cache.getTimeToLive());
    cache.put(1, "apple");
    Clock.setTime(1500);
    // System.out.println(cache.getTimeToLive());
    // Add an orange, it will expire at 2500
    cache.put(2, "orange");

    assertEquals("apple", cache.get(1));
    assertEquals("orange", cache.get(2));
    assertEquals(2, cache.size());

    // Set time to 2300 and check that only the apple has disappeared
    Clock.setTime(2300);

    assertNull(cache.get(1));
    assertEquals("orange", cache.get(2));
    assertEquals(1, cache.size());
  }
Esempio n. 8
0
  @Test
  public void testContainsKeyAndContainsValue() {
    assertFalse(cache.containsKey(1));
    assertFalse(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));

    cache.put(1, "apple");
    assertTrue(cache.containsKey(1));
    assertTrue(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));

    Clock.setTime(3000);
    assertFalse(cache.containsKey(1));
    assertFalse(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));
  }
 @Test
 public void shouldBeAbleToDefineElapsedTime() throws Exception {
   Date startOfTheCentury = ymdDateFormat.parse("2000-01-01");
   new DateAlteringClock(startOfTheCentury).freeze().elapse(39);
   assertThat(Clock.currentTimeInMillis(), is(startOfTheCentury.getTime() + 39));
 }
 @Test
 public void currentClockTimeInMillisShouldBeRebasedToConstructorArg() throws Exception {
   Date startOfTheDecade = ymdDateFormat.parse("2010-01-01");
   new DateAlteringClock(startOfTheDecade);
   assertThat(ymdDateFormat.format(new Date(Clock.currentTimeInMillis())), is("2010-01-01"));
 }
 @After
 public void restoreDefaultClock() {
   Clock.restoreDefaultClock();
 }
Esempio n. 12
0
 @Before
 public void setUp() throws Exception {
   Clock.setTime(1000);
   cache = new CacheMapImpl<>();
   cache.setTimeToLive(TIME_TO_LIVE);
 }