@Test
public void caching_and_invalidation() throws Exception
{
// Alas, mixing and matching live code with mocks
ResourceDigestGenerator generator = mockResourceDigestGenerator();
InvalidationListener listener = mockInvalidationListener();
File f = createTestFile();
URL url = f.toURL();
Resource r = mockResource();
long lastUpdated = f.lastModified();
lastUpdated -= lastUpdated % 1000;
train_getPath(r, PATH);
train_toURL(r, url);
train_requiresDigest(generator, PATH, true);
train_generateChecksum(generator, url, DIGEST);
replay();
ResourceDigestManagerImpl cache = new ResourceDigestManagerImpl(generator, resourceChangeTracker);
cache.listenForInvalidations();
cache.addInvalidationListener(listener);
assertEquals(cache.requiresDigest(r), true);
assertEquals(cache.getTimeModified(r), lastUpdated);
assertEquals(cache.getDigest(r), DIGEST);
// No updates yet
getService(UpdateListenerHub.class).fireCheckForUpdates();
verify();
Thread.sleep(1500);
touch(f);
lastUpdated = f.lastModified();
lastUpdated -= lastUpdated % 1000;
String expectedDigest = "FREDBARNEY";
train_getPath(r, PATH);
train_toURL(r, url);
train_requiresDigest(generator, PATH, true);
train_generateChecksum(generator, url, expectedDigest);
listener.objectWasInvalidated();
replay();
getService(UpdateListenerHub.class).fireCheckForUpdates();