CacheBuilder builder = factory.createCacheBuilder();
builder.setClock(clockMock);
builder.setMaxCount(2);
builder.setObjectProvider(providerMock);
InternalCache cache = (InternalCache) builder.buildCache();
final Group root = cache.getRootGroup();
// =====================================================================
// Set Expectations
// =====================================================================
expectations.add(new OrderedExpectations() {
public void add() {
addProviderExpectation(key1, null,
new ProviderResult(value1, root, true, null));
addProviderExpectation(key2, null,
new ProviderResult(value2, root, true, null));
addProviderExpectation(key3, null,
new ProviderResult(value3, root, true, null));
removalListenerMock.fuzzy
.entryRemoved(EXPECTED_CACHE_ENTRY)
.does(new ExpectCacheEntry(key1, value1));
addProviderExpectation(key1, null,
new ProviderResult(value1, root, true, null));
removalListenerMock.fuzzy
.entryRemoved(EXPECTED_CACHE_ENTRY)
.does(new ExpectCacheEntry(key3, value3));
}
});
// =====================================================================
// Test Expectations
// =====================================================================
root.addRemovalListener(removalListenerMock);
Object object;
// Check the cache integrity.
ensureCacheIntegrity(cache);
object = cache.retrieve(key1);
assertSame(value1, object);
// Check the cache integrity.
ensureCacheIntegrity(cache);
object = cache.retrieve(key2);
assertSame(value2, object);
// Check the cache integrity.
ensureCacheIntegrity(cache);
// At this point cache contains (key2, key1).
// Retrieving this should discard the entry for key1.
object = cache.retrieve(key3);
assertSame(value3, object);
// Check the cache integrity.
ensureCacheIntegrity(cache);
// At this point cache contains (key3, key2).
object = cache.retrieve(key2);
assertSame(value2, object);
// Check the cache integrity.
ensureCacheIntegrity(cache);
// At this point cache contains (key2, key3).
// Retrieving this should discard the entry for key3.
object = cache.retrieve(key1);
assertSame(value1, object);
// Check the cache integrity.
ensureCacheIntegrity(cache);
// At this point cache contains (key1, key2).
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(2000));
StatisticsSnapshot snapshot = root.getStatisticsSnapshot();
assertEquals(2, snapshot.getEntryCount());
assertEquals(1, snapshot.getHitCount());
assertEquals(4, snapshot.getMissedAddedCount());
}