@Test
  public void testRefreshAbsentElement() throws Exception {
    final IncrementingCacheEntryFactory factory = new IncrementingCacheEntryFactory();
    selfPopulatingCache = new SelfPopulatingCache(cache, factory);

    selfPopulatingCache.get("key1");
    selfPopulatingCache.get("key2");
    assertEquals(2, selfPopulatingCache.getSize());
    assertEquals(2, factory.getCount());

    // full refresh
    selfPopulatingCache.refresh();
    assertEquals(2, selfPopulatingCache.getSize());
    assertEquals(4, factory.getCount());

    // single element refresh which is not in the cache
    Element e3 = selfPopulatingCache.refresh("key3");
    assertEquals(3, selfPopulatingCache.getSize());
    assertEquals(5, factory.getCount());
    assertNotNull(e3);
    assertEquals("key3", e3.getKey());
    assertEquals(Integer.valueOf(5), e3.getValue());
  }
  @Test
  public void testRefreshElement() throws Exception {
    final IncrementingCacheEntryFactory factory = new IncrementingCacheEntryFactory();
    selfPopulatingCache = new SelfPopulatingCache(cache, factory);

    Element e1 = selfPopulatingCache.get("key1");
    Element e2 = selfPopulatingCache.get("key2");
    assertEquals(2, selfPopulatingCache.getSize());
    assertEquals(2, factory.getCount());
    assertEquals(Integer.valueOf(1), e1.getValue());
    assertEquals(Integer.valueOf(2), e2.getValue());

    // full refresh
    selfPopulatingCache.refresh();
    e1 = selfPopulatingCache.get("key1");
    e2 = selfPopulatingCache.get("key2");
    assertEquals(2, selfPopulatingCache.getSize());
    assertEquals(4, factory.getCount());
    // we cannot be sure which order key1 or key2 gets refreshed,
    // as the implementation makes no guarantee over the sequence
    // of the refresh; all we can be sure of is that between
    // them key1&2 must have the values 3 & 4
    int e1i = ((Integer) e1.getValue()).intValue();
    int e2i = ((Integer) e2.getValue()).intValue();
    assertTrue(((e1i == 3) && (e2i == 4)) || ((e1i == 4) && (e2i == 3)));

    // single element refresh
    selfPopulatingCache.get("key2");
    Element e2r = selfPopulatingCache.refresh("key2");
    assertEquals(2, selfPopulatingCache.getSize());
    assertEquals(5, factory.getCount());
    assertNotNull(e2r);
    assertEquals("key2", e2r.getKey());
    assertEquals(Integer.valueOf(5), e2r.getValue());

    // additional element
    Element e3 = selfPopulatingCache.get("key3");
    assertEquals(3, selfPopulatingCache.getSize());
    assertEquals(6, factory.getCount());
    assertNotNull(e3);
    assertEquals("key3", e3.getKey());
    assertEquals(Integer.valueOf(6), e3.getValue());

    // full refresh
    selfPopulatingCache.refresh();
    assertEquals(3, selfPopulatingCache.getSize());
    assertEquals(9, factory.getCount());
  }